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

Explore configuration with real regex instead of strings #165

Open
lydell opened this issue Apr 13, 2024 · 2 comments · May be fixed by #174
Open

Explore configuration with real regex instead of strings #165

lydell opened this issue Apr 13, 2024 · 2 comments · May be fixed by #174
Labels
help wanted Extra attention is needed

Comments

@lydell
Copy link
Owner

lydell commented Apr 13, 2024

Now that ESLint 9 is here with Flat config written in JavaScript, it would be cool if it was possible to use regex literals instead of string literals when configuring this plugin.

Pros:

  • No double escaping. Can write /\w/ instead of "\\w".
  • Can choose flags. Like adding i for case in-sensitive.
  • Can get more help from an IDE since it knows you are writing a regex.

What needs to be done:

  • See if this is possible at all in Flat config.
  • Figure out the eslintrc compatibility story.
  • Update docs.
  • Update tests.
  • Support strings forever since it’s easy and then doesn’t force people to do boring update chores?

Example (the default groups):

import simpleImportSort from "eslint-plugin-simple-import-sort";

export default [
  {
    plugins: {
      "simple-import-sort": simpleImportSort,
    },
    rules: {
      "simple-import-sort/imports": ["error", {
        groups: [
          // Side effect imports.
          [/^\u0000/u],
          // Node.js builtins prefixed with `node:`.
          [/^node:/u],
          // Packages.
          // Things that start with a letter (or digit or underscore), or `@` followed by a letter.
          [/^@?\w/u],
          // Absolute imports and other imports such as Vue-style `@/foo`.
          // Anything not matched in another group.
          [/^/u],
          // Relative imports.
          // Anything that starts with a dot.
          [/^\./u],
        ]
      }],
    },
  },
];
@lydell lydell added the help wanted Extra attention is needed label Apr 13, 2024
@GollyJer
Copy link

regex literals would be cool. To solve your first problem I've been using String.raw

'simple-import-sort/imports': [
        'error',
        {
          groups: [
            // react, react-native, and side effect imports.
            ['^react$', '^react-native$', String.raw`^\u0000`],

            // Things that start with a letter (or digit or underscore), or `@` followed by a letter.
            [String.raw`^@?\w`],

            // Anything starting with a tilde. Anything starting with a dot.
            ['^~/', String.raw`^\.`],

            // type imports
            [String.raw`^@?\w.*\u0000$`, String.raw`^[^.].*\u0000$`, String.raw`^\..*\u0000$`],
          ],
        },

It's maybe slightly more readable and done via another eslint rule so I don't really have to think about it. 😛

@fisker
Copy link
Contributor

fisker commented Jul 2, 2024

See if this is possible at all in Flat config.
Figure out the eslintrc compatibility story.

In my experience, regex literal works for both flat config and eslintrc.

unicorn/prevent-abbreviation accepts Array<string | RegExp> for many years. https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prevent-abbreviations.md#ignore

lishaduck added a commit to lishaduck/eslint-plugin-simple-import-sort that referenced this issue Jul 10, 2024
@lishaduck lishaduck linked a pull request Jul 10, 2024 that will close this issue
lishaduck added a commit to lishaduck/eslint-plugin-simple-import-sort that referenced this issue Jul 10, 2024
lishaduck added a commit to lishaduck/eslint-plugin-simple-import-sort that referenced this issue Jul 10, 2024
lishaduck added a commit to lishaduck/eslint-plugin-simple-import-sort that referenced this issue Jul 10, 2024
lishaduck added a commit to lishaduck/eslint-plugin-simple-import-sort that referenced this issue Jul 10, 2024
lishaduck added a commit to lishaduck/eslint-plugin-simple-import-sort that referenced this issue Jul 10, 2024
@lishaduck lishaduck moved this from Todo to In Review in @lishaduck's todo list Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants