简体   繁体   中英

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?

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',
      },
    },
  ],
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM