Skip to content

Commit 7d3c361

Browse files
concavelenzcopybara-github
authored andcommitted
Rewrite let and const in MoveFunctionDeclarations (followup CL will rename the pass) to enable wrapping top level code in try/catch blocks.
PiperOrigin-RevId: 323053574
1 parent 4b13080 commit 7d3c361

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.javascript.jscomp.NodeTraversal.Callback;
2424
import com.google.javascript.rhino.IR;
2525
import com.google.javascript.rhino.Node;
26+
import com.google.javascript.rhino.Token;
2627
import java.util.ArrayList;
2728
import java.util.List;
2829
import java.util.Map.Entry;
@@ -118,5 +119,10 @@ public void visit(NodeTraversal t, Node n, Node parent) {
118119
if (NodeUtil.isClassDeclaration(n)) {
119120
classes.add(n);
120121
}
122+
123+
if (n.isConst() || n.isLet()) {
124+
n.setToken(Token.VAR);
125+
compiler.reportChangeToEnclosingScope(n);
126+
}
121127
}
122128
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,18 @@ public void testNoMoveDeepFunctionDeclarations() {
8686
testSame("a; if (a) function f(){};");
8787
testSame("a; if (a) { function f(){} }");
8888
}
89+
90+
@Test
91+
public void testMigrateLetConstToVar1() {
92+
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT_2015);
93+
test("let a = 1;", "var a = 1");
94+
test("const a = 2;", "var a = 2");
95+
}
96+
97+
@Test
98+
public void testMigrateLetConstToVar2() {
99+
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT_2015);
100+
testSame("{let a = 1;}");
101+
testSame("{const a = 2;}");
102+
}
89103
}

0 commit comments

Comments
 (0)