"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;
ignoreArrayIndexes: {
type: "boolean",
default: false
+ },
+ ignoreDefaultValues: {
+ type: "boolean",
+ default: false
}
},
additionalProperties: false
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"];
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
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")
);
}
return {
Literal(node) {
- if (!isNumericLiteral(node)) {
+ if (!astUtils.isNumericLiteral(node)) {
return;
}
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))
return;
}
- const parent = fullNumberNode.parent;
-
if (parent.type === "VariableDeclarator") {
if (enforceConst && parent.parent.kind !== "const") {
context.report({