-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add autoImportSpecifierExcludeRegexes preference
#59543
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
andrewbranch
merged 5 commits into
microsoft:main
from
andrewbranch:feature/autoImportSpecifierExcludeRegexes
Aug 9, 2024
+183
−14
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79607c1
Simple implementation of autoImportSpecifierExcludeRegexes
andrewbranch ef8341a
Add completions test
andrewbranch a86fef6
Update API baseline
andrewbranch 941c1cf
Fix lint
andrewbranch e38941b
Allow patterns to be given as regex literals with slashes and flags
andrewbranch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tests/cases/fourslash/autoImportSpecifierExcludeRegexes1.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
|
|
||
| // @module: preserve | ||
|
|
||
| // @Filename: /node_modules/lib/index.d.ts | ||
| //// declare module "ambient" { | ||
| //// export const x: number; | ||
| //// } | ||
| //// declare module "ambient/utils" { | ||
| //// export const x: number; | ||
| //// } | ||
|
|
||
| // @Filename: /index.ts | ||
| //// x/**/ | ||
|
|
||
| verify.importFixModuleSpecifiers("", ["ambient", "ambient/utils"]); | ||
| verify.importFixModuleSpecifiers("", ["ambient"], { autoImportSpecifierExcludeRegexes: ["utils"] }); | ||
| // case sensitive, no match | ||
| verify.importFixModuleSpecifiers("", ["ambient", "ambient/utils"], { autoImportSpecifierExcludeRegexes: ["/UTILS/"] }); | ||
| // case insensitive flag given | ||
| verify.importFixModuleSpecifiers("", ["ambient"], { autoImportSpecifierExcludeRegexes: ["/UTILS/i"] }); | ||
| // invalid due to unescaped slash, treated as pattern | ||
| verify.importFixModuleSpecifiers("", ["ambient", "ambient/utils"], { autoImportSpecifierExcludeRegexes: ["/ambient/utils/"] }); | ||
| verify.importFixModuleSpecifiers("", ["ambient"], { autoImportSpecifierExcludeRegexes: ["/ambient\\/utils/"] }); | ||
| // no trailing slash, treated as pattern, slash doesn't need to be escaped | ||
| verify.importFixModuleSpecifiers("", ["ambient"], { autoImportSpecifierExcludeRegexes: ["/.*?$"]}); | ||
| // no leading slash, treated as pattern, slash doesn't need to be escaped | ||
| verify.importFixModuleSpecifiers("", ["ambient"], { autoImportSpecifierExcludeRegexes: ["^ambient/"] }); | ||
| verify.importFixModuleSpecifiers("", ["ambient/utils"], { autoImportSpecifierExcludeRegexes: ["ambient$"] }); | ||
| verify.importFixModuleSpecifiers("", ["ambient", "ambient/utils"], { autoImportSpecifierExcludeRegexes: ["oops("] }); | ||
|
|
||
| verify.completions({ | ||
| marker: "", | ||
| includes: [{ | ||
| name: "x", | ||
| source: "ambient", | ||
| sourceDisplay: "ambient", | ||
| hasAction: true, | ||
| sortText: completion.SortText.AutoImportSuggestions | ||
| }, { | ||
| name: "x", | ||
| source: "ambient/utils", | ||
| sourceDisplay: "ambient/utils", | ||
| hasAction: true, | ||
| sortText: completion.SortText.AutoImportSuggestions | ||
| }], | ||
| preferences: { | ||
| includeCompletionsForModuleExports: true, | ||
| allowIncompleteCompletions: true | ||
| } | ||
| }); | ||
|
|
||
| verify.completions({ | ||
| marker: "", | ||
| excludes: ["ambient/utils"], | ||
| preferences: { | ||
| includeCompletionsForModuleExports: true, | ||
| allowIncompleteCompletions: true, | ||
| autoImportSpecifierExcludeRegexes: ["utils"] | ||
| }, | ||
| }) |
25 changes: 25 additions & 0 deletions
25
tests/cases/fourslash/autoImportSpecifierExcludeRegexes2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
|
|
||
| // @Filename: /tsconfig.json | ||
| //// { | ||
| //// "compilerOptions": { | ||
| //// "module": "preserve", | ||
| //// "paths": { | ||
| //// "@app/*": ["./src/*"] | ||
| //// } | ||
| //// } | ||
| //// } | ||
|
|
||
| // @Filename: /src/utils.ts | ||
| //// export function add(a: number, b: number) {} | ||
|
|
||
| // @Filename: /src/index.ts | ||
| //// add/**/ | ||
|
|
||
| verify.importFixModuleSpecifiers("", ["./utils"]); | ||
| verify.importFixModuleSpecifiers("", ["@app/utils"], { autoImportSpecifierExcludeRegexes: ["^\\./"] }); | ||
|
|
||
| verify.importFixModuleSpecifiers("", ["@app/utils"], { importModuleSpecifierPreference: "non-relative" }); | ||
| verify.importFixModuleSpecifiers("", ["./utils"], { importModuleSpecifierPreference: "non-relative", autoImportSpecifierExcludeRegexes: ["^@app/"] }); | ||
|
|
||
| verify.importFixModuleSpecifiers("", [], { autoImportSpecifierExcludeRegexes: ["utils"] }); |
25 changes: 25 additions & 0 deletions
25
tests/cases/fourslash/autoImportSpecifierExcludeRegexes3.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
|
|
||
| // @module: preserve | ||
|
|
||
| // @Filename: /node_modules/pkg/package.json | ||
| //// { | ||
| //// "name": "pkg", | ||
| //// "version": "1.0.0", | ||
| //// "exports": { | ||
| //// ".": "./index.js", | ||
| //// "./utils": "./utils.js" | ||
| //// } | ||
| //// } | ||
|
|
||
| // @Filename: /node_modules/pkg/utils.d.ts | ||
| //// export function add(a: number, b: number) {} | ||
|
|
||
| // @Filename: /node_modules/pkg/index.d.ts | ||
| //// export * from "./utils"; | ||
|
|
||
| // @Filename: /src/index.ts | ||
| //// add/**/ | ||
|
|
||
| verify.importFixModuleSpecifiers("", ["pkg", "pkg/utils"]); | ||
| verify.importFixModuleSpecifiers("", ["pkg/utils"], { autoImportSpecifierExcludeRegexes: ["^pkg$"] }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wont this keep memoized map forever in tsserver - think you would want to memoize this and keep lifetime same as excludeRegex or something like that ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I think normally the preference won’t change (or won’t change much) during the life of the server. It might be nice not to have to redo this on every request. Theoretically this could waste memory, but it will only have an impact if the user is changing between thousands of unique strings...