1
1
import { AST_NODE_TYPES } from "@typescript-eslint/utils" ;
2
2
3
3
import { ConceptMap , mergeConceptMaps } from "../concept" ;
4
- import { FQN , ProcessingContext } from "../context" ;
4
+ import { FQN , GlobalContext , ProcessingContext } from "../context" ;
5
5
import { ExecutionCondition } from "../execution-condition" ;
6
6
import { ModulePathUtils } from "../utils/modulepath.utils" ;
7
7
import { Processor } from "../processor" ;
@@ -27,11 +27,11 @@ export class ImportDeclarationProcessor extends Processor {
27
27
let target = new FQN ( "" ) ;
28
28
let isModule = false ;
29
29
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 ) ;
31
31
const importedName = specifier . imported . type === AST_NODE_TYPES . Identifier ? specifier . imported . name : specifier . imported . raw ;
32
32
target = new FQN ( importSourceFqn . globalFqn + "." + importedName , importSourceFqn . localFqn + "." + importedName ) ;
33
33
} 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 ) ;
35
35
target = new FQN ( importSourceFqn . globalFqn + ".default" , importSourceFqn . localFqn + ".default" ) ;
36
36
} else if ( specifier . type === AST_NODE_TYPES . ImportNamespaceSpecifier ) {
37
37
target = new FQN ( path . resolve ( globalContext . projectInfo . rootPath , importSource ) , importSource ) ;
@@ -49,7 +49,7 @@ export class ImportDeclarationProcessor extends Processor {
49
49
const targetDeclName = ModulePathUtils . extractFQNIdentifier ( target . globalFqn ) ;
50
50
51
51
let packageName : string | undefined = undefined ;
52
- if ( resolvedModulePath . startsWith ( globalContext . projectInfo . rootPath + "/node_modules" ) ) {
52
+ if ( resolvedModulePath . startsWith ( globalContext . projectInfo . rootPath + "/node_modules" ) ) {
53
53
// only resolve node package name, if it's an actual node module, not some re-mapped source file (see tsconfig.json -> "paths" option)
54
54
packageName = NodeUtils . getPackageNameForPath ( globalContext . projectInfo . rootPath , resolvedModulePath ) ;
55
55
}
@@ -81,4 +81,13 @@ export class ImportDeclarationProcessor extends Processor {
81
81
}
82
82
return mergeConceptMaps ( ...concepts ) ;
83
83
}
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
+ }
84
93
}
0 commit comments