Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eslint disable extends in override

In the case where you have an override where you want to "downgrade" the js parser, how do you turn off the extends from the parent? parserOptions is easy to override because it is key-based. extends as an empty array does nothing, as it tries to append an empty list to the original. If you set it to null, you get a config parser error.

module.exports = {
  root: true,
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module'
  },
  extends: [
    'eslint:recommended'
  ],
  overrides: [
    {
      files: ['vendor/**/*.js'],
      parserOptions: {
        ecmaVersion: 5,
        sourceType: 'script'
      },
      extends: [] // I want 'eslint:recommended' removed.
    }
  ]
};

The workaround involves looping over any extended rulesets and turning them all off.

module.exports = {
  root: true,
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module'
  },
  extends: [
    'eslint:recommended'
  ],
  overrides: [
    {
      files: ['vendor/**/*.js'],
      parserOptions: {
        ecmaVersion: 5,
        sourceType: 'script'
      },
      rules: Object.keys(require(path.resolve(path.dirname(require.resolve('eslint')), '../conf/eslint-recommended')).rules).reduce((rules, rule) => {
        rules[rule] = 0;
        return rules;
      }, {})
    }
  ]
};

Is there a cleaner way to do this?

like image 783
Kelly Selden Avatar asked Jul 19 '19 07:07

Kelly Selden


1 Answers

Here are two ways to accomplish this:

  1. Separate files: You can place a different .eslintrc.js file inside vendor/ with root: true. That will ignore the .eslintrc.js at your project root.

  2. Separate overrides:

module.exports = {
  root: true,
  overrides: [
    {
      files: ['*.js'],
      excludedFiles: 'vendor/**/*.js',
      // OR
      // files: ['src/**/*.js']
      parserOptions: {
        ecmaVersion: 2018,
        sourceType: 'module',
      },
      extends: ['eslint:recommended'],
    },
    {
      files: ['vendor/**/*.js'],
      parserOptions: {
        ecmaVersion: 5,
        sourceType: 'script',
      },
    },
  ],
};
like image 66
plasticrake Avatar answered Oct 21 '22 07:10

plasticrake