Skip to content

Commit

Permalink
Break apart cases in Es6RewriteArrowFunctionTest to be narrower and…
Browse files Browse the repository at this point in the history
… make them type-checkable (e.g. complete externs, declaring referenced field).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=198099973
  • Loading branch information
nreid260 authored and lauraharker committed May 29, 2018
1 parent 524c5fd commit d68c426
Showing 1 changed file with 169 additions and 72 deletions.
241 changes: 169 additions & 72 deletions test/com/google/javascript/jscomp/Es6RewriteArrowFunctionTest.java
Expand Up @@ -21,9 +21,14 @@ public class Es6RewriteArrowFunctionTest extends CompilerTestCase {

private LanguageMode languageOut;

public Es6RewriteArrowFunctionTest() {
super(MINIMAL_EXTERNS);
}

@Override
protected void setUp() throws Exception {
super.setUp();

setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
languageOut = LanguageMode.ECMASCRIPT3;
}
Expand All @@ -40,37 +45,55 @@ protected Es6RewriteArrowFunction getProcessor(Compiler compiler) {
return new Es6RewriteArrowFunction(compiler);
}

public void testArrowFunction() {
public void testAssigningArrowToVariable_BlockBody() {
test("var f = x => { return x+1; };", "var f = function(x) { return x+1; };");
}

test("var odds = [1,2,3,4].filter((n) => n%2 == 1);",
"var odds = [1,2,3,4].filter(function(n) { return n%2 == 1; });");

public void testAssigningArrowToVariable_ExpressionBody() {
test("var f = x => x+1;", "var f = function(x) { return x+1; };");
}

public void testPassingArrowToMethod_ExpressionBody() {
test(
externs(
MINIMAL_EXTERNS
+ lines(
"/**",
" * @param {function(T):boolean} predicate",
" * @return {!Array<T>}",
" */",
"Array.prototype.filter = function(predicate) { };")),
srcs("var odds = [1,2,3,4].filter((n) => n%2 == 1);"),
expected("var odds = [1,2,3,4].filter(function(n) { return n%2 == 1; });"));
}

public void testCapturingThisInArrow_ExpressionBody() {
test("var f = () => this;",
"const $jscomp$this = this; var f = function() { return $jscomp$this; };");
}

public void testCapturingThisInArrow_BlockBody() {
test(
"var f = x => { this.needsBinding(); return 0; };",
lines(
"const $jscomp$this = this;",
"var f = function(x) {",
" $jscomp$this.needsBinding();",
" return 0;",
"};"));

test(
lines(
"var f = x => {", " this.init();", " this.doThings();", " this.done();", "};"),
lines(
"const $jscomp$this = this;",
"var f = function(x) {",
" $jscomp$this.init();",
" $jscomp$this.doThings();",
" $jscomp$this.done();",
"};"));
externs(
lines(
"window.init = function() { };",
"window.doThings = function() { };",
"window.done = function() { };")),
srcs(
lines(
"var f = x => {", " this.init();", " this.doThings();", " this.done();", "};")),
expected(
lines(
"const $jscomp$this = this;",
"var f = function(x) {",
" $jscomp$this.init();",
" $jscomp$this.doThings();",
" $jscomp$this.done();",
"};")));
}

public void testCapturingThisInArrowPlacesAliasAboveContainingStatement() {
// We use `switch` here becuse it's a very complex kind of statement.
test(
"switch(a) { case b: (() => { this; })(); }",
lines(
Expand All @@ -79,7 +102,10 @@ public void testArrowFunction() {
" case b:",
" (function() { $jscomp$this; })();",
"}"));
}

public void testCapturingThisInMultipleArrowsPlacesOneAliasAboveContainingStatement() {
// We use `switch` here becuse it's a very complex kind of statement.
test(
lines(
"switch(a) {",
Expand All @@ -96,8 +122,11 @@ public void testArrowFunction() {
" case c:",
" (function() { $jscomp$this; })();",
"}"));
}

test(
public void testCapturingThisInMultipleArrowsPlacesOneAliasAboveAllContainingStatements() {
// We use `switch` here becuse it's a very complex kind of statement.
test(
lines(
"switch(a) {",
" case b:",
Expand All @@ -119,7 +148,7 @@ public void testArrowFunction() {
"}"));
}

public void testArguments() {
public void testCapturingEnclosingFunctionArgumentsInArrow() {
test(
lines(
"function f() {",
Expand All @@ -133,12 +162,12 @@ public void testArguments() {
"}"));
}

public void testArrowFunctionInObject() {
public void testAssigningArrowToObjectLiteralField_ExpressionBody() {
test("var obj = { f: () => 'bar' };",
"var obj = { f: function() { return 'bar'; } };");
}

public void testArrowInClass() {
public void testCapturingThisInArrowFromClassMethod() {
test(
lines(
"class C {",
Expand Down Expand Up @@ -171,7 +200,7 @@ public void testArrowInClass() {
"}"));
}

public void testArrowInConstructorWithSuperCall() {
public void testCapturingThisInArrowFromClassConstructorWithSuperCall() {
test(
lines(
"class B {",
Expand Down Expand Up @@ -204,6 +233,9 @@ public void testArrowInConstructorWithSuperCall() {
" this.wrappedYGetter = function() { return $jscomp$this.y; };",
" }",
"}"));
}

public void testCapturingThisInArrowFromClassConstructorWithMultipleSuperCallPaths() {
test(
lines(
"class B {",
Expand Down Expand Up @@ -244,54 +276,99 @@ public void testArrowInConstructorWithSuperCall() {
"}"));
}

public void testMultipleArrowsInSameScope() {
public void testMultipleArrowsInSameFreeScope() {
test(
"var a1 = x => x+1; var a2 = x => x-1;",
"var a1 = function(x) { return x+1; }; var a2 = function(x) { return x-1; };");
}

public void testMultipleArrowsInSameFunctionScope() {
test(
"function f() { var a1 = x => x+1; var a2 = x => x-1; }",
lines(
"function f() {",
" var a1 = function(x) { return x+1; };",
" var a2 = function(x) { return x-1; };",
"}"));
}

public void testCapturingThisInMultipleArrowsInSameFunctionScope() {
test(
"function f() { var a1 = () => this.x; var a2 = () => this.y; }",
lines(
"function f() {",
" const $jscomp$this = this;",
" var a1 = function() { return $jscomp$this.x; };",
" var a2 = function() { return $jscomp$this.y; };",
"}"));
"({",
" x: 0,",
" y: 'a',",
" f: function() {",
" var a1 = () => this.x;",
" var a2 = () => this.y;",
" },",
"})"),
lines(
"({",
" x: 0,",
" y: 'a',",
" f: function() {",
" const $jscomp$this = this;",
" var a1 = function() { return $jscomp$this.x; };",
" var a2 = function() { return $jscomp$this.y; };",
" },",
"})"));
}

public void testPassingMultipleArrowsInSameFreeScopeAsMethodParams() {
test(
"var a = [1,2,3,4]; var b = a.map(x => x+1).map(x => x*x);",
lines(
"var a = [1,2,3,4];",
"var b = a.map(function(x) { return x+1; }).map(function(x) { return x*x; });"));
externs(
MINIMAL_EXTERNS
+ lines(
"/**",
" * @template Y",
" * @param {function(T):Y} mapper",
" * @return {!Array<Y>}",
" */",
"Array.prototype.map = function(mapper) { };")),
srcs("var a = [1,2,3,4]; var b = a.map(x => x+1).map(x => x*x);"),
expected(
lines(
"var a = [1,2,3,4];",
"var b = a.map(function(x) { return x+1; }).map(function(x) { return x*x; });")));
}

public void testMultipleArrowsInSameFunctionScopeAsMethodParams() {
test(
lines(
"function f() {",
" var a = [1,2,3,4];",
" var b = a.map(x => x+1).map(x => x*x);",
"}"),
lines(
"function f() {",
" var a = [1,2,3,4];",
" var b = a.map(function(x) { return x+1; }).map(function(x) { return x*x; });",
"}"));
externs(
MINIMAL_EXTERNS
+ lines(
"/**",
" * @template Y",
" * @param {function(T):Y} mapper",
" * @return {!Array<Y>}",
" */",
"Array.prototype.map = function(mapper) { };")),
srcs(
lines(
"function f() {",
" var a = [1,2,3,4];",
" var b = a.map(x => x+1).map(x => x*x);",
"}")),
expected(
lines(
"function f() {",
" var a = [1,2,3,4];",
" var b = a.map(function(x) { return x+1; }).map(function(x) { return x*x; });",
"}")));
}

public void testArrowNestedScope() {
public void testCapturingThisInArrowFromNestedScopes() {
test(
lines(
"var outer = {",
" x: null,",
"",
" f: function() {",
" var a1 = () => this.x;",
" var inner = {",
" y: null,",
"",
" f: function() {",
" var a2 = () => this.y;",
" }",
Expand All @@ -300,50 +377,70 @@ public void testArrowNestedScope() {
"}"),
lines(
"var outer = {",
" x: null,",
"",
" f: function() {",
" const $jscomp$this = this;",
" var a1 = function() { return $jscomp$this.x; }",
" var inner = {",
" y: null,",
"",
" f: function() {",
" const $jscomp$this = this;",
" var a2 = function() { return $jscomp$this.y; }",
" }",
" };",
" }",
"}"));
}

public void testCapturingThisInArrowWithNestedConstutor() {
test(
lines(
"function f() {",
" var setup = () => {",
" function Foo() { this.x = 5; }",
" this.f = new Foo;",
" }",
"}"),
"({",
" f: null,",
"",
" g: function() {",
" var setup = () => {",
" /** @constructor @struct */",
" function Foo() { this.x = 5; }",
"",
" this.f = new Foo;",
" };",
" },",
"})"),
lines(
"function f() {",
" const $jscomp$this = this;",
" var setup = function() {",
" function Foo() { this.x = 5; }",
" $jscomp$this.f = new Foo;",
" }",
"}"));
"({",
" f: null,",
"",
" g: function() {",
" const $jscomp$this = this;",
" var setup = function() {",
" /** @constructor @struct */",
" function Foo() { this.x = 5; }",
"",
" $jscomp$this.f = new Foo;",
" };",
" },",
"})"));
}

public void testArrowception() {
public void testNestingArrow() {
test("var f = x => y => x+y;",
"var f = function(x) {return function(y) { return x+y; }; };");
}

public void testArrowceptionWithThis() {
public void testNestingArrowsCapturingThis() {
test(
"var f = (x => { var g = (y => { this.foo(); }) });",
lines(
"const $jscomp$this = this;",
"var f = function(x) {",
" var g = function(y) {",
" $jscomp$this.foo();",
" }",
"}"));
externs("window.foo = function() { };"),
srcs("var f = (x => { var g = (y => { this.foo(); }) });"),
expected(
lines(
"const $jscomp$this = this;",
"var f = function(x) {",
" var g = function(y) {",
" $jscomp$this.foo();",
" }",
"}")));
}
}

0 comments on commit d68c426

Please sign in to comment.