Skip to content
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

Add TS Server option to exclude files from auto-imports #49578

Merged
merged 5 commits into from
Jun 17, 2022

Conversation

andrewbranch
Copy link
Member

Fixes #35395

Adds a TS Server option (to be specified in .vscode/settings.json) called autoImportFileExcludePatterns. The format is identical to tsconfig.json exclude specs, except I plan to do the following normalization on the VS Code side before sending to TS Server:

  • Relative paths will be resolved relative to the folder VS Code has opened.
  • Paths starting with a * will be prefixed with /, e.g. "**/node_modules/aws-sdk" will become "/**/node_modules/aws-sdk"
  • Non-rooted paths (e.g. "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 calling getMergedSymbol 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.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jun 16, 2022
@dalmo3
Copy link

dalmo3 commented Jul 30, 2022

There might be some demand for file-level excludes: see date-fns/date-fns#3113 which could be alleviated by excluding date-fns/!()/**. Although in fairness that looks like a flaw at the library level.

@sergeysova
Copy link

It would be cool to include option for tsconfig.json too. For example to allow WebStorm parse and use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exclude specific files from auto import suggestions
5 participants