@@ -179,7 +179,7 @@ namespace ts.Completions.PathCompletions {
179
179
180
180
function getCompletionsForPathMapping (
181
181
path : string , patterns : ReadonlyArray < string > , fragment : string , baseUrl : string , fileExtensions : ReadonlyArray < string > , host : LanguageServiceHost ,
182
- ) : string [ ] {
182
+ ) : ReadonlyArray < string > {
183
183
if ( ! endsWith ( path , "*" ) ) {
184
184
// For a path mapping "foo": ["/x/y/z.ts"], add "foo" itself as a completion.
185
185
return ! stringContains ( path , "*" ) && startsWith ( path , fragment ) ? [ path ] : emptyArray ;
@@ -231,16 +231,19 @@ namespace ts.Completions.PathCompletions {
231
231
// Trim away prefix and suffix
232
232
return mapDefined ( concatenate ( matches , directories ) , match => {
233
233
const normalizedMatch = normalizePath ( match ) ;
234
- if ( ! endsWith ( normalizedMatch , normalizedSuffix ) || ! startsWith ( normalizedMatch , completePrefix ) ) {
235
- return ;
236
- }
237
-
238
- const start = completePrefix . length ;
239
- const length = normalizedMatch . length - start - normalizedSuffix . length ;
240
- return removeFileExtension ( normalizedMatch . substr ( start , length ) ) ;
234
+ const inner = withoutStartAndEnd ( normalizedMatch , completePrefix , normalizedSuffix ) ;
235
+ return inner !== undefined ? removeLeadingDirectorySeparator ( removeFileExtension ( inner ) ) : undefined ;
241
236
} ) ;
242
237
}
243
238
239
+ function withoutStartAndEnd ( s : string , start : string , end : string ) : string | undefined {
240
+ return startsWith ( s , start ) && endsWith ( s , end ) ? s . slice ( start . length , s . length - end . length ) : undefined ;
241
+ }
242
+
243
+ function removeLeadingDirectorySeparator ( path : string ) : string {
244
+ return path [ 0 ] === directorySeparator ? path . slice ( 1 ) : path ;
245
+ }
246
+
244
247
function enumeratePotentialNonRelativeModules ( fragment : string , scriptPath : string , options : CompilerOptions , typeChecker : TypeChecker , host : LanguageServiceHost ) : string [ ] {
245
248
// Check If this is a nested module
246
249
const isNestedModule = stringContains ( fragment , directorySeparator ) ;
0 commit comments