lydell / eslint-plugin-simple-import-sort

Easy autofixable import sorting.
MIT License
2.07k stars 71 forks source link

Explore configuration with real regex instead of strings #165

Open lydell opened 5 months ago

lydell commented 5 months ago

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:

What needs to be done:

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],
        ]
      }],
    },
  },
];
GollyJer commented 4 months ago

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 commented 3 months ago

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