Skip to content

Fix auto import path mapping when first pattern fails #54868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,9 @@ function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<rea
validateEnding({ ending, value })
) {
const matchedStar = value.substring(prefix.length, value.length - suffix.length);
return pathIsRelative(matchedStar) ? undefined : key.replace("*", matchedStar);
if (!pathIsRelative(matchedStar)) {
return key.replace("*", matchedStar);

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding

This replaces only the first occurrence of "*".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=(

Edit: Is there an unambiguous way to replace only once so codeql won't complain? Probably not, that would be too easy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you can use a global regex, but I don’t want to allocate an object when a string literal will do.

}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/fourslash/autoImportPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path="fourslash.ts" />

// @Filename: /package1/jsconfig.json
//// {
//// "compilerOptions": {
//// checkJs: true,
//// "paths": {
//// "package1/*": ["./*"],
//// "package2/*": ["../package2/*"]
//// },
//// "baseUrl": "."
//// },
//// "include": [
//// ".",
//// "../package2"
//// ]
//// }

// @Filename: /package1/file1.js
//// bar/**/

// @Filename: /package2/file1.js
//// export const bar = 0;

verify.importFixModuleSpecifiers("", ["package2/file1"], { importModuleSpecifierPreference: "shortest" });