Skip to content

No path resolution for ambient modules#455

Merged
Perryvw merged 21 commits intoTypeScriptToLua:masterfrom
hazzard993:fake-module-imports
Mar 14, 2019
Merged

No path resolution for ambient modules#455
Perryvw merged 21 commits intoTypeScriptToLua:masterfrom
hazzard993:fake-module-imports

Conversation

@hazzard993
Copy link
Copy Markdown
Contributor

TypeScript allows the following.

mod.d.ts

declare module "mod" {
    export let x = 5;
}

src/main.ts

import * as module from "mod";
console.log(module.x);

tstl makes the require path src.mod because it tried to resolve the path.
tsc doesn't resolve these, it stays as mod.

This PR it to make sure that paths to ambient modules are not resolved.

let requireCall: tstl.CallExpression;
if (type && this.isAmbientModuleDeclaration(type)) {
requireCall = tstl.createCallExpression(
tstl.createIdentifier("require"),
Copy link
Copy Markdown
Member

@Perryvw Perryvw Feb 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nicer to move this logic to createModuleRequire by adding an optional resolveModule: bool = true parameter.

}

public isAmbientModuleDeclaration(type: ts.Type): boolean {
return type.symbol && type.symbol.valueDeclaration.kind === ts.SyntaxKind.ModuleDeclaration;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically, these types of functions are put in TSHelper

@ark120202
Copy link
Copy Markdown
Contributor

I don't think it's good to give declare module statement a different meaning. In ts it's valid to type multiple modules in a single .d.ts file, plus it may be used to annotate files by patterns with a * token. I think it would be better to add a new annotation (on declaration) for this.

src/TSHelper.ts Outdated
}

public static isAmbientModuleDeclaration(type: ts.Type): boolean {
return type.symbol && type.symbol.valueDeclaration.kind === ts.SyntaxKind.ModuleDeclaration;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some discussion in the discord we came to the conclusion that it would be a better solution to add a compiler directive @noResolution on the module, to minimize the risk of this breaking something.

const regex = /require\("(.*?)"\)/;
const lua = util.transpileString(`
import * as fake from "fake";
declare module "fake" {}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test do anything? You didn't decorate your module with the decorator. (Also the name is misleading)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope it did not. It doesn't look like you can declare a module and import it in the same file. I've made a change to the StringCompilerProgram to allow multiple files to be considered during compilation so I can put through two files to try out this decorator.

src/Compiler.ts Outdated
input: string,
options: CompilerOptions = defaultCompilerOptions,
filePath = "file.ts",
extraFiles?: { [filename: string]: string }): ts.Program {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda dislike this last parameter, I think it would be nicer if you change the type of input to be string | {[fileName: string]: string}, and then use a typeof check in the function what you are actually doing. (Maybe you could even remove the last parameter - depending on how much that breaks - since you can then do the same with the first parameter).

@Perryvw Perryvw merged commit f9a225e into TypeScriptToLua:master Mar 14, 2019
@hazzard993 hazzard993 deleted the fake-module-imports branch March 18, 2019 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants