项目原始demo,不改动
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

no-magic-numbers.js 5.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @fileoverview Rule to flag statements that use magic numbers (adapted from https://github.com/danielstjules/buddy.js)
  3. * @author Vincent Lemeunier
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "disallow magic numbers",
  13. category: "Best Practices",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/no-magic-numbers"
  16. },
  17. schema: [{
  18. type: "object",
  19. properties: {
  20. detectObjects: {
  21. type: "boolean"
  22. },
  23. enforceConst: {
  24. type: "boolean"
  25. },
  26. ignore: {
  27. type: "array",
  28. items: {
  29. type: "number"
  30. },
  31. uniqueItems: true
  32. },
  33. ignoreArrayIndexes: {
  34. type: "boolean"
  35. }
  36. },
  37. additionalProperties: false
  38. }]
  39. },
  40. create(context) {
  41. const config = context.options[0] || {},
  42. detectObjects = !!config.detectObjects,
  43. enforceConst = !!config.enforceConst,
  44. ignore = config.ignore || [],
  45. ignoreArrayIndexes = !!config.ignoreArrayIndexes;
  46. /**
  47. * Returns whether the node is number literal
  48. * @param {Node} node - the node literal being evaluated
  49. * @returns {boolean} true if the node is a number literal
  50. */
  51. function isNumber(node) {
  52. return typeof node.value === "number";
  53. }
  54. /**
  55. * Returns whether the number should be ignored
  56. * @param {number} num - the number
  57. * @returns {boolean} true if the number should be ignored
  58. */
  59. function shouldIgnoreNumber(num) {
  60. return ignore.indexOf(num) !== -1;
  61. }
  62. /**
  63. * Returns whether the number should be ignored when used as a radix within parseInt() or Number.parseInt()
  64. * @param {ASTNode} parent - the non-"UnaryExpression" parent
  65. * @param {ASTNode} node - the node literal being evaluated
  66. * @returns {boolean} true if the number should be ignored
  67. */
  68. function shouldIgnoreParseInt(parent, node) {
  69. return parent.type === "CallExpression" && node === parent.arguments[1] &&
  70. (parent.callee.name === "parseInt" ||
  71. parent.callee.type === "MemberExpression" &&
  72. parent.callee.object.name === "Number" &&
  73. parent.callee.property.name === "parseInt");
  74. }
  75. /**
  76. * Returns whether the number should be ignored when used to define a JSX prop
  77. * @param {ASTNode} parent - the non-"UnaryExpression" parent
  78. * @returns {boolean} true if the number should be ignored
  79. */
  80. function shouldIgnoreJSXNumbers(parent) {
  81. return parent.type.indexOf("JSX") === 0;
  82. }
  83. /**
  84. * Returns whether the number should be ignored when used as an array index with enabled 'ignoreArrayIndexes' option.
  85. * @param {ASTNode} parent - the non-"UnaryExpression" parent.
  86. * @returns {boolean} true if the number should be ignored
  87. */
  88. function shouldIgnoreArrayIndexes(parent) {
  89. return parent.type === "MemberExpression" && ignoreArrayIndexes;
  90. }
  91. return {
  92. Literal(node) {
  93. const okTypes = detectObjects ? [] : ["ObjectExpression", "Property", "AssignmentExpression"];
  94. if (!isNumber(node)) {
  95. return;
  96. }
  97. let fullNumberNode;
  98. let parent;
  99. let value;
  100. let raw;
  101. // For negative magic numbers: update the value and parent node
  102. if (node.parent.type === "UnaryExpression" && node.parent.operator === "-") {
  103. fullNumberNode = node.parent;
  104. parent = fullNumberNode.parent;
  105. value = -node.value;
  106. raw = `-${node.raw}`;
  107. } else {
  108. fullNumberNode = node;
  109. parent = node.parent;
  110. value = node.value;
  111. raw = node.raw;
  112. }
  113. if (shouldIgnoreNumber(value) ||
  114. shouldIgnoreParseInt(parent, fullNumberNode) ||
  115. shouldIgnoreArrayIndexes(parent) ||
  116. shouldIgnoreJSXNumbers(parent)) {
  117. return;
  118. }
  119. if (parent.type === "VariableDeclarator") {
  120. if (enforceConst && parent.parent.kind !== "const") {
  121. context.report({
  122. node: fullNumberNode,
  123. message: "Number constants declarations must use 'const'."
  124. });
  125. }
  126. } else if (
  127. okTypes.indexOf(parent.type) === -1 ||
  128. (parent.type === "AssignmentExpression" && parent.left.type === "Identifier")
  129. ) {
  130. context.report({
  131. node: fullNumberNode,
  132. message: "No magic number: {{raw}}.",
  133. data: {
  134. raw
  135. }
  136. });
  137. }
  138. }
  139. };
  140. }
  141. };