Skip to content

Commit 5bb99c1

Browse files
Fixed import resolution
1 parent 9fe2e61 commit 5bb99c1

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

typescript/src/core/processors/import-declaration.processor.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
22

33
import { ConceptMap, mergeConceptMaps } from "../concept";
4-
import { FQN, ProcessingContext } from "../context";
4+
import { FQN, GlobalContext, ProcessingContext } from "../context";
55
import { ExecutionCondition } from "../execution-condition";
66
import { ModulePathUtils } from "../utils/modulepath.utils";
77
import { Processor } from "../processor";
@@ -27,11 +27,11 @@ export class ImportDeclarationProcessor extends Processor {
2727
let target = new FQN("");
2828
let isModule = false;
2929
if (specifier.type === AST_NODE_TYPES.ImportSpecifier) {
30-
const importSourceFqn = ModulePathUtils.toFQN(NodeUtils.resolveImportPath(importSource, globalContext.projectInfo, globalContext.sourceFilePathAbsolute));
30+
const importSourceFqn = this.importSourceToFqn(importSource, globalContext);
3131
const importedName = specifier.imported.type === AST_NODE_TYPES.Identifier ? specifier.imported.name : specifier.imported.raw;
3232
target = new FQN(importSourceFqn.globalFqn + "." + importedName, importSourceFqn.localFqn + "." + importedName);
3333
} else if (specifier.type === AST_NODE_TYPES.ImportDefaultSpecifier) {
34-
const importSourceFqn = ModulePathUtils.toFQN(NodeUtils.resolveImportPath(importSource, globalContext.projectInfo, globalContext.sourceFilePathAbsolute));
34+
const importSourceFqn = this.importSourceToFqn(importSource, globalContext);
3535
target = new FQN(importSourceFqn.globalFqn + ".default", importSourceFqn.localFqn + ".default");
3636
} else if (specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier) {
3737
target = new FQN(path.resolve(globalContext.projectInfo.rootPath, importSource), importSource);
@@ -49,7 +49,7 @@ export class ImportDeclarationProcessor extends Processor {
4949
const targetDeclName = ModulePathUtils.extractFQNIdentifier(target.globalFqn);
5050

5151
let packageName: string | undefined = undefined;
52-
if(resolvedModulePath.startsWith(globalContext.projectInfo.rootPath + "/node_modules")) {
52+
if (resolvedModulePath.startsWith(globalContext.projectInfo.rootPath + "/node_modules")) {
5353
// only resolve node package name, if it's an actual node module, not some re-mapped source file (see tsconfig.json -> "paths" option)
5454
packageName = NodeUtils.getPackageNameForPath(globalContext.projectInfo.rootPath, resolvedModulePath);
5555
}
@@ -81,4 +81,13 @@ export class ImportDeclarationProcessor extends Processor {
8181
}
8282
return mergeConceptMaps(...concepts);
8383
}
84+
85+
private importSourceToFqn(importSource: string, globalContext: GlobalContext): FQN {
86+
const importPath = NodeUtils.resolveImportPath(importSource, globalContext.projectInfo, globalContext.sourceFilePathAbsolute);
87+
if (path.relative(globalContext.projectInfo.rootPath, importPath).startsWith("node_modules")) {
88+
return ModulePathUtils.toFQN(importSource);
89+
} else {
90+
return ModulePathUtils.toFQN(importPath);
91+
}
92+
}
8493
}

0 commit comments

Comments
 (0)