@@ -179,7 +179,7 @@ namespace ts.Completions.PathCompletions {
179179
180180 function getCompletionsForPathMapping (
181181 path : string , patterns : ReadonlyArray < string > , fragment : string , baseUrl : string , fileExtensions : ReadonlyArray < string > , host : LanguageServiceHost ,
182- ) : string [ ] {
182+ ) : ReadonlyArray < string > {
183183 if ( ! endsWith ( path , "*" ) ) {
184184 // For a path mapping "foo": ["/x/y/z.ts"], add "foo" itself as a completion.
185185 return ! stringContains ( path , "*" ) && startsWith ( path , fragment ) ? [ path ] : emptyArray ;
@@ -231,16 +231,19 @@ namespace ts.Completions.PathCompletions {
231231 // Trim away prefix and suffix
232232 return mapDefined ( concatenate ( matches , directories ) , match => {
233233 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 ;
241236 } ) ;
242237 }
243238
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+
244247 function enumeratePotentialNonRelativeModules ( fragment : string , scriptPath : string , options : CompilerOptions , typeChecker : TypeChecker , host : LanguageServiceHost ) : string [ ] {
245248 // Check If this is a nested module
246249 const isNestedModule = stringContains ( fragment , directorySeparator ) ;
0 commit comments