项目原始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.
 
 
 
 

173 líneas
5.1 KiB

  1. /**
  2. * @fileoverview Rule to flag use of parseInt without a radix argument
  3. * @author James Allardice
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const MODE_ALWAYS = "always",
  14. MODE_AS_NEEDED = "as-needed";
  15. /**
  16. * Checks whether a given variable is shadowed or not.
  17. *
  18. * @param {eslint-scope.Variable} variable - A variable to check.
  19. * @returns {boolean} `true` if the variable is shadowed.
  20. */
  21. function isShadowed(variable) {
  22. return variable.defs.length >= 1;
  23. }
  24. /**
  25. * Checks whether a given node is a MemberExpression of `parseInt` method or not.
  26. *
  27. * @param {ASTNode} node - A node to check.
  28. * @returns {boolean} `true` if the node is a MemberExpression of `parseInt`
  29. * method.
  30. */
  31. function isParseIntMethod(node) {
  32. return (
  33. node.type === "MemberExpression" &&
  34. !node.computed &&
  35. node.property.type === "Identifier" &&
  36. node.property.name === "parseInt"
  37. );
  38. }
  39. /**
  40. * Checks whether a given node is a valid value of radix or not.
  41. *
  42. * The following values are invalid.
  43. *
  44. * - A literal except numbers.
  45. * - undefined.
  46. *
  47. * @param {ASTNode} radix - A node of radix to check.
  48. * @returns {boolean} `true` if the node is valid.
  49. */
  50. function isValidRadix(radix) {
  51. return !(
  52. (radix.type === "Literal" && typeof radix.value !== "number") ||
  53. (radix.type === "Identifier" && radix.name === "undefined")
  54. );
  55. }
  56. /**
  57. * Checks whether a given node is a default value of radix or not.
  58. *
  59. * @param {ASTNode} radix - A node of radix to check.
  60. * @returns {boolean} `true` if the node is the literal node of `10`.
  61. */
  62. function isDefaultRadix(radix) {
  63. return radix.type === "Literal" && radix.value === 10;
  64. }
  65. //------------------------------------------------------------------------------
  66. // Rule Definition
  67. //------------------------------------------------------------------------------
  68. module.exports = {
  69. meta: {
  70. docs: {
  71. description: "enforce the consistent use of the radix argument when using `parseInt()`",
  72. category: "Best Practices",
  73. recommended: false,
  74. url: "https://eslint.org/docs/rules/radix"
  75. },
  76. schema: [
  77. {
  78. enum: ["always", "as-needed"]
  79. }
  80. ]
  81. },
  82. create(context) {
  83. const mode = context.options[0] || MODE_ALWAYS;
  84. /**
  85. * Checks the arguments of a given CallExpression node and reports it if it
  86. * offends this rule.
  87. *
  88. * @param {ASTNode} node - A CallExpression node to check.
  89. * @returns {void}
  90. */
  91. function checkArguments(node) {
  92. const args = node.arguments;
  93. switch (args.length) {
  94. case 0:
  95. context.report({
  96. node,
  97. message: "Missing parameters."
  98. });
  99. break;
  100. case 1:
  101. if (mode === MODE_ALWAYS) {
  102. context.report({
  103. node,
  104. message: "Missing radix parameter."
  105. });
  106. }
  107. break;
  108. default:
  109. if (mode === MODE_AS_NEEDED && isDefaultRadix(args[1])) {
  110. context.report({
  111. node,
  112. message: "Redundant radix parameter."
  113. });
  114. } else if (!isValidRadix(args[1])) {
  115. context.report({
  116. node,
  117. message: "Invalid radix parameter."
  118. });
  119. }
  120. break;
  121. }
  122. }
  123. return {
  124. "Program:exit"() {
  125. const scope = context.getScope();
  126. let variable;
  127. // Check `parseInt()`
  128. variable = astUtils.getVariableByName(scope, "parseInt");
  129. if (!isShadowed(variable)) {
  130. variable.references.forEach(reference => {
  131. const node = reference.identifier;
  132. if (astUtils.isCallee(node)) {
  133. checkArguments(node.parent);
  134. }
  135. });
  136. }
  137. // Check `Number.parseInt()`
  138. variable = astUtils.getVariableByName(scope, "Number");
  139. if (!isShadowed(variable)) {
  140. variable.references.forEach(reference => {
  141. const node = reference.identifier.parent;
  142. if (isParseIntMethod(node) && astUtils.isCallee(node)) {
  143. checkArguments(node.parent);
  144. }
  145. });
  146. }
  147. }
  148. };
  149. }
  150. };