Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Add NewExpression support to comma-style #9591

Merged
merged 1 commit into from Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -48,7 +48,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 @@ -293,6 +294,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 }]
}
]
});