项目原始demo,不改动
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
Este repositorio está archivado. Puede ver los archivos y clonarlo, pero no puede subir cambios o reportar incidencias ni pedir Pull Requests.
 
 
 
 

113 líneas
3.2 KiB

  1. /**
  2. * @fileoverview Rule to
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Gets the variable object of `arguments` which is defined implicitly.
  11. * @param {eslint-scope.Scope} scope - A scope to get.
  12. * @returns {eslint-scope.Variable} The found variable object.
  13. */
  14. function getVariableOfArguments(scope) {
  15. const variables = scope.variables;
  16. for (let i = 0; i < variables.length; ++i) {
  17. const variable = variables[i];
  18. if (variable.name === "arguments") {
  19. /*
  20. * If there was a parameter which is named "arguments", the implicit "arguments" is not defined.
  21. * So does fast return with null.
  22. */
  23. return (variable.identifiers.length === 0) ? variable : null;
  24. }
  25. }
  26. /* istanbul ignore next : unreachable */
  27. return null;
  28. }
  29. /**
  30. * Checks if the given reference is not normal member access.
  31. *
  32. * - arguments .... true // not member access
  33. * - arguments[i] .... true // computed member access
  34. * - arguments[0] .... true // computed member access
  35. * - arguments.length .... false // normal member access
  36. *
  37. * @param {eslint-scope.Reference} reference - The reference to check.
  38. * @returns {boolean} `true` if the reference is not normal member access.
  39. */
  40. function isNotNormalMemberAccess(reference) {
  41. const id = reference.identifier;
  42. const parent = id.parent;
  43. return !(
  44. parent.type === "MemberExpression" &&
  45. parent.object === id &&
  46. !parent.computed
  47. );
  48. }
  49. //------------------------------------------------------------------------------
  50. // Rule Definition
  51. //------------------------------------------------------------------------------
  52. module.exports = {
  53. meta: {
  54. docs: {
  55. description: "require rest parameters instead of `arguments`",
  56. category: "ECMAScript 6",
  57. recommended: false,
  58. url: "https://eslint.org/docs/rules/prefer-rest-params"
  59. },
  60. schema: []
  61. },
  62. create(context) {
  63. /**
  64. * Reports a given reference.
  65. *
  66. * @param {eslint-scope.Reference} reference - A reference to report.
  67. * @returns {void}
  68. */
  69. function report(reference) {
  70. context.report({
  71. node: reference.identifier,
  72. loc: reference.identifier.loc,
  73. message: "Use the rest parameters instead of 'arguments'."
  74. });
  75. }
  76. /**
  77. * Reports references of the implicit `arguments` variable if exist.
  78. *
  79. * @returns {void}
  80. */
  81. function checkForArguments() {
  82. const argumentsVar = getVariableOfArguments(context.getScope());
  83. if (argumentsVar) {
  84. argumentsVar
  85. .references
  86. .filter(isNotNormalMemberAccess)
  87. .forEach(report);
  88. }
  89. }
  90. return {
  91. "FunctionDeclaration:exit": checkForArguments,
  92. "FunctionExpression:exit": checkForArguments
  93. };
  94. }
  95. };