UNPKG

3.31 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 );
28}
29
30/**
31 * Checks whether or not the tokens of two given nodes are same.
32 * @param {ASTNode} left - A node 1 to compare.
33 * @param {ASTNode} right - A node 2 to compare.
34 * @param {SourceCode} sourceCode - The ESLint source code object.
35 * @returns {boolean} the source code for the given node.
36 */
37function equalTokens(left, right, sourceCode) {
38 const tokensL = sourceCode.getTokens(left);
39 const tokensR = sourceCode.getTokens(right);
40
41 if (tokensL.length !== tokensR.length) {
42 return false;
43 }
44 for (let i = 0; i < tokensL.length; ++i) {
45 if (tokensL[i].type !== tokensR[i].type ||
46 tokensL[i].value !== tokensR[i].value
47 ) {
48 return false;
49 }
50 }
51
52 return true;
53}
54
55/**
56 * Checks whether or not `thisArg` is not changed by `.apply()`.
57 * @param {ASTNode|null} expectedThis - The node that is the owner of the applied function.
58 * @param {ASTNode} thisArg - The node that is given to the first argument of the `.apply()`.
59 * @param {RuleContext} context - The ESLint rule context object.
60 * @returns {boolean} Whether or not `thisArg` is not changed by `.apply()`.
61 */
62function isValidThisArg(expectedThis, thisArg, context) {
63 if (!expectedThis) {
64 return astUtils.isNullOrUndefined(thisArg);
65 }
66 return equalTokens(expectedThis, thisArg, context);
67}
68
69//------------------------------------------------------------------------------
70// Rule Definition
71//------------------------------------------------------------------------------
72
73module.exports = {
74 meta: {
75 docs: {
76 description: "require spread operators instead of `.apply()`",
77 category: "ECMAScript 6",
78 recommended: false
79 },
80
81 schema: []
82 },
83
84 create(context) {
85 const sourceCode = context.getSourceCode();
86
87 return {
88 CallExpression(node) {
89 if (!isVariadicApplyCalling(node)) {
90 return;
91 }
92
93 const applied = node.callee.object;
94 const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
95 const thisArg = node.arguments[0];
96
97 if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
98 context.report(node, "use the spread operator instead of the '.apply()'.");
99 }
100 }
101 };
102 }
103};