Skip to content

Commit

Permalink
Make sure that usages in shorthand object literals are counted as usa…
Browse files Browse the repository at this point in the history
…ges.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132907205
  • Loading branch information
tbreisacher authored and blickly committed Sep 12, 2016
1 parent 7fe7f8b commit eb91be9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/com/google/javascript/jscomp/CheckRequiresForConstructors.java
Expand Up @@ -71,6 +71,8 @@ public static enum Mode {
private Mode mode; private Mode mode;


private final Set<String> providedNames = new HashSet<>(); private final Set<String> providedNames = new HashSet<>();

// Keys are the local name of a required namespace. Values are the goog.require CALL node.
private final Map<String, Node> requires = new HashMap<>(); private final Map<String, Node> requires = new HashMap<>();


// Only used in single-file mode. // Only used in single-file mode.
Expand Down Expand Up @@ -222,6 +224,12 @@ public void visit(NodeTraversal t, Node n, Node parent) {
visitQualifiedName(n); visitQualifiedName(n);
} }
break; break;
case STRING_KEY:
if (parent.isObjectLit() && !n.hasChildren()) {
// Object literal shorthand. This is a usage of the name.
visitQualifiedName(n);
}
break;
case CALL: case CALL:
visitCallNode(t, n, parent); visitCallNode(t, n, parent);
break; break;
Expand Down Expand Up @@ -463,22 +471,24 @@ private void addWeakUsagesOfAllPrefixes(String qualifiedName) {
weakUsages.add(qualifiedName); weakUsages.add(qualifiedName);
} }


private void visitQualifiedName(Node getpropOrName) { private void visitQualifiedName(Node n) {
if (getpropOrName.isName() && getpropOrName.getString() != null) { Preconditions.checkState(n.isName() || n.isGetProp() || n.isStringKey(), n);

if (n.isName() && n.getString() != null) {
// If the referenced thing is a goog.require as desugared from goog.module(). // If the referenced thing is a goog.require as desugared from goog.module().
if (getpropOrName.getBooleanProp(Node.GOOG_MODULE_REQUIRE)) { if (n.getBooleanProp(Node.GOOG_MODULE_REQUIRE)) {
Node declStatement = NodeUtil.getEnclosingStatement(getpropOrName); Node declStatement = NodeUtil.getEnclosingStatement(n);
if (NodeUtil.isNameDeclaration(declStatement)) { if (NodeUtil.isNameDeclaration(declStatement)) {
for (Node varChild : declStatement.children()) { for (Node varChild : declStatement.children()) {
// Normal declaration. // Normal declaration.
if (varChild.isName()) { if (varChild.isName()) {
requires.put(varChild.getString(), getpropOrName); requires.put(varChild.getString(), n);
} }
// Object destructuring declaration. // Object destructuring declaration.
if (varChild.isObjectPattern()) { if (varChild.isObjectPattern()) {
for (Node objectChild : varChild.children()) { for (Node objectChild : varChild.children()) {
if (objectChild.isStringKey()) { if (objectChild.isStringKey()) {
requires.put(objectChild.getString(), getpropOrName); requires.put(objectChild.getString(), n);
} }
} }
} }
Expand All @@ -487,7 +497,8 @@ private void visitQualifiedName(Node getpropOrName) {
} }
} }


addWeakUsagesOfAllPrefixes(getpropOrName.getQualifiedName()); String qualifiedName = n.isStringKey() ? n.getString() : n.getQualifiedName();
addWeakUsagesOfAllPrefixes(qualifiedName);
} }


private void visitNewNode(NodeTraversal t, Node newNode) { private void visitNewNode(NodeTraversal t, Node newNode) {
Expand Down
14 changes: 14 additions & 0 deletions test/com/google/javascript/jscomp/ExtraRequireTest.java
Expand Up @@ -75,6 +75,20 @@ public void testNoWarning_externsNew() {
test(externs, js, js, null, null, null); test(externs, js, js, null, null, null);
} }


public void testNoWarning_objlitShorthand() {
testSameEs6(
LINE_JOINER.join(
"goog.module('example.module');",
"",
"const X = goog.require('example.X');",
"alert({X});"));

testSameEs6(
LINE_JOINER.join(
"goog.require('X');",
"alert({X});"));
}

public void testNoWarning_InnerClassInExtends() { public void testNoWarning_InnerClassInExtends() {
String js = String js =
LINE_JOINER.join( LINE_JOINER.join(
Expand Down

0 comments on commit eb91be9

Please sign in to comment.