From 7dffe482d646d4e5f94fa87a22f3b5b2e0a4b189 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Wed, 23 Oct 2019 23:48:22 +0900 Subject: [PATCH] Update: Enable function string option in comma-dangle (fixes #12058) (#12462) --- lib/rules/comma-dangle.js | 10 +- tests/lib/rules/comma-dangle.js | 300 ++++++++++++++++++++++++++++++-- 2 files changed, 293 insertions(+), 17 deletions(-) diff --git a/lib/rules/comma-dangle.js b/lib/rules/comma-dangle.js index 0c177296ea1..fb2d167c77e 100644 --- a/lib/rules/comma-dangle.js +++ b/lib/rules/comma-dangle.js @@ -41,18 +41,17 @@ function isTrailingCommaAllowed(lastItem) { /** * Normalize option value. * @param {string|Object|undefined} optionValue The 1st option value to normalize. + * @param {number} ecmaVersion The normalized ECMAScript version. * @returns {Object} The normalized option value. */ -function normalizeOptions(optionValue) { +function normalizeOptions(optionValue, ecmaVersion) { if (typeof optionValue === "string") { return { arrays: optionValue, objects: optionValue, imports: optionValue, exports: optionValue, - - // For backward compatibility, always ignore functions. - functions: "ignore" + functions: (!ecmaVersion || ecmaVersion < 8) ? "ignore" : optionValue }; } if (typeof optionValue === "object" && optionValue !== null) { @@ -135,7 +134,8 @@ module.exports = { }, create(context) { - const options = normalizeOptions(context.options[0]); + const options = normalizeOptions(context.options[0], context.parserOptions.ecmaVersion); + const sourceCode = context.getSourceCode(); /** diff --git a/tests/lib/rules/comma-dangle.js b/tests/lib/rules/comma-dangle.js index 573c0c2bd42..6d51825eb88 100644 --- a/tests/lib/rules/comma-dangle.js +++ b/tests/lib/rules/comma-dangle.js @@ -235,31 +235,100 @@ ruleTester.run("comma-dangle", rule, { options: ["only-multiline"], parserOptions: { ecmaVersion: 6, sourceType: "module" } }, - - - // trailing comma in functions -- ignore by default { - code: "function foo(a,) {}", + code: "function foo(a) {}", + options: ["always"] + }, + { + code: "foo(a)", + options: ["always"] + }, + { + code: "function foo(a) {}", + options: ["never"] + }, + { + code: "foo(a)", + options: ["never"] + }, + { + code: "function foo(a,\nb) {}", + options: ["always-multiline"] + }, + { + code: "foo(a,\nb)", + options: ["always-multiline"] + }, + { + code: "function foo(a,\nb) {}", + options: ["only-multiline"] + }, + { + code: "foo(a,\nb)", + options: ["only-multiline"] + }, + { + code: "function foo(a) {}", + options: ["always"], + parserOptions: { ecmaVersion: 7 } + }, + { + code: "foo(a)", + options: ["always"], + parserOptions: { ecmaVersion: 7 } + }, + { + code: "function foo(a) {}", + options: ["never"], + parserOptions: { ecmaVersion: 7 } + }, + { + code: "foo(a)", + options: ["never"], + parserOptions: { ecmaVersion: 7 } + }, + { + code: "function foo(a,\nb) {}", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 7 } + }, + { + code: "foo(a,\nb)", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 7 } + }, + { + code: "function foo(a,\nb) {}", + options: ["only-multiline"], + parserOptions: { ecmaVersion: 7 } + }, + { + code: "foo(a,\nb)", + options: ["only-multiline"], + parserOptions: { ecmaVersion: 7 } + }, + { + code: "function foo(a) {}", options: ["never"], parserOptions: { ecmaVersion: 8 } }, { - code: "foo(a,)", + code: "foo(a)", options: ["never"], parserOptions: { ecmaVersion: 8 } }, { - code: "function foo(a) {}", + code: "function foo(a,) {}", options: ["always"], parserOptions: { ecmaVersion: 8 } }, { - code: "foo(a)", + code: "foo(a,)", options: ["always"], parserOptions: { ecmaVersion: 8 } }, { - code: "function foo(\na,\nb\n) {}", + code: "function foo(\na,\nb,\n) {}", options: ["always-multiline"], parserOptions: { ecmaVersion: 8 } }, @@ -269,22 +338,22 @@ ruleTester.run("comma-dangle", rule, { parserOptions: { ecmaVersion: 8 } }, { - code: "function foo(a,b,) {}", + code: "function foo(a,b) {}", options: ["always-multiline"], parserOptions: { ecmaVersion: 8 } }, { - code: "foo(a,b,)", + code: "foo(a,b)", options: ["always-multiline"], parserOptions: { ecmaVersion: 8 } }, { - code: "function foo(a,b,) {}", + code: "function foo(a,b) {}", options: ["only-multiline"], parserOptions: { ecmaVersion: 8 } }, { - code: "foo(a,b,)", + code: "foo(a,b)", options: ["only-multiline"], parserOptions: { ecmaVersion: 8 } }, @@ -315,6 +384,11 @@ ruleTester.run("comma-dangle", rule, { options: [{ functions: "always" }], parserOptions: { ecmaVersion: 8 } }, + { + code: "foo(a,)", + options: [{ functions: "always" }], + parserOptions: { ecmaVersion: 9 } + }, { code: "bar(...a,)", options: [{ functions: "always" }], @@ -1283,6 +1357,208 @@ ruleTester.run("comma-dangle", rule, { parserOptions: { ecmaVersion: 8 }, errors: [{ messageId: "unexpected", type: "SpreadElement" }] }, + { + code: "function foo(a,) {}", + output: "function foo(a) {}", + options: ["never"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "(function foo(a,) {})", + output: "(function foo(a) {})", + options: ["never"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "(a,) => a", + output: "(a) => a", + options: ["never"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "(a,) => (a)", + output: "(a) => (a)", + options: ["never"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "({foo(a,) {}})", + output: "({foo(a) {}})", + options: ["never"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "class A {foo(a,) {}}", + output: "class A {foo(a) {}}", + options: ["never"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "foo(a,)", + output: "foo(a)", + options: ["never"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "foo(...a,)", + output: "foo(...a)", + options: ["never"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "SpreadElement" }] + }, + + { + code: "function foo(a) {}", + output: "function foo(a,) {}", + options: ["always"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, + { + code: "(function foo(a) {})", + output: "(function foo(a,) {})", + options: ["always"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, + { + code: "(a) => a", + output: "(a,) => a", + options: ["always"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, + { + code: "(a) => (a)", + output: "(a,) => (a)", + options: ["always"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, + { + code: "({foo(a) {}})", + output: "({foo(a,) {},})", + options: ["always"], + parserOptions: { ecmaVersion: 8 }, + errors: [ + { messageId: "missing", type: "Identifier" }, + { messageId: "missing", type: "Property" } + ] + }, + { + code: "class A {foo(a) {}}", + output: "class A {foo(a,) {}}", + options: ["always"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, + { + code: "foo(a)", + output: "foo(a,)", + options: ["always"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, + { + code: "foo(...a)", + output: "foo(...a,)", + options: ["always"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "SpreadElement" }] + }, + + { + code: "function foo(a,) {}", + output: "function foo(a) {}", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "(function foo(a,) {})", + output: "(function foo(a) {})", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "foo(a,)", + output: "foo(a)", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "foo(...a,)", + output: "foo(...a)", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "SpreadElement" }] + }, + { + code: "function foo(\na,\nb\n) {}", + output: "function foo(\na,\nb,\n) {}", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, + { + code: "foo(\na,\nb\n)", + output: "foo(\na,\nb,\n)", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, + { + code: "foo(\n...a,\n...b\n)", + output: "foo(\n...a,\n...b,\n)", + options: ["always-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "missing", type: "SpreadElement" }] + }, + + { + code: "function foo(a,) {}", + output: "function foo(a) {}", + options: ["only-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "(function foo(a,) {})", + output: "(function foo(a) {})", + options: ["only-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "foo(a,)", + output: "foo(a)", + options: ["only-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "Identifier" }] + }, + { + code: "foo(...a,)", + output: "foo(...a)", + options: ["only-multiline"], + parserOptions: { ecmaVersion: 8 }, + errors: [{ messageId: "unexpected", type: "SpreadElement" }] + }, + { + code: "function foo(a) {}", + output: "function foo(a,) {}", + options: ["always"], + parserOptions: { ecmaVersion: 9 }, + errors: [{ messageId: "missing", type: "Identifier" }] + }, // separated options {