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-numeric-literals.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. type: "suggestion",
  35. docs: {
  36. description: "disallow `parseInt()` and `Number.parseInt()` in favor of binary, octal, and hexadecimal literals",
  37. category: "ECMAScript 6",
  38. recommended: false,
  39. url: "https://eslint.org/docs/rules/prefer-numeric-literals"
  40. },
  41. schema: [],
  42. fixable: "code"
  43. },
  44. create(context) {
  45. const sourceCode = context.getSourceCode();
  46. const radixMap = {
  47. 2: "binary",
  48. 8: "octal",
  49. 16: "hexadecimal"
  50. };
  51. const prefixMap = {
  52. 2: "0b",
  53. 8: "0o",
  54. 16: "0x"
  55. };
  56. //----------------------------------------------------------------------
  57. // Public
  58. //----------------------------------------------------------------------
  59. return {
  60. CallExpression(node) {
  61. // doesn't check parseInt() if it doesn't have a radix argument
  62. if (node.arguments.length !== 2) {
  63. return;
  64. }
  65. // only error if the radix is 2, 8, or 16
  66. const radixName = radixMap[node.arguments[1].value];
  67. if (isParseInt(node.callee) &&
  68. radixName &&
  69. node.arguments[0].type === "Literal"
  70. ) {
  71. context.report({
  72. node,
  73. message: "Use {{radixName}} literals instead of {{functionName}}().",
  74. data: {
  75. radixName,
  76. functionName: sourceCode.getText(node.callee)
  77. },
  78. fix(fixer) {
  79. const newPrefix = prefixMap[node.arguments[1].value];
  80. if (+(newPrefix + node.arguments[0].value) !== parseInt(node.arguments[0].value, node.arguments[1].value)) {
  81. /*
  82. * If the newly-produced literal would be invalid, (e.g. 0b1234),
  83. * or it would yield an incorrect parseInt result for some other reason, don't make a fix.
  84. */
  85. return null;
  86. }
  87. return fixer.replaceText(node, prefixMap[node.arguments[1].value] + node.arguments[0].value);
  88. }
  89. });
  90. }
  91. }
  92. };
  93. }
  94. };