Skip to content

Commit

Permalink
Support let, const, and enhanced object literals in StripCode
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=182793727
  • Loading branch information
lauraharker authored and Tyler Breisacher committed Jan 22, 2018
1 parent 43bbc25 commit b54e481
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 19 deletions.
43 changes: 27 additions & 16 deletions src/com/google/javascript/jscomp/StripCode.java
Expand Up @@ -108,6 +108,8 @@ private class Strip extends AbstractPostOrderCallback {
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getToken()) {
case VAR:
case CONST:
case LET:
removeVarDeclarationsByNameOrRvalue(t, n, parent);
break;

Expand Down Expand Up @@ -148,19 +150,18 @@ public void visit(NodeTraversal t, Node n, Node parent) {
}

/**
* Removes declarations of any variables whose names are strip names or
* whose whose r-values are static method calls on strip types. Builds a set
* of removed variables so that all references to them can be removed.
* Removes declarations of any variables whose names are strip names or whose whose r-values are
* static method calls on strip types. Builds a set of removed variables so that all references
* to them can be removed.
*
* @param t The traversal
* @param n A VAR node
* @param n A VAR, CONST, or LET node
* @param parent {@code n}'s parent
*/
void removeVarDeclarationsByNameOrRvalue(NodeTraversal t, Node n,
Node parent) {
void removeVarDeclarationsByNameOrRvalue(NodeTraversal t, Node n, Node parent) {
Node next = null;
for (Node nameNode = n.getFirstChild(); nameNode != null;
nameNode = next) {
// TODO(b/72223678): handle destructuring declarations below.
for (Node nameNode = n.getFirstChild(); nameNode != null; nameNode = next) {
next = nameNode.getNext();
String name = nameNode.getString();
if (isStripName(name)
Expand Down Expand Up @@ -190,6 +191,8 @@ void maybeRemoveReferenceToRemovedVariable(NodeTraversal t, Node n,
Node parent) {
switch (parent.getToken()) {
case VAR:
case CONST:
case LET:
// This is a variable declaration, not a reference.
break;

Expand Down Expand Up @@ -382,14 +385,22 @@ void eliminateKeysWithStripNamesFromObjLit(NodeTraversal t, Node n) {
// ...
Node key = n.getFirstChild();
while (key != null) {
if (isStripName(key.getString())) {
Node next = key.getNext();
n.removeChild(key);
NodeUtil.markFunctionsDeleted(key, compiler);
key = next;
compiler.reportChangeToEnclosingScope(n);
} else {
key = key.getNext();
switch (key.getToken()) {
case GETTER_DEF:
case SETTER_DEF:
case STRING_KEY:
case MEMBER_FUNCTION_DEF:
if (isStripName(key.getString())) {
Node next = key.getNext();
n.removeChild(key);
NodeUtil.markFunctionsDeleted(key, compiler);
key = next;
compiler.reportChangeToEnclosingScope(n);
break;
}
// fall through
default:
key = key.getNext();
}
}
}
Expand Down
70 changes: 67 additions & 3 deletions test/com/google/javascript/jscomp/StripCodeTest.java
Expand Up @@ -121,6 +121,33 @@ public void testLoggerDefinedInPrototype5() {
"}");
}

public void testLoggerDefinedInPrototype6() {
test(
lines(
"var a = {};",
"a.b = function() {};",
"a.b.prototype = { ",
" logger(a) {this.x = goog.debug.Logger.getLogger('a.b.c')}",
"}"),
"var a = {}; a.b = function() {}; a.b.prototype = {}");
}

public void testLoggerDefinedInPrototype7() {
test(
lines(
"var a = {};",
"a.b = function() {};",
"a.b.prototype = { ",
" ['logger']: goog.debug.Logger.getLogger('a.b.c')",
"}"),
lines(
"var a = {};",
"a.b = function() {};",
"a.b.prototype = { ",
" ['logger']: null",
"}"));
}

public void testLoggerDefinedStatically() {
test("a.b.c = function() {};" +
"a.b.c.logger = goog.debug.Logger.getLogger('a.b.c');",
Expand Down Expand Up @@ -169,6 +196,35 @@ public void testLoggerDefinedInObjectLiteral4() {
"};");
}

public void testLoggerDefinedInObjectLiteral5() {
test(
lines(
"var a = {};",
"a.b = {",
" x: 0,",
" logger() { return goog.debug.Logger.getLogger('a.b.c'); }",
"};"),
"var a = {}; a.b={x:0}");
}

public void testLoggerDefinedInObjectLiteral6() {
test(
lines(
"var a = {};",
"a.b = {",
" x: 0,",
" ['logger']: goog.debug.Logger.getLogger('a.b.c')",
"};",
"a.b['logger']();"),
lines(
"var a = {};",
"a.b = {",
" x: 0,",
" ['logger']: null", // Don't strip computed properties
"};",
"a.b['logger']()"));
}

public void testLoggerDefinedInPrototypeAndUsedInConstructor() {
test("a.b.c = function(level) {" +
" if (!this.logger.isLoggable(level)) {" +
Expand Down Expand Up @@ -197,6 +253,14 @@ public void testLoggerVarDeclaration() {
test("var logger = opt_logger || goog.debug.LogManager.getRoot();", "");
}

public void testLoggerLetDeclaration() {
test("let logger = opt_logger || goog.debug.LogManager.getRoot();", "");
}

public void testLoggerConstDeclaration() {
test("const logger = opt_logger || goog.debug.LogManager.getRoot();", "");
}

public void testLoggerMethodCallByVariableType() {
test("var x = goog.debug.Logger.getLogger('a.b.c'); y.info(a); x.info(a);",
"y.info(a)");
Expand Down Expand Up @@ -400,17 +464,17 @@ public void testReportErrorOnStripInNestedAssignment() {
StripCode.STRIP_ASSIGNMENT_ERROR);
}

public void testNewOperatior1() {
public void testNewOperator1() {
test("function foo() {} foo.bar = new goog.debug.Logger();",
"function foo() {} foo.bar = null;");
}

public void testNewOperatior2() {
public void testNewOperator2() {
test("function foo() {} foo.bar = (new goog.debug.Logger()).foo();",
"function foo() {} foo.bar = null;");
}

public void testNewOperatior3() {
public void testNewOperator3() {
test("(new goog.debug.Logger()).foo().bar = 2;",
"2;");
}
Expand Down

0 comments on commit b54e481

Please sign in to comment.