Fix import path completions for wildcard patterns with extensions
Fixes #62706
Problem
Import path completions were missing for package.json exports using wildcard patterns with file extensions.
Example:
{
"exports": {
"./glob/path/*.js": "./build/*.js"
}
}
Typing import {} from "@local/a/glob/path/" should suggest bar.js, baz.js but completions were missing.
Root Cause
Two bugs in src/services/stringCompletions.ts:
-
Line 1234: Wildcard detection used
endsWith(pattern, "/*")which only matched patterns ending with"/*", missing patterns like"./path/*.js" -
Line 1166-1168: Completion generation didn't append file extension suffixes from patterns
Solution
Change 1: Broaden wildcard detection (Line 1234)
// Before
const isExportsOrImportsWildcard = (isExports || isImports) && endsWith(pattern, "/*");
// After
const isExportsOrImportsWildcard = (isExports || isImports) && pattern.includes("/*");
This correctly detects all Node.js wildcard patterns containing "/*" regardless of trailing characters.
Change 2: Append file extension suffixes (Line 1166-1168)
// Before
return flatMap(patterns, pattern => getModulesForPathsPattern(...));
// After
return flatMap(patterns, pattern =>
getModulesForPathsPattern(...)
?.map(entry => parsedPath.suffix && startsWith(parsedPath.suffix, ".")
? { ...entry, name: entry.name + parsedPath.suffix }
: entry)
);
This adds file extension suffixes (.js, .css, .d.ts, etc.) to completions while correctly ignoring path components (like "/suffix").
Testing
New Test Case
Added tests/cases/fourslash/completionForPackageExportsGlobWithExtension.ts to verify wildcard patterns with extensions work correctly.
Validation Results
Before fix:
- 99,000 passing
- 1 failing (new test:
"Error: completion 'bar.js' not found")
After fix:
- 99,001 passing
- 0 failing
- 0 regressions
Patterns Fixed
"./glob/path/*.js"✅"./styles/*.css"✅"./dist/*.d.ts"✅"./esm/*.mjs"✅"./cjs/*.cjs"✅- And more...
Compliance
Aligns with Node.js Package Entry Points specification which supports wildcard patterns like:
{
"exports": {
"./features/*.js": "./src/features/*.js",
"./lib/*/index.js": "./src/*/index.js"
}
}
All valid patterns contain "/*" substring.
Files Changed
src/services/stringCompletions.ts(2 lines modified)tests/cases/fourslash/completionForPackageExportsGlobWithExtension.ts(new test)
this PR contains random unrelated changes from another PR