Skip to content

Commit

Permalink
Modified CheckGlobalNames and GlobalNamespace to pickup and check cla…
Browse files Browse the repository at this point in the history
…sses, let/const and extended obj literal.

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=99501618
  • Loading branch information
blickly committed Jul 31, 2015
1 parent ccff651 commit 0a99028
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/CheckGlobalNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void reportRefToUndefinedName(Name name, Ref ref) {
}

/**
* Checks whether the given name is a property, and whether that property
* The input name is a property. Check whether this property
* must be initialized with its full qualified name.
*/
private boolean propertyMustBeInitializedByFullName(Name name) {
Expand Down Expand Up @@ -275,7 +275,8 @@ private boolean propertyMustBeInitializedByFullName(Name name) {
return false;
}

if (name.parent.type == Name.Type.OBJECTLIT) {
if (name.parent.type == Name.Type.OBJECTLIT
|| name.parent.type == Name.Type.CLASS) {
return true;
}

Expand Down
46 changes: 45 additions & 1 deletion src/com/google/javascript/jscomp/GlobalNamespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,16 +316,20 @@ public void collect(JSModule module, Scope scope, Node n) {
case Token.GETTER_DEF:
case Token.SETTER_DEF:
case Token.STRING_KEY:
case Token.MEMBER_FUNCTION_DEF:
// This may be a key in an object literal declaration.
name = null;
if (parent != null && parent.isObjectLit()) {
name = getNameForObjLitKey(n);
} else if (parent != null && parent.isClassMembers()) {
name = getNameForClassMembers(n);
}
if (name == null) {
return;
}
isSet = true;
switch (n.getType()) {
case Token.MEMBER_FUNCTION_DEF:
case Token.STRING_KEY:
type = getValueType(n.getFirstChild());
break;
Expand All @@ -344,6 +348,8 @@ public void collect(JSModule module, Scope scope, Node n) {
if (parent != null) {
switch (parent.getType()) {
case Token.VAR:
case Token.LET:
case Token.CONST:
isSet = true;
Node rvalue = n.getFirstChild();
type = rvalue == null ? Name.Type.OTHER : getValueType(rvalue);
Expand All @@ -370,6 +376,10 @@ public void collect(JSModule module, Scope scope, Node n) {
isSet = true;
type = Name.Type.OTHER;
break;
case Token.CLASS:
isSet = true;
type = Name.Type.CLASS;
break;
default:
if (NodeUtil.isAssignmentOp(parent) &&
parent.getFirstChild() == n) {
Expand Down Expand Up @@ -464,7 +474,7 @@ String getNameForObjLitKey(Node n) {
// NAME (gramps)
// OBJLIT (parent)
// STRING (n)
if (greatGramps == null || !greatGramps.isVar()) {
if (greatGramps == null || !NodeUtil.isNameDeclaration(greatGramps)) {
return null;
}
name = gramps.getString();
Expand Down Expand Up @@ -501,14 +511,42 @@ String getNameForObjLitKey(Node n) {
return null;
}

/**
* Gets the fully qualified name corresponding to an class member function,
* as long as it and its prefix property names are valid JavaScript
* identifiers.
*
* For example, if called with node {@code n} representing "y" in any of
* the following expressions, the result would be "x.y":
* <code> class x{y(){}}; </code>
* <code> var x = class{y(){}}; </code>
* <code> var x; x = class{y(){}}; </code>
*
* @param n A child of an CLASS_MEMBERS node
* @return The global name, or null if {@code n} doesn't correspond to
* a class member function that can be named
*/
String getNameForClassMembers(Node n) {
Node parent = n.getParent();
Preconditions.checkState(parent.isClassMembers());
String className = NodeUtil.getClassName(parent.getParent());
return className == null ? null : className + '.' + n.getString();
}

/**
* Gets the type of a value or simple expression.
*
* @param n An r-value in an assignment or variable declaration (not null)
* @return A {@link Name.Type}
*/
Name.Type getValueType(Node n) {
// Shorthand assignment of extended object literal
if (n == null) {
return Name.Type.OTHER;
}
switch (n.getType()) {
case Token.CLASS:
return Name.Type.CLASS;
case Token.OBJECTLIT:
return Name.Type.OBJECTLIT;
case Token.FUNCTION:
Expand Down Expand Up @@ -727,6 +765,8 @@ Ref.Type determineGetTypeForHookOrBooleanExpr(
case Token.INSTANCEOF:
case Token.EXPR_RESULT:
case Token.VAR:
case Token.LET:
case Token.CONST:
case Token.IF:
case Token.WHILE:
case Token.FOR:
Expand Down Expand Up @@ -885,6 +925,7 @@ Name getOrCreateName(String name) {
*/
static class Name implements StaticTypedSlot<TypeI> {
enum Type {
CLASS,
OBJECTLIT,
FUNCTION,
GET,
Expand Down Expand Up @@ -1219,8 +1260,11 @@ private static JSDocInfo getDocInfoForDeclaration(Ref ref) {
switch (refParent.getType()) {
case Token.FUNCTION:
case Token.ASSIGN:
case Token.CLASS:
return refParent.getJSDocInfo();
case Token.VAR:
case Token.LET:
case Token.CONST:
return ref.node == refParent.getFirstChild() ?
refParent.getJSDocInfo() : ref.node.getJSDocInfo();
case Token.OBJECTLIT:
Expand Down
Loading

0 comments on commit 0a99028

Please sign in to comment.