@@ -16,15 +16,18 @@ namespace ts.moduleSpecifiers {
16
16
const getCanonicalFileName = hostGetCanonicalFileName ( host ) ;
17
17
const sourceDirectory = getDirectoryPath ( importingSourceFile . fileName ) ;
18
18
19
- return getAllModulePaths ( program , moduleSymbol . valueDeclaration . getSourceFile ( ) ) . map ( moduleFileName => {
20
- const global = tryGetModuleNameFromAmbientModule ( moduleSymbol )
21
- || tryGetModuleNameFromTypeRoots ( compilerOptions , host , getCanonicalFileName , moduleFileName , addJsExtension )
22
- || tryGetModuleNameAsNodeModule ( compilerOptions , moduleFileName , host , getCanonicalFileName , sourceDirectory )
23
- || rootDirs && tryGetModuleNameFromRootDirs ( rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName ) ;
24
- if ( global ) {
25
- return [ global ] ;
26
- }
19
+ const ambient = tryGetModuleNameFromAmbientModule ( moduleSymbol ) ;
20
+ if ( ambient ) return [ [ ambient ] ] ;
21
+
22
+ const modulePaths = getAllModulePaths ( program , moduleSymbol . valueDeclaration . getSourceFile ( ) ) ;
27
23
24
+ const global = mapDefined ( modulePaths , moduleFileName =>
25
+ tryGetModuleNameFromTypeRoots ( compilerOptions , host , getCanonicalFileName , moduleFileName , addJsExtension ) ||
26
+ tryGetModuleNameAsNodeModule ( compilerOptions , moduleFileName , host , getCanonicalFileName , sourceDirectory ) ||
27
+ rootDirs && tryGetModuleNameFromRootDirs ( rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName ) ) ;
28
+ if ( global . length ) return global . map ( g => [ g ] ) ;
29
+
30
+ return modulePaths . map ( moduleFileName => {
28
31
const relativePath = removeExtensionAndIndexPostFix ( ensurePathIsNonModuleName ( getRelativePathFromDirectory ( sourceDirectory , moduleFileName , getCanonicalFileName ) ) , moduleResolutionKind , addJsExtension ) ;
29
32
if ( ! baseUrl || preferences . importModuleSpecifierPreference === "relative" ) {
30
33
return [ relativePath ] ;
@@ -191,11 +194,12 @@ namespace ts.moduleSpecifiers {
191
194
// Simplify the full file path to something that can be resolved by Node.
192
195
193
196
// If the module could be imported by a directory name, use that directory's name
194
- let moduleSpecifier = getDirectoryOrExtensionlessFileName ( moduleFileName ) ;
197
+ const moduleSpecifier = getDirectoryOrExtensionlessFileName ( moduleFileName ) ;
195
198
// Get a path that's relative to node_modules or the importing file's path
196
- moduleSpecifier = getNodeResolvablePath ( moduleSpecifier ) ;
199
+ // if node_modules folder is in this folder or any of its parent folders, no need to keep it.
200
+ if ( ! startsWith ( sourceDirectory , moduleSpecifier . substring ( 0 , parts . topLevelNodeModulesIndex ) ) ) return undefined ;
197
201
// If the module was found in @types , get the actual Node package name
198
- return getPackageNameFromAtTypesDirectory ( moduleSpecifier ) ;
202
+ return getPackageNameFromAtTypesDirectory ( moduleSpecifier . substring ( parts . topLevelPackageNameIndex + 1 ) ) ;
199
203
200
204
function getDirectoryOrExtensionlessFileName ( path : string ) : string {
201
205
// If the file is the main module, it can be imported by the package name
@@ -224,17 +228,6 @@ namespace ts.moduleSpecifiers {
224
228
225
229
return fullModulePathWithoutExtension ;
226
230
}
227
-
228
- function getNodeResolvablePath ( path : string ) : string {
229
- const basePath = path . substring ( 0 , parts . topLevelNodeModulesIndex ) ;
230
- if ( sourceDirectory . indexOf ( basePath ) === 0 ) {
231
- // if node_modules folder is in this folder or any of its parent folders, no need to keep it.
232
- return path . substring ( parts . topLevelPackageNameIndex + 1 ) ;
233
- }
234
- else {
235
- return ensurePathIsNonModuleName ( getRelativePathFromDirectory ( sourceDirectory , path , getCanonicalFileName ) ) ;
236
- }
237
- }
238
231
}
239
232
240
233
interface NodeModulePathParts {
0 commit comments