Skip to content

Commit 891842d

Browse files
axetroyfisker
andauthoredAug 24, 2024··
prefer-string-slice: Remove unsafe autofix for String#substr() (#2427)
Co-authored-by: fisker <lionkay@gmail.com>
1 parent 7369077 commit 891842d

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed
 

‎rules/prefer-string-slice.js

-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const {getStaticValue} = require('@eslint-community/eslint-utils');
33
const {getParenthesizedText, getParenthesizedRange} = require('./utils/parentheses.js');
4-
const isNumber = require('./utils/is-number.js');
54
const {replaceArgument} = require('./fix/index.js');
65
const {isNumberLiteral, isMethodCall} = require('./ast/index.js');
76

@@ -64,13 +63,6 @@ function * fixSubstrArguments({node, fixer, context, abort}) {
6463
return;
6564
}
6665

67-
if (argumentNodes.every(node => isNumber(node, scope))) {
68-
const firstArgumentText = getParenthesizedText(firstArgument, sourceCode);
69-
70-
yield fixer.insertTextBeforeRange(secondArgumentRange, `${firstArgumentText} + `);
71-
return;
72-
}
73-
7466
return abort();
7567
}
7668

‎test/prefer-string-slice.mjs

+4-10
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ test({
8484
},
8585
{
8686
code: '"foo".substr(bar.length, Math.min(baz, 100))',
87-
output: '"foo".slice(bar.length, bar.length + Math.min(baz, 100))',
8887
errors: errorsSubstr,
8988
},
9089
{
9190
code: '"foo".substr(1, "abc".length)',
92-
output: '"foo".slice(1, 1 + "abc".length)',
9391
errors: errorsSubstr,
9492
},
9593
{
@@ -101,10 +99,6 @@ test({
10199
const length = 123;
102100
"foo".substr(1, length)
103101
`,
104-
output: outdent`
105-
const length = 123;
106-
"foo".slice(1, 1 + length)
107-
`,
108102
errors: errorsSubstr,
109103
},
110104
{
@@ -140,10 +134,6 @@ test({
140134
const length = 123;
141135
"foo".substr(1, length - 4)
142136
`,
143-
output: outdent`
144-
const length = 123;
145-
"foo".slice(1, 1 + length - 4)
146-
`,
147137
errors: errorsSubstr,
148138
},
149139
{
@@ -326,5 +316,9 @@ test.snapshot({
326316
'foo.substring(0, (10, 1))',
327317
'foo.substring(0, await 1)',
328318
'foo.substring((10, bar))',
319+
outdent`
320+
const string = "::";
321+
const output = string.substr(-2, 2);
322+
`,
329323
],
330324
});

‎test/snapshots/prefer-string-slice.mjs.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ Generated by [AVA](https://avajs.dev).
122122
1 | foo.substr((0, bar.length), (0, baz.length))␊
123123
`
124124

125-
> Output
126-
127-
`␊
128-
1 | foo.slice((0, bar.length), (0, bar.length) + (0, baz.length))␊
129-
`
130-
131125
> Error 1/1
132126
133127
`␊
@@ -218,3 +212,20 @@ Generated by [AVA](https://avajs.dev).
218212
> 1 | foo.substring((10, bar))␊
219213
| ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`String#slice()\` over \`String#substring()\`.␊
220214
`
215+
216+
## invalid(11): const string = "::"; const output = string.substr(-2, 2);
217+
218+
> Input
219+
220+
`␊
221+
1 | const string = "::";␊
222+
2 | const output = string.substr(-2, 2);␊
223+
`
224+
225+
> Error 1/1
226+
227+
`␊
228+
1 | const string = "::";␊
229+
> 2 | const output = string.substr(-2, 2);␊
230+
| ^^^^^^^^^^^^^^^^^^^^ Prefer \`String#slice()\` over \`String#substr()\`.␊
231+
`
60 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.