Skip to content

Commit bccecc5

Browse files
hazzard993Perryvw
authored andcommitted
Fix noResolution for named imports (#636)
* Fix noResolution for named imports * Fix no element imports and use alternate symbol resolution solution * Use collectCustomDecorators and remove isNonNamespaceModuleDeclaration * Change decorator map abbreviation
1 parent 2535f39 commit bccecc5

File tree

2 files changed

+96
-18
lines changed

2 files changed

+96
-18
lines changed

src/LuaTransformer.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from "path";
22
import * as ts from "typescript";
33
import { CompilerOptions, LuaTarget } from "./CompilerOptions";
4-
import { DecoratorKind } from "./Decorator";
4+
import { Decorator, DecoratorKind } from "./Decorator";
55
import * as tstl from "./LuaAST";
66
import { LuaLibFeature } from "./LuaLib";
77
import { ContextType, TSHelper as tsHelper } from "./TSHelper";
@@ -357,11 +357,21 @@ export class LuaTransformer {
357357
scope.importStatements = [];
358358
}
359359

360+
let shouldResolve = true;
361+
const moduleOwnerSymbol = this.checker.getSymbolAtLocation(statement.moduleSpecifier);
362+
if (moduleOwnerSymbol) {
363+
const decorators = new Map<DecoratorKind, Decorator>();
364+
tsHelper.collectCustomDecorators(moduleOwnerSymbol, this.checker, decorators);
365+
if (decorators.has(DecoratorKind.NoResolution)) {
366+
shouldResolve = false;
367+
}
368+
}
369+
360370
const moduleSpecifier = statement.moduleSpecifier as ts.StringLiteral;
361371
const importPath = moduleSpecifier.text.replace(new RegExp('"', "g"), "");
372+
const requireCall = this.createModuleRequire(statement.moduleSpecifier as ts.StringLiteral, shouldResolve);
362373

363374
if (!statement.importClause) {
364-
const requireCall = this.createModuleRequire(statement.moduleSpecifier as ts.StringLiteral);
365375
result.push(tstl.createExpressionStatement(requireCall));
366376
if (scope.importStatements) {
367377
scope.importStatements.push(...result);
@@ -376,10 +386,6 @@ export class LuaTransformer {
376386
throw TSTLErrors.UnsupportedImportType(statement.importClause);
377387
}
378388

379-
const type = this.checker.getTypeAtLocation(imports);
380-
const shouldResolve = !tsHelper.getCustomDecorators(type, this.checker).has(DecoratorKind.NoResolution);
381-
const requireCall = this.createModuleRequire(statement.moduleSpecifier as ts.StringLiteral, shouldResolve);
382-
383389
if (ts.isNamedImports(imports)) {
384390
const filteredElements = imports.elements.filter(e => {
385391
const decorators = tsHelper.getCustomDecorators(this.checker.getTypeAtLocation(e), this.checker);

test/unit/require.spec.ts

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,92 @@ test.each([
9191
}
9292
);
9393

94-
test.each([{ comment: "", expectedPath: "src.fake" }, { comment: "/** @noResolution */", expectedPath: "fake" }])(
95-
"noResolution on ambient modules causes no path alterations (%p)",
96-
({ comment, expectedPath }) => {
97-
const lua = util.transpileString({
98-
"src/main.ts": `import * as fake from "fake"; fake;`,
99-
"module.d.ts": `${comment} declare module "fake" {}`,
100-
});
101-
const match = requireRegex.exec(lua);
94+
test.each([
95+
{
96+
declarationStatement: `
97+
declare module 'fake' {}
98+
`,
99+
mainCode: "import * as fake from 'fake'; fake;",
100+
expectedPath: "src.fake",
101+
},
102+
{
103+
declarationStatement: `
104+
/** @noResolution */
105+
declare module 'fake' {}
106+
`,
107+
mainCode: "import * as fake from 'fake'; fake;",
108+
expectedPath: "fake",
109+
},
110+
{
111+
declarationStatement: `
112+
declare module 'fake' {
113+
export const x: number;
114+
}
115+
`,
116+
mainCode: "import { x } from 'fake'; x;",
117+
expectedPath: "src.fake",
118+
},
119+
{
120+
declarationStatement: `
121+
/** @noResolution */
122+
declare module 'fake' {
123+
export const x: number;
124+
}
125+
`,
126+
mainCode: "import { x } from 'fake'; x;",
127+
expectedPath: "fake",
128+
},
129+
{
130+
declarationStatement: `
131+
/** @noResolution */
132+
declare module 'fake' {
133+
export const x: number;
134+
}
135+
declare module 'fake' {
136+
export const y: number;
137+
}
138+
`,
139+
mainCode: "import { y } from 'fake'; y;",
140+
expectedPath: "fake",
141+
},
142+
{
143+
declarationStatement: `
144+
declare module 'fake' {
145+
export const x: number;
146+
}
147+
declare module 'fake' {
148+
export const y: number;
149+
}
150+
`,
151+
mainCode: "import { y } from 'fake'; y;",
152+
expectedPath: "src.fake",
153+
},
154+
{
155+
declarationStatement: `
156+
declare module 'fake' {}
157+
`,
158+
mainCode: "import 'fake';",
159+
expectedPath: "src.fake",
160+
},
161+
{
162+
declarationStatement: `
163+
/** @noResolution */
164+
declare module 'fake' {}
165+
`,
166+
mainCode: "import 'fake';",
167+
expectedPath: "fake",
168+
},
169+
])("noResolution prevents any module path resolution behaviour", ({ declarationStatement, mainCode, expectedPath }) => {
170+
const lua = util.transpileString({
171+
"src/main.ts": mainCode,
172+
"module.d.ts": declarationStatement,
173+
});
174+
const match = requireRegex.exec(lua);
102175

103-
if (util.expectToBeDefined(match)) {
104-
expect(match[1]).toBe(expectedPath);
105-
}
176+
if (util.expectToBeDefined(match)) {
177+
expect(match[1]).toBe(expectedPath);
106178
}
107-
);
179+
});
108180

109181
test("ImportEquals declaration require", () => {
110182
const input = `import foo = require("./foo/bar"); foo;`;

0 commit comments

Comments
 (0)