Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple paths to eslint-plugin-import pattern

I am looking to use eslint to force my imports to be in a standard order.

I have got this working, however it leads to a long eslint file as I have many different paths I have TypeScript aliases set up with that I want the eslint rules to exclude when sorting the external packages.

I have it working like this:

"import/order": [
  "error",
  {
    "groups": ["builtin", "external", "internal"],
    "pathGroups": [
      {
        "pattern": "react*",
        "group": "external",
        "position": "before"
      },
      {
        "pattern": "Components/**",
        "group": "internal"
      },
      {
        "pattern": "Constants/**",
        "group": "internal"
      }
      ... more patterns here
    ],
    "pathGroupsExcludedImportTypes": ["react", "internal"],
    "alphabetize": {
      "order": "asc",
      "caseInsensitive": true
    }
  }
],

I was wondering if there was a way to group the patterns, so I could do something more like this:

"import/order": [
  "error",
  {
    "groups": ["builtin", "external", "internal"],
    "pathGroups": [
      {
        "pattern": "react*",
        "group": "external",
        "position": "before"
      },
      {
        "pattern": "Components/**|Constants/**|AnotherAlias/**|AnotherAlias/**",
        "group": "internal"
      }
    ],
    "pathGroupsExcludedImportTypes": ["react", "internal"],
    "alphabetize": {
      "order": "asc",
      "caseInsensitive": true
    }
  }
],
like image 756
user3284707 Avatar asked Aug 10 '26 10:08

user3284707


1 Answers

The pattern is a minimatch pattern. As you can see using this (I think) unofficial tester, to make the pattern match a string among a set, you should use the set operator:

{string1,string2,string3}

In your case, then, you can write like this

//...
"pathGroups": [
      {
        "pattern": "react*",
        "group": "external",
        "position": "before"
      },
      {
        "pattern": "{Components,ConstantsAnotherAlias,AnotherAlias}/**",
        "group": "internal"
      }
    ],
//...

I'm not sure that this would fix your issue, though. I solved it by properly setting the eslint setting import/internal-regex. This setting tells eslint, and then the plugin (as you can see here), how to tell if an import is internal. So you could set it:

//...
"settings": {
  "import/internal-regex": "(Components|Constants|AnotherAlias|AnotherAlias)(/.+)?"
}
//...
like image 190
balsick Avatar answered Aug 15 '26 04:08

balsick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!