UNPKG

4.09 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 * Checks whether or not the tokens of two given nodes are same.
33 * @param {ASTNode} left - A node 1 to compare.
34 * @param {ASTNode} right - A node 2 to compare.
35 * @param {SourceCode} sourceCode - The ESLint source code object.
36 * @returns {boolean} the source code for the given node.
37 */
38function equalTokens(left, right, sourceCode) {
39 const tokensL = sourceCode.getTokens(left);
40 const tokensR = sourceCode.getTokens(right);
41
42 if (tokensL.length !== tokensR.length) {
43 return false;
44 }
45 for (let i = 0; i < tokensL.length; ++i) {
46 if (tokensL[i].type !== tokensR[i].type ||
47 tokensL[i].value !== tokensR[i].value
48 ) {
49 return false;
50 }
51 }
52
53 return true;
54}
55
56/**
57 * Checks whether or not `thisArg` is not changed by `.apply()`.
58 * @param {ASTNode|null} expectedThis - The node that is the owner of the applied function.
59 * @param {ASTNode} thisArg - The node that is given to the first argument of the `.apply()`.
60 * @param {RuleContext} context - The ESLint rule context object.
61 * @returns {boolean} Whether or not `thisArg` is not changed by `.apply()`.
62 */
63function isValidThisArg(expectedThis, thisArg, context) {
64 if (!expectedThis) {
65 return astUtils.isNullOrUndefined(thisArg);
66 }
67 return equalTokens(expectedThis, thisArg, context);
68}
69
70//------------------------------------------------------------------------------
71// Rule Definition
72//------------------------------------------------------------------------------
73
74module.exports = {
75 meta: {
76 docs: {
77 description: "require spread operators instead of `.apply()`",
78 category: "ECMAScript 6",
79 recommended: false
80 },
81
82 schema: [],
83
84 fixable: "code"
85 },
86
87 create(context) {
88 const sourceCode = context.getSourceCode();
89
90 return {
91 CallExpression(node) {
92 if (!isVariadicApplyCalling(node)) {
93 return;
94 }
95
96 const applied = node.callee.object;
97 const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
98 const thisArg = node.arguments[0];
99
100 if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
101 context.report({
102 node,
103 message: "Use the spread operator instead of '.apply()'.",
104 fix(fixer) {
105 if (expectedThis && expectedThis.type !== "Identifier") {
106
107 // Don't fix cases where the `this` value could be a computed expression.
108 return null;
109 }
110
111 const propertyDot = sourceCode.getFirstTokenBetween(applied, node.callee.property, token => token.value === ".");
112
113 return fixer.replaceTextRange([propertyDot.range[0], node.range[1]], `(...${sourceCode.getText(node.arguments[1])})`);
114 }
115 });
116 }
117 }
118 };
119 }
120};