]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/lib/rules/no-magic-numbers.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / lib / rules / no-magic-numbers.js
index cd07f5c3bda9d54e68bd2fe710cd7fc878973a90..510b3f9b26172ec7617d4a0312bc32bc2251707e 100644 (file)
@@ -5,7 +5,7 @@
 
 "use strict";
 
-const { isNumericLiteral } = require("./utils/ast-utils");
+const astUtils = require("./utils/ast-utils");
 
 // Maximum array length by the ECMAScript Specification.
 const MAX_ARRAY_LENGTH = 2 ** 32 - 1;
@@ -61,6 +61,10 @@ module.exports = {
                 ignoreArrayIndexes: {
                     type: "boolean",
                     default: false
+                },
+                ignoreDefaultValues: {
+                    type: "boolean",
+                    default: false
                 }
             },
             additionalProperties: false
@@ -77,7 +81,8 @@ module.exports = {
             detectObjects = !!config.detectObjects,
             enforceConst = !!config.enforceConst,
             ignore = (config.ignore || []).map(normalizeIgnoreValue),
-            ignoreArrayIndexes = !!config.ignoreArrayIndexes;
+            ignoreArrayIndexes = !!config.ignoreArrayIndexes,
+            ignoreDefaultValues = !!config.ignoreDefaultValues;
 
         const okTypes = detectObjects ? [] : ["ObjectExpression", "Property", "AssignmentExpression"];
 
@@ -90,6 +95,17 @@ module.exports = {
             return ignore.indexOf(value) !== -1;
         }
 
+        /**
+         * Returns whether the number is a default value assignment.
+         * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
+         * @returns {boolean} true if the number is a default value
+         */
+        function isDefaultValue(fullNumberNode) {
+            const parent = fullNumberNode.parent;
+
+            return parent.type === "AssignmentPattern" && parent.right === fullNumberNode;
+        }
+
         /**
          * Returns whether the given node is used as a radix within parseInt() or Number.parseInt()
          * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
@@ -100,12 +116,8 @@ module.exports = {
 
             return parent.type === "CallExpression" && fullNumberNode === parent.arguments[1] &&
                 (
-                    parent.callee.name === "parseInt" ||
-                    (
-                        parent.callee.type === "MemberExpression" &&
-                        parent.callee.object.name === "Number" &&
-                        parent.callee.property.name === "parseInt"
-                    )
+                    astUtils.isSpecificId(parent.callee, "parseInt") ||
+                    astUtils.isSpecificMemberAccess(parent.callee, "Number", "parseInt")
                 );
         }
 
@@ -157,7 +169,7 @@ module.exports = {
 
         return {
             Literal(node) {
-                if (!isNumericLiteral(node)) {
+                if (!astUtils.isNumericLiteral(node)) {
                     return;
                 }
 
@@ -176,9 +188,12 @@ module.exports = {
                     raw = node.raw;
                 }
 
+                const parent = fullNumberNode.parent;
+
                 // Always allow radix arguments and JSX props
                 if (
                     isIgnoredValue(value) ||
+                    (ignoreDefaultValues && isDefaultValue(fullNumberNode)) ||
                     isParseIntRadix(fullNumberNode) ||
                     isJSXNumber(fullNumberNode) ||
                     (ignoreArrayIndexes && isArrayIndex(fullNumberNode, value))
@@ -186,8 +201,6 @@ module.exports = {
                     return;
                 }
 
-                const parent = fullNumberNode.parent;
-
                 if (parent.type === "VariableDeclarator") {
                     if (enforceConst && parent.parent.kind !== "const") {
                         context.report({