2 * @fileoverview Rule to flag when using constructor without parentheses
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
27 description: "enforce or disallow parentheses when invoking a constructor with no arguments",
28 category: "Stylistic Issues",
30 url: "https://eslint.org/docs/rules/new-parens"
40 enum: ["always", "never"]
49 missing: "Missing '()' invoking a constructor.",
50 unnecessary: "Unnecessary '()' invoking a constructor with no arguments."
55 const options = context.options;
56 const always = options[0] !== "never"; // Default is always
58 const sourceCode = context.getSourceCode();
62 if (node.arguments.length !== 0) {
63 return; // if there are arguments, there have to be parens
66 const lastToken = sourceCode.getLastToken(node);
67 const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken);
69 // `hasParens` is true only if the new expression ends with its own parens, e.g., new new foo() does not end with its own parens
70 const hasParens = hasLastParen &&
71 astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken)) &&
72 node.callee.range[1] < node.range[1];
79 fix: fixer => fixer.insertTextAfter(node, "()")
86 messageId: "unnecessary",
88 fixer.remove(sourceCode.getTokenBefore(lastToken)),
89 fixer.remove(lastToken),
90 fixer.insertTextBefore(node, "("),
91 fixer.insertTextAfter(node, ")")