Skip to content

Commit

Permalink
Update: Add NewExpression support to comma-style (#9591)
Browse files Browse the repository at this point in the history
  • Loading branch information
RazerM authored and not-an-aardvark committed Jan 20, 2018
1 parent 4f898c7 commit af043eb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/rules/comma-style.md
Expand Up @@ -37,6 +37,7 @@ This rule also accepts an additional `exceptions` object:
* `"ObjectExpression": true` ignores comma style in object literals
* `"ObjectPattern": true` ignores comma style in object patterns of destructuring
* `"VariableDeclaration": true` ignores comma style in variable declarations
* `"NewExpression": true` ignores comma style in the parameters of constructor expressions

A way to determine the node types as defined by [ESTree](https://github.com/estree/estree) is to use the [online demo](https://eslint.org/parser).

Expand Down
8 changes: 7 additions & 1 deletion lib/rules/comma-style.js
Expand Up @@ -49,7 +49,8 @@ module.exports = {
FunctionDeclaration: true,
FunctionExpression: true,
ImportDeclaration: true,
ObjectPattern: true
ObjectPattern: true,
NewExpression: true
};

if (context.options.length === 2 && context.options[1].hasOwnProperty("exceptions")) {
Expand Down Expand Up @@ -294,6 +295,11 @@ module.exports = {
validateComma(node, "specifiers");
};
}
if (!exceptions.NewExpression) {
nodes.NewExpression = function(node) {
validateComma(node, "arguments");
};
}

return nodes;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/lib/rules/comma-style.js
Expand Up @@ -40,6 +40,7 @@ ruleTester.run("comma-style", rule, {
"var foo = [\n(bar),\nbaz\n];",
"var foo = [\n(bar\n),\nbaz\n];",
"var foo = [\n(\nbar\n),\nbaz\n];",
"new Foo(a\n,b);",
{ code: "var foo = [\n(bar\n)\n,baz\n];", options: ["first"] },
"var foo = \n1, \nbar = [1,\n2,\n3]",
{ code: "var foo = ['apples'\n,'oranges'];", options: ["first"] },
Expand All @@ -49,6 +50,7 @@ ruleTester.run("comma-style", rule, {
{ code: "var foo = [1 \n ,2 \n, 3];", options: ["first"] },
{ code: "function foo(){return {'a': 1\n,'b': 2}}", options: ["first"] },
{ code: "function foo(){var a=[1\n, 2]}", options: ["first"] },
{ code: "new Foo(a,\nb);", options: ["first"] },
"f(1\n, 2);",
"function foo(a\n, b) { return a + b; }",
{
Expand Down Expand Up @@ -129,6 +131,14 @@ ruleTester.run("comma-style", rule, {
ecmaVersion: 6
}
},
{
code: "new Foo(a,\nb);",
options: ["first", {
exceptions: {
NewExpression: true
}
}]
},
{
code: "f(1\n, 2);",
options: ["last", {
Expand Down Expand Up @@ -211,6 +221,22 @@ ruleTester.run("comma-style", rule, {
parserOptions: {
ecmaVersion: 6
}
},
{
code: "new Foo(a,\nb);",
options: ["last", {
exceptions: {
NewExpression: false
}
}]
},
{
code: "new Foo(a\n,b);",
options: ["last", {
exceptions: {
NewExpression: true
}
}]
}
],

Expand Down Expand Up @@ -255,6 +281,16 @@ ruleTester.run("comma-style", rule, {
type: "VariableDeclarator"
}]
},
{
code: "new Foo(a\n,\nb);",
output: "new Foo(a,\nb);",
options: ["last", {
exceptions: {
NewExpression: false
}
}],
errors: [{ message: BAD_LN_BRK_MSG }]
},
{
code: "var foo = 1\n,bar = 2;",
output: "var foo = 1,\nbar = 2;",
Expand Down Expand Up @@ -543,6 +579,16 @@ ruleTester.run("comma-style", rule, {
type: "Property"
}]
},
{
code: "new Foo(a,\nb);",
output: "new Foo(a\n,b);",
options: ["first", {
exceptions: {
NewExpression: false
}
}],
errors: [{ message: FIRST_MSG }]
},
{
code: "var foo = [\n(bar\n)\n,\nbaz\n];",
output: "var foo = [\n(bar\n),\nbaz\n];",
Expand All @@ -555,6 +601,16 @@ ruleTester.run("comma-style", rule, {
code: "[(foo),\n,\nbar]",
output: "[(foo),,\nbar]",
errors: [{ message: BAD_LN_BRK_MSG }]
},
{
code: "new Foo(a\n,b);",
output: "new Foo(a,\nb);",
options: ["last", {
exceptions: {
NewExpression: false
}
}],
errors: [{ message: LAST_MSG }]
}
]
});

0 comments on commit af043eb

Please sign in to comment.