Skip to content

Commit 0dd855d

Browse files
committed
Fix no element imports and use alternate symbol resolution solution
1 parent 2e7eec1 commit 0dd855d

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

src/LuaTransformer.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,28 @@ export class LuaTransformer {
357357
scope.importStatements = [];
358358
}
359359

360+
let shouldResolve = true;
361+
const moduleOwnerSymbol = this.checker.getSymbolAtLocation(statement.moduleSpecifier);
362+
if (moduleOwnerSymbol) {
363+
for (const declaration of moduleOwnerSymbol.declarations) {
364+
if (tsHelper.isNonNamespaceModuleDeclaration(declaration)) {
365+
const type = this.checker.getTypeAtLocation(declaration);
366+
if (type) {
367+
const decorators = tsHelper.getCustomDecorators(type, this.checker);
368+
if (decorators.has(DecoratorKind.NoResolution)) {
369+
shouldResolve = false;
370+
break;
371+
}
372+
}
373+
}
374+
}
375+
}
376+
360377
const moduleSpecifier = statement.moduleSpecifier as ts.StringLiteral;
361378
const importPath = moduleSpecifier.text.replace(new RegExp('"', "g"), "");
379+
const requireCall = this.createModuleRequire(statement.moduleSpecifier as ts.StringLiteral, shouldResolve);
362380

363381
if (!statement.importClause) {
364-
const requireCall = this.createModuleRequire(statement.moduleSpecifier as ts.StringLiteral);
365382
result.push(tstl.createExpressionStatement(requireCall));
366383
if (scope.importStatements) {
367384
scope.importStatements.push(...result);
@@ -376,28 +393,6 @@ export class LuaTransformer {
376393
throw TSTLErrors.UnsupportedImportType(statement.importClause);
377394
}
378395

379-
let shouldResolve = true;
380-
if (ts.isNamedImports(imports)) {
381-
for (const importSpecifier of imports.elements) {
382-
const parentModule = tsHelper.getImportSpecifierModuleDeclaration(importSpecifier, this.checker);
383-
if (parentModule) {
384-
const type = this.checker.getTypeAtLocation(parentModule);
385-
const decorators = tsHelper.getCustomDecorators(type, this.checker);
386-
if (decorators.has(DecoratorKind.NoResolution)) {
387-
shouldResolve = false;
388-
break;
389-
}
390-
}
391-
}
392-
} else if (ts.isNamespaceImport(imports)) {
393-
const type = this.checker.getTypeAtLocation(imports);
394-
if (tsHelper.getCustomDecorators(type, this.checker).has(DecoratorKind.NoResolution)) {
395-
shouldResolve = false;
396-
}
397-
}
398-
399-
const requireCall = this.createModuleRequire(statement.moduleSpecifier as ts.StringLiteral, shouldResolve);
400-
401396
if (ts.isNamedImports(imports)) {
402397
const filteredElements = imports.elements.filter(e => {
403398
const decorators = tsHelper.getCustomDecorators(this.checker.getTypeAtLocation(e), this.checker);

src/TSHelper.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -358,27 +358,6 @@ export class TSHelper {
358358
return directivesMap;
359359
}
360360

361-
public static getImportSpecifierModuleDeclaration(
362-
node: ts.ImportSpecifier,
363-
checker: ts.TypeChecker
364-
): ts.ModuleDeclaration | undefined {
365-
const symbol = checker.getSymbolAtLocation(node.name);
366-
if (symbol) {
367-
const originalSymbol = checker.getAliasedSymbol(symbol);
368-
if (originalSymbol.declarations) {
369-
for (const declaration of originalSymbol.declarations) {
370-
const parentAmbientModule = this.findFirstNodeAbove(
371-
declaration,
372-
this.isNonNamespaceModuleDeclaration
373-
);
374-
if (parentAmbientModule) {
375-
return parentAmbientModule;
376-
}
377-
}
378-
}
379-
}
380-
}
381-
382361
// Search up until finding a node satisfying the callback
383362
public static findFirstNodeAbove<T extends ts.Node>(
384363
node: ts.Node,

test/unit/require.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ test.each([
151151
mainCode: "import { y } from 'fake'; y;",
152152
expectedPath: "src.fake",
153153
},
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+
},
154169
])("noResolution prevents any module path resolution behaviour", ({ declarationStatement, mainCode, expectedPath }) => {
155170
const lua = util.transpileString({
156171
"src/main.ts": mainCode,

0 commit comments

Comments
 (0)