Skip to content

Commit

Permalink
Fix spread optimization on array literals with holes
Browse files Browse the repository at this point in the history
Do not fold if the array literal has any holes as it's a syntax error to have holes (empty arg) if we're the spread happens to be a call arg.

PiperOrigin-RevId: 554601815
  • Loading branch information
rishipal authored and copybara-github committed Aug 7, 2023
1 parent 7bb038f commit 2979771
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/com/google/javascript/jscomp/PeepholeFoldConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,13 @@ private Node tryFoldSpread(Node spread) {
Node parent = spread.getParent();
Node child = spread.getOnlyChild();
if (child.isArrayLit()) {
for (Node n = child.getFirstChild(); n != null; n = n.getNext()) {
if (n.getToken().equals(Token.EMPTY)) {
// Do not fold if the array literal has any holes as it's a syntax error to have holes
// (empty arg) if the spread happens to be a call arg
return spread;
}
}
parent.addChildrenAfter(child.removeChildren(), spread);
spread.detach();
reportChangeToEnclosingScope(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ public void testFoldArraySpread() {
@Test
public void testFoldArrayLitSpreadInArg() {
test("foo(...[0], 1)", "foo(0, 1)");
testSame("foo(...[,,,,\"foo\"], 1)");
testSame("foo(...(false ? [0] : [1]))"); // other opts need to fold the ternery first
}

Expand Down

0 comments on commit 2979771

Please sign in to comment.