123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- function isVariadicApplyCalling(node) {
- return (
- node.callee.type === "MemberExpression" &&
- node.callee.property.type === "Identifier" &&
- node.callee.property.name === "apply" &&
- node.callee.computed === false &&
- node.arguments.length === 2 &&
- node.arguments[1].type !== "ArrayExpression" &&
- node.arguments[1].type !== "SpreadElement"
- );
- }
- function isValidThisArg(expectedThis, thisArg, context) {
- if (!expectedThis) {
- return astUtils.isNullOrUndefined(thisArg);
- }
- return astUtils.equalTokens(expectedThis, thisArg, context);
- }
- module.exports = {
- meta: {
- type: "suggestion",
- docs: {
- description: "require spread operators instead of `.apply()`",
- category: "ECMAScript 6",
- recommended: false,
- url: "https://eslint.org/docs/rules/prefer-spread"
- },
- schema: [],
- fixable: null
- },
- create(context) {
- const sourceCode = context.getSourceCode();
- return {
- CallExpression(node) {
- if (!isVariadicApplyCalling(node)) {
- return;
- }
- const applied = node.callee.object;
- const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
- const thisArg = node.arguments[0];
- if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
- context.report({
- node,
- message: "Use the spread operator instead of '.apply()'."
- });
- }
- }
- };
- }
- };
|