diff --git a/test/com/google/javascript/jscomp/InlineAliasesTest.java b/test/com/google/javascript/jscomp/InlineAliasesTest.java index b034a4a54ee..4f1c502bcb3 100644 --- a/test/com/google/javascript/jscomp/InlineAliasesTest.java +++ b/test/com/google/javascript/jscomp/InlineAliasesTest.java @@ -467,11 +467,11 @@ public void testObjectDestructuringWithParametersAndStyleShortcut() { * Tests using CONST to show behavior. Compiler inlining support not provided for CONST, may be * implemented later. */ - public void testSimpleConstAliasInJSDoc01() { + public void testSimpleConstAliasInJSDoc() { 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;"); } @@ -619,4 +619,159 @@ public void testConstObjectDestructuringWithParametersAndStyleShortcut() { "}", "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});")); + } }