Skip to content

Commit

Permalink
Fix crash on array holes (#2129)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 17, 2023
1 parent 32c3923 commit f10f1a6
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions rules/prefer-array-find.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const isDestructuringFirstElement = node => {
&& right === node
&& left.type === 'ArrayPattern'
&& left.elements.length === 1
&& left.elements[0]
&& left.elements[0].type !== 'RestElement';
};

Expand Down Expand Up @@ -232,6 +233,7 @@ const create = context => {
if (!(
node.id.type === 'ArrayPattern'
&& node.id.elements.length === 1
&& node.id.elements[0]
&& node.id.elements[0].type !== 'RestElement'
&& isArrayFilterCall(node.init)
)) {
Expand All @@ -250,6 +252,7 @@ const create = context => {
if (!(
node.left.type === 'ArrayPattern'
&& node.left.elements.length === 1
&& node.left.elements[0]
&& node.left.elements[0].type !== 'RestElement'
&& isArrayFilterCall(node.right)
)) {
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-array-flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const arrayReduce = {
firstArgumentBody.type === 'ArrayExpression'
&& firstArgumentBody.elements.length === 2
&& firstArgumentBody.elements.every((node, index) =>
node.type === 'SpreadElement'
node?.type === 'SpreadElement'
&& node.argument.type === 'Identifier'
&& isSameIdentifier(firstArgument.params[index], node.argument),
)
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-set-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const create = context => {
})
|| node.object.type !== 'ArrayExpression'
|| node.object.elements.length !== 1
|| node.object.elements[0].type !== 'SpreadElement'
|| node.object.elements[0]?.type !== 'SpreadElement'
) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions test/prefer-array-find.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ test({
'const [] = array.filter(bar)',
'const [foo, another] = array.filter(bar)',
'const [, foo] = array.filter(bar)',
'const [,] = array.filter(bar)',
// `RestElement`
'const [...foo] = array.filter(bar)',

Expand Down Expand Up @@ -382,6 +383,7 @@ test({
'[] = array.filter(bar)',
'[foo, another] = array.filter(bar)',
'[, foo] = array.filter(bar)',
'[,] = array.filter(bar)',
// `RestElement`
'[...foo] = array.filter(bar)',

Expand Down Expand Up @@ -600,6 +602,8 @@ test({
'const foo = array.filter(bar); [first, another] = foo;',
'const foo = array.filter(bar); const [,first] = foo;',
'const foo = array.filter(bar); [,first] = foo;',
'const foo = array.filter(bar); const [,] = foo;',
'const foo = array.filter(bar); [,] = foo;',
'const foo = array.filter(bar); const [...first] = foo;',
'const foo = array.filter(bar); [...first] = foo;',
outdent`
Expand Down
2 changes: 2 additions & 0 deletions test/prefer-array-flat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ test.snapshot({
'array.reduce((a, b) => [...a, ...b, c], [])',
'array.reduce((a, b) => [...a, ...b,,], [])',
'array.reduce((a, b) => [,...a, ...b], [])',
'array.reduce((a, b) => [, ], [])',
'array.reduce((a, b) => [, ,], [])',
],
invalid: [
'array.reduce((a, b) => [...a, ...b], [])',
Expand Down
1 change: 1 addition & 0 deletions test/prefer-set-size.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test.snapshot({
'const [foo] = new Set([]);[...foo].length;',
'[...foo].length',
'var foo = new Set(); var foo = new Set(); [...foo].length',
'[,].length',
],
invalid: [
'[...new Set(array)].length',
Expand Down

0 comments on commit f10f1a6

Please sign in to comment.