]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/prefer-spread.js
2 * @fileoverview A rule to suggest using of the spread operator instead of `.apply()`.
3 * @author Toru Nagashima
8 const astUtils
= require("./utils/ast-utils");
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
15 * Checks whether or not a node is a `.apply()` for variadic.
16 * @param {ASTNode} node A CallExpression node to check.
17 * @returns {boolean} Whether or not the node is a `.apply()` for variadic.
19 function isVariadicApplyCalling(node
) {
21 astUtils
.isSpecificMemberAccess(node
.callee
, null, "apply") &&
22 node
.arguments
.length
=== 2 &&
23 node
.arguments
[1].type
!== "ArrayExpression" &&
24 node
.arguments
[1].type
!== "SpreadElement"
29 * Checks whether or not `thisArg` is not changed by `.apply()`.
30 * @param {ASTNode|null} expectedThis The node that is the owner of the applied function.
31 * @param {ASTNode} thisArg The node that is given to the first argument of the `.apply()`.
32 * @param {RuleContext} context The ESLint rule context object.
33 * @returns {boolean} Whether or not `thisArg` is not changed by `.apply()`.
35 function isValidThisArg(expectedThis
, thisArg
, context
) {
37 return astUtils
.isNullOrUndefined(thisArg
);
39 return astUtils
.equalTokens(expectedThis
, thisArg
, context
);
42 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
46 /** @type {import('../shared/types').Rule} */
52 description
: "Require spread operators instead of `.apply()`",
54 url
: "https://eslint.org/docs/latest/rules/prefer-spread"
61 preferSpread
: "Use the spread operator instead of '.apply()'."
66 const sourceCode
= context
.sourceCode
;
69 CallExpression(node
) {
70 if (!isVariadicApplyCalling(node
)) {
74 const applied
= astUtils
.skipChainExpression(astUtils
.skipChainExpression(node
.callee
).object
);
75 const expectedThis
= (applied
.type
=== "MemberExpression") ? applied
.object
: null;
76 const thisArg
= node
.arguments
[0];
78 if (isValidThisArg(expectedThis
, thisArg
, sourceCode
)) {
81 messageId
: "preferSpread"