]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/radix.js
import eslint 7.28.0
[pve-eslint.git] / eslint / lib / rules / radix.js
1 /**
2 * @fileoverview Rule to flag use of parseInt without a radix argument
3 * @author James Allardice
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Helpers
16 //------------------------------------------------------------------------------
17
18 const MODE_ALWAYS = "always",
19 MODE_AS_NEEDED = "as-needed";
20
21 const validRadixValues = new Set(Array.from({ length: 37 - 2 }, (_, index) => index + 2));
22
23 /**
24 * Checks whether a given variable is shadowed or not.
25 * @param {eslint-scope.Variable} variable A variable to check.
26 * @returns {boolean} `true` if the variable is shadowed.
27 */
28 function isShadowed(variable) {
29 return variable.defs.length >= 1;
30 }
31
32 /**
33 * Checks whether a given node is a MemberExpression of `parseInt` method or not.
34 * @param {ASTNode} node A node to check.
35 * @returns {boolean} `true` if the node is a MemberExpression of `parseInt`
36 * method.
37 */
38 function isParseIntMethod(node) {
39 return (
40 node.type === "MemberExpression" &&
41 !node.computed &&
42 node.property.type === "Identifier" &&
43 node.property.name === "parseInt"
44 );
45 }
46
47 /**
48 * Checks whether a given node is a valid value of radix or not.
49 *
50 * The following values are invalid.
51 *
52 * - A literal except integers between 2 and 36.
53 * - undefined.
54 * @param {ASTNode} radix A node of radix to check.
55 * @returns {boolean} `true` if the node is valid.
56 */
57 function isValidRadix(radix) {
58 return !(
59 (radix.type === "Literal" && !validRadixValues.has(radix.value)) ||
60 (radix.type === "Identifier" && radix.name === "undefined")
61 );
62 }
63
64 /**
65 * Checks whether a given node is a default value of radix or not.
66 * @param {ASTNode} radix A node of radix to check.
67 * @returns {boolean} `true` if the node is the literal node of `10`.
68 */
69 function isDefaultRadix(radix) {
70 return radix.type === "Literal" && radix.value === 10;
71 }
72
73 //------------------------------------------------------------------------------
74 // Rule Definition
75 //------------------------------------------------------------------------------
76
77 module.exports = {
78 meta: {
79 type: "suggestion",
80
81 docs: {
82 description: "enforce the consistent use of the radix argument when using `parseInt()`",
83 category: "Best Practices",
84 recommended: false,
85 url: "https://eslint.org/docs/rules/radix",
86 suggestion: true
87 },
88
89 schema: [
90 {
91 enum: ["always", "as-needed"]
92 }
93 ],
94
95 messages: {
96 missingParameters: "Missing parameters.",
97 redundantRadix: "Redundant radix parameter.",
98 missingRadix: "Missing radix parameter.",
99 invalidRadix: "Invalid radix parameter, must be an integer between 2 and 36.",
100 addRadixParameter10: "Add radix parameter `10` for parsing decimal numbers."
101 }
102 },
103
104 create(context) {
105 const mode = context.options[0] || MODE_ALWAYS;
106
107 /**
108 * Checks the arguments of a given CallExpression node and reports it if it
109 * offends this rule.
110 * @param {ASTNode} node A CallExpression node to check.
111 * @returns {void}
112 */
113 function checkArguments(node) {
114 const args = node.arguments;
115
116 switch (args.length) {
117 case 0:
118 context.report({
119 node,
120 messageId: "missingParameters"
121 });
122 break;
123
124 case 1:
125 if (mode === MODE_ALWAYS) {
126 context.report({
127 node,
128 messageId: "missingRadix",
129 suggest: [
130 {
131 messageId: "addRadixParameter10",
132 fix(fixer) {
133 const sourceCode = context.getSourceCode();
134 const tokens = sourceCode.getTokens(node);
135 const lastToken = tokens[tokens.length - 1]; // Parenthesis.
136 const secondToLastToken = tokens[tokens.length - 2]; // May or may not be a comma.
137 const hasTrailingComma = secondToLastToken.type === "Punctuator" && secondToLastToken.value === ",";
138
139 return fixer.insertTextBefore(lastToken, hasTrailingComma ? " 10," : ", 10");
140 }
141 }
142 ]
143 });
144 }
145 break;
146
147 default:
148 if (mode === MODE_AS_NEEDED && isDefaultRadix(args[1])) {
149 context.report({
150 node,
151 messageId: "redundantRadix"
152 });
153 } else if (!isValidRadix(args[1])) {
154 context.report({
155 node,
156 messageId: "invalidRadix"
157 });
158 }
159 break;
160 }
161 }
162
163 return {
164 "Program:exit"() {
165 const scope = context.getScope();
166 let variable;
167
168 // Check `parseInt()`
169 variable = astUtils.getVariableByName(scope, "parseInt");
170 if (variable && !isShadowed(variable)) {
171 variable.references.forEach(reference => {
172 const node = reference.identifier;
173
174 if (astUtils.isCallee(node)) {
175 checkArguments(node.parent);
176 }
177 });
178 }
179
180 // Check `Number.parseInt()`
181 variable = astUtils.getVariableByName(scope, "Number");
182 if (variable && !isShadowed(variable)) {
183 variable.references.forEach(reference => {
184 const node = reference.identifier.parent;
185 const maybeCallee = node.parent.type === "ChainExpression"
186 ? node.parent
187 : node;
188
189 if (isParseIntMethod(node) && astUtils.isCallee(maybeCallee)) {
190 checkArguments(maybeCallee.parent);
191 }
192 });
193 }
194 }
195 };
196 }
197 };