Skip to content

Commit 92bebe1

Browse files
authored
prefer-spread: Report array.slice(0) (#1182)
1 parent f969970 commit 92bebe1

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

rules/prefer-spread.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {getParenthesizedRange} = require('./utils/parentheses');
77
const shouldAddParenthesesToSpreadElementArgument = require('./utils/should-add-parentheses-to-spread-element-argument');
88
const replaceNodeOrTokenAndSpacesBefore = require('./utils/replace-node-or-token-and-spaces-before');
99
const removeSpacesAfter = require('./utils/remove-spaces-after');
10+
const isLiteralValue = require('./utils/is-literal-value');
1011

1112
const ERROR_ARRAY_FROM = 'array-from';
1213
const ERROR_ARRAY_CONCAT = 'array-concat';
@@ -55,7 +56,8 @@ const arrayConcatCallSelector = [
5556
const arraySliceCallSelector = [
5657
methodSelector({
5758
name: 'slice',
58-
length: 0
59+
min: 0,
60+
max: 1
5961
}),
6062
'[callee.object.type!="ArrayExpression"]'
6163
].join('');
@@ -391,6 +393,11 @@ const create = context => {
391393
context.report(problem);
392394
},
393395
[arraySliceCallSelector](node) {
396+
const [firstArgument] = node.arguments;
397+
if (firstArgument && !isLiteralValue(firstArgument, 0)) {
398+
return;
399+
}
400+
394401
context.report({
395402
node: node.callee.property,
396403
messageId: ERROR_ARRAY_SLICE,

test/package.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const deprecatedRules = Object.entries(index.rules)
2121

2222
const testSorted = (t, actualOrder, sourceName) => {
2323
actualOrder = actualOrder.filter(x => !ignoredRules.includes(x));
24-
const sortedOrder = actualOrder.slice(0).sort();
24+
const sortedOrder = [...actualOrder].sort();
2525

2626
for (const [wantedIndex, name] of sortedOrder.entries()) {
2727
const actualIndex = actualOrder.indexOf(name);

test/prefer-spread.mjs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,13 @@ test.snapshot({
278278
'array.slice',
279279
'array.slice(1)',
280280
'array.slice(...[])',
281-
'array.slice(0)',
281+
'array.slice(...[0])',
282+
'array.slice(0 + 0)',
283+
'array.slice("")',
284+
'array.slice(null)',
285+
'const ZERO = 0;array.slice(ZERO)',
286+
'array.slice(0, array.length)',
287+
'array.slice(0, 0)',
282288
'array.notSlice()',
283289
// Why would someone write these
284290
'[...foo].slice()',
@@ -298,6 +304,10 @@ test.snapshot({
298304
`,
299305
// `{String,TypedArray}#slice` are wrongly detected
300306
'"".slice()',
301-
'new Uint8Array([10, 20, 30, 40, 50]).slice()'
307+
'new Uint8Array([10, 20, 30, 40, 50]).slice()',
308+
'array.slice(0)',
309+
'array.slice(0b0)',
310+
'array.slice(0.00)',
311+
'array.slice(0.00, )'
302312
]
303313
});

test/snapshots/prefer-spread.mjs.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,3 +1960,67 @@ Generated by [AVA](https://avajs.dev).
19601960
> 1 | new Uint8Array([10, 20, 30, 40, 50]).slice()␊
19611961
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
19621962
`
1963+
1964+
## Invalid #10
1965+
1 | array.slice(0)
1966+
1967+
> Output
1968+
1969+
`␊
1970+
1 | [...array]␊
1971+
`
1972+
1973+
> Error 1/1
1974+
1975+
`␊
1976+
> 1 | array.slice(0)␊
1977+
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
1978+
`
1979+
1980+
## Invalid #11
1981+
1 | array.slice(0b0)
1982+
1983+
> Output
1984+
1985+
`␊
1986+
1 | [...array]␊
1987+
`
1988+
1989+
> Error 1/1
1990+
1991+
`␊
1992+
> 1 | array.slice(0b0)␊
1993+
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
1994+
`
1995+
1996+
## Invalid #12
1997+
1 | array.slice(0.00)
1998+
1999+
> Output
2000+
2001+
`␊
2002+
1 | [...array]␊
2003+
`
2004+
2005+
> Error 1/1
2006+
2007+
`␊
2008+
> 1 | array.slice(0.00)␊
2009+
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
2010+
`
2011+
2012+
## Invalid #13
2013+
1 | array.slice(0.00, )
2014+
2015+
> Output
2016+
2017+
`␊
2018+
1 | [...array]␊
2019+
`
2020+
2021+
> Error 1/1
2022+
2023+
`␊
2024+
> 1 | array.slice(0.00, )␊
2025+
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
2026+
`
140 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)