Skip to content

Commit

Permalink
Update: Enable function string option in comma-dangle (fixes #12058) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan authored and platinumazure committed Oct 23, 2019
1 parent e15e1f9 commit 7dffe48
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 17 deletions.
10 changes: 5 additions & 5 deletions lib/rules/comma-dangle.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

/**
Expand Down
300 changes: 288 additions & 12 deletions tests/lib/rules/comma-dangle.js
Expand Up @@ -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 }
},
Expand All @@ -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 }
},
Expand Down Expand Up @@ -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" }],
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 7dffe48

Please sign in to comment.