Search code examples
configurationeslinteslintrctypescript-eslint

Override global eslint config rules in subdirectory


I'm migrating from tslint to eslint. However I'm unable to apply the rules in a similar way. A .eslintrc.json in a subdirectory is just ignored. But I need to override some rules for some subdirectories. In this case, the selector prefix should be ui instead app for all files in this subdirectory.

Root .eslintrc.json

{
   "root": true,
   "ignorePatterns": ["projects/**/*"],
   "overrides": [
      {
         "files": ["*.ts"],
         "parserOptions": {
            "project": ["tsconfig.json"],
            "createDefaultProgram": true
         },
         "extends": ["plugin:@angular-eslint/recommended", "plugin:@angular-eslint/template/process-inline-templates"],
         "rules": {
            "@angular-eslint/directive-selector": [
               "error",
               {
                  "type": "attribute",
                  "prefix": "app",
                  "style": "camelCase"
               }
            ],
            "@angular-eslint/component-selector": [
               "error",
               {
                  "type": "element",
                  "prefix": "app",
                  "style": "kebab-case"
               }
            ]
         }
      },
      {
         "files": ["*.html"],
         "extends": ["plugin:@angular-eslint/template/recommended"],
         "rules": {}
      }
   ]
}

src/elements/.eslintrc.json

{
   "overrides": [
      {
         "files": ["*.ts"],
         "rules": {
            "@angular-eslint/directive-selector": [
               "error",
               {
                  "type": "attribute",
                  "prefix": "ui",
                  "style": "camelCase"
               }
            ],
            "@angular-eslint/component-selector": [
               "error",
               {
                  "type": "element",
                  "prefix": "ui",
                  "style": "kebab-case"
               }
            ]
         }
      }
   ]
}

Solution

  • Turned out I had to add the tsconfig.json path in parserOptions. So the overriding file looks like this:

    {
       "overrides": [
          {
             "files": ["*.ts"],
             "parserOptions": {
                "project": ["../../../tsconfig.json"],
                "createDefaultProgram": true
             },
             "rules": {
                "@angular-eslint/directive-selector": [
                   "error",
                   {
                      "type": "attribute",
                      "prefix": "ui",
                      "style": "camelCase"
                   }
                ],
                "@angular-eslint/component-selector": [
                   "error",
                   {
                      "type": "element",
                      "prefix": "ui",
                      "style": "kebab-case"
                   }
                ]
             }
          }
       ]
    }