Skip to content

Commit

Permalink
Fix the ProcessClosurePrimitives pass to not emit INVALID_CLOSURE_CAL…
Browse files Browse the repository at this point in the history
…L_SCOPE_ERROR in modules

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=258454747
  • Loading branch information
rishipal authored and tjgq committed Jul 17, 2019
1 parent e4ccffd commit a96aaaf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private boolean validatePrimitiveCallWithMessage(
return true;
}

if (!t.inGlobalHoistScope()) {
if (!t.inGlobalHoistScope() && !t.inModuleScope()) {
compiler.report(JSError.make(n, INVALID_CLOSURE_CALL_SCOPE_ERROR));
return false;
} else if (!n.getParent().isExprResult() && !"goog.define".equals(methodName)) {
Expand Down Expand Up @@ -828,7 +828,9 @@ private boolean verifyDefine(NodeTraversal t, Node parent, Node methodName, Node
// Calls to goog.define must be in the global hoist scope. This is copied from
// validate(Un)aliasablePrimitiveCall.
// TODO(sdh): loosen this restriction if the results are assigned?
if (!compiler.getOptions().shouldPreserveGoogModule() && !t.inGlobalHoistScope()) {
if (!compiler.getOptions().shouldPreserveGoogModule()
&& !t.inGlobalHoistScope()
&& !t.inModuleScope()) {
compiler.report(JSError.make(methodName.getParent(), INVALID_CLOSURE_CALL_SCOPE_ERROR));
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@ public void testInvalidDefine() {
INVALID_CLOSURE_CALL_SCOPE_ERROR);
}

@Test
public void testValidDefine() {
testNoWarning("goog.module('a'); /** @define {boolean} */\n goog.define('goog.DEBUG', true);");
testNoWarning("goog.provide('b'); /** @define {boolean} */\n goog.define('goog.DEBUG', true);");
testNoWarning("goog.module('c'); goog.forwardDeclare('A.b');");
testNoWarning("goog.module('d'); goog.addDependency('C.D');");
}

@Test
public void testDefineValues() {
testSame("var CLOSURE_DEFINES = {'FOO': 'string'};");
Expand Down

0 comments on commit a96aaaf

Please sign in to comment.