Ali
Ali

Reputation: 1472

In Eslint, how to extend one single rule and not override it totally?

I just wanted to extend a rule (not to override it totally) from a parent .eslintrc.json to the sub directory app/.eslintrc.json file.

This is my parent .eslintrc.json file:

{
  ...

  "overrides": [
    {
      "files": ["*.ts"],
      "rules": {
        "@my-rule-name": [
          "error",
          {
            "param0": "value0",
            "param1": "value1",
            "paramArray": [
              { "a": 0 }
            ]
          }
        ]
      }
    }
  ]
}

This is my sub directory app/.eslintrc.json file:

{
  ...

  "overrides": [
    {
      "files": ["*.ts"],
      "extends": ["../.eslintrc.json"],
      "rules": {
        "@my-rule-name": [
          "error",
          {
            "param2": "value2",
            "paramArray": [
              { "b": 1 }
            ]
          }
        ]
      }
    }
  ]
}

For the child file, I actually like to keep param0, param1, and paramArray from the parent, and just add param2 and more items to the paramArray. Is it possible?

Thanks in advance for any help. I really appreciate that.

Upvotes: 1

Views: 376

Answers (1)

Brad Zacher
Brad Zacher

Reputation: 3263

It is not possible to share configuration like this between configs using standard ESLint tooling.

You will need to build some utilities to help you share it, if you want to go this route.

Often times people will resort to a pattern like

// parent
export const oneSpecificRuleConfig = { 
  "param0": "value0",
  "param1": "value1",
  "paramArray": [
    { "a": 0 }
  ]
};
export default {
  // ...
  "@my-rule-name": ["error", oneSpecificRuleConfig],
};

// child config
import { onsSpecificRuleConfig } from 'parent_config';

export default {
  // ...
  "@my-rule-name": ["error", {
    ...oneSpecificRuleConfig,
    "param2": "value2",
    "paramArray": [
      { "b": 1 }
    ]
  }],
};

Upvotes: 3

Related Questions