Skip to content

Commit

Permalink
Update: add fixer for new-parens (fixes #6994) (#7047)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and kaicataldo committed Sep 6, 2016
1 parent ff19aa9 commit 657eee5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/rules/new-parens.md
@@ -1,5 +1,7 @@
# require parentheses when invoking a constructor with no arguments (new-parens)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

JavaScript allows the omission of parentheses when invoking a function via the `new` keyword and the constructor has no arguments. However, some coders believe that omitting the parentheses is inconsistent with the rest of the language and thus makes code less clear.

```js
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/new-parens.js
Expand Up @@ -55,7 +55,9 @@ module.exports = {
recommended: false
},

schema: []
schema: [],

fixable: "code"
},

create(context) {
Expand All @@ -73,7 +75,8 @@ module.exports = {
if (!(token && isOpeningParen(token) && isInRange(token, node))) {
context.report({
node,
message: "Missing '()' invoking a constructor."
message: "Missing '()' invoking a constructor.",
fix: fixer => fixer.insertTextAfter(node, "()")
});
}
}
Expand Down
51 changes: 44 additions & 7 deletions tests/lib/rules/new-parens.js
Expand Up @@ -25,15 +25,52 @@ ruleTester.run("new-parens", rule, {
"var a = new (Date)();",
"var a = new ((Date))();",
"var a = (new Date());",
"var a = new foo.Bar();",
"var a = (new Foo()).bar;",
],
invalid: [
{ code: "var a = new Date;", errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}] },
{ code: "var a = new Date", errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}] },
{ code: "var a = new (Date);", errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}] },
{ code: "var a = new (Date)", errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}] },
{ code: "var a = (new Date)", errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}] },
{
code: "var a = new Date;",
output: "var a = new Date();",
errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression" }]
},
{
code: "var a = new Date",
output: "var a = new Date()",
errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}]
},
{
code: "var a = new (Date);",
output: "var a = new (Date)();",
errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}]
},
{
code: "var a = new (Date)",
output: "var a = new (Date)()",
errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}]
},
{
code: "var a = (new Date)",
output: "var a = (new Date())",
errors: [{ message: "Missing '()' invoking a constructor.",
type: "NewExpression"}]
},
{

// This `()` is `CallExpression`'s. This is a call of the result of `new Date`.
{ code: "var a = (new Date)()", errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}] },
// This `()` is `CallExpression`'s. This is a call of the result of `new Date`.
code: "var a = (new Date)()",
output: "var a = (new Date())()",
errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression"}]
},
{
code: "var a = new foo.Bar;",
output: "var a = new foo.Bar();",
errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression" }]
},
{
code: "var a = (new Foo).bar;",
output: "var a = (new Foo()).bar;",
errors: [{ message: "Missing '()' invoking a constructor.", type: "NewExpression" }]
}
]
});

0 comments on commit 657eee5

Please sign in to comment.