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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: add fixer for new-parens (fixes #6994) #7047

Merged
merged 1 commit into from
Sep 6, 2016
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
2 changes: 2 additions & 0 deletions docs/rules/new-parens.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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" }]
}
]
});