Skip to content

Commit 2979771

Browse files
rishipalcopybara-github
authored andcommitted
Fix spread optimization on array literals with holes
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
1 parent 7bb038f commit 2979771

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/com/google/javascript/jscomp/PeepholeFoldConstants.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,13 @@ private Node tryFoldSpread(Node spread) {
16431643
Node parent = spread.getParent();
16441644
Node child = spread.getOnlyChild();
16451645
if (child.isArrayLit()) {
1646+
for (Node n = child.getFirstChild(); n != null; n = n.getNext()) {
1647+
if (n.getToken().equals(Token.EMPTY)) {
1648+
// Do not fold if the array literal has any holes as it's a syntax error to have holes
1649+
// (empty arg) if the spread happens to be a call arg
1650+
return spread;
1651+
}
1652+
}
16461653
parent.addChildrenAfter(child.removeChildren(), spread);
16471654
spread.detach();
16481655
reportChangeToEnclosingScope(parent);

test/com/google/javascript/jscomp/PeepholeFoldConstantsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,7 @@ public void testFoldArraySpread() {
14121412
@Test
14131413
public void testFoldArrayLitSpreadInArg() {
14141414
test("foo(...[0], 1)", "foo(0, 1)");
1415+
testSame("foo(...[,,,,\"foo\"], 1)");
14151416
testSame("foo(...(false ? [0] : [1]))"); // other opts need to fold the ternery first
14161417
}
14171418

0 commit comments

Comments
 (0)