- Configuration Comments - use JS comments to embed configuration information directly into a file.
- Configuration Files - use a JS, JSON, or YAML file to specify configuration information for an entire directory and all of its subdirectories. This can be in the form of an
.eslintrc
file or aneslintConfig
field in apackage.json
file, both of which ESLint will look for and reed automatically, or you can specify a configuration file on the command line.
Specifying Parser Options
ESLint allows you to specify the JS language options you want to support. By Default, ESLint support only ES5 syntax. You can override that setting to enable support for ES6 and 7 as well as JSX by using parser options.
Parser options are set in your .eslintrc
file by using the parserOptions
property. The available options are:
ecmaVersion
- set to 3, 5(default), 6 or 7 to specify the version of ECMAScript you want to use.sourceType
- set to"script"
(default) or"module"
if your code is in ECMAScript modules.emcaFeatures
- an object indicating which additional language features you’d like to use:* `globalReturn` - allow `return` statements in the global scope.
impliedStrict
- enable globalstrict mode
(ifecmaVersion
is 5 or greater)jsx
: enableJSX
experimentalObjectRestSpread
- enable support for the experimentalobject rest/spread properties
An example:
{
"parserOptions":{
"emcaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules":{
"semi": 2
}
}
`</pre>
### Specifying Parser
By Default, ESLint uses `Espree` as its parser. You can optionally specify that a different parser should be used in your configuration file so long as the parser meets the following requirements:
It must be an npm module installled locally;
It must have an Esprima-compatible interface
It must produce Esprima-compatible AST and token objects.
Specifying ENV
browser
node
commonjs
es6
mocha
`{ "env":{ "browser": true, "node": true } } ### Configuring Rules ESLint comes with a large number of rules, you can modify which rules your project use either using configuration comments or configuration files. To change a rules setting, you must set the rule ID equal to one of these values: - `"off"` or `0` to turn rule off. - `"warn"` or `1` to turn the rule as a warning(doesn't affect exit code) - `"error"` or `2` to turn the rule on as an error(exit code is 1 when triggered) ### Demo `
npm install -g eslint
`
eslint –init
1 | 生成`.eslintrc.js` |