Shivaprasad
Shivaprasad

Reputation: 429

Combining ESLint rules during an override

I am working on a project which has a mix of TypeScript and JavaScript files and I am trying to setup linting rules for them. I am using ESLint to lint the JS files and the typescript eslint plugin to lint the typescript files. Since the codebase I am adding linting to is fairly large I had to turn off a few rules for both JS and TS. This proved to be a little problematic when I tried to create the configuration file for ESLint. In my configuration file. I had the main set of rules for the JS files and then created an override section for *.ts files where I can use the typescript lint rules. My config file looks something like below (The rules section is just representational).

{
    "extends": "eslint:recommended",
    "rules": {
        "no-unused-vars": "off",
        "no-undef": "error",
        "max-classes-per-file": "off"
    },
    "env": {
        "es6": true,
        "browser": true
    },
    "parserOptions": {
        "ecmaVersion": 10
    },
    "overrides": [
        {
            "files": [
                "*.ts"
            ],
            "extends": [
                "eslint:recommended",
                "plugin:@typescript-eslint/recommended"
            ],
            "rules": {
                "@typescript-eslint/no-unsafe-argument": "off",
                "@typescript-eslint/no-unsafe-member-access": "off"
            },
            "parserOptions": {
                "project": "tsconfig.json"
            }
        }
    ]
}

I had to create an override section for TS files since not all JS files were included in tsconfig.json which became problematic when trying to lint all JS and TS files through the same configuration.

The problem I am now running into is that creating the override section for TS files now completely ignores the rule overrides I had done in the JS section and I end up having to configure each ESLint rule in both the JS config the override I have for the TS files. Is there a way I can combine rules from the parent section in the override so that it is just adding additional rules instead of completely replacing them?

Upvotes: 12

Views: 26425

Answers (2)

JoeTidee
JoeTidee

Reputation: 26054

I using eslint 7.32.0 and the root rules also apply to the file types defined in the overrides section.

Upvotes: 1

GOTO 0
GOTO 0

Reputation: 47702

You can add a second override for both *.js and *.ts files, and disable rules there, i.e.:


{
    "extends": "eslint:recommended",
    "rules": {
        "no-undef": "error"
    },
    "env": {
        "es6": true,
        "browser": true
    },
    "parserOptions": {
        "ecmaVersion": 10
    },
    "overrides": [
        {
            "files": [
                "*.ts"
            ],
            "extends": [
                "eslint:recommended",
                "plugin:@typescript-eslint/recommended"
            ],
            "rules": {
                "@typescript-eslint/no-unsafe-argument": "off",
                "@typescript-eslint/no-unsafe-member-access": "off"
            },
            "parserOptions": {
                "project": "tsconfig.json"
            }
        },
        {
            "files": [
                "*.js",
                "*.ts"
            ],
            "rules": {
                "no-unused-vars": "off",
                "max-classes-per-file": "off"
            }
        }
    ]
}

Upvotes: 14

Related Questions