Skip to content

Commit

Permalink
Use NodeUtil.isNameDeclaration instead of isVar in StripCode
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=183728534
  • Loading branch information
lauraharker committed Jan 30, 2018
1 parent fa7cb57 commit f03041b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/StripCode.java
Expand Up @@ -549,7 +549,7 @@ boolean isMethodOrCtorCallThatTriggersRemoval(


if (parent != null && parent.isName()) { if (parent != null && parent.isName()) {
Node grandparent = parent.getParent(); Node grandparent = parent.getParent();
if (grandparent != null && grandparent.isVar()) { if (grandparent != null && NodeUtil.isNameDeclaration(grandparent)) {
// The call's return value is being used to initialize a newly // The call's return value is being used to initialize a newly
// declared variable. We should leave the call intact for now. // declared variable. We should leave the call intact for now.
// That way, when the traversal reaches the variable declaration, // That way, when the traversal reaches the variable declaration,
Expand Down
32 changes: 30 additions & 2 deletions test/com/google/javascript/jscomp/StripCodeTest.java
Expand Up @@ -281,18 +281,46 @@ public void testLoggerConstDeclaration() {
test("const logger = opt_logger || goog.debug.LogManager.getRoot();", ""); test("const logger = opt_logger || goog.debug.LogManager.getRoot();", "");
} }


public void testLoggerMethodCallByVariableType() { public void testLoggerMethodCallByVariableType_var() {
test("var x = goog.debug.Logger.getLogger('a.b.c'); y.info(a); x.info(a);", test("var x = goog.debug.Logger.getLogger('a.b.c'); y.info(a); x.info(a);",
"y.info(a)"); "y.info(a)");
} }


public void testSubPropertyAccessByVariableName() { public void testLoggerMethodCallByVariableType_let() {
test("let x = goog.debug.Logger.getLogger('a.b.c'); y.info(a); x.info(a);",
"y.info(a)");
}

public void testLoggerMethodCallByVariableType_const() {
test("const x = goog.debug.Logger.getLogger('a.b.c'); y.info(a); x.info(a);",
"y.info(a)");
}

public void testSubPropertyAccessByVariableName_var() {
test("var x, y = goog.debug.Logger.getLogger('a.b.c');" + test("var x, y = goog.debug.Logger.getLogger('a.b.c');" +
"var logger = x;" + "var logger = x;" +
"var curlevel = logger.level_ ? logger.getLevel().name : 3;", "var curlevel = logger.level_ ? logger.getLevel().name : 3;",
"var x;var curlevel=null?null:3"); "var x;var curlevel=null?null:3");
} }


public void testSubPropertyAccessByVariableName_let() {
test(
lines(
"let x, y = goog.debug.Logger.getLogger('a.b.c');",
"let logger = x;",
"let curlevel = logger.level_ ? logger.getLevel().name : 3;"),
"let x; let curlevel=null?null:3");
}

public void testSubPropertyAccessByVariableName_const() {
test(
lines(
"const x = undefined, y = goog.debug.Logger.getLogger('a.b.c');",
"const logger = x;",
"const curlevel = logger.level_ ? logger.getLevel().name : 3;"),
"const x = undefined; const curlevel=null?null:3");
}

public void testPrefixedVariableName() { public void testPrefixedVariableName() {
test("this.blcLogger_ = goog.debug.Logger.getLogger('a.b.c');" + test("this.blcLogger_ = goog.debug.Logger.getLogger('a.b.c');" +
"this.blcLogger_.fine('Raised dirty states.');", ""); "this.blcLogger_.fine('Raised dirty states.');", "");
Expand Down

0 comments on commit f03041b

Please sign in to comment.