Add TS Server option to exclude files from auto-imports #49578
Merged
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.
Fixes #35395
Adds a TS Server option (to be specified in
.vscode/settings.json
) calledautoImportFileExcludePatterns
. The format is identical to tsconfig.jsonexclude
specs, except I plan to do the following normalization on the VS Code side before sending to TS Server:*
will be prefixed with/
, e.g."**/node_modules/aws-sdk"
will become"/**/node_modules/aws-sdk"
"node_modules/aws-sdk"
) will be prefixed with/**/
to match any directory prefix.It’s also worth noting how this setting affects ambient modules and module augmentations (
autoImportFileExcludePatterns3.ts
in the tests). While the input to this setting refers to files, I’ve chosen to limit the granularity at which we filter to be per-module, not per-export. That means if more than one file declares an ambient module"foo"
, you can’t exclude the exports declared in just one of those files. We end up callinggetMergedSymbol
on these modules before looking at their individual exports, so we see all the exports from many files lumped together. (Keep in mind this setting does not exclude anything from your program, so excluded files in this setting still contribute to overall program knowledge.) If you wanted that kind of granularity, we’d have to look at the declarations of each export and evaluate the regexes against those rather than just doing it at the file level. With the right memoization, that might be possible, but I’d prefer to wait and see if there’s demand for it. As it is, to exclude an ambient module, the exclude patterns have to match every file that declares it, and its exports are either all included or all excluded.