Skip to content

Commit b87fe4c

Browse files
rishipalcopybara-github
authored andcommitted
Move es6RewriteArrowFunction pass post normalize
PiperOrigin-RevId: 623500740
1 parent 8331642 commit b87fe4c

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ private void visitArrowFunction(NodeTraversal t, Node n, ThisAndArgumentsContext
103103
n.setIsArrowFunction(false);
104104

105105
Node body = n.getLastChild();
106-
if (!body.isBlock()) {
107-
body.detach();
108-
body = IR.block(IR.returnNode(body)).srcrefTreeIfMissing(body);
109-
n.addChildToBack(body);
110-
}
106+
checkState(body.isBlock(), "Arrow function body must be a block after normalization");
111107

112108
ThisAndArgumentsReferenceUpdater updater =
113109
new ThisAndArgumentsReferenceUpdater(compiler, context, astFactory);

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,6 @@ public static void addEarlyOptimizationTranspilationPasses(
203203
if (options.needsTranspilationOf(Feature.NEW_TARGET)) {
204204
passes.maybeAdd(rewriteNewDotTarget);
205205
}
206-
if (options.needsTranspilationOf(Feature.ARROW_FUNCTIONS)) {
207-
passes.maybeAdd(es6RewriteArrowFunction);
208-
}
209206
}
210207

211208
/**
@@ -219,6 +216,10 @@ public static void addPostNormalizationTranspilationPasses(
219216
// TODO(b/197349249): Move passes from `addEarlyOptimizationTranspilationPasses()` to here
220217
// until that method can be deleted as a no-op.
221218

219+
if (options.needsTranspilationOf(Feature.ARROW_FUNCTIONS)) {
220+
passes.maybeAdd(es6RewriteArrowFunction);
221+
}
222+
222223
if (options.needsTranspilationOf(Feature.CLASSES)) {
223224
passes.maybeAdd(es6RewriteClass);
224225
}

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void setUp() throws Exception {
3939
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
4040
languageOut = LanguageMode.ECMASCRIPT3;
4141

42+
enableNormalize();
4243
enableTypeInfoValidation();
4344
enableTypeCheck();
4445
replaceTypesWithColors();
@@ -280,9 +281,9 @@ public void testCapturingThisInArrowFromClassConstructorWithSuperCall() {
280281
" }",
281282
"}",
282283
"class C extends B {",
283-
" constructor(x, y) {",
284+
" constructor(x$jscomp$1, y) {",
284285
" console.log('statement before super');",
285-
" super(x);",
286+
" super(x$jscomp$1);",
286287
" const $jscomp$this$UID$2 = this;", // Must not use `this` before super() call.
287288
" this.wrappedXGetter = function() { return $jscomp$this$UID$2.x; };",
288289
" this.y = y;",
@@ -325,11 +326,11 @@ public void testCapturingThisInArrowFromClassConstructorWithMultipleSuperCallPat
325326
" }",
326327
"}",
327328
"class C extends B {",
328-
" constructor(x, y) {",
329-
" if (x < 1) {",
330-
" super(x);",
329+
" constructor(x$jscomp$1, y) {",
330+
" if (x$jscomp$1 < 1) {",
331+
" super(x$jscomp$1);",
331332
" } else {",
332-
" super(-x);",
333+
" super(-x$jscomp$1);",
333334
" }",
334335
" const $jscomp$this$UID$2 = this;", // Must not use `this` before super() call.
335336
" this.wrappedXGetter = function() { return $jscomp$this$UID$2.x; };",
@@ -343,7 +344,8 @@ public void testCapturingThisInArrowFromClassConstructorWithMultipleSuperCallPat
343344
public void testMultipleArrowsInSameFreeScope() {
344345
testArrowRewriting(
345346
"var a1 = x => x+1; var a2 = x => x-1;",
346-
"var a1 = function(x) { return x+1; }; var a2 = function(x) { return x-1; };");
347+
"var a1 = function(x) { return x+1; }; var a2 = function(x$jscomp$1) { return x$jscomp$1-1;"
348+
+ " };");
347349
}
348350

349351
@Test
@@ -353,7 +355,7 @@ public void testMultipleArrowsInSameFunctionScope() {
353355
lines(
354356
"function f() {",
355357
" var a1 = function(x) { return x+1; };",
356-
" var a2 = function(x) { return x-1; };",
358+
" var a2 = function(x$jscomp$1) { return x$jscomp$1-1; };",
357359
"}"));
358360
}
359361

@@ -423,7 +425,8 @@ public void testPassingMultipleArrowsInSameFreeScopeAsMethodParams() {
423425
expected(
424426
lines(
425427
"var a = [1,2,3,4];",
426-
"var b = a.map(function(x) { return x+1; }).map(function(x) { return x*x; });")));
428+
"var b = a.map(function(x) { return x+1; }).map(function(x$jscomp$1) { return"
429+
+ " x$jscomp$1*x$jscomp$1; });")));
427430
}
428431

429432
@Test
@@ -448,7 +451,8 @@ public void testMultipleArrowsInSameFunctionScopeAsMethodParams() {
448451
lines(
449452
"function f() {",
450453
" var a = [1,2,3,4];",
451-
" var b = a.map(function(x) { return x+1; }).map(function(x) { return x*x; });",
454+
" var b = a.map(function(x) { return x+1; }).map(function(x$jscomp$1) { return"
455+
+ " x$jscomp$1*x$jscomp$1; });",
452456
"}")));
453457
}
454458

0 commit comments

Comments
 (0)