@@ -127,53 +127,30 @@ namespace ts.moduleSpecifiers {
127
127
}
128
128
129
129
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix ( relativeToBaseUrl , ending , compilerOptions ) ;
130
- if ( paths ) {
131
- const fromPaths = tryGetModuleNameFromPaths ( removeFileExtension ( relativeToBaseUrl ) , importRelativeToBaseUrl , paths ) ;
132
- if ( fromPaths ) {
133
- return [ fromPaths ] ;
134
- }
135
- }
130
+ const fromPaths = paths && tryGetModuleNameFromPaths ( removeFileExtension ( relativeToBaseUrl ) , importRelativeToBaseUrl , paths ) ;
131
+ const nonRelative = fromPaths === undefined ? importRelativeToBaseUrl : fromPaths ;
136
132
137
133
if ( relativePreference === RelativePreference . NonRelative ) {
138
- return [ importRelativeToBaseUrl ] ;
134
+ return [ nonRelative ] ;
139
135
}
140
136
141
137
if ( relativePreference !== RelativePreference . Auto ) Debug . assertNever ( relativePreference ) ;
142
138
143
- if ( isPathRelativeToParent ( relativeToBaseUrl ) ) {
139
+ if ( isPathRelativeToParent ( nonRelative ) ) {
144
140
return [ relativePath ] ;
145
141
}
146
142
147
- /*
148
- Prefer a relative import over a baseUrl import if it doesn't traverse up to baseUrl.
149
-
150
- Suppose we have:
151
- baseUrl = /base
152
- sourceDirectory = /base/a/b
153
- moduleFileName = /base/foo/bar
154
- Then:
155
- relativePath = ../../foo/bar
156
- getRelativePathNParents(relativePath) = 2
157
- pathFromSourceToBaseUrl = ../../
158
- getRelativePathNParents(pathFromSourceToBaseUrl) = 2
159
- 2 < 2 = false
160
- In this case we should prefer using the baseUrl path "/a/b" instead of the relative path "../../foo/bar".
161
-
162
- Suppose we have:
163
- baseUrl = /base
164
- sourceDirectory = /base/foo/a
165
- moduleFileName = /base/foo/bar
166
- Then:
167
- relativePath = ../a
168
- getRelativePathNParents(relativePath) = 1
169
- pathFromSourceToBaseUrl = ../../
170
- getRelativePathNParents(pathFromSourceToBaseUrl) = 2
171
- 1 < 2 = true
172
- In this case we should prefer using the relative path "../a" instead of the baseUrl path "foo/a".
173
- */
174
- const pathFromSourceToBaseUrl = ensurePathIsNonModuleName ( getRelativePathFromDirectory ( sourceDirectory , baseUrl , getCanonicalFileName ) ) ;
175
- const relativeFirst = getRelativePathNParents ( relativePath ) < getRelativePathNParents ( pathFromSourceToBaseUrl ) ;
176
- return relativeFirst ? [ relativePath , importRelativeToBaseUrl ] : [ importRelativeToBaseUrl , relativePath ] ;
143
+ // Prefer a relative import over a baseUrl import if it has fewer components.
144
+ const relativeFirst = countPathComponents ( relativePath ) < countPathComponents ( nonRelative ) ;
145
+ return relativeFirst ? [ relativePath , nonRelative ] : [ nonRelative , relativePath ] ;
146
+ }
147
+
148
+ function countPathComponents ( path : string ) : number {
149
+ let count = 0 ;
150
+ for ( let i = startsWith ( path , "./" ) ? 2 : 0 ; i < path . length ; i ++ ) {
151
+ if ( path . charCodeAt ( i ) === CharacterCodes . slash ) count ++ ;
152
+ }
153
+ return count ;
177
154
}
178
155
179
156
function usesJsExtensionOnImports ( { imports } : SourceFile ) : boolean {
@@ -245,15 +222,6 @@ namespace ts.moduleSpecifiers {
245
222
return result ;
246
223
}
247
224
248
- function getRelativePathNParents ( relativePath : string ) : number {
249
- const components = getPathComponents ( relativePath ) ;
250
- if ( components [ 0 ] || components . length === 1 ) return 0 ;
251
- for ( let i = 1 ; i < components . length ; i ++ ) {
252
- if ( components [ i ] !== ".." ) return i - 1 ;
253
- }
254
- return components . length - 1 ;
255
- }
256
-
257
225
function tryGetModuleNameFromAmbientModule ( moduleSymbol : Symbol ) : string | undefined {
258
226
const decl = find ( moduleSymbol . declarations ,
259
227
d => isNonGlobalAmbientModule ( d ) && ( ! isExternalModuleAugmentation ( d ) || ! isExternalModuleNameRelative ( getTextOfIdentifierOrLiteral ( d . name ) ) )
0 commit comments