Skip to content

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

Conversation

andrewbranch
Copy link
Member

Closes #55092

This has the limitation that it doesn't compose very intelligently with the importModuleSpecifierEnding preference—you can set that to "js" and then write a regex that excludes \.js$ and the result will be that you get no (relative) auto imports at all. But I think I'd like to try this and get feedback.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Aug 7, 2024
@andrewbranch andrewbranch changed the title Add autoImportSpecifierExcludeRegexes Add autoImportSpecifierExcludeRegexes preference Aug 7, 2024
@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

}

function isExcludedByRegex(moduleSpecifier: string, excludeRegexes: readonly string[] | undefined): boolean {
return some(excludeRegexes, pattern => !!stringToRegex(pattern)?.test(moduleSpecifier));
Copy link
Member

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 ?

Copy link
Member Author

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...

@@ -127,6 +128,15 @@ import {
UserPreferences,
} from "./_namespaces/ts.js";

const stringToRegex = memoizeOne((pattern: string) => {
try {
return new RegExp(pattern);
Copy link
Member

Choose a reason for hiding this comment

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

Do we have any other settings which are controlled by a regex like this? Trying to decide if people expect to be able to set it to a literal like "/foo/i" or something, but maybe that's a bit much.

That and, should this use getRegexFromPattern and handle case insensitive paths?

Copy link
Member Author

Choose a reason for hiding this comment

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

We don’t, but C Spell does seem to allow slashes and flags. I’m not opposed to implementing that. Without the ability to provide flags, I figured case sensitive was the best option.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think people can already do like (?:i) at the start of their patterns too?

I don't know how stable this option is or if you're just experimenting, though. That and I sort of bet people will want to write patterns that start with /...

Copy link
Contributor

Choose a reason for hiding this comment

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

(?:i) isn't supported in js yet afaik so maybe better to require the whole /.../. Requires a little extra parsing but should be more explicit that this is a regex too

Copy link
Member

Choose a reason for hiding this comment

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

Ah, yeah, I'm thinking of re2/Go syntax.

Other places that take regexes are git.branchValidationRegex in vscode, which just passes it into new RegExp https://github.com/microsoft/vscode/blob/8f88d203d76dee8c92e491f8a1e1331d74792b42/extensions/git/src/commands.ts#L2679

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

Looks good, with the caveat that I don't know this area very well.

One question: since autoImportFileExcludePatterns already exists, do these preferences always show up in a way that makes people aware that both exist? Eg in a graphical config UI next to each other, or in completions for the prefix "autoImport|". If that's not the case, I'd be worried that people would discover one but not the other.

@andrewbranch
Copy link
Member Author

@sandersn yes, they will, after the corresponding VS Code PR.

@andrewbranch andrewbranch merged commit 09caaf6 into microsoft:main Aug 9, 2024
32 checks passed
@andrewbranch andrewbranch deleted the feature/autoImportSpecifierExcludeRegexes branch August 9, 2024 18:12
@Knagis
Copy link
Contributor

Knagis commented Aug 27, 2024

not sure if it is worth opening separate issue as it might already been discussed.

Would it be an option to improve this feature to allow specifying for which files the exclusion apply?

In other words, as an example, while in shared-package, i want auto imports for all files, however, while in another-package, i only want to allow shared-package/index.ts and nothing else from there.

@andrewbranch
Copy link
Member Author

I’m assuming you’re using relative imports within shared-package, so you can just exclude ^shared-package/ everywhere.

@sandersn sandersn removed this from PR Backlog Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow ambient module names to be filtered out of auto-imports or something
8 participants