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

Support RegExp in ignore option of no-unresolved #3087

Open
regseb opened this issue Oct 20, 2024 · 5 comments
Open

Support RegExp in ignore option of no-unresolved #3087

regseb opened this issue Oct 20, 2024 · 5 comments

Comments

@regseb
Copy link

regseb commented Oct 20, 2024

An array of RegExp pattern strings must be set to the ignore option of the import/no-unresolved rule. With a string, linters (and text editors) don't know that it contains a regex and can't check it.

It would be simpler and safer if we could set the RegExp directly:

export default [{
  rules: {
    "import/no-unresolved": ["error", { ignore: [/\.img$/v] }],
  }
]};
@ljharb
Copy link
Member

ljharb commented Oct 20, 2024

Supporting regular expressions in an eslint config is a CVE magnet, and a very bad idea.

What would linters be checking?

@regseb
Copy link
Author

regseb commented Oct 20, 2024

ESLint configuration is written in JavaScript. You can already use RegExp in the configuration:

export default [{
  rules: {
    "import/no-unresolved": ["error", { ignore: [/\.img$/.source] }],
  }
]};

It's cumbersome to write a RegExp, convert it to a string (using .source), and then convert it back to a RegExp (in eslint-module-utils).

eslint-plugin-unicorn supports RegExp in the ignore option of the catch-error-name rule.


I check my ESLint configuration with ESLint, which has a few rules on RegExp. And I have plugins that add others, such as eslint-plugin-regexp.

For example, in this configuration, I don't get a warning by regexp/no-dupe-disjunctions that the jpg extension is present twice (oops, I forgot the e in jpeg).

export default [{
  rules: {
    "import/no-unresolved": ["error", { ignore: ["\\.(png|jpg|gif|webp|jpg)$"] }],
  }
]};

@ljharb
Copy link
Member

ljharb commented Oct 20, 2024

In flat config, yes - not so in eslintrc. Either way, allowing regexp is a footgun because of the CVE potential (whether it's initially a string or not).

The only patterns that I'd see accepting are globs.

@regseb
Copy link
Author

regseb commented Oct 22, 2024

You can add an ignoreGlob option: an array of glob strings. To avoid a breaking change, you deprecate the ignore option. And in eslint-module-utils, you filter on both lists (ignoreGlob and ignore).

And you can do the same for the setting import/ignore.

@ljharb
Copy link
Member

ljharb commented Oct 22, 2024

Sure, that would be fine (i'd call it ignorePatterns, though)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants