Skip to content

Commit 7bdf0dd

Browse files
authored
prefer-spread: Use removeMethodCall for better fix (#1426)
1 parent 4897202 commit 7bdf0dd

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

rules/prefer-spread.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const {getParenthesizedRange, getParenthesizedText} = require('./utils/parenthes
66
const shouldAddParenthesesToSpreadElementArgument = require('./utils/should-add-parentheses-to-spread-element-argument.js');
77
const isLiteralValue = require('./utils/is-literal-value.js');
88
const {isNodeMatches} = require('./utils/is-node-matches.js');
9-
const {replaceNodeOrTokenAndSpacesBefore, removeSpacesAfter} = require('./fix/index.js');
9+
const {
10+
replaceNodeOrTokenAndSpacesBefore,
11+
removeSpacesAfter,
12+
removeMethodCall,
13+
} = require('./fix/index.js');
1014

1115
const ERROR_ARRAY_FROM = 'array-from';
1216
const ERROR_ARRAY_CONCAT = 'array-concat';
@@ -76,15 +80,6 @@ const isArrayLiteralHasTrailingComma = (node, sourceCode) => {
7680
return isCommaToken(sourceCode.getLastToken(node, 1));
7781
};
7882

79-
const getRangeAfterCalleeObject = (node, sourceCode) => {
80-
const {object} = node.callee;
81-
const parenthesizedRange = getParenthesizedRange(object, sourceCode);
82-
const [, start] = parenthesizedRange;
83-
const [, end] = node.range;
84-
85-
return [start, end];
86-
};
87-
8883
function fixConcat(node, sourceCode, fixableArguments) {
8984
const array = node.callee.object;
9085
const concatCallArguments = node.arguments;
@@ -186,11 +181,11 @@ function fixConcat(node, sourceCode, fixableArguments) {
186181
yield fixer.insertTextBefore(node, ';');
187182
}
188183

189-
yield (
190-
concatCallArguments.length - fixableArguments.length === 0 ?
191-
fixer.replaceTextRange(getRangeAfterCalleeObject(node, sourceCode), '') :
192-
removeArguments(fixer)
193-
);
184+
if (concatCallArguments.length - fixableArguments.length === 0) {
185+
yield * removeMethodCall(fixer, node, sourceCode);
186+
} else {
187+
yield removeArguments(fixer);
188+
}
194189

195190
const text = getFixedText();
196191

@@ -302,7 +297,7 @@ function fixSlice(node, sourceCode) {
302297

303298
// The array is already accessing `.slice`, there should not any case need add extra `()`
304299

305-
yield fixer.replaceTextRange(getRangeAfterCalleeObject(node, sourceCode), '');
300+
yield * removeMethodCall(fixer, node, sourceCode);
306301
};
307302
}
308303

test/prefer-spread.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ test.snapshot({
191191
'[1,].concat([2, 3])',
192192
'[1,].concat(2,)',
193193
'[1,].concat([2, 3],)',
194-
'(( (([1,])).concat( (([2, 3])) ,) ))',
195-
'(( (([1,])).concat( (([2, 3])) , bar ) ))',
194+
'(( (( (( [1,] )).concat ))( (([2, 3])) ,) ))',
195+
'(( (( (( [1,] )).concat ))( (([2, 3])) , bar ) ))',
196196
'foo.concat(2)',
197197
'foo.concat([2, 3])',
198198
'foo.concat(2,)',
199199
'foo.concat([2, 3],)',
200-
'(( ((foo)).concat( (([2, 3])) ,) ))',
201-
'(( ((foo)).concat( (([2, 3])) , bar ) ))',
200+
'(( (( ((foo)).concat ))( (([2, 3])) ,) ))',
201+
'(( (( ((foo)).concat ))( (([2, 3])) , bar ) ))',
202202
// Semicolon
203203
outdent`
204204
bar()
@@ -321,7 +321,7 @@ test.snapshot({
321321
'array.slice(1).slice()',
322322
'array.slice().slice(1)',
323323
'const copy = array.slice()',
324-
'(( ((array)).slice() ))',
324+
'(( (( (( array )).slice ))() ))',
325325
// Semicolon
326326
outdent`
327327
bar()

test/snapshots/prefer-spread.mjs.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -990,35 +990,35 @@ Generated by [AVA](https://avajs.dev).
990990
`
991991

992992
## Invalid #9
993-
1 | (( (([1,])).concat( (([2, 3])) ,) ))
993+
1 | (( (( (( [1,] )).concat ))( (([2, 3])) ,) ))
994994

995995
> Output
996996
997997
`␊
998-
1 | (( (([1, 2, 3,])) ))␊
998+
1 | (( (( (( [1, 2, 3,] )) )) ))␊
999999
`
10001000

10011001
> Error 1/1
10021002
10031003
`␊
1004-
> 1 | (( (([1,])).concat( (([2, 3])) ,) ))␊
1005-
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
1004+
> 1 | (( (( (( [1,] )).concat ))( (([2, 3])) ,) ))␊
1005+
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
10061006
`
10071007

10081008
## Invalid #10
1009-
1 | (( (([1,])).concat( (([2, 3])) , bar ) ))
1009+
1 | (( (( (( [1,] )).concat ))( (([2, 3])) , bar ) ))
10101010

10111011
> Output
10121012
10131013
`␊
1014-
1 | (( (([1, 2, 3,])).concat( bar ) ))␊
1014+
1 | (( (( (( [1, 2, 3,] )).concat ))( bar ) ))␊
10151015
`
10161016

10171017
> Error 1/1
10181018
10191019
`␊
1020-
> 1 | (( (([1,])).concat( (([2, 3])) , bar ) ))␊
1021-
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
1020+
> 1 | (( (( (( [1,] )).concat ))( (([2, 3])) , bar ) ))␊
1021+
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
10221022
`
10231023

10241024
## Invalid #11
@@ -1086,35 +1086,35 @@ Generated by [AVA](https://avajs.dev).
10861086
`
10871087

10881088
## Invalid #15
1089-
1 | (( ((foo)).concat( (([2, 3])) ,) ))
1089+
1 | (( (( ((foo)).concat ))( (([2, 3])) ,) ))
10901090

10911091
> Output
10921092
10931093
`␊
1094-
1 | (( [...((foo)), 2, 3] ))␊
1094+
1 | (( (( [...((foo)), 2, 3] )) ))␊
10951095
`
10961096

10971097
> Error 1/1
10981098
10991099
`␊
1100-
> 1 | (( ((foo)).concat( (([2, 3])) ,) ))␊
1101-
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
1100+
> 1 | (( (( ((foo)).concat ))( (([2, 3])) ,) ))␊
1101+
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
11021102
`
11031103

11041104
## Invalid #16
1105-
1 | (( ((foo)).concat( (([2, 3])) , bar ) ))
1105+
1 | (( (( ((foo)).concat ))( (([2, 3])) , bar ) ))
11061106

11071107
> Output
11081108
11091109
`␊
1110-
1 | (( [...((foo)), 2, 3].concat( bar ) ))␊
1110+
1 | (( (( [...((foo)), 2, 3].concat ))( bar ) ))␊
11111111
`
11121112

11131113
> Error 1/1
11141114
11151115
`␊
1116-
> 1 | (( ((foo)).concat( (([2, 3])) , bar ) ))␊
1117-
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
1116+
> 1 | (( (( ((foo)).concat ))( (([2, 3])) , bar ) ))␊
1117+
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
11181118
`
11191119

11201120
## Invalid #17
@@ -2074,19 +2074,19 @@ Generated by [AVA](https://avajs.dev).
20742074
`
20752075

20762076
## Invalid #6
2077-
1 | (( ((array)).slice() ))
2077+
1 | (( (( (( array )).slice ))() ))
20782078

20792079
> Output
20802080
20812081
`␊
2082-
1 | (( [...((array))] ))␊
2082+
1 | (( [...(( (( array )) ))] ))␊
20832083
`
20842084

20852085
> Error 1/1
20862086
20872087
`␊
2088-
> 1 | (( ((array)).slice() ))␊
2089-
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
2088+
> 1 | (( (( (( array )).slice ))() ))␊
2089+
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
20902090
`
20912091

20922092
## Invalid #7

test/snapshots/prefer-spread.mjs.snap

14 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)