]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/prefer-numeric-literals.js
2 * @fileoverview Rule to disallow `parseInt()` in favor of binary, octal, and hexadecimal literals
3 * @author Annie Zhang, Henry Zhu
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const radixMap
= new Map([
19 [2, { system
: "binary", literalPrefix
: "0b" }],
20 [8, { system
: "octal", literalPrefix
: "0o" }],
21 [16, { system
: "hexadecimal", literalPrefix
: "0x" }]
25 * Checks to see if a CallExpression's callee node is `parseInt` or
27 * @param {ASTNode} calleeNode The callee node to evaluate.
28 * @returns {boolean} True if the callee is `parseInt` or `Number.parseInt`,
31 function isParseInt(calleeNode
) {
33 astUtils
.isSpecificId(calleeNode
, "parseInt") ||
34 astUtils
.isSpecificMemberAccess(calleeNode
, "Number", "parseInt")
38 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
47 description
: "disallow `parseInt()` and `Number.parseInt()` in favor of binary, octal, and hexadecimal literals",
48 category
: "ECMAScript 6",
50 url
: "https://eslint.org/docs/rules/prefer-numeric-literals"
56 useLiteral
: "Use {{system}} literals instead of {{functionName}}()."
63 const sourceCode
= context
.getSourceCode();
65 //----------------------------------------------------------------------
67 //----------------------------------------------------------------------
71 "CallExpression[arguments.length=2]"(node
) {
72 const [strNode
, radixNode
] = node
.arguments
,
73 str
= astUtils
.getStaticStringValue(strNode
),
74 radix
= radixNode
.value
;
78 astUtils
.isStringLiteral(strNode
) &&
79 radixNode
.type
=== "Literal" &&
80 typeof radix
=== "number" &&
81 radixMap
.has(radix
) &&
82 isParseInt(node
.callee
)
85 const { system
, literalPrefix
} = radixMap
.get(radix
);
89 messageId
: "useLiteral",
92 functionName
: sourceCode
.getText(node
.callee
)
95 if (sourceCode
.getCommentsInside(node
).length
) {
99 const replacement
= `${literalPrefix}${str}`;
101 if (+replacement
!== parseInt(str
, radix
)) {
104 * If the newly-produced literal would be invalid, (e.g. 0b1234),
105 * or it would yield an incorrect parseInt result for some other reason, don't make a fix.
107 * If `str` had numeric separators, `+replacement` will evaluate to `NaN` because unary `+`
108 * per the specification doesn't support numeric separators. Thus, the above condition will be `true`
109 * (`NaN !== anything` is always `true`) regardless of the `parseInt(str, radix)` value.
110 * Consequently, no autofixes will be made. This is correct behavior because `parseInt` also
111 * doesn't support numeric separators, but it does parse part of the string before the first `_`,
112 * so the autofix would be invalid:
114 * parseInt("1_1", 2) // === 1
120 const tokenBefore
= sourceCode
.getTokenBefore(node
),
121 tokenAfter
= sourceCode
.getTokenAfter(node
);
127 tokenBefore
.range
[1] === node
.range
[0] &&
128 !astUtils
.canTokensBeAdjacent(tokenBefore
, replacement
)
135 node
.range
[1] === tokenAfter
.range
[0] &&
136 !astUtils
.canTokensBeAdjacent(replacement
, tokenAfter
)
141 return fixer
.replaceText(node
, `${prefix}${replacement}${suffix}`);