Skip to content

Commit

Permalink
prefer-spread: Use removeMethodCall for better fix (#1426)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 14, 2021
1 parent 4897202 commit 7bdf0dd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
27 changes: 11 additions & 16 deletions rules/prefer-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const {getParenthesizedRange, getParenthesizedText} = require('./utils/parenthes
const shouldAddParenthesesToSpreadElementArgument = require('./utils/should-add-parentheses-to-spread-element-argument.js');
const isLiteralValue = require('./utils/is-literal-value.js');
const {isNodeMatches} = require('./utils/is-node-matches.js');
const {replaceNodeOrTokenAndSpacesBefore, removeSpacesAfter} = require('./fix/index.js');
const {
replaceNodeOrTokenAndSpacesBefore,
removeSpacesAfter,
removeMethodCall,
} = require('./fix/index.js');

const ERROR_ARRAY_FROM = 'array-from';
const ERROR_ARRAY_CONCAT = 'array-concat';
Expand Down Expand Up @@ -76,15 +80,6 @@ const isArrayLiteralHasTrailingComma = (node, sourceCode) => {
return isCommaToken(sourceCode.getLastToken(node, 1));
};

const getRangeAfterCalleeObject = (node, sourceCode) => {
const {object} = node.callee;
const parenthesizedRange = getParenthesizedRange(object, sourceCode);
const [, start] = parenthesizedRange;
const [, end] = node.range;

return [start, end];
};

function fixConcat(node, sourceCode, fixableArguments) {
const array = node.callee.object;
const concatCallArguments = node.arguments;
Expand Down Expand Up @@ -186,11 +181,11 @@ function fixConcat(node, sourceCode, fixableArguments) {
yield fixer.insertTextBefore(node, ';');
}

yield (
concatCallArguments.length - fixableArguments.length === 0 ?
fixer.replaceTextRange(getRangeAfterCalleeObject(node, sourceCode), '') :
removeArguments(fixer)
);
if (concatCallArguments.length - fixableArguments.length === 0) {
yield * removeMethodCall(fixer, node, sourceCode);
} else {
yield removeArguments(fixer);
}

const text = getFixedText();

Expand Down Expand Up @@ -302,7 +297,7 @@ function fixSlice(node, sourceCode) {

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

yield fixer.replaceTextRange(getRangeAfterCalleeObject(node, sourceCode), '');
yield * removeMethodCall(fixer, node, sourceCode);
};
}

Expand Down
10 changes: 5 additions & 5 deletions test/prefer-spread.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ test.snapshot({
'[1,].concat([2, 3])',
'[1,].concat(2,)',
'[1,].concat([2, 3],)',
'(( (([1,])).concat( (([2, 3])) ,) ))',
'(( (([1,])).concat( (([2, 3])) , bar ) ))',
'(( (( (( [1,] )).concat ))( (([2, 3])) ,) ))',
'(( (( (( [1,] )).concat ))( (([2, 3])) , bar ) ))',
'foo.concat(2)',
'foo.concat([2, 3])',
'foo.concat(2,)',
'foo.concat([2, 3],)',
'(( ((foo)).concat( (([2, 3])) ,) ))',
'(( ((foo)).concat( (([2, 3])) , bar ) ))',
'(( (( ((foo)).concat ))( (([2, 3])) ,) ))',
'(( (( ((foo)).concat ))( (([2, 3])) , bar ) ))',
// Semicolon
outdent`
bar()
Expand Down Expand Up @@ -321,7 +321,7 @@ test.snapshot({
'array.slice(1).slice()',
'array.slice().slice(1)',
'const copy = array.slice()',
'(( ((array)).slice() ))',
'(( (( (( array )).slice ))() ))',
// Semicolon
outdent`
bar()
Expand Down
40 changes: 20 additions & 20 deletions test/snapshots/prefer-spread.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -990,35 +990,35 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #9
1 | (( (([1,])).concat( (([2, 3])) ,) ))
1 | (( (( (( [1,] )).concat ))( (([2, 3])) ,) ))

> Output
`␊
1 | (( (([1, 2, 3,])) ))␊
1 | (( (( (( [1, 2, 3,] )) )) ))␊
`

> Error 1/1
`␊
> 1 | (( (([1,])).concat( (([2, 3])) ,) ))␊
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
> 1 | (( (( (( [1,] )).concat ))( (([2, 3])) ,) ))␊
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
`

## Invalid #10
1 | (( (([1,])).concat( (([2, 3])) , bar ) ))
1 | (( (( (( [1,] )).concat ))( (([2, 3])) , bar ) ))

> Output
`␊
1 | (( (([1, 2, 3,])).concat( bar ) ))␊
1 | (( (( (( [1, 2, 3,] )).concat ))( bar ) ))␊
`

> Error 1/1
`␊
> 1 | (( (([1,])).concat( (([2, 3])) , bar ) ))␊
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
> 1 | (( (( (( [1,] )).concat ))( (([2, 3])) , bar ) ))␊
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
`

## Invalid #11
Expand Down Expand Up @@ -1086,35 +1086,35 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #15
1 | (( ((foo)).concat( (([2, 3])) ,) ))
1 | (( (( ((foo)).concat ))( (([2, 3])) ,) ))

> Output
`␊
1 | (( [...((foo)), 2, 3] ))␊
1 | (( (( [...((foo)), 2, 3] )) ))␊
`

> Error 1/1
`␊
> 1 | (( ((foo)).concat( (([2, 3])) ,) ))␊
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
> 1 | (( (( ((foo)).concat ))( (([2, 3])) ,) ))␊
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
`

## Invalid #16
1 | (( ((foo)).concat( (([2, 3])) , bar ) ))
1 | (( (( ((foo)).concat ))( (([2, 3])) , bar ) ))

> Output
`␊
1 | (( [...((foo)), 2, 3].concat( bar ) ))␊
1 | (( (( [...((foo)), 2, 3].concat ))( bar ) ))␊
`

> Error 1/1
`␊
> 1 | (( ((foo)).concat( (([2, 3])) , bar ) ))␊
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
> 1 | (( (( ((foo)).concat ))( (([2, 3])) , bar ) ))␊
| ^^^^^^ Prefer the spread operator over \`Array#concat(…)\`.␊
`

## Invalid #17
Expand Down Expand Up @@ -2074,19 +2074,19 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #6
1 | (( ((array)).slice() ))
1 | (( (( (( array )).slice ))() ))

> Output
`␊
1 | (( [...((array))] ))␊
1 | (( [...(( (( array )) ))] ))␊
`

> Error 1/1
`␊
> 1 | (( ((array)).slice() ))␊
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
> 1 | (( (( (( array )).slice ))() ))␊
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
`

## Invalid #7
Expand Down
Binary file modified test/snapshots/prefer-spread.mjs.snap
Binary file not shown.

0 comments on commit 7bdf0dd

Please sign in to comment.