Skip to content

Commit

Permalink
Don't warn about extra requires for unqualifed names.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=116067252
  • Loading branch information
blickly committed Mar 2, 2016
1 parent 00e866d commit 65808a6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
Expand Up @@ -149,8 +149,13 @@ public void visit(NodeTraversal t, Node n, Node parent) {
maybeAddConstructor(n); maybeAddConstructor(n);
} }
break; break;
case Token.NAME:
if (!NodeUtil.isLValue(n)) {
visitQualifiedName(n);
}
break;
case Token.GETPROP: case Token.GETPROP:
visitGetProp(n); visitQualifiedName(n);
break; break;
case Token.CALL: case Token.CALL:
visitCallNode(n, parent); visitCallNode(n, parent);
Expand Down Expand Up @@ -293,7 +298,7 @@ private void visitCallNode(Node call, Node parent) {
} }
} }


private void visitGetProp(Node getprop) { private void visitQualifiedName(Node getprop) {
// For "foo.bar.baz.qux" add weak usages for "foo.bar.baz.qux", foo.bar.baz", // For "foo.bar.baz.qux" add weak usages for "foo.bar.baz.qux", foo.bar.baz",
// "foo.bar", and "foo" because those might all be goog.provide'd in different files, // "foo.bar", and "foo" because those might all be goog.provide'd in different files,
// so it doesn't make sense to require the user to goog.require all of them. // so it doesn't make sense to require the user to goog.require all of them.
Expand Down Expand Up @@ -507,7 +512,7 @@ public void visit(Node typeNode) {
// var MyHandler = function() {}; // var MyHandler = function() {};
Node getprop = NodeUtil.newQName(compiler, typeString); Node getprop = NodeUtil.newQName(compiler, typeString);
getprop.useSourceInfoIfMissingFromForTree(typeNode); getprop.useSourceInfoIfMissingFromForTree(typeNode);
visitGetProp(getprop); visitQualifiedName(getprop);
} else { } else {
// Even if the root namespace is in externs, add a weak usage because the full // Even if the root namespace is in externs, add a weak usage because the full
// namespace may still be goog.provided. // namespace may still be goog.provided.
Expand Down
7 changes: 7 additions & 0 deletions src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -2681,9 +2681,16 @@ public static boolean isLValue(Node n) {
|| parent.isInc() || parent.isInc()
|| parent.isParamList() || parent.isParamList()
|| parent.isCatch() || parent.isCatch()
|| isImportedName(n)
|| isLhsByDestructuring(n); || isLhsByDestructuring(n);
} }


public static boolean isImportedName(Node n) {
Node parent = n.getParent();
return parent.isImport()
|| parent.isImportSpec() && parent.getLastChild() == n;
}

public static boolean isLhsByDestructuring(Node n) { public static boolean isLhsByDestructuring(Node n) {
Node parent = n.getParent(); Node parent = n.getParent();


Expand Down
4 changes: 4 additions & 0 deletions src/com/google/javascript/rhino/Node.java
Expand Up @@ -2679,6 +2679,10 @@ public boolean isImport() {
return this.getType() == Token.IMPORT; return this.getType() == Token.IMPORT;
} }


public boolean isImportSpec() {
return this.getType() == Token.IMPORT_SPEC;
}

public boolean isIn() { public boolean isIn() {
return this.getType() == Token.IN; return this.getType() == Token.IN;
} }
Expand Down
28 changes: 28 additions & 0 deletions test/com/google/javascript/jscomp/SingleFileCheckRequiresTest.java
Expand Up @@ -63,6 +63,34 @@ public void testReferenceToQualifiedName() {
MISSING_REQUIRE_WARNING); MISSING_REQUIRE_WARNING);
} }


public void testReferenceToUnqualifiedName() {
testSameEs6(
LINE_JOINER.join(
"goog.module('a.b.c');",
"var z = goog.require('x.y.z');",
"",
"exports = { foobar : z };"));

testSameEs6(
LINE_JOINER.join(
"goog.module('a.b.c');",
"var {z} = goog.require('x.y');",
"",
"exports = { foobar : z };"));

testSameEs6(
LINE_JOINER.join(
"import {z} from 'x.y'",
"",
"export var foobar = z;"));

testSameEs6(
LINE_JOINER.join(
"import z from 'x.y.z'",
"",
"export var foobar = z;"));
}

public void testExtraRequire() { public void testExtraRequire() {
testErrorEs6("goog.require('foo.Bar');", EXTRA_REQUIRE_WARNING); testErrorEs6("goog.require('foo.Bar');", EXTRA_REQUIRE_WARNING);
} }
Expand Down

0 comments on commit 65808a6

Please sign in to comment.