From 757c49584a5852c468c1b4a0b74ad3aa39d954e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 12 Jun 2021 22:21:31 +0800 Subject: [PATCH] Chore: add some rules to eslint-config-eslint (#14692) * Chore: add some rules to eslint-config-eslint note: this is a breaking for eslint-config-eslint * chore: fix some linting problems * Update lib/eslint/eslint.js Co-authored-by: Milos Djermanovic * chore: update tests Co-authored-by: Milos Djermanovic --- lib/eslint/eslint.js | 7 +++++-- lib/init/npm-utils.js | 3 +-- lib/rule-tester/rule-tester.js | 16 +++++++++++----- lib/rules/comma-style.js | 3 +-- lib/rules/indent.js | 6 ++---- packages/eslint-config-eslint/default.yml | 6 ++++++ tests/lib/eslint/eslint.js | 2 +- 7 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/eslint/eslint.js b/lib/eslint/eslint.js index ae2d2100861..c387ca72c3a 100644 --- a/lib/eslint/eslint.js +++ b/lib/eslint/eslint.js @@ -552,9 +552,12 @@ class ESLint { ...unknownOptions } = options || {}; - for (const key of Object.keys(unknownOptions)) { - throw new Error(`'options' must not include the unknown option '${key}'`); + const unknownOptionKeys = Object.keys(unknownOptions); + + if (unknownOptionKeys.length > 0) { + throw new Error(`'options' must not include the unknown option(s): ${unknownOptionKeys.join(", ")}`); } + if (filePath !== void 0 && !isNonEmptyString(filePath)) { throw new Error("'options.filePath' must be a non-empty string or undefined"); } diff --git a/lib/init/npm-utils.js b/lib/init/npm-utils.js index 35191cc0876..b91a824b126 100644 --- a/lib/init/npm-utils.js +++ b/lib/init/npm-utils.js @@ -50,8 +50,7 @@ function findPackageJson(startDir) { */ function installSyncSaveDev(packages) { const packageList = Array.isArray(packages) ? packages : [packages]; - const npmProcess = spawn.sync("npm", ["i", "--save-dev"].concat(packageList), - { stdio: "inherit" }); + const npmProcess = spawn.sync("npm", ["i", "--save-dev"].concat(packageList), { stdio: "inherit" }); const error = npmProcess.error; if (error && error.code === "ENOENT") { diff --git a/lib/rule-tester/rule-tester.js b/lib/rule-tester/rule-tester.js index b08303c62b2..f177ae3cc17 100644 --- a/lib/rule-tester/rule-tester.js +++ b/lib/rule-tester/rule-tester.js @@ -610,7 +610,8 @@ class RuleTester { const messages = result.messages; assert.strictEqual(messages.length, 0, util.format("Should have no errors but had %d: %s", - messages.length, util.inspect(messages))); + messages.length, + util.inspect(messages))); assertASTDidntChange(result.beforeAST, result.afterAST); } @@ -665,13 +666,18 @@ class RuleTester { } assert.strictEqual(messages.length, item.errors, util.format("Should have %d error%s but had %d: %s", - item.errors, item.errors === 1 ? "" : "s", messages.length, util.inspect(messages))); + item.errors, + item.errors === 1 ? "" : "s", + messages.length, + util.inspect(messages))); } else { assert.strictEqual( - messages.length, item.errors.length, - util.format( + messages.length, item.errors.length, util.format( "Should have %d error%s but had %d: %s", - item.errors.length, item.errors.length === 1 ? "" : "s", messages.length, util.inspect(messages) + item.errors.length, + item.errors.length === 1 ? "" : "s", + messages.length, + util.inspect(messages) ) ); diff --git a/lib/rules/comma-style.js b/lib/rules/comma-style.js index f1a23d63b78..fbdecccaac2 100644 --- a/lib/rules/comma-style.js +++ b/lib/rules/comma-style.js @@ -207,8 +207,7 @@ module.exports = { * they are always valid regardless of an undefined item. */ if (astUtils.isCommaToken(commaToken)) { - validateCommaItemSpacing(previousItemToken, commaToken, - currentItemToken, reportItem); + validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem); } if (item) { diff --git a/lib/rules/indent.js b/lib/rules/indent.js index b1af2a73b33..04f41db9e26 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -1177,8 +1177,7 @@ module.exports = { offsets.setDesiredOffset(questionMarkToken, firstToken, 1); offsets.setDesiredOffset(colonToken, firstToken, 1); - offsets.setDesiredOffset(firstConsequentToken, firstToken, - firstConsequentToken.type === "Punctuator" && + offsets.setDesiredOffset(firstConsequentToken, firstToken, firstConsequentToken.type === "Punctuator" && options.offsetTernaryExpressions ? 2 : 1); /* @@ -1204,8 +1203,7 @@ module.exports = { * If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up * having no expected indentation. */ - offsets.setDesiredOffset(firstAlternateToken, firstToken, - firstAlternateToken.type === "Punctuator" && + offsets.setDesiredOffset(firstAlternateToken, firstToken, firstAlternateToken.type === "Punctuator" && options.offsetTernaryExpressions ? 2 : 1); } } diff --git a/packages/eslint-config-eslint/default.yml b/packages/eslint-config-eslint/default.yml index 8a85313045c..d2b31d9ed77 100644 --- a/packages/eslint-config-eslint/default.yml +++ b/packages/eslint-config-eslint/default.yml @@ -29,6 +29,7 @@ rules: consistent-return: "error" curly: ["error", "all"] default-case: "error" + default-case-last: "error" default-param-last: "error" dot-location: ["error", "property"] dot-notation: ["error", { allowKeywords: true }] @@ -36,8 +37,10 @@ rules: eqeqeq: "error" func-call-spacing: "error" func-style: ["error", "declaration"] + function-call-argument-newline: ["error", "consistent"] function-paren-newline: ["error", "consistent"] generator-star-spacing: "error" + grouped-accessor-pairs: "error" guard-for-in: "error" jsdoc/check-alignment: "error" jsdoc/check-param-names: "error" @@ -79,6 +82,7 @@ rules: no-caller: "error" no-confusing-arrow: "error" no-console: "error" + no-constructor-return: "error" no-delete-var: "error" no-else-return: ["error", { allowElseIf: false }] no-eval: "error" @@ -133,6 +137,7 @@ rules: no-underscore-dangle: ["error", {allowAfterThis: true}] no-unmodified-loop-condition: "error" no-unneeded-ternary: "error" + no-unreachable-loop: "error" no-unused-expressions: "error" no-unused-vars: ["error", {vars: "all", args: "after-used", caughtErrors: "all"}] no-use-before-define: "error" @@ -174,6 +179,7 @@ rules: ] prefer-arrow-callback: "error" prefer-const: "error" + prefer-exponentiation-operator: "error" prefer-numeric-literals: "error" prefer-promise-reject-errors: "error" prefer-regex-literals: "error" diff --git a/tests/lib/eslint/eslint.js b/tests/lib/eslint/eslint.js index c7783a6c059..0845d697083 100644 --- a/tests/lib/eslint/eslint.js +++ b/tests/lib/eslint/eslint.js @@ -846,7 +846,7 @@ describe("ESLint", () => { it("should throw if 'options' argument contains unknown key", async () => { eslint = new ESLint(); - await assert.rejects(() => eslint.lintText("var a = 0", { filename: "foo.js" }), /'options' must not include the unknown option 'filename'/u); + await assert.rejects(() => eslint.lintText("var a = 0", { filename: "foo.js" }), /'options' must not include the unknown option\(s\): filename/u); }); it("should throw if non-string value is given to 'options.filePath' option", async () => {