项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.
 
 
 
 

114 rader
3.7 KiB

  1. /**
  2. * @fileoverview Rule to disallow `parseInt()` in favor of binary, octal, and hexadecimal literals
  3. * @author Annie Zhang, Henry Zhu
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Checks to see if a CallExpression's callee node is `parseInt` or
  11. * `Number.parseInt`.
  12. * @param {ASTNode} calleeNode The callee node to evaluate.
  13. * @returns {boolean} True if the callee is `parseInt` or `Number.parseInt`,
  14. * false otherwise.
  15. */
  16. function isParseInt(calleeNode) {
  17. switch (calleeNode.type) {
  18. case "Identifier":
  19. return calleeNode.name === "parseInt";
  20. case "MemberExpression":
  21. return calleeNode.object.type === "Identifier" &&
  22. calleeNode.object.name === "Number" &&
  23. calleeNode.property.type === "Identifier" &&
  24. calleeNode.property.name === "parseInt";
  25. // no default
  26. }
  27. return false;
  28. }
  29. //------------------------------------------------------------------------------
  30. // Rule Definition
  31. //------------------------------------------------------------------------------
  32. module.exports = {
  33. meta: {
  34. docs: {
  35. description: "disallow `parseInt()` and `Number.parseInt()` in favor of binary, octal, and hexadecimal literals",
  36. category: "ECMAScript 6",
  37. recommended: false,
  38. url: "https://eslint.org/docs/rules/prefer-numeric-literals"
  39. },
  40. schema: [],
  41. fixable: "code"
  42. },
  43. create(context) {
  44. const sourceCode = context.getSourceCode();
  45. const radixMap = {
  46. 2: "binary",
  47. 8: "octal",
  48. 16: "hexadecimal"
  49. };
  50. const prefixMap = {
  51. 2: "0b",
  52. 8: "0o",
  53. 16: "0x"
  54. };
  55. //----------------------------------------------------------------------
  56. // Public
  57. //----------------------------------------------------------------------
  58. return {
  59. CallExpression(node) {
  60. // doesn't check parseInt() if it doesn't have a radix argument
  61. if (node.arguments.length !== 2) {
  62. return;
  63. }
  64. // only error if the radix is 2, 8, or 16
  65. const radixName = radixMap[node.arguments[1].value];
  66. if (isParseInt(node.callee) &&
  67. radixName &&
  68. node.arguments[0].type === "Literal"
  69. ) {
  70. context.report({
  71. node,
  72. message: "Use {{radixName}} literals instead of {{functionName}}().",
  73. data: {
  74. radixName,
  75. functionName: sourceCode.getText(node.callee)
  76. },
  77. fix(fixer) {
  78. const newPrefix = prefixMap[node.arguments[1].value];
  79. if (+(newPrefix + node.arguments[0].value) !== parseInt(node.arguments[0].value, node.arguments[1].value)) {
  80. /*
  81. * If the newly-produced literal would be invalid, (e.g. 0b1234),
  82. * or it would yield an incorrect parseInt result for some other reason, don't make a fix.
  83. */
  84. return null;
  85. }
  86. return fixer.replaceText(node, prefixMap[node.arguments[1].value] + node.arguments[0].value);
  87. }
  88. });
  89. }
  90. }
  91. };
  92. }
  93. };