Skip to content

Commit

Permalink
Adding let inlining test cases to show difference in behavior between…
Browse files Browse the repository at this point in the history
… var and let. Part of ES6 compatibility for Inline Type Aliases (full functionality + tests not yet complete).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158765016
  • Loading branch information
hjkaria authored and Tyler Breisacher committed Jun 13, 2017
1 parent 69ac13f commit 0180fe6
Showing 1 changed file with 157 additions and 2 deletions.
159 changes: 157 additions & 2 deletions test/com/google/javascript/jscomp/InlineAliasesTest.java
Expand Up @@ -467,11 +467,11 @@ public void testObjectDestructuringWithParametersAndStyleShortcut() {
* Tests using CONST to show behavior. Compiler inlining support not provided for CONST, may be * Tests using CONST to show behavior. Compiler inlining support not provided for CONST, may be
* implemented later. * implemented later.
*/ */
public void testSimpleConstAliasInJSDoc01() { public void testSimpleConstAliasInJSDoc() {
testSame("function Foo(){}; const /** @const */ alias = Foo; /** @type {alias} */ var x;"); testSame("function Foo(){}; const /** @const */ alias = Foo; /** @type {alias} */ var x;");
} }


public void testSimpleConstAliasInCode01() { public void testSimpleConstAliasInCode() {
testSame("function Foo(){}; const /** @const */ alias = Foo; var x = new alias;"); testSame("function Foo(){}; const /** @const */ alias = Foo; var x = new alias;");
} }


Expand Down Expand Up @@ -619,4 +619,159 @@ public void testConstObjectDestructuringWithParametersAndStyleShortcut() {
"}", "}",
"h({name: A, val: A});")); "h({name: A, val: A});"));
} }

/**
* Tests using LET to show behavior. Compiler inlining support not provided for LET, may be
* implemented later.
*/
public void testSimpleLetAliasInJSDoc() {
testSame("function Foo(){}; let /** @const */ alias = Foo; /** @type {alias} */ var x;");
}

public void testSimpleLetAliasInCode() {
testSame("function Foo(){}; let /** @const */ alias = Foo; var x = new alias;");
}

public void testUnqualifiedHoistedLetAliasesInCode() {
testSame(
LINE_JOINER.join(
"function Foo(){};",
"function Bar(){ var x = alias; };",
"let /** @const */ alias = Foo;"));
}

public void testTransitiveLetAliases() {
testSame(
LINE_JOINER.join(
"/** @const */ var ns = {};",
"/** @constructor */ ns.Foo = function() {};",
"/** @constructor */ ns.Foo.Bar = function() {};",
"let /** @const */ alias = ns.Foo;",
"let /** @const */ alias2 = alias.Bar;",
"var x = new alias2"));
}

public void testUnqualifiedLetAliasChains() {
testSame(
LINE_JOINER.join(
"/** @constructor */ var Foo = function() {};",
"let /** @const */ alias1 = Foo;",
"let /** @const */ alias2 = alias1;",
"var x = new alias2"));
}

public void testQualifiedLetAliasChains() {
testSame(
LINE_JOINER.join(
"/** @const */ var ns = {};",
"/** @constructor */ ns.Foo = function() {};",
"let /** @const */ alias1 = ns.Foo;",
"let /** @const */ alias2 = alias1;",
"var x = new alias2"));
}

public void testLetAliasedEnums() {
testSame("/** @enum {number} */ var E = { A : 1 }; let /** @const */ alias = E.A; alias;");
}

public void testES6LetAliasClassDeclarationWithoutNew() {
testSame("class Foo{}; let /** @const */ alias = Foo; var x = alias;");
}

public void testArrayDestructuringLetAssign() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"var a = [5, A];",
"var [one, two] = a;"));
}

public void testLetArrayDestructuringFromFunction() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"function f() {",
" return [A, 3];",
"}",
"var a, b;",
"[a, b] = f();"));
}

public void testLetObjectDestructuringBasicAssign() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"var o = {p: A, q: 5};",
"var {p, q} = o;"));
}

public void testWithLetObjectDestructuringAssignWithoutDeclaration() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"({a, b} = {a: A, b: A});"));
}

public void testLetObjectDestructuringAssignNewVarNames() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"var o = {p: A, q: true};",
"var {p: newName1, q: newName2} = o;"));
}

public void testLetObjectDestructuringDefaultVals() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"var {a = A, b = A} = {a: 13};"));
}

public void testLetArrayDestructuringWithParameter() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"function f([name, val]) {",
" console.log(name, val);",
"}",
"f([A, A]);"));
}

public void testLetObjectDestructuringWithParameters() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"function g({",
" name: n,",
" val: v",
"}) {",
" console.log(n, v);",
"}",
"g({",
" name: A,",
" val: A",
"});"));
}

public void testLetObjectDestructuringWithParametersAndStyleShortcut() {
testSame(
LINE_JOINER.join(
"var Foo = class {};",
"let /** @const */ A = Foo;",
"function h({",
" name,",
" val",
"}) {",
" console.log(name, val);",
"}",
"h({name: A, val: A});"));
}
} }

0 comments on commit 0180fe6

Please sign in to comment.