Enforce or disallow parentheses when invoking a constructor with no arguments
Some problems reported by this rule are automatically fixable by the --fix
command line option
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.
var person = new Person;
This rule can enforce or disallow parentheses when invoking a constructor with no arguments using the new
keyword.
This rule takes one option.
"always"
enforces parenthesis after a new constructor with no arguments (default)"never"
enforces no parenthesis after a new constructor with no argumentsExamples of incorrect code for this rule with the "always"
option:
/*eslint new-parens: "error"*/
var person = new Person;
var person = new (Person);
Examples of correct code for this rule with the "always"
option:
/*eslint new-parens: "error"*/
var person = new Person();
var person = new (Person)();
Examples of incorrect code for this rule with the "never"
option:
/*eslint new-parens: ["error", "never"]*/
var person = new Person();
var person = new (Person)();
Examples of correct code for this rule with the "never"
option:
/*eslint new-parens: ["error", "never"]*/
var person = new Person;
var person = (new Person);
var person = new Person("Name");
This rule was introduced in ESLint v0.0.6.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/new-parens