简体   繁体   中英

How to use more than 1 set of rules in extends in eslint yaml file?

If I have

env:
  commonjs: true
  es6: true
  node: true
extends:
- plugin:mocha/recommended
...

I get no error, ie linting passes with

eslint .

but if I try and add in eslint:recommended in addition to the mocha one, ie

env:
  commonjs: true
  es6: true
  node: true
extends:
- eslint:recommended
- plugin:mocha/recommended
...

I get loads of errors as the mocha one isn't applied

71:3   error  'it' is not defined          no-undef  <-- from the mocha tests

How can I have both recommendations in a yaml file?

This is nothing to do with using a YAML file, and the Mocha plugin is being applied in both cases. The first linting passes because, without eslint:recommended , the rule no-undef is not enabled . For example, given the following basic setup:

temp/
  node_modules/
  .eslintrc.yml
  package.json
  package-lock.json
  test.js

Where test.js contains:

describe("something",function () {
  it("should pass linting", function () {});
});

and .eslintrc.yml contains:

extends:
- plugin:mocha/recommended

then linting passes. But if I just add that one rule:

extends:
- plugin:mocha/recommended
rules:
  no-undef: error

then linting fails with:

path/to/temp/test.js
  1:1  error  'describe' is not defined  no-undef
  2:3  error  'it' is not defined        no-undef

✖ 2 problems (2 errors, 0 warnings)

Per the configuration docs , you need to set the environment for the globals to be defined; for example, the following configuration is back to passing:

env:
  mocha: true
extends:
- plugin:mocha/recommended
rules:
  no-undef: error

Now you can enable the full suite of recommended rules and still pass linting:

env:
  mocha: true
extends:
- eslint:recommended
- plugin:mocha/recommended

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