]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/new-parens.js
2 * @fileoverview Rule to flag when using constructor without parentheses
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
22 /** @type {import('../shared/types').Rule} */
28 description
: "Enforce or disallow parentheses when invoking a constructor with no arguments",
30 url
: "https://eslint.org/docs/latest/rules/new-parens"
36 enum: ["always", "never"]
40 missing
: "Missing '()' invoking a constructor.",
41 unnecessary
: "Unnecessary '()' invoking a constructor with no arguments."
46 const options
= context
.options
;
47 const always
= options
[0] !== "never"; // Default is always
49 const sourceCode
= context
.sourceCode
;
53 if (node
.arguments
.length
!== 0) {
54 return; // if there are arguments, there have to be parens
57 const lastToken
= sourceCode
.getLastToken(node
);
58 const hasLastParen
= lastToken
&& astUtils
.isClosingParenToken(lastToken
);
60 // `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
61 const hasParens
= hasLastParen
&&
62 astUtils
.isOpeningParenToken(sourceCode
.getTokenBefore(lastToken
)) &&
63 node
.callee
.range
[1] < node
.range
[1];
70 fix
: fixer
=> fixer
.insertTextAfter(node
, "()")
77 messageId
: "unnecessary",
79 fixer
.remove(sourceCode
.getTokenBefore(lastToken
)),
80 fixer
.remove(lastToken
),
81 fixer
.insertTextBefore(node
, "("),
82 fixer
.insertTextAfter(node
, ")")