Skip to content

Commit

Permalink
Renamed isThisProp to isThisPropInsideClassWithName, added Java d…
Browse files Browse the repository at this point in the history
…ocs to ClassUtil and changed the method name `getPrototypeNameOfThisProp` to`getFullyQualifiedNameOfThisProp`.

The renaming was decided to best describe the functionality of the methods. Refactoring was done to ensure the method calls are valid.

PiperOrigin-RevId: 549152392
  • Loading branch information
Closure Team authored and copybara-github committed Jul 19, 2023
1 parent 4579891 commit a57f7dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/com/google/javascript/jscomp/ijs/ClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ private ClassUtil() {}

private static final QualifiedName GOOG_DEFINECLASS = QualifiedName.of("goog.defineClass");

static boolean isThisProp(Node getprop) {
return getClassNameOfThisProp(getprop) != null;
/**
* Return whether the given node represents a GETPROP with a first child THIS inside a named
* class.
*/
static boolean isThisPropInsideClassWithName(Node maybeGetProp) {
return getClassNameOfThisProp(maybeGetProp) != null;
}

static String getPrototypeNameOfThisProp(Node getprop) {
String className = checkNotNull(getClassNameOfThisProp(getprop));
return className + ".prototype." + getprop.getString();
/**
* Return the fully qualified name of a this.property inside a constructor. This method called
* should only be called if `isThisPropInsideClassWithName` returns true.
*/
static String getFullyQualifiedNameOfThisProp(Node getProp) {
String className = checkNotNull(getClassNameOfThisProp(getProp));
return className + ".prototype." + getProp.getString();
}

private static @Nullable String getClassNameOfThisProp(Node getprop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
Node lhs = expr.getFirstChild();
if (!lhs.isQualifiedName()
|| (lhs.isName() && !t.inGlobalScope() && !t.inModuleScope())
|| (!ClassUtil.isThisProp(lhs)
|| (!ClassUtil.isThisPropInsideClassWithName(lhs)
&& !t.inGlobalHoistScope()
&& !t.inModuleHoistScope())) {
NodeUtil.deleteNode(n, t.getCompiler());
Expand Down Expand Up @@ -360,7 +360,7 @@ protected void processConstWithRhs(NodeTraversal t, Node nameNode) {
JSDocInfo originalJsdoc = jsdocNode.getJSDocInfo();
Node rhs = NodeUtil.getRValueOfLValue(nameNode);
JSDocInfo newJsdoc = JsdocUtil.getJSDocForRhs(rhs, originalJsdoc);
if (newJsdoc == null && ClassUtil.isThisProp(nameNode)) {
if (newJsdoc == null && ClassUtil.isThisPropInsideClassWithName(nameNode)) {
Var decl = findNameDeclaration(t.getScope(), rhs);
newJsdoc = JsdocUtil.getJSDocForName(decl, originalJsdoc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ private PotentialDeclaration(String fullyQualifiedName, Node lhs, @Nullable Node
static PotentialDeclaration fromName(Node nameNode) {
checkArgument(nameNode.isQualifiedName(), nameNode);
Node rhs = NodeUtil.getRValueOfLValue(nameNode);
if (ClassUtil.isThisProp(nameNode)) {
String name = ClassUtil.getPrototypeNameOfThisProp(nameNode);
if (ClassUtil.isThisPropInsideClassWithName(nameNode)) {
String name = ClassUtil.getFullyQualifiedNameOfThisProp(nameNode);
return new ThisPropDeclaration(name, nameNode, rhs);
}
return new NameDeclaration(nameNode.getQualifiedName(), nameNode, rhs);
Expand Down Expand Up @@ -678,7 +678,7 @@ static boolean isImportRhs(@Nullable Node rhs) {
}

static boolean isAliasDeclaration(Node lhs, @Nullable Node rhs) {
return !ClassUtil.isThisProp(lhs)
return !ClassUtil.isThisPropInsideClassWithName(lhs)
&& isConstToBeInferred(lhs)
&& rhs != null
&& isQualifiedAliasExpression(rhs);
Expand Down

0 comments on commit a57f7dd

Please sign in to comment.