UNPKG

3.43 kBJavaScriptView Raw
1/**
2 * @fileoverview A rule to suggest using of the spread operator instead of `.apply()`.
3 * @author Toru Nagashima
4 */
5
6"use strict";
7
8const astUtils = require("../ast-utils");
9
10//------------------------------------------------------------------------------
11// Helpers
12//------------------------------------------------------------------------------
13
14/**
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.
18 */
19function isVariadicApplyCalling(node) {
20 return (
21 node.callee.type === "MemberExpression" &&
22 node.callee.property.type === "Identifier" &&
23 node.callee.property.name === "apply" &&
24 node.callee.computed === false &&
25 node.arguments.length === 2 &&
26 node.arguments[1].type !== "ArrayExpression" &&
27 node.arguments[1].type !== "SpreadElement"
28 );
29}
30
31
32/**
33 * Checks whether or not `thisArg` is not changed by `.apply()`.
34 * @param {ASTNode|null} expectedThis - The node that is the owner of the applied function.
35 * @param {ASTNode} thisArg - The node that is given to the first argument of the `.apply()`.
36 * @param {RuleContext} context - The ESLint rule context object.
37 * @returns {boolean} Whether or not `thisArg` is not changed by `.apply()`.
38 */
39function isValidThisArg(expectedThis, thisArg, context) {
40 if (!expectedThis) {
41 return astUtils.isNullOrUndefined(thisArg);
42 }
43 return astUtils.equalTokens(expectedThis, thisArg, context);
44}
45
46//------------------------------------------------------------------------------
47// Rule Definition
48//------------------------------------------------------------------------------
49
50module.exports = {
51 meta: {
52 docs: {
53 description: "require spread operators instead of `.apply()`",
54 category: "ECMAScript 6",
55 recommended: false,
56 url: "https://eslint.org/docs/rules/prefer-spread"
57 },
58
59 schema: [],
60
61 fixable: "code"
62 },
63
64 create(context) {
65 const sourceCode = context.getSourceCode();
66
67 return {
68 CallExpression(node) {
69 if (!isVariadicApplyCalling(node)) {
70 return;
71 }
72
73 const applied = node.callee.object;
74 const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
75 const thisArg = node.arguments[0];
76
77 if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
78 context.report({
79 node,
80 message: "Use the spread operator instead of '.apply()'.",
81 fix(fixer) {
82 if (expectedThis && expectedThis.type !== "Identifier") {
83
84 // Don't fix cases where the `this` value could be a computed expression.
85 return null;
86 }
87
88 const propertyDot = sourceCode.getFirstTokenBetween(applied, node.callee.property, token => token.value === ".");
89
90 return fixer.replaceTextRange([propertyDot.range[0], node.range[1]], `(...${sourceCode.getText(node.arguments[1])})`);
91 }
92 });
93 }
94 }
95 };
96 }
97};