Skip to content

Commit

Permalink
Add more tests for destructuring and CollapseProperties
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=225557493
  • Loading branch information
lauraharker authored and blickly committed Dec 17, 2018
1 parent 1f820b5 commit a846235
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 8 deletions.
66 changes: 63 additions & 3 deletions test/com/google/javascript/jscomp/AggressiveInlineAliasesTest.java
Expand Up @@ -18,11 +18,9 @@


import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;


import com.google.common.truth.Expect;
import com.google.javascript.jscomp.GlobalNamespace.Name; import com.google.javascript.jscomp.GlobalNamespace.Name;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
Expand All @@ -32,7 +30,6 @@
public class AggressiveInlineAliasesTest extends CompilerTestCase { public class AggressiveInlineAliasesTest extends CompilerTestCase {


private AggressiveInlineAliases lastAggressiveInlineAliases; private AggressiveInlineAliases lastAggressiveInlineAliases;
@Rule public final Expect expect = Expect.create();


private static final String EXTERNS = private static final String EXTERNS =
"var window;" "var window;"
Expand Down Expand Up @@ -1893,6 +1890,69 @@ public void testCommaExpression() {
"use(Letters.B);")); "use(Letters.B);"));
} }


@Test
public void testInlineDestructuredAliasProp() {
// TODO(b/117673791): replace `b.y` with `a.x`
testSame("var a = {x: 2}; var b = {}; b.y = a.x; var {y} = b; use(y);");
}

@Test
public void testInlineDestructuredAliasPropWithKeyBefore() {
// TODO(b/117673791): replace `y` with `a.x`
testSame("var a = {x: 2}; var b = {z: 3}; b.y = a.x; b.z = 4; var {z, y} = b; use(y + z);");
}

@Test
public void testInlineDestructuredAliasPropWithKeyAfter() {
// TODO(b/117673791): replace `b.y` with `a.x`
testSame("var a = {x: 2}; var b = {z: 3}; b.y = a.x; b.z = 4; var {y, z} = b; use(y + z);");
}

@Test
public void testInlineDestructuredAliasPropWithKeyBeforeAndAfter() {
// TODO(b/117673791): replace `b.y` with `a.x`
testSame(
lines(
"var a = {x: 2};",
"var b = {z: 3};",
"b.y = a.x;",
"b.z = 4;", // add second assign so that this won't get inlined
"var {x, y, z} = b;",
"use(y + z);"));
}

@Test
public void testDestructuredPropAccessInAssignWithKeyBefore() {
// TODO(b/117673791): replace `b.y` with `a.x`
testSame(
lines(
"var a = {x: 2};",
"var b = {};",
"b.y = a.x;",
"var obj = {};",
"({missing: obj.foo, y: obj.foo} = b);",
"use(obj.foo);"));
}

@Test
public void testDestructuredPropAccessInAssignWithKeyAfter() {
// TODO(b/117673791): replace `b.y` with `a.x`
testSame(
lines(
"var a = {x: 2};",
"var b = {};",
"b.y = a.x;",
"var obj = {};",
"({y: obj.foo, missing: obj.foo} = b);",
"use(obj.foo);"));
}

@Test
public void testDestructuredPropAccessInDeclarationWithDefault() {
// TODO(b/117673791): replace `b.y` with `a.x`
testSame(lines("var a = {x: {}};", "var b = {};", "b.y = a.x;", "var {y = 0} = b;", "use(y);"));
}

/** /**
* To ensure that as we modify the AST, the GlobalNamespace stays up-to-date, we do a consistency * To ensure that as we modify the AST, the GlobalNamespace stays up-to-date, we do a consistency
* check after every unit test. * check after every unit test.
Expand Down
22 changes: 22 additions & 0 deletions test/com/google/javascript/jscomp/CheckGlobalNamesTest.java
Expand Up @@ -260,6 +260,17 @@ public void testTypedefGivesNoWarning() {
testSame("var a = {}; /** @typedef {number} */ a.b;"); testSame("var a = {}; /** @typedef {number} */ a.b;");
} }


@Test
public void testTypedefAliasGivesNoWarning() {
// TODO(b/117673791): don't warn, this might be for typechecking
testWarning("var a = {}; /** @typedef {number} */ a.b; const b = a.b;", UNDEFINED_NAME_WARNING);
}

@Test
public void testDestructuringTypedefAliasGivesNoWarning() {
testSame("var a = {}; /** @typedef {number} */ a.b; const {b} = a;");
}

@Test @Test
public void testRefToDescendantOfUndefinedPropertyGivesCorrectWarning() { public void testRefToDescendantOfUndefinedPropertyGivesCorrectWarning() {
testWarning(NAMES + "a.x.b = 3;", UNDEFINED_NAME_WARNING, testWarning(NAMES + "a.x.b = 3;", UNDEFINED_NAME_WARNING,
Expand Down Expand Up @@ -557,6 +568,17 @@ public void testEs6NonSubclass_stillWarnsForMissingProperty() {
testWarning("class Child {} Child.f();", UNDEFINED_NAME_WARNING); testWarning("class Child {} Child.f();", UNDEFINED_NAME_WARNING);
} }


@Test
public void testDestructuringUndefinedProperty() {
// TODO(lharker): this should warn
testSame(
lines(
"var ns = {};", //
"/** @enum */",
"ns.Modes = {A, B};",
"const {C} = ns.Modes;"));
}

@Test @Test
public void testObjectDestructuringAlias() { public void testObjectDestructuringAlias() {
testSame( testSame(
Expand Down
67 changes: 62 additions & 5 deletions test/com/google/javascript/jscomp/CollapsePropertiesTest.java
Expand Up @@ -2016,15 +2016,72 @@ public void testDestructuredProperiesObjectLit() {
} }


@Test @Test
public void testDestructuredArrays() { public void testCanCollapseSinglePropertyInObjectPattern() {
// TODO(b/117673791): collapse x.y -> x$y
testSame("const x = {y: 1}; const {y} = x; use(y);");
}

@Test
public void testCanCollapseSinglePropertyInObjectPatternWithDefaultValue() {
// TODO(b/117673791): collapse x.y - > x$y
testSame("const x = {y: 1}; const {y = 0} = x; use(y);");
}

@Test
public void testCanCollapsePropertyInObjectPatternWithKeyBefore() {
// TODO(b/117673791): collapse x.y -> x$y
testSame("let x = {y: 1, /** @nocollapse */ z: 2}; let {z, y} = x; use(y, z);");
}

@Test
public void testCanCollapsePropertyInObjectPatternWithKeyAfter() {
// TODO(b/117673791): collapse x.y -> x$y
testSame("let x = {y: 1, /** @nocollapse */ z: 2}; let {y, z=y} = x; use(y, z);");
}

@Test
public void testCanCollapsePropertyInObjectPatternWithKeyBeforeAndAfter() {
test(
lines(
"let foo = {bar: {y: 1}};", //
"let {x1, x2, y, z1, z2} = foo.bar;",
"use(x1, x2, y, z1, z2);"),
lines(
"var foo$bar = {y: 1};",
// TODO(b/117673791): collapse foo$bar.y -> foo$bar$y
"let {x1, x2, y, z1, z2} = foo$bar;",
"use(x1, x2, y, z1, z2);"));
}

@Test
public void testCannotCollapsePropertyInNestedObjectPattern() {
// TODO(b/117673791): collapse x.y -> x$y
testSame("const x = {y: {z: 1}}; const {y: {z}} = x; use(z);");
}

@Test
public void testCanCollapseSinglePropertyInObjectPatternAssign() {
// TODO(b/117673791): collapse x.y -> x$y
testSame("const x = {y: 1}; var y; ({y} = x); use(y);");
}

@Test
public void testPropertyInArray() {
testSame("var a, b = [{}, {}]; a.foo = 5; b.bar = 6;"); testSame("var a, b = [{}, {}]; a.foo = 5; b.bar = 6;");


test("var a = {}; a.b = {}; [a.b.c, a.b.d] = [1, 2];", test("var a = {}; a.b = 5; var c, d = [6, a.b]", "var a$b = 5; var c, d = [6, a$b];");
"var a$b = {}; [a$b.c, a$b.d] = [1, 2];"); }

@Test
public void testCollapsePropertySetInPattern() {
// TODO(b/120303257): collapse lvalues in destructuring patterns. We delayed implementing this
// because it's uncommon to have properties as lvalues in destructuring patterns.
test(
"var a = {}; a.b = {}; [a.b.c, a.b.d] = [1, 2];", "var a$b = {}; [a$b.c, a$b.d] = [1, 2];");


test( test(
"var a = {}; a.b = 5; var c, d = [6, a.b]", "var a = {}; a.b = {}; ({x: a.b.c, y: a.b.d} = {});",
"var a$b = 5; var c, d = [6, a$b];"); "var a$b = {}; ({x: a$b.c, y: a$b.d} = {});");
} }


@Test @Test
Expand Down
Expand Up @@ -1437,6 +1437,40 @@ public void testDestructingAliasWithConstructor() {
warning(CollapseProperties.UNSAFE_NAMESPACE_WARNING)); warning(CollapseProperties.UNSAFE_NAMESPACE_WARNING));
} }


@Test
public void namespaceInDestructuringPattern() {
// TODO(b/117673791): collapse ns.x and ns.y
testSame(
lines(
"const ns = {};",
"ns.x = 1;",
"ns.y = 2;",
"let {x, y} = ns;",
"x = 4;", // enforce that we can't inline x -> ns.x because it's set multiple times
"use(x + y);"));
}

@Test
public void inlineDestructuringPatternConstructorWithProperty() {
test(
lines(
"const ns = {};",
"/** @constructor */",
"ns.Y = function() {};",
"ns.Y.prop = 3;",
"let {Y} = ns;",
"use(Y.prop);"),
lines(
"const ns = {};",
"/** @constructor */",
"var ns$Y = function() {};",
"var ns$Y$prop = 3;",
"let {Y} = ns;",
"use(Y.prop);"),
// TODO(b/117673791): replace Y.prop -> ns$Y$prop; right now this is broken.
warning(CollapseProperties.UNSAFE_NAMESPACE_WARNING));
}

@Test @Test
public void testDefaultParamAlias1() { public void testDefaultParamAlias1() {
test( test(
Expand Down

0 comments on commit a846235

Please sign in to comment.