Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. module.exports = {
  40. meta: {
  41. type: "suggestion",
  42. docs: {
  43. description: "require spread operators instead of `.apply()`",
  44. category: "ECMAScript 6",
  45. recommended: false,
  46. url: "https://eslint.org/docs/rules/prefer-spread"
  47. },
  48. schema: [],
  49. fixable: null,
  50. messages: {
  51. preferSpread: "Use the spread operator instead of '.apply()'."
  52. }
  53. },
  54. create(context) {
  55. const sourceCode = context.getSourceCode();
  56. return {
  57. CallExpression(node) {
  58. if (!isVariadicApplyCalling(node)) {
  59. return;
  60. }
  61. const applied = astUtils.skipChainExpression(astUtils.skipChainExpression(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. messageId: "preferSpread"
  68. });
  69. }
  70. }
  71. };
  72. }
  73. };