diff --git a/src/rules/no-undefined.md b/src/rules/no-undefined.md index d5741ca..793265f 100644 --- a/src/rules/no-undefined.md +++ b/src/rules/no-undefined.md @@ -56,6 +56,8 @@ if (foo === undefined) { function foo(undefined) { // ... } + +bar(undefined, "lorem"); ``` ::: @@ -76,6 +78,8 @@ if (typeof foo === "undefined") { } global.undefined = "foo"; + +bar(void 0, "lorem"); ``` ::: diff --git a/src/rules/no-underscore-dangle.md b/src/rules/no-underscore-dangle.md index c9807fb..a2eeca1 100644 --- a/src/rules/no-underscore-dangle.md +++ b/src/rules/no-underscore-dangle.md @@ -53,13 +53,15 @@ const foo = (_bar) => {}; 此规则选项为对象: -* `"allow"` 允许指定的标识符有悬空的下划线。 -* `"allowAfterThis": false`(默认值)不允许在 `this` 对象的成员中使用悬空的下划线。 -* `"allowAfterSuper": false`(默认值)不允许在 `super` 对象的成员中使用悬空的下划线。 -* `"allowAfterThisConstructor": false`(默认值)不允许在 `this.constructor` 对象的成员中使用悬空的下划线。 -* `"enforceInMethodNames": false`(默认值)允许在方法名称中使用悬空的下划线。 -* `"enforceInClassFields": false`(默认值)允许在 es2022 类字段名中使用悬空的下划线。 -* `"allowFunctionParams": true`(默认值)允许在函数参数名称中使用悬空的下划线。 +* `"allow"` 允许指定的标识符有悬空的下划线 +* `"allowAfterThis": false`(默认值)不允许在 `this` 对象的成员中使用悬空的下划线 +* `"allowAfterSuper": false`(默认值)不允许在 `super` 对象的成员中使用悬空的下划线 +* `"allowAfterThisConstructor": false`(默认值)不允许在 `this.constructor` 对象的成员中使用悬空的下划线 +* `"enforceInMethodNames": false`(默认值)允许在方法名称中使用悬空的下划线 +* `"enforceInClassFields": false`(默认值)允许在 es2022 类字段名中使用悬空的下划线 +* `"allowInArrayDestructuring": true`(默认值)允许在由数组解构分配的变量名称中使用悬空的下划线 +* `"allowInObjectDestructuring": true`(默认值)允许在对象解构分配的变量名称中使用悬空的下划线 +* `"allowFunctionParams": true`(默认值)允许在函数参数名称中使用悬空的下划线 ### allow @@ -181,6 +183,47 @@ class Foo { ::: +### allowInArrayDestructuring + +使用此规则与 `{ "allowInArrayDestructuring": false }` 选项的**错误**示例: + +::: incorrect + +```js +/*eslint no-underscore-dangle: ["error", { "allowInArrayDestructuring": false }]*/ +const [_foo, _bar] = list; +const [foo_, ..._bar] = list; +const [foo, [bar, _baz]] = list; +``` + +::: + +### allowInObjectDestructuring + +使用此规则与 `{ "allowInObjectDestructuring": false }` 选项的**错误**示例: + +::: incorrect + +```js +/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/ +const { foo, bar: _bar } = collection; +const { foo, bar, _baz } = collection; +``` + +::: + +使用此规则与 `{ "allowInObjectDestructuring": false }` 选项的**正确**示例: + +::: correct + +```js +/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/ +const { foo, bar, _baz: { a, b } } = collection; +const { foo, bar, _baz: baz } = collection; +``` + +::: + ### allowFunctionParams 使用此规则与 `{ "allowFunctionParams": false }` 选项的**错误**示例: diff --git a/src/user-guide/configuring/configuration-files-new.md b/src/user-guide/configuring/configuration-files-new.md index 5663db1..f1cc28a 100644 --- a/src/user-guide/configuring/configuration-files-new.md +++ b/src/user-guide/configuring/configuration-files-new.md @@ -35,21 +35,21 @@ export default [ 每个配置对象都包括了 ESLint 检查一组文件所需的所有信息。配置对象由以下属性组成: -* `files` - 用于表示配置适用的文件范围的 glob 模式数组。在没有指定的情况下,配置对象适用于所有文件。 -* `ignores` - 一个表示配置对象不应适用的文件的 glob 模式数组。如果没有指定,配置对象将适用于所有由 `files` 匹配的文件。 -* `languageOptions` - 一个对象,包含与如何为 linting 配置 JavaScript 有关的设置。 +* `files` - 表示配置适用的文件范围的 glob 模式数组。在没有指定的情况下,配置对象适用于所有与其他配置对象匹配的文件。 +* `ignores` - 表示配置对象不应适用的文件的 glob 模式数组。如果没有指定,配置对象将适用于所有由 `files` 匹配的文件。 +* `languageOptions` - 包含如何配置检查过程中 JavaScript 设置的对。 * `ecmaVersion` - 支持 ECMAScript 的版本。可以是任何年份(`2022`)或版本(`5`)。设置为 `"latest"` 则使用受支持的最新版本(默认为 `"latest"`)。 - * `sourceType` - JavaScript 源码类型。传统脚本文件可以使用 `"script"`,ECMAScript 模块(ESM)可以用 `"module"` ,CommonJS 文件使用 `"commonjs`(默认情况下,用于 `.js` 和 `.mjs` 文件使用 `"module"`;`.cjs` 文件使用 `"commonjs"`) - * `globals` - 用于指定额外对象的对象,这些对象应该在 linting 期间被添加到全局范围。 - * `parser` - 包含 `parse()`方法的对象,或者表示插件内解析器名称的字符串(如 `"pluginName/parserName"`,默认为 `"@/espree"`) + * `sourceType` - JavaScript 源码类型。传统脚本文件可以使用 `"script"`,ECMAScript 模块(ESM)可以用 `"module"` ,CommonJS 文件使用 `"commonjs`(默认情况下,`.js` 和 `.mjs` 文件使用 `"module"`;`.cjs` 文件使用 `"commonjs"`) + * `globals` - 指定额外对象的对象,这些对象应该在检查期间会被添加到全局范围。 + * `parser` - 包含 `parse()` 方法的对象,或者表示插件内解析器名称的字符串(如 `"pluginName/parserName"`,默认为 `"@/espree"`) * `parserOptions` - 指定额外选项的对象,直接传递给解析器的 `parser()` 方法。可用选项基于解析器。 * `linterOptions` - 对象,包含与提示过程有关的设置。 - * `noInlineConfig` - 布尔值,表示是否允许内联配置。 - * `reportUnusedDisableDirectives` - 一个布尔值,表示是否应该跟踪和报告未用的禁用指令。 + * `noInlineConfig` - 表示是否允许内联配置布尔值。 + * `reportUnusedDisableDirectives` - 表示是否应该跟踪和报告未用的禁用指令的布尔值。 * `processor` - 包含 `preprocess()` 和 `postprocess()` 方法的对象,或者表示插件内处理器名称的字符串(如 `"pluginName/processorName"`)。 * `plugins` - 包含插件名称与对应的插件对象的名值对对象。如果指定了 `files`,则只适用于与之匹配匹配的文件。 * `rules` - 包含规则配置的对象。如果指定了 `files` 或 `ignores`,则规则配置只适用于与之匹配匹配的文用。 -* `settings` - 包括名值对的对象,这些信息应该对所有规则可用。 +* `settings` - 包含对所有规则可供的名值对的对象。 ### 指定 `files` 和 `ignores` diff --git a/src/user-guide/core-concepts.md b/src/user-guide/core-concepts.md index ea5849e..a0bed90 100644 --- a/src/user-guide/core-concepts.md +++ b/src/user-guide/core-concepts.md @@ -47,38 +47,36 @@ ESLint 插件是一个包含 ESLint 规则、配置、解析器和环境变量 ## 解析器 -ESLint 解析器 converts code into an abstract syntax tree that ESLint can evaluate. By default, ESLint uses the built-in [Espree](https://github.com/eslint/espree) parser, which is compatible with standard JavaScript runtimes and versions. - ESLint 解析器将代码转换为 ESLint 可以评估的抽象语法树(AST, abstract syntax tree)。默认情况下,ESLint 使用内置的与标准 JavaScript运行时和版本兼容的 [Espree](https://github.com/eslint/espree) 解析器。 自定义解析器让 ESLint 可以解析非标准的 JavaScript 语法。通常自定义解析器会被包含在可共享配置或插件中,这一你就不需要直接使用它们了。 -比如用于让 ESLint 可以解析 TypeScript 代码的 [@typescript-eslint/parser](https://www.npmjs.com/package/@typescript-eslint/parser) 解析器就被包含在 [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint) 项目中。 +比如用于让 ESLint 可以解析 TypeScript 代码的 [@typescript-eslint/parser](https://www.npmjs.com/package/@typescript-eslint/parser) 解析器就被包含在 [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint) 项目中。 ## 自定义处理器 -ESLint 处理器可以从其他类型的文件中提取 JavaScript 代码,然后让 ESLint 对 JavaScript 代码进行检查。另外,你也可以在用 ESLint 解析 JavaScript 代码之前使用一个处理器对其进行处理。 +ESLint 处理器可以从其他类型的文件中提取 JavaScript 代码,然后让 ESLint 对 JavaScript 代码进行检查。另外,你也可以在用 ESLint 解析 JavaScript 代码之前使用处理器先对其进行处理。 例如 [eslint-plugin-markdown](https://github.com/eslint/eslint-plugin-markdown) 就包括一个自定义处理器,让你可以对 Markdown 代码块内的 JavaScript 代码进行检查。 ## 格式化工具 -ESLint 格式化工具决定了命令行检查结果的样子。 +ESLint 格式化工具决定了命令行输出的检查结果的样子。 更多信息请参见[格式化工具](./formatters/)。 ## 集成 -ESLint 相关集成生态是使 ESLint 成为如此有用的工具的原因之一。例如,许多代码编辑器都有 ESLint 扩展,这让工作时,可以即刻在文件中查看相关的代码 ESLint 结果,这样你就不需要使用 ESLint CLI 来查看检查结果。 +ESLint 相关集成生态是使 ESLint 成为如此有用的工具的原因之一。例如,许多代码编辑器都有 ESLint 扩展,这让工作时,可以即刻在文件中查看相关的代码 ESLint 结果,这样你就不需要使用 ESLint 命令行来查看检查结果。 更多信息请参见[集成](./integrations)。 -## CLI & Node.js API +## 命令行 & Node.js API -ESLint CLI 是一个命令行界面,让你可以在终端进行检查。CLI 有各种可以传递给命令的选项。 +ESLint 命令行是一个命令行界面,让你可以在终端进行检查。命令行有各种可以传递给命令的选项。 ESLint 的 Node.js API 让你可以在 Node.js 代码中以编程的方式使用 ESLint。该 API 在开发插件、集成和其他与 ESLint 相关的工具时非常有用。 -除非你以某种方式扩展 ESLint,否则你就应该使用 CLI。 +除非你要以某种方式扩展 ESLint,否则你就应该使用命令行。 更多信息请参见[命令行界面](./command-line-interface)和 [Node.js API](../developer-guide/nodejs-api)。 diff --git a/src/user-guide/formatters/index.md b/src/user-guide/formatters/index.md index b62cd9e..52411f2 100644 --- a/src/user-guide/formatters/index.md +++ b/src/user-guide/formatters/index.md @@ -71,7 +71,7 @@ npx eslint --format fullOfProblems.js 输出示例: ```text -<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file name="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js"><error line="1" column="10" severity="error" message="&apos;addOne&apos; is defined but never used. (no-unused-vars)" source="eslint.rules.no-unused-vars" /><error line="2" column="9" severity="error" message="Use the isNaN function to compare with NaN. (use-isnan)" source="eslint.rules.use-isnan" /><error line="3" column="16" severity="error" message="Unexpected space before unary operator &apos;++&apos;. (space-unary-ops)" source="eslint.rules.space-unary-ops" /><error line="3" column="20" severity="warning" message="Missing semicolon. (semi)" source="eslint.rules.semi" /><error line="4" column="12" severity="warning" message="Unnecessary &apos;else&apos; after &apos;return&apos;. (no-else-return)" source="eslint.rules.no-else-return" /><error line="5" column="1" severity="warning" message="Expected indentation of 8 spaces but found 6. (indent)" source="eslint.rules.indent" /><error line="5" column="7" severity="error" message="Function &apos;addOne&apos; expected a return value. (consistent-return)" source="eslint.rules.consistent-return" /><error line="5" column="13" severity="warning" message="Missing semicolon. (semi)" source="eslint.rules.semi" /><error line="7" column="2" severity="error" message="Unnecessary semicolon. (no-extra-semi)" source="eslint.rules.no-extra-semi" /></file></checkstyle> + ``` ### compact @@ -81,13 +81,13 @@ npx eslint --format fullOfProblems.js 输出示例: ```text -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 1, col 10, Error - 'addOne' is defined but never used. (no-unused-vars) +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 1, col 10, Error - 'addOne' is defined but never used. (no-unused-vars) /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 2, col 9, Error - Use the isNaN function to compare with NaN. (use-isnan) -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 3, col 16, Error - Unexpected space before unary operator '++'. (space-unary-ops) +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 3, col 16, Error - Unexpected space before unary operator '++'. (space-unary-ops) /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 3, col 20, Warning - Missing semicolon. (semi) -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 4, col 12, Warning - Unnecessary 'else' after 'return'. (no-else-return) +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 4, col 12, Warning - Unnecessary 'else' after 'return'. (no-else-return) /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 5, col 1, Warning - Expected indentation of 8 spaces but found 6. (indent) -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 5, col 7, Error - Function 'addOne' expected a return value. (consistent-return) +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 5, col 7, Error - Function 'addOne' expected a return value. (consistent-return) /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 5, col 13, Warning - Missing semicolon. (semi) /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js: line 7, col 2, Error - Unnecessary semicolon. (no-extra-semi) @@ -109,7 +109,7 @@ npx eslint --format fullOfProblems.js 输出示例: ```text -<?xml version="1.0" encoding="utf-8"?><jslint><file name="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js"><issue line="1" char="10" evidence="" reason="&apos;addOne&apos; is defined but never used. (no-unused-vars)" /><issue line="2" char="9" evidence="" reason="Use the isNaN function to compare with NaN. (use-isnan)" /><issue line="3" char="16" evidence="" reason="Unexpected space before unary operator &apos;++&apos;. (space-unary-ops)" /><issue line="3" char="20" evidence="" reason="Missing semicolon. (semi)" /><issue line="4" char="12" evidence="" reason="Unnecessary &apos;else&apos; after &apos;return&apos;. (no-else-return)" /><issue line="5" char="1" evidence="" reason="Expected indentation of 8 spaces but found 6. (indent)" /><issue line="5" char="7" evidence="" reason="Function &apos;addOne&apos; expected a return value. (consistent-return)" /><issue line="5" char="13" evidence="" reason="Missing semicolon. (semi)" /><issue line="7" char="2" evidence="" reason="Unnecessary semicolon. (no-extra-semi)" /></file></jslint> + ``` ### json-with-metadata @@ -121,7 +121,7 @@ npx eslint --format fullOfProblems.js 输出示例: ```text -{"results":[{"filePath":"/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js","messages":[{"ruleId":"no-unused-vars","severity":2,"message":"'addOne' is defined but never used.","line":1,"column":10,"nodeType":"Identifier","messageId":"unusedVar","endLine":1,"endColumn":16},{"ruleId":"use-isnan","severity":2,"message":"Use the isNaN function to compare with NaN.","line":2,"column":9,"nodeType":"BinaryExpression","messageId":"comparisonWithNaN","endLine":2,"endColumn":17},{"ruleId":"space-unary-ops","severity":2,"message":"Unexpected space before unary operator '++'.","line":3,"column":16,"nodeType":"UpdateExpression","messageId":"unexpectedBefore","endLine":3,"endColumn":20,"fix":{"range":[57,58],"text":""}},{"ruleId":"semi","severity":1,"message":"Missing semicolon.","line":3,"column":20,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":4,"endColumn":1,"fix":{"range":[60,60],"text":";"}},{"ruleId":"no-else-return","severity":1,"message":"Unnecessary 'else' after 'return'.","line":4,"column":12,"nodeType":"BlockStatement","messageId":"unexpected","endLine":6,"endColumn":6,"fix":{"range":[0,94],"text":"function addOne(i) {\n if (i != NaN) {\n return i ++\n } \n return\n \n}"}},{"ruleId":"indent","severity":1,"message":"Expected indentation of 8 spaces but found 6.","line":5,"column":1,"nodeType":"Keyword","messageId":"wrongIndentation","endLine":5,"endColumn":7,"fix":{"range":[74,80],"text":" "}},{"ruleId":"consistent-return","severity":2,"message":"Function 'addOne' expected a return value.","line":5,"column":7,"nodeType":"ReturnStatement","messageId":"missingReturnValue","endLine":5,"endColumn":13},{"ruleId":"semi","severity":1,"message":"Missing semicolon.","line":5,"column":13,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":6,"endColumn":1,"fix":{"range":[86,86],"text":";"}},{"ruleId":"no-extra-semi","severity":2,"message":"Unnecessary semicolon.","line":7,"column":2,"nodeType":"EmptyStatement","messageId":"unexpected","endLine":7,"endColumn":3,"fix":{"range":[93,95],"text":"}"}}],"suppressedMessages":[],"errorCount":5,"fatalErrorCount":0,"warningCount":4,"fixableErrorCount":2,"fixableWarningCount":4,"source":"function addOne(i) {\n if (i != NaN) {\n return i ++\n } else {\n return\n }\n};"}],"metadata":{"rulesMeta":{"no-else-return":{"type":"suggestion","docs":{"description":"Disallow `else` blocks after `return` statements in `if` statements","recommended":false,"url":"https://eslint.org/docs/rules/no-else-return"},"schema":[{"type":"object","properties":{"allowElseIf":{"type":"boolean","default":true}},"additionalProperties":false}],"fixable":"code","messages":{"unexpected":"Unnecessary 'else' after 'return'."}},"indent":{"type":"layout","docs":{"description":"Enforce consistent indentation","recommended":false,"url":"https://eslint.org/docs/rules/indent"},"fixable":"whitespace","schema":[{"oneOf":[{"enum":["tab"]},{"type":"integer","minimum":0}]},{"type":"object","properties":{"SwitchCase":{"type":"integer","minimum":0,"default":0},"VariableDeclarator":{"oneOf":[{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},{"type":"object","properties":{"var":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"let":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"const":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]}},"additionalProperties":false}]},"outerIIFEBody":{"oneOf":[{"type":"integer","minimum":0},{"enum":["off"]}]},"MemberExpression":{"oneOf":[{"type":"integer","minimum":0},{"enum":["off"]}]},"FunctionDeclaration":{"type":"object","properties":{"parameters":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"body":{"type":"integer","minimum":0}},"additionalProperties":false},"FunctionExpression":{"type":"object","properties":{"parameters":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"body":{"type":"integer","minimum":0}},"additionalProperties":false},"StaticBlock":{"type":"object","properties":{"body":{"type":"integer","minimum":0}},"additionalProperties":false},"CallExpression":{"type":"object","properties":{"arguments":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]}},"additionalProperties":false},"ArrayExpression":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"ObjectExpression":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"ImportDeclaration":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"flatTernaryExpressions":{"type":"boolean","default":false},"offsetTernaryExpressions":{"type":"boolean","default":false},"ignoredNodes":{"type":"array","items":{"type":"string","not":{"pattern":":exit$"}}},"ignoreComments":{"type":"boolean","default":false}},"additionalProperties":false}],"messages":{"wrongIndentation":"Expected indentation of {{expected}} but found {{actual}}."}},"space-unary-ops":{"type":"layout","docs":{"description":"Enforce consistent spacing before or after unary operators","recommended":false,"url":"https://eslint.org/docs/rules/space-unary-ops"},"fixable":"whitespace","schema":[{"type":"object","properties":{"words":{"type":"boolean","default":true},"nonwords":{"type":"boolean","default":false},"overrides":{"type":"object","additionalProperties":{"type":"boolean"}}},"additionalProperties":false}],"messages":{"unexpectedBefore":"Unexpected space before unary operator '{{operator}}'.","unexpectedAfter":"Unexpected space after unary operator '{{operator}}'.","unexpectedAfterWord":"Unexpected space after unary word operator '{{word}}'.","wordOperator":"Unary word operator '{{word}}' must be followed by whitespace.","operator":"Unary operator '{{operator}}' must be followed by whitespace.","beforeUnaryExpressions":"Space is required before unary expressions '{{token}}'."}},"semi":{"type":"layout","docs":{"description":"Require or disallow semicolons instead of ASI","recommended":false,"url":"https://eslint.org/docs/rules/semi"},"fixable":"code","schema":{"anyOf":[{"type":"array","items":[{"enum":["never"]},{"type":"object","properties":{"beforeStatementContinuationChars":{"enum":["always","any","never"]}},"additionalProperties":false}],"minItems":0,"maxItems":2},{"type":"array","items":[{"enum":["always"]},{"type":"object","properties":{"omitLastInOneLineBlock":{"type":"boolean"}},"additionalProperties":false}],"minItems":0,"maxItems":2}]},"messages":{"missingSemi":"Missing semicolon.","extraSemi":"Extra semicolon."}},"consistent-return":{"type":"suggestion","docs":{"description":"Require `return` statements to either always or never specify values","recommended":false,"url":"https://eslint.org/docs/rules/consistent-return"},"schema":[{"type":"object","properties":{"treatUndefinedAsUnspecified":{"type":"boolean","default":false}},"additionalProperties":false}],"messages":{"missingReturn":"Expected to return a value at the end of {{name}}.","missingReturnValue":"{{name}} expected a return value.","unexpectedReturnValue":"{{name}} expected no return value."}}}}} +{"results":[{"filePath":"/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js","messages":[{"ruleId":"no-unused-vars","severity":2,"message":"'addOne' is defined but never used.","line":1,"column":10,"nodeType":"Identifier","messageId":"unusedVar","endLine":1,"endColumn":16},{"ruleId":"use-isnan","severity":2,"message":"Use the isNaN function to compare with NaN.","line":2,"column":9,"nodeType":"BinaryExpression","messageId":"comparisonWithNaN","endLine":2,"endColumn":17},{"ruleId":"space-unary-ops","severity":2,"message":"Unexpected space before unary operator '++'.","line":3,"column":16,"nodeType":"UpdateExpression","messageId":"unexpectedBefore","endLine":3,"endColumn":20,"fix":{"range":[57,58],"text":""}},{"ruleId":"semi","severity":1,"message":"Missing semicolon.","line":3,"column":20,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":4,"endColumn":1,"fix":{"range":[60,60],"text":";"}},{"ruleId":"no-else-return","severity":1,"message":"Unnecessary 'else' after 'return'.","line":4,"column":12,"nodeType":"BlockStatement","messageId":"unexpected","endLine":6,"endColumn":6,"fix":{"range":[0,94],"text":"function addOne(i) {\n if (i != NaN) {\n return i ++\n } \n return\n \n}"}},{"ruleId":"indent","severity":1,"message":"Expected indentation of 8 spaces but found 6.","line":5,"column":1,"nodeType":"Keyword","messageId":"wrongIndentation","endLine":5,"endColumn":7,"fix":{"range":[74,80],"text":" "}},{"ruleId":"consistent-return","severity":2,"message":"Function 'addOne' expected a return value.","line":5,"column":7,"nodeType":"ReturnStatement","messageId":"missingReturnValue","endLine":5,"endColumn":13},{"ruleId":"semi","severity":1,"message":"Missing semicolon.","line":5,"column":13,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":6,"endColumn":1,"fix":{"range":[86,86],"text":";"}},{"ruleId":"no-extra-semi","severity":2,"message":"Unnecessary semicolon.","line":7,"column":2,"nodeType":"EmptyStatement","messageId":"unexpected","endLine":7,"endColumn":3,"fix":{"range":[93,95],"text":"}"}}],"suppressedMessages":[],"errorCount":5,"fatalErrorCount":0,"warningCount":4,"fixableErrorCount":2,"fixableWarningCount":4,"source":"function addOne(i) {\n if (i != NaN) {\n return i ++\n } else {\n return\n }\n};"}],"metadata":{"rulesMeta":{"no-else-return":{"type":"suggestion","docs":{"description":"Disallow `else` blocks after `return` statements in `if` statements","recommended":false,"url":"https://eslint.org/docs/rules/no-else-return"},"schema":[{"type":"object","properties":{"allowElseIf":{"type":"boolean","default":true}},"additionalProperties":false}],"fixable":"code","messages":{"unexpected":"Unnecessary 'else' after 'return'."}},"indent":{"type":"layout","docs":{"description":"Enforce consistent indentation","recommended":false,"url":"https://eslint.org/docs/rules/indent"},"fixable":"whitespace","schema":[{"oneOf":[{"enum":["tab"]},{"type":"integer","minimum":0}]},{"type":"object","properties":{"SwitchCase":{"type":"integer","minimum":0,"default":0},"VariableDeclarator":{"oneOf":[{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},{"type":"object","properties":{"var":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"let":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"const":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]}},"additionalProperties":false}]},"outerIIFEBody":{"oneOf":[{"type":"integer","minimum":0},{"enum":["off"]}]},"MemberExpression":{"oneOf":[{"type":"integer","minimum":0},{"enum":["off"]}]},"FunctionDeclaration":{"type":"object","properties":{"parameters":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"body":{"type":"integer","minimum":0}},"additionalProperties":false},"FunctionExpression":{"type":"object","properties":{"parameters":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"body":{"type":"integer","minimum":0}},"additionalProperties":false},"StaticBlock":{"type":"object","properties":{"body":{"type":"integer","minimum":0}},"additionalProperties":false},"CallExpression":{"type":"object","properties":{"arguments":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]}},"additionalProperties":false},"ArrayExpression":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"ObjectExpression":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"ImportDeclaration":{"oneOf":[{"type":"integer","minimum":0},{"enum":["first","off"]}]},"flatTernaryExpressions":{"type":"boolean","default":false},"offsetTernaryExpressions":{"type":"boolean","default":false},"ignoredNodes":{"type":"array","items":{"type":"string","not":{"pattern":":exit$"}}},"ignoreComments":{"type":"boolean","default":false}},"additionalProperties":false}],"messages":{"wrongIndentation":"Expected indentation of {{expected}} but found {{actual}}."}},"space-unary-ops":{"type":"layout","docs":{"description":"Enforce consistent spacing before or after unary operators","recommended":false,"url":"https://eslint.org/docs/rules/space-unary-ops"},"fixable":"whitespace","schema":[{"type":"object","properties":{"words":{"type":"boolean","default":true},"nonwords":{"type":"boolean","default":false},"overrides":{"type":"object","additionalProperties":{"type":"boolean"}}},"additionalProperties":false}],"messages":{"unexpectedBefore":"Unexpected space before unary operator '{{operator}}'.","unexpectedAfter":"Unexpected space after unary operator '{{operator}}'.","unexpectedAfterWord":"Unexpected space after unary word operator '{{word}}'.","wordOperator":"Unary word operator '{{word}}' must be followed by whitespace.","operator":"Unary operator '{{operator}}' must be followed by whitespace.","beforeUnaryExpressions":"Space is required before unary expressions '{{token}}'."}},"semi":{"type":"layout","docs":{"description":"Require or disallow semicolons instead of ASI","recommended":false,"url":"https://eslint.org/docs/rules/semi"},"fixable":"code","schema":{"anyOf":[{"type":"array","items":[{"enum":["never"]},{"type":"object","properties":{"beforeStatementContinuationChars":{"enum":["always","any","never"]}},"additionalProperties":false}],"minItems":0,"maxItems":2},{"type":"array","items":[{"enum":["always"]},{"type":"object","properties":{"omitLastInOneLineBlock":{"type":"boolean"}},"additionalProperties":false}],"minItems":0,"maxItems":2}]},"messages":{"missingSemi":"Missing semicolon.","extraSemi":"Extra semicolon."}},"consistent-return":{"type":"suggestion","docs":{"description":"Require `return` statements to either always or never specify values","recommended":false,"url":"https://eslint.org/docs/rules/consistent-return"},"schema":[{"type":"object","properties":{"treatUndefinedAsUnspecified":{"type":"boolean","default":false}},"additionalProperties":false}],"messages":{"missingReturn":"Expected to return a value at the end of {{name}}.","missingReturnValue":"{{name}} expected a return value.","unexpectedReturnValue":"{{name}} expected no return value."}}}}} ``` ### json @@ -133,7 +133,7 @@ npx eslint --format fullOfProblems.js 输出示例: ```text -[{"filePath":"/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js","messages":[{"ruleId":"no-unused-vars","severity":2,"message":"'addOne' is defined but never used.","line":1,"column":10,"nodeType":"Identifier","messageId":"unusedVar","endLine":1,"endColumn":16},{"ruleId":"use-isnan","severity":2,"message":"Use the isNaN function to compare with NaN.","line":2,"column":9,"nodeType":"BinaryExpression","messageId":"comparisonWithNaN","endLine":2,"endColumn":17},{"ruleId":"space-unary-ops","severity":2,"message":"Unexpected space before unary operator '++'.","line":3,"column":16,"nodeType":"UpdateExpression","messageId":"unexpectedBefore","endLine":3,"endColumn":20,"fix":{"range":[57,58],"text":""}},{"ruleId":"semi","severity":1,"message":"Missing semicolon.","line":3,"column":20,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":4,"endColumn":1,"fix":{"range":[60,60],"text":";"}},{"ruleId":"no-else-return","severity":1,"message":"Unnecessary 'else' after 'return'.","line":4,"column":12,"nodeType":"BlockStatement","messageId":"unexpected","endLine":6,"endColumn":6,"fix":{"range":[0,94],"text":"function addOne(i) {\n if (i != NaN) {\n return i ++\n } \n return\n \n}"}},{"ruleId":"indent","severity":1,"message":"Expected indentation of 8 spaces but found 6.","line":5,"column":1,"nodeType":"Keyword","messageId":"wrongIndentation","endLine":5,"endColumn":7,"fix":{"range":[74,80],"text":" "}},{"ruleId":"consistent-return","severity":2,"message":"Function 'addOne' expected a return value.","line":5,"column":7,"nodeType":"ReturnStatement","messageId":"missingReturnValue","endLine":5,"endColumn":13},{"ruleId":"semi","severity":1,"message":"Missing semicolon.","line":5,"column":13,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":6,"endColumn":1,"fix":{"range":[86,86],"text":";"}},{"ruleId":"no-extra-semi","severity":2,"message":"Unnecessary semicolon.","line":7,"column":2,"nodeType":"EmptyStatement","messageId":"unexpected","endLine":7,"endColumn":3,"fix":{"range":[93,95],"text":"}"}}],"suppressedMessages":[],"errorCount":5,"fatalErrorCount":0,"warningCount":4,"fixableErrorCount":2,"fixableWarningCount":4,"source":"function addOne(i) {\n if (i != NaN) {\n return i ++\n } else {\n return\n }\n};"}] +[{"filePath":"/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js","messages":[{"ruleId":"no-unused-vars","severity":2,"message":"'addOne' is defined but never used.","line":1,"column":10,"nodeType":"Identifier","messageId":"unusedVar","endLine":1,"endColumn":16},{"ruleId":"use-isnan","severity":2,"message":"Use the isNaN function to compare with NaN.","line":2,"column":9,"nodeType":"BinaryExpression","messageId":"comparisonWithNaN","endLine":2,"endColumn":17},{"ruleId":"space-unary-ops","severity":2,"message":"Unexpected space before unary operator '++'.","line":3,"column":16,"nodeType":"UpdateExpression","messageId":"unexpectedBefore","endLine":3,"endColumn":20,"fix":{"range":[57,58],"text":""}},{"ruleId":"semi","severity":1,"message":"Missing semicolon.","line":3,"column":20,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":4,"endColumn":1,"fix":{"range":[60,60],"text":";"}},{"ruleId":"no-else-return","severity":1,"message":"Unnecessary 'else' after 'return'.","line":4,"column":12,"nodeType":"BlockStatement","messageId":"unexpected","endLine":6,"endColumn":6,"fix":{"range":[0,94],"text":"function addOne(i) {\n if (i != NaN) {\n return i ++\n } \n return\n \n}"}},{"ruleId":"indent","severity":1,"message":"Expected indentation of 8 spaces but found 6.","line":5,"column":1,"nodeType":"Keyword","messageId":"wrongIndentation","endLine":5,"endColumn":7,"fix":{"range":[74,80],"text":" "}},{"ruleId":"consistent-return","severity":2,"message":"Function 'addOne' expected a return value.","line":5,"column":7,"nodeType":"ReturnStatement","messageId":"missingReturnValue","endLine":5,"endColumn":13},{"ruleId":"semi","severity":1,"message":"Missing semicolon.","line":5,"column":13,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":6,"endColumn":1,"fix":{"range":[86,86],"text":";"}},{"ruleId":"no-extra-semi","severity":2,"message":"Unnecessary semicolon.","line":7,"column":2,"nodeType":"EmptyStatement","messageId":"unexpected","endLine":7,"endColumn":3,"fix":{"range":[93,95],"text":"}"}}],"suppressedMessages":[],"errorCount":5,"fatalErrorCount":0,"warningCount":4,"fixableErrorCount":2,"fixableWarningCount":4,"source":"function addOne(i) {\n if (i != NaN) {\n return i ++\n } else {\n return\n }\n};"}] ``` ### junit @@ -143,20 +143,20 @@ npx eslint --format fullOfProblems.js 输出示例: ```text -<?xml version="1.0" encoding="utf-8"?> -<testsuites> -<testsuite package="org.eslint" time="0" tests="9" errors="9" name="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js"> -<testcase time="0" name="org.eslint.no-unused-vars" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="&apos;addOne&apos; is defined but never used."><![CDATA[line 1, col 10, Error - &apos;addOne&apos; is defined but never used. (no-unused-vars)]]></failure></testcase> -<testcase time="0" name="org.eslint.use-isnan" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="Use the isNaN function to compare with NaN."><![CDATA[line 2, col 9, Error - Use the isNaN function to compare with NaN. (use-isnan)]]></failure></testcase> -<testcase time="0" name="org.eslint.space-unary-ops" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="Unexpected space before unary operator &apos;++&apos;."><![CDATA[line 3, col 16, Error - Unexpected space before unary operator &apos;++&apos;. (space-unary-ops)]]></failure></testcase> -<testcase time="0" name="org.eslint.semi" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="Missing semicolon."><![CDATA[line 3, col 20, Warning - Missing semicolon. (semi)]]></failure></testcase> -<testcase time="0" name="org.eslint.no-else-return" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="Unnecessary &apos;else&apos; after &apos;return&apos;."><![CDATA[line 4, col 12, Warning - Unnecessary &apos;else&apos; after &apos;return&apos;. (no-else-return)]]></failure></testcase> -<testcase time="0" name="org.eslint.indent" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="Expected indentation of 8 spaces but found 6."><![CDATA[line 5, col 1, Warning - Expected indentation of 8 spaces but found 6. (indent)]]></failure></testcase> -<testcase time="0" name="org.eslint.consistent-return" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="Function &apos;addOne&apos; expected a return value."><![CDATA[line 5, col 7, Error - Function &apos;addOne&apos; expected a return value. (consistent-return)]]></failure></testcase> -<testcase time="0" name="org.eslint.semi" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="Missing semicolon."><![CDATA[line 5, col 13, Warning - Missing semicolon. (semi)]]></failure></testcase> -<testcase time="0" name="org.eslint.no-extra-semi" classname="/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems"><failure message="Unnecessary semicolon."><![CDATA[line 7, col 2, Error - Unnecessary semicolon. (no-extra-semi)]]></failure></testcase> -</testsuite> -</testsuites> + + + + + + + + + + + + + + ``` @@ -169,13 +169,13 @@ npx eslint --format fullOfProblems.js ```text /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js - 1:10 error 'addOne' is defined but never used no-unused-vars + 1:10 error 'addOne' is defined but never used no-unused-vars 2:9 error Use the isNaN function to compare with NaN use-isnan - 3:16 error Unexpected space before unary operator '++' space-unary-ops + 3:16 error Unexpected space before unary operator '++' space-unary-ops 3:20 warning Missing semicolon semi - 4:12 warning Unnecessary 'else' after 'return' no-else-return + 4:12 warning Unnecessary 'else' after 'return' no-else-return 5:1 warning Expected indentation of 8 spaces but found 6 indent - 5:7 error Function 'addOne' expected a return value consistent-return + 5:7 error Function 'addOne' expected a return value consistent-return 5:13 warning Missing semicolon semi 7:2 error Unnecessary semicolon no-extra-semi @@ -195,7 +195,7 @@ TAP version 13 1..1 not ok 1 - /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js --- - message: '''addOne'' is defined but never used.' + message: '''addOne'' is defined but never used.' severity: error data: line: 1 @@ -208,7 +208,7 @@ not ok 1 - /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProbl line: 2 column: 9 ruleId: use-isnan - - message: Unexpected space before unary operator '++'. + - message: Unexpected space before unary operator '++'. severity: error data: line: 3 @@ -220,7 +220,7 @@ not ok 1 - /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProbl line: 3 column: 20 ruleId: semi - - message: Unnecessary 'else' after 'return'. + - message: Unnecessary 'else' after 'return'. severity: warning data: line: 4 @@ -232,7 +232,7 @@ not ok 1 - /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProbl line: 5 column: 1 ruleId: indent - - message: Function 'addOne' expected a return value. + - message: Function 'addOne' expected a return value. severity: error data: line: 5 @@ -261,13 +261,13 @@ not ok 1 - /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProbl 输出示例: ```text -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:1:10: 'addOne' is defined but never used. [Error/no-unused-vars] +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:1:10: 'addOne' is defined but never used. [Error/no-unused-vars] /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:2:9: Use the isNaN function to compare with NaN. [Error/use-isnan] -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:3:16: Unexpected space before unary operator '++'. [Error/space-unary-ops] +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:3:16: Unexpected space before unary operator '++'. [Error/space-unary-ops] /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:3:20: Missing semicolon. [Warning/semi] -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:4:12: Unnecessary 'else' after 'return'. [Warning/no-else-return] +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:4:12: Unnecessary 'else' after 'return'. [Warning/no-else-return] /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:5:1: Expected indentation of 8 spaces but found 6. [Warning/indent] -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:5:7: Function 'addOne' expected a return value. [Error/consistent-return] +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:5:7: Function 'addOne' expected a return value. [Error/consistent-return] /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:5:13: Missing semicolon. [Warning/semi] /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js:7:2: Unnecessary semicolon. [Error/no-extra-semi] @@ -281,13 +281,13 @@ not ok 1 - /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProbl 输出示例: ```text -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(1,10): error no-unused-vars : 'addOne' is defined but never used. +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(1,10): error no-unused-vars : 'addOne' is defined but never used. /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(2,9): error use-isnan : Use the isNaN function to compare with NaN. -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(3,16): error space-unary-ops : Unexpected space before unary operator '++'. +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(3,16): error space-unary-ops : Unexpected space before unary operator '++'. /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(3,20): warning semi : Missing semicolon. -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(4,12): warning no-else-return : Unnecessary 'else' after 'return'. +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(4,12): warning no-else-return : Unnecessary 'else' after 'return'. /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(5,1): warning indent : Expected indentation of 8 spaces but found 6. -/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(5,7): error consistent-return : Function 'addOne' expected a return value. +/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(5,7): error consistent-return : Function 'addOne' expected a return value. /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(5,13): warning semi : Missing semicolon. /var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js(7,2): error no-extra-semi : Unnecessary semicolon. diff --git a/src/user-guide/getting-started.md b/src/user-guide/getting-started.md index f5e0531..96bf1ad 100644 --- a/src/user-guide/getting-started.md +++ b/src/user-guide/getting-started.md @@ -8,15 +8,15 @@ eleventyNavigation: --- -ESLint 是一个根据方案识别并报告 ECMAScript/JavaScript 代码问题的工具,其目的是使代码风格更加一致并避免错误。在很多地方它都与 JSLint 和 JSHint 类似,除了: +ESLint 是一个根据方案识别并报告 ECMAScript/JavaScript 代码问题的工具,其目的是使代码风格更加一致并避免错误。 -* ESLint 使用 [Espree](https://github.com/eslint/espree) 对 JavaScript 进行解析。 -* ESLint 在代码中使用 AST 评估方案。 -* ESLint 完全是插件式的,每个规则都是一个插件,你可以在运行时中添加更多插件。 +ESLint 是完全插件化的。每一条规则都是一个插件,你可以在运行时添加更多的插件。你也可以添加社区插件、配置和解析器来扩展 ESLint 的功能。 -## 安装并使用 +## 使用前提 -前提条件:内置 SSL 支持的 [Node.js](https://nodejs.org/en/) 版本(`^12.22.0`、`^14.17.0` 或 `>=16.0.0`),如果你使用的是官方 Node.js 发行版,那么已经内置了 SSL 支持。 +要使用 ESLint,你必须使用内置 SSL 支持的 [Node.js](https://nodejs.org/en/) 版本(`^12.22.0`、`^14.17.0` 或 `>=16.0.0`),如果你使用的是官方 Node.js 发行版,那么已经内置了 SSL 支持。 + +## 快速开始 你可以使用该命令安装并配置 ESLint: @@ -28,15 +28,15 @@ npm init @eslint/config ```shell # 使用 `eslint-config-semistandard` 可共享配置 -# npm 6.x -npm init @eslint/config --config semistandard -# ⚠️ npm 7+ 需要使用额外的双杠: +# npm 7+ npm init @eslint/config -- --config semistandard # 或(可以省略 `eslint-config` 前缀) npm init @eslint/config -- --config eslint-config-semistandard +# ⚠️ npm 6.x 无需使用额外的双杠: +npm init @eslint/config --config semistandard ``` -`--config` 标志也支持传递数组 +`--config` 标志也支持传递数组: ```shell npm init @eslint/config -- --config semistandard,standard @@ -56,8 +56,6 @@ npx eslint yourfile.js yarn run eslint yourfile.js ``` -也可以全局安装 ESLint 而不仅限于本地(使用 `npm install eslint --global`)。但并不推荐这样做,因为无论使用哪种安装方式,你都需要在本地安装插件和可共享配置。 - ## 配置 **注意**:如果你正在使用 1.0.0 以前的版本,请阅读[迁移指南](migrating-to-1.0.0)。 @@ -89,7 +87,55 @@ yarn run eslint yourfile.js } ``` -这一行将启用[所有标记为“推荐”的规则](../rules/)。另外,你也可以通过在 [npmjs.com](https://www.npmjs.com/search?q=eslint-config) 上搜索“eslint-config”并使用别人创建的配置。在没有扩展别人的可共享配置或在配置中明确启用规则时,ESLint 不会限制你的代码。 +这一行将启用[所有标记为“推荐”的规则](../rules)。另外,你也可以通过在 [npmjs.com](https://www.npmjs.com/search?q=eslint-config) 上搜索“eslint-config”并使用别人创建的配置。在没有使用别人的可共享配置或在配置中明确启用规则时,ESLint 不会检查你的代码。 + +## 全局安装 + +也可以全局安装 ESLint 而不仅限于羡慕本地(使用 `npm install eslint --global`)。但并不推荐这样做,因为即使全局安装 ESLint,你仍需要在本地安装插件和可共享配置。 + +## 手动设置 + +你也可以在项目中手动设置 ESLint. + +在开始前你必须确保存在 `package.json` 文件。如果不存在,请优先运行 `npm init` 或 `yarn init` 来创建此文件。 + +1. 在项目中安装 ESLint 包: + + ```shell + npm install --save-dev eslint + ``` + +1. 添加任一[支持的配置文件格式](./configuring/configuration-files#配置文件格式)的 `.eslintrc` 文件。 + + ```shell + # 创建 JavaScript 配置文件 + touch .eslintrc.js + ``` + +1. 在 `.eslintrc` 文件中添加配置。阅读[配置 ESLint 文档](configuring/)学习如何添加规则、环境、自定义配置、插件以及其他内容。 + + ```js + // .eslintrc.js 示例 + module.exports = { + "env": { + "browser": true, + "es2021": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + } + ``` + +1. 使用 ESLint 命令行检查代码: + + ```shell + npx eslint project-dir/ file1.js + ``` + + 更多关于可用命令行选项的信息,参见[命令行文档](./command-line-interface)。 ---