Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prefer-spread.js 3.4KB

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