Skip to content

Commit f10f1a6

Browse files
authored
Fix crash on array holes (#2129)
1 parent 32c3923 commit f10f1a6

File tree

6 files changed

+12
-2
lines changed

6 files changed

+12
-2
lines changed

rules/prefer-array-find.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ const isDestructuringFirstElement = node => {
168168
&& right === node
169169
&& left.type === 'ArrayPattern'
170170
&& left.elements.length === 1
171+
&& left.elements[0]
171172
&& left.elements[0].type !== 'RestElement';
172173
};
173174

@@ -232,6 +233,7 @@ const create = context => {
232233
if (!(
233234
node.id.type === 'ArrayPattern'
234235
&& node.id.elements.length === 1
236+
&& node.id.elements[0]
235237
&& node.id.elements[0].type !== 'RestElement'
236238
&& isArrayFilterCall(node.init)
237239
)) {
@@ -250,6 +252,7 @@ const create = context => {
250252
if (!(
251253
node.left.type === 'ArrayPattern'
252254
&& node.left.elements.length === 1
255+
&& node.left.elements[0]
253256
&& node.left.elements[0].type !== 'RestElement'
254257
&& isArrayFilterCall(node.right)
255258
)) {

rules/prefer-array-flat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const arrayReduce = {
9090
firstArgumentBody.type === 'ArrayExpression'
9191
&& firstArgumentBody.elements.length === 2
9292
&& firstArgumentBody.elements.every((node, index) =>
93-
node.type === 'SpreadElement'
93+
node?.type === 'SpreadElement'
9494
&& node.argument.type === 'Identifier'
9595
&& isSameIdentifier(firstArgument.params[index], node.argument),
9696
)

rules/prefer-set-size.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const create = context => {
7070
})
7171
|| node.object.type !== 'ArrayExpression'
7272
|| node.object.elements.length !== 1
73-
|| node.object.elements[0].type !== 'SpreadElement'
73+
|| node.object.elements[0]?.type !== 'SpreadElement'
7474
) {
7575
return;
7676
}

test/prefer-array-find.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ test({
150150
'const [] = array.filter(bar)',
151151
'const [foo, another] = array.filter(bar)',
152152
'const [, foo] = array.filter(bar)',
153+
'const [,] = array.filter(bar)',
153154
// `RestElement`
154155
'const [...foo] = array.filter(bar)',
155156

@@ -382,6 +383,7 @@ test({
382383
'[] = array.filter(bar)',
383384
'[foo, another] = array.filter(bar)',
384385
'[, foo] = array.filter(bar)',
386+
'[,] = array.filter(bar)',
385387
// `RestElement`
386388
'[...foo] = array.filter(bar)',
387389

@@ -600,6 +602,8 @@ test({
600602
'const foo = array.filter(bar); [first, another] = foo;',
601603
'const foo = array.filter(bar); const [,first] = foo;',
602604
'const foo = array.filter(bar); [,first] = foo;',
605+
'const foo = array.filter(bar); const [,] = foo;',
606+
'const foo = array.filter(bar); [,] = foo;',
603607
'const foo = array.filter(bar); const [...first] = foo;',
604608
'const foo = array.filter(bar); [...first] = foo;',
605609
outdent`

test/prefer-array-flat.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ test.snapshot({
7878
'array.reduce((a, b) => [...a, ...b, c], [])',
7979
'array.reduce((a, b) => [...a, ...b,,], [])',
8080
'array.reduce((a, b) => [,...a, ...b], [])',
81+
'array.reduce((a, b) => [, ], [])',
82+
'array.reduce((a, b) => [, ,], [])',
8183
],
8284
invalid: [
8385
'array.reduce((a, b) => [...a, ...b], [])',

test/prefer-set-size.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test.snapshot({
2121
'const [foo] = new Set([]);[...foo].length;',
2222
'[...foo].length',
2323
'var foo = new Set(); var foo = new Set(); [...foo].length',
24+
'[,].length',
2425
],
2526
invalid: [
2627
'[...new Set(array)].length',

0 commit comments

Comments
 (0)