Skip to content

Commit

Permalink
Don't fuse for-loop initializers with let/const in StatementFusion.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178031548
  • Loading branch information
lauraharker authored and blickly committed Dec 6, 2017
1 parent 4641404 commit dca454a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/StatementFusion.java
Expand Up @@ -137,7 +137,7 @@ private boolean isFusableControlStatement(Node n) {
return n.hasChildren();
case FOR:
// Avoid cases where we have for(var x;_;_) { ....
return !n.getFirstChild().isVar();
return !NodeUtil.isNameDeclaration(n.getFirstChild());
case FOR_IN:
// Avoid cases where we have for(var x = foo() in a) { ....
return !mayHaveSideEffects(n.getFirstChild());
Expand Down
13 changes: 10 additions & 3 deletions test/com/google/javascript/jscomp/StatementFusionTest.java
Expand Up @@ -28,7 +28,6 @@ public final class StatementFusionTest extends CompilerTestCase {
protected void setUp() throws Exception {
super.setUp();
favorsCommas = false;
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT5);
}

@Override
Expand Down Expand Up @@ -87,17 +86,25 @@ public void testFuseIntoForIn1() {
}

public void testFuseIntoForIn2() {
// This test case causes a parse warning in ES5 strict out, but is a parse error in ES6+ out.
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT5_STRICT);
setExpectParseWarningsThisTest();
fuseSame("a();for(var x = b() in y){}");
}

public void testFuseIntoVanillaFor() {
public void testFuseIntoVanillaFor1() {
fuse("a;b;c;for(;g;){}", "for(a,b,c;g;){}");
fuse("a;b;c;for(d;g;){}", "for(a,b,c,d;g;){}");
fuse("a;b;c;for(d,e;g;){}", "for(a,b,c,d,e;g;){}");
fuseSame("a();for(var x;g;){}");
}

public void testFuseIntoVanillaFor2() {
fuseSame("a;b;c;for(var d;g;){}");
fuseSame("a;b;c;for(let d;g;){}");
fuseSame("a;b;c;for(const d = 5;g;){}");
}

public void testFuseIntoLabel() {
fuse("a;b;c;label:for(x in y){}", "label:for(x in a,b,c,y){}");
fuse("a;b;c;label:for(;g;){}", "label:for(a,b,c;g;){}");
Expand Down Expand Up @@ -146,7 +153,7 @@ public void testFavorComma5() {
test("a;b;c;if(d){}d;e;f;if(g){}", "if(a,b,c,d){}if(d,e,f,g){}");
}

public void testNoGlobalSchopeChanges() {
public void testNoGlobalScopeChanges() {
testSame("a,b,c");
}

Expand Down

0 comments on commit dca454a

Please sign in to comment.