Skip to content

Commit

Permalink
Move the check for goog.scope aliases into the strictMissingRequires …
Browse files Browse the repository at this point in the history
…group because there is a lot of existing code that violates it by creating an alias for a prefix of a required name, instead of a required name itself.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=126116204
  • Loading branch information
tbreisacher authored and blickly committed Jun 28, 2016
1 parent e3745a6 commit 6e9c403
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Expand Up @@ -92,6 +92,10 @@ public static enum Mode {
DiagnosticType.disabled(
"JSC_MISSING_REQUIRE_WARNING", "missing require: ''{0}''");

static final DiagnosticType MISSING_REQUIRE_FOR_GOOG_SCOPE =
DiagnosticType.disabled(
"JSC_MISSING_REQUIRE_FOR_GOOG_SCOPE", "missing require: ''{0}''");

static final DiagnosticType MISSING_REQUIRE_CALL_WARNING =
DiagnosticType.disabled(
"JSC_MISSING_REQUIRE_CALL_WARNING", "missing require: ''{0}''");
Expand Down Expand Up @@ -315,6 +319,9 @@ private void visitScriptNode(NodeTraversal t) {
String defaultName = parentNamespace != null ? parentNamespace : namespace;
String nameToReport = Iterables.getFirst(classNames, defaultName);
compiler.report(t.makeError(node, MISSING_REQUIRE_CALL_WARNING, nameToReport));
} else if (node.getParent().isName()
&& node.getParent().getGrandparent() == googScopeBlock) {
compiler.report(t.makeError(node, MISSING_REQUIRE_FOR_GOOG_SCOPE, namespace));
} else {
compiler.report(t.makeError(node, MISSING_REQUIRE_WARNING, namespace));
}
Expand Down
1 change: 1 addition & 0 deletions src/com/google/javascript/jscomp/DiagnosticGroups.java
Expand Up @@ -412,6 +412,7 @@ public DiagnosticGroup forName(String name) {
public static final DiagnosticGroup STRICT_MISSING_REQUIRE =
DiagnosticGroups.registerGroup("strictMissingRequire",
CheckRequiresForConstructors.MISSING_REQUIRE_WARNING,
CheckRequiresForConstructors.MISSING_REQUIRE_FOR_GOOG_SCOPE,
CheckRequiresForConstructors.MISSING_REQUIRE_CALL_WARNING);

public static final DiagnosticGroup EXTRA_REQUIRE =
Expand Down
25 changes: 23 additions & 2 deletions test/com/google/javascript/jscomp/MissingRequireTest.java
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.truth.Truth.assertThat;
import static com.google.javascript.jscomp.CheckRequiresForConstructors.MISSING_REQUIRE_CALL_WARNING;
import static com.google.javascript.jscomp.CheckRequiresForConstructors.MISSING_REQUIRE_FOR_GOOG_SCOPE;
import static com.google.javascript.jscomp.CheckRequiresForConstructors.MISSING_REQUIRE_WARNING;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -60,6 +61,10 @@ private void testMissingRequire(String[] js, String warningText) {
test(js, js, null, MISSING_REQUIRE_WARNING, warningText);
}

private void testMissingRequireForScope(String[] js, String warningText) {
test(js, js, null, MISSING_REQUIRE_FOR_GOOG_SCOPE, warningText);
}

public void testPassWithNoNewNodes() {
String js = "var str = 'g4'; /* does not use new */";
testSame(js);
Expand Down Expand Up @@ -813,7 +818,7 @@ public void testAliasConstructorToPrivateVariable() {
testSame(js);
}

public void testMissingGoogRequireFromGoogScope() {
public void testMissingGoogRequireFromGoogScope1() {
String good = ""
+ "goog.provide('foo.bar.Baz');\n"
+ "/** @constructor */\n"
Expand All @@ -827,7 +832,23 @@ public void testMissingGoogRequireFromGoogScope() {
+ "});\n";
String[] js = new String[] {good, bad};
String warning = "missing require: 'foo.bar.Baz'";
testMissingRequire(js, warning);
testMissingRequireForScope(js, warning);
}

public void testMissingGoogRequireFromGoogScope2() {
String good = ""
+ "goog.provide('foo.bar.Baz');\n"
+ "/** @constructor */\n"
+ "foo.bar.Baz = function() {}\n";
String bad = ""
+ "goog.require('foo.bar.Baz');\n"
+ "goog.scope(function() {\n"
+ " var bar = foo.bar;\n"
+ " use(new bar.Baz);\n"
+ "});";
String[] js = new String[] {good, bad};
String warning = "missing require: 'foo.bar'";
testMissingRequireForScope(js, warning);
}

public void testNoMissingGoogRequireFromGoogScope() {
Expand Down

0 comments on commit 6e9c403

Please sign in to comment.