prefer-spread.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @fileoverview A rule to suggest using of the spread operator instead of `.apply()`.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. const astUtils = require("./utils/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Helpers
  9. //------------------------------------------------------------------------------
  10. /**
  11. * Checks whether or not a node is a `.apply()` for variadic.
  12. * @param {ASTNode} node A CallExpression node to check.
  13. * @returns {boolean} Whether or not the node is a `.apply()` for variadic.
  14. */
  15. function isVariadicApplyCalling(node) {
  16. return (
  17. astUtils.isSpecificMemberAccess(node.callee, null, "apply") &&
  18. node.arguments.length === 2 &&
  19. node.arguments[1].type !== "ArrayExpression" &&
  20. node.arguments[1].type !== "SpreadElement"
  21. );
  22. }
  23. /**
  24. * Checks whether or not `thisArg` is not changed by `.apply()`.
  25. * @param {ASTNode|null} expectedThis The node that is the owner of the applied function.
  26. * @param {ASTNode} thisArg The node that is given to the first argument of the `.apply()`.
  27. * @param {RuleContext} context The ESLint rule context object.
  28. * @returns {boolean} Whether or not `thisArg` is not changed by `.apply()`.
  29. */
  30. function isValidThisArg(expectedThis, thisArg, context) {
  31. if (!expectedThis) {
  32. return astUtils.isNullOrUndefined(thisArg);
  33. }
  34. return astUtils.equalTokens(expectedThis, thisArg, context);
  35. }
  36. //------------------------------------------------------------------------------
  37. // Rule Definition
  38. //------------------------------------------------------------------------------
  39. /** @type {import('../types').Rule.RuleModule} */
  40. module.exports = {
  41. meta: {
  42. type: "suggestion",
  43. docs: {
  44. description: "Require spread operators instead of `.apply()`",
  45. recommended: false,
  46. frozen: true,
  47. url: "https://eslint.org/docs/latest/rules/prefer-spread",
  48. },
  49. schema: [],
  50. fixable: null,
  51. messages: {
  52. preferSpread: "Use the spread operator instead of '.apply()'.",
  53. },
  54. },
  55. create(context) {
  56. const sourceCode = context.sourceCode;
  57. return {
  58. CallExpression(node) {
  59. if (!isVariadicApplyCalling(node)) {
  60. return;
  61. }
  62. const applied = astUtils.skipChainExpression(
  63. astUtils.skipChainExpression(node.callee).object,
  64. );
  65. const expectedThis =
  66. applied.type === "MemberExpression" ? applied.object : null;
  67. const thisArg = node.arguments[0];
  68. if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
  69. context.report({
  70. node,
  71. messageId: "preferSpread",
  72. });
  73. }
  74. },
  75. };
  76. },
  77. };