]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-useless-constructor.js
38c3bc3a05cde0de99c94724d9f5b04734125c87
2 * @fileoverview Rule to flag the use of redundant constructors in classes.
3 * @author Alberto RodrÃguez
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
12 * Checks whether a given array of statements is a single call of `super`.
13 * @param {ASTNode[]} body An array of statements to check.
14 * @returns {boolean} `true` if the body is a single call of `super`.
16 function isSingleSuperCall(body
) {
19 body
[0].type
=== "ExpressionStatement" &&
20 body
[0].expression
.type
=== "CallExpression" &&
21 body
[0].expression
.callee
.type
=== "Super"
26 * Checks whether a given node is a pattern which doesn't have any side effects.
27 * Default parameters and Destructuring parameters can have side effects.
28 * @param {ASTNode} node A pattern node.
29 * @returns {boolean} `true` if the node doesn't have any side effects.
31 function isSimple(node
) {
32 return node
.type
=== "Identifier" || node
.type
=== "RestElement";
36 * Checks whether a given array of expressions is `...arguments` or not.
37 * `super(...arguments)` passes all arguments through.
38 * @param {ASTNode[]} superArgs An array of expressions to check.
39 * @returns {boolean} `true` if the superArgs is `...arguments`.
41 function isSpreadArguments(superArgs
) {
43 superArgs
.length
=== 1 &&
44 superArgs
[0].type
=== "SpreadElement" &&
45 superArgs
[0].argument
.type
=== "Identifier" &&
46 superArgs
[0].argument
.name
=== "arguments"
51 * Checks whether given 2 nodes are identifiers which have the same name or not.
52 * @param {ASTNode} ctorParam A node to check.
53 * @param {ASTNode} superArg A node to check.
54 * @returns {boolean} `true` if the nodes are identifiers which have the same
57 function isValidIdentifierPair(ctorParam
, superArg
) {
59 ctorParam
.type
=== "Identifier" &&
60 superArg
.type
=== "Identifier" &&
61 ctorParam
.name
=== superArg
.name
66 * Checks whether given 2 nodes are a rest/spread pair which has the same values.
67 * @param {ASTNode} ctorParam A node to check.
68 * @param {ASTNode} superArg A node to check.
69 * @returns {boolean} `true` if the nodes are a rest/spread pair which has the
72 function isValidRestSpreadPair(ctorParam
, superArg
) {
74 ctorParam
.type
=== "RestElement" &&
75 superArg
.type
=== "SpreadElement" &&
76 isValidIdentifierPair(ctorParam
.argument
, superArg
.argument
)
81 * Checks whether given 2 nodes have the same value or not.
82 * @param {ASTNode} ctorParam A node to check.
83 * @param {ASTNode} superArg A node to check.
84 * @returns {boolean} `true` if the nodes have the same value or not.
86 function isValidPair(ctorParam
, superArg
) {
88 isValidIdentifierPair(ctorParam
, superArg
) ||
89 isValidRestSpreadPair(ctorParam
, superArg
)
94 * Checks whether the parameters of a constructor and the arguments of `super()`
95 * have the same values or not.
96 * @param {ASTNode} ctorParams The parameters of a constructor to check.
97 * @param {ASTNode} superArgs The arguments of `super()` to check.
98 * @returns {boolean} `true` if those have the same values.
100 function isPassingThrough(ctorParams
, superArgs
) {
101 if (ctorParams
.length
!== superArgs
.length
) {
105 for (let i
= 0; i
< ctorParams
.length
; ++i
) {
106 if (!isValidPair(ctorParams
[i
], superArgs
[i
])) {
115 * Checks whether the constructor body is a redundant super call.
116 * @param {Array} body constructor body content.
117 * @param {Array} ctorParams The params to check against super call.
118 * @returns {boolean} true if the constructor body is redundant
120 function isRedundantSuperCall(body
, ctorParams
) {
122 isSingleSuperCall(body
) &&
123 ctorParams
.every(isSimple
) &&
125 isSpreadArguments(body
[0].expression
.arguments
) ||
126 isPassingThrough(ctorParams
, body
[0].expression
.arguments
)
131 //------------------------------------------------------------------------------
133 //------------------------------------------------------------------------------
135 /** @type {import('../shared/types').Rule} */
141 description
: "Disallow unnecessary constructors",
143 url
: "https://eslint.org/docs/rules/no-useless-constructor"
149 noUselessConstructor
: "Useless constructor."
156 * Checks whether a node is a redundant constructor
157 * @param {ASTNode} node node to check
160 function checkForConstructor(node
) {
161 if (node
.kind
!== "constructor") {
166 * Prevent crashing on parsers which do not require class constructor
167 * to have a body, e.g. typescript and flow
169 if (!node
.value
.body
) {
173 const body
= node
.value
.body
.body
;
174 const ctorParams
= node
.value
.params
;
175 const superClass
= node
.parent
.parent
.superClass
;
177 if (superClass
? isRedundantSuperCall(body
, ctorParams
) : (body
.length
=== 0)) {
180 messageId
: "noUselessConstructor"
186 MethodDefinition
: checkForConstructor