@@ -4728,30 +4728,33 @@ namespace ts {
47284728
47294729 function enumeratePotentialNonRelativeModules ( fragment : string , scriptPath : string , options : CompilerOptions ) : string [ ] {
47304730 // Check If this is a nested module
4731- const isNestedModule = fragment . indexOf ( directorySeparator ) !== - 1 ;
4731+ const isNestedModule = fragment . indexOf ( directorySeparator ) !== - 1 ;
4732+ const moduleNameFragment = isNestedModule ? fragment . substr ( 0 , fragment . lastIndexOf ( directorySeparator ) ) : undefined ;
47324733
47334734 // Get modules that the type checker picked up
47344735 const ambientModules = ts . map ( program . getTypeChecker ( ) . getAmbientModules ( ) , sym => stripQuotes ( sym . name ) ) ;
47354736 let nonRelativeModules = ts . filter ( ambientModules , moduleName => startsWith ( moduleName , fragment ) ) ;
47364737
47374738 // Nested modules of the form "module-name/sub" need to be adjusted to only return the string
4738- // after the last '/' that appears in the fragment because editors insert the completion
4739- // only after that character
4740- nonRelativeModules = ts . map ( nonRelativeModules , moduleName => {
4741- if ( moduleName . indexOf ( directorySeparator ) !== - 1 ) {
4742- if ( isNestedModule ) {
4743- return moduleName . substr ( fragment . lastIndexOf ( directorySeparator ) + 1 ) ;
4744- }
4745- }
4746- return moduleName ;
4747- } ) ;
4739+ // after the last '/' that appears in the fragment because that's where the replacement span
4740+ // starts
4741+ if ( isNestedModule ) {
4742+ const moduleNameWithSeperator = ensureTrailingDirectorySeparator ( moduleNameFragment ) ;
4743+ nonRelativeModules = ts . map ( nonRelativeModules , moduleName => {
4744+ if ( startsWith ( fragment , moduleNameWithSeperator ) ) {
4745+ return moduleName . substr ( moduleNameWithSeperator . length ) ;
4746+ }
4747+ return moduleName ;
4748+ } ) ;
4749+ }
4750+
47484751
47494752 if ( ! options . moduleResolution || options . moduleResolution === ModuleResolutionKind . NodeJs ) {
47504753 forEach ( enumerateNodeModulesVisibleToScript ( host , scriptPath ) , visibleModule => {
47514754 if ( ! isNestedModule ) {
47524755 nonRelativeModules . push ( visibleModule . moduleName ) ;
47534756 }
4754- else {
4757+ else if ( startsWith ( visibleModule . moduleName , moduleNameFragment ) ) {
47554758 const nestedFiles = host . readDirectory ( visibleModule . moduleDir , supportedTypeScriptExtensions , /*exclude*/ undefined , /*include*/ [ "./*" ] ) ;
47564759
47574760 forEach ( nestedFiles , ( f ) => {
0 commit comments