Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita102eaa

Browse files
mdjermanovicplatinumazure
authored andcommitted
Fix: prefer-numeric-literals invalid autofix with adjacent tokens (#12387)
1 parent6e7c18d commita102eaa

File tree

2 files changed

+130
-4
lines changed

2 files changed

+130
-4
lines changed

‎lib/rules/prefer-numeric-literals.js‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
"use strict";
77

8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
constastUtils=require("./utils/ast-utils");
13+
814
//------------------------------------------------------------------------------
915
// Helpers
1016
//------------------------------------------------------------------------------
@@ -94,21 +100,43 @@ module.exports = {
94100
functionName:sourceCode.getText(node.callee)
95101
},
96102
fix(fixer){
97-
constnewPrefix=prefixMap[node.arguments[1].value];
98-
99103
if(sourceCode.getCommentsInside(node).length){
100104
returnnull;
101105
}
102106

103-
if(+(newPrefix+node.arguments[0].value)!==parseInt(node.arguments[0].value,node.arguments[1].value)){
107+
constreplacement=`${prefixMap[node.arguments[1].value]}${node.arguments[0].value}`;
108+
109+
if(+replacement!==parseInt(node.arguments[0].value,node.arguments[1].value)){
104110

105111
/*
106112
* If the newly-produced literal would be invalid, (e.g. 0b1234),
107113
* or it would yield an incorrect parseInt result for some other reason, don't make a fix.
108114
*/
109115
returnnull;
110116
}
111-
returnfixer.replaceText(node,prefixMap[node.arguments[1].value]+node.arguments[0].value);
117+
118+
consttokenBefore=sourceCode.getTokenBefore(node),
119+
tokenAfter=sourceCode.getTokenAfter(node);
120+
letprefix="",
121+
suffix="";
122+
123+
if(
124+
tokenBefore&&
125+
tokenBefore.range[1]===node.range[0]&&
126+
!astUtils.canTokensBeAdjacent(tokenBefore,replacement)
127+
){
128+
prefix=" ";
129+
}
130+
131+
if(
132+
tokenAfter&&
133+
node.range[1]===tokenAfter.range[0]&&
134+
!astUtils.canTokensBeAdjacent(replacement,tokenAfter)
135+
){
136+
suffix=" ";
137+
}
138+
139+
returnfixer.replaceText(node,`${prefix}${replacement}${suffix}`);
112140
}
113141
});
114142
}

‎tests/lib/rules/prefer-numeric-literals.js‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,104 @@ ruleTester.run("prefer-numeric-literals", rule, {
9292
errors:[{message:"Use hexadecimal literals instead of Number.parseInt()."}]
9393
},
9494

95+
// Adjacent tokens tests
96+
{
97+
code:"parseInt('11', 2)",
98+
output:"0b11",
99+
errors:[{message:"Use binary literals instead of parseInt()."}]
100+
},
101+
{
102+
code:"Number.parseInt('67', 8)",
103+
output:"0o67",
104+
errors:[{message:"Use octal literals instead of Number.parseInt()."}]
105+
},
106+
{
107+
code:"5+parseInt('A', 16)",
108+
output:"5+0xA",
109+
errors:[{message:"Use hexadecimal literals instead of parseInt()."}]
110+
},
111+
{
112+
code:"function *f(){ yield(Number).parseInt('11', 2) }",
113+
output:"function *f(){ yield 0b11 }",
114+
parserOptions:{ecmaVersion:6},
115+
errors:[{message:"Use binary literals instead of (Number).parseInt()."}]
116+
},
117+
{
118+
code:"function *f(){ yield(Number.parseInt)('67', 8) }",
119+
output:"function *f(){ yield 0o67 }",
120+
parserOptions:{ecmaVersion:6},
121+
errors:[{message:"Use octal literals instead of Number.parseInt()."}]
122+
},
123+
{
124+
code:"function *f(){ yield(parseInt)('A', 16) }",
125+
output:"function *f(){ yield 0xA }",
126+
parserOptions:{ecmaVersion:6},
127+
errors:[{message:"Use hexadecimal literals instead of parseInt()."}]
128+
},
129+
{
130+
code:"function *f(){ yield Number.parseInt('11', 2) }",
131+
output:"function *f(){ yield 0b11 }",
132+
parserOptions:{ecmaVersion:6},
133+
errors:[{message:"Use binary literals instead of Number.parseInt()."}]
134+
},
135+
{
136+
code:"function *f(){ yield/**/Number.parseInt('67', 8) }",
137+
output:"function *f(){ yield/**/0o67 }",
138+
parserOptions:{ecmaVersion:6},
139+
errors:[{message:"Use octal literals instead of Number.parseInt()."}]
140+
},
141+
{
142+
code:"function *f(){ yield(parseInt('A', 16)) }",
143+
output:"function *f(){ yield(0xA) }",
144+
parserOptions:{ecmaVersion:6},
145+
errors:[{message:"Use hexadecimal literals instead of parseInt()."}]
146+
},
147+
{
148+
code:"parseInt('11', 2)+5",
149+
output:"0b11+5",
150+
errors:[{message:"Use binary literals instead of parseInt()."}]
151+
},
152+
{
153+
code:"Number.parseInt('17', 8)+5",
154+
output:"0o17+5",
155+
errors:[{message:"Use octal literals instead of Number.parseInt()."}]
156+
},
157+
{
158+
code:"parseInt('A', 16)+5",
159+
output:"0xA+5",
160+
errors:[{message:"Use hexadecimal literals instead of parseInt()."}]
161+
},
162+
{
163+
code:"parseInt('11', 2)in foo",
164+
output:"0b11 in foo",
165+
errors:[{message:"Use binary literals instead of parseInt()."}]
166+
},
167+
{
168+
code:"Number.parseInt('17', 8)in foo",
169+
output:"0o17 in foo",
170+
errors:[{message:"Use octal literals instead of Number.parseInt()."}]
171+
},
172+
{
173+
code:"parseInt('A', 16)in foo",
174+
output:"0xA in foo",
175+
errors:[{message:"Use hexadecimal literals instead of parseInt()."}]
176+
},
177+
{
178+
code:"parseInt('11', 2) in foo",
179+
output:"0b11 in foo",
180+
errors:[{message:"Use binary literals instead of parseInt()."}]
181+
},
182+
{
183+
code:"Number.parseInt('17', 8)/**/in foo",
184+
output:"0o17/**/in foo",
185+
errors:[{message:"Use octal literals instead of Number.parseInt()."}]
186+
},
187+
{
188+
code:"(parseInt('A', 16))in foo",
189+
output:"(0xA)in foo",
190+
errors:[{message:"Use hexadecimal literals instead of parseInt()."}]
191+
},
192+
95193
// Should not autofix if it would remove comments
96194
{
97195
code:"/* comment */Number.parseInt('11', 2);",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp