2 * @fileoverview Rule to flag use of parseInt without a radix argument
3 * @author James Allardice
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const MODE_ALWAYS = "always",
19 MODE_AS_NEEDED = "as-needed";
22 * Checks whether a given variable is shadowed or not.
23 * @param {eslint-scope.Variable} variable A variable to check.
24 * @returns {boolean} `true` if the variable is shadowed.
26 function isShadowed(variable) {
27 return variable.defs.length >= 1;
31 * Checks whether a given node is a MemberExpression of `parseInt` method or not.
32 * @param {ASTNode} node A node to check.
33 * @returns {boolean} `true` if the node is a MemberExpression of `parseInt`
36 function isParseIntMethod(node) {
38 node.type === "MemberExpression" &&
40 node.property.type === "Identifier" &&
41 node.property.name === "parseInt"
46 * Checks whether a given node is a valid value of radix or not.
48 * The following values are invalid.
50 * - A literal except numbers.
52 * @param {ASTNode} radix A node of radix to check.
53 * @returns {boolean} `true` if the node is valid.
55 function isValidRadix(radix) {
57 (radix.type === "Literal" && typeof radix.value !== "number") ||
58 (radix.type === "Identifier" && radix.name === "undefined")
63 * Checks whether a given node is a default value of radix or not.
64 * @param {ASTNode} radix A node of radix to check.
65 * @returns {boolean} `true` if the node is the literal node of `10`.
67 function isDefaultRadix(radix) {
68 return radix.type === "Literal" && radix.value === 10;
71 //------------------------------------------------------------------------------
73 //------------------------------------------------------------------------------
80 description: "enforce the consistent use of the radix argument when using `parseInt()`",
81 category: "Best Practices",
83 url: "https://eslint.org/docs/rules/radix"
88 enum: ["always", "as-needed"]
94 const mode = context.options[0] || MODE_ALWAYS;
97 * Checks the arguments of a given CallExpression node and reports it if it
99 * @param {ASTNode} node A CallExpression node to check.
102 function checkArguments(node) {
103 const args = node.arguments;
105 switch (args.length) {
109 message: "Missing parameters."
114 if (mode === MODE_ALWAYS) {
117 message: "Missing radix parameter."
123 if (mode === MODE_AS_NEEDED && isDefaultRadix(args[1])) {
126 message: "Redundant radix parameter."
128 } else if (!isValidRadix(args[1])) {
131 message: "Invalid radix parameter."
140 const scope = context.getScope();
143 // Check `parseInt()`
144 variable = astUtils.getVariableByName(scope, "parseInt");
145 if (!isShadowed(variable)) {
146 variable.references.forEach(reference => {
147 const node = reference.identifier;
149 if (astUtils.isCallee(node)) {
150 checkArguments(node.parent);
155 // Check `Number.parseInt()`
156 variable = astUtils.getVariableByName(scope, "Number");
157 if (!isShadowed(variable)) {
158 variable.references.forEach(reference => {
159 const node = reference.identifier.parent;
161 if (isParseIntMethod(node) && astUtils.isCallee(node)) {
162 checkArguments(node.parent);