Skip to content

Commit

Permalink
Stop considering MEMBER_FUNCTION_DEF as a qualified name component
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=233487288
  • Loading branch information
nreid260 authored and tjgq committed Feb 12, 2019
1 parent a958d21 commit 1bce7e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
10 changes: 9 additions & 1 deletion src/com/google/javascript/rhino/Node.java
Expand Up @@ -2214,6 +2214,9 @@ public final boolean isQualifiedName() {
return true;
case GETPROP:
return getFirstChild().isQualifiedName();

case MEMBER_FUNCTION_DEF:
// These are explicitly *not* qualified name components.
default:
return false;
}
Expand All @@ -2236,7 +2239,6 @@ private boolean matchesQualifiedName(String qname, int endIndex) {

switch (this.getToken()) {
case NAME:
case MEMBER_FUNCTION_DEF:
String name = getString();
return start == 0 && !name.isEmpty() && name.length() == endIndex && qname.startsWith(name);
case THIS:
Expand All @@ -2249,6 +2251,9 @@ private boolean matchesQualifiedName(String qname, int endIndex) {
&& prop.length() == endIndex - start
&& prop.regionMatches(0, qname, start, endIndex - start)
&& getFirstChild().matchesQualifiedName(qname, start - 1);

case MEMBER_FUNCTION_DEF:
// These are explicitly *not* qualified name components.
default:
return false;
}
Expand All @@ -2274,6 +2279,9 @@ public final boolean matchesQualifiedName(Node n) {
// ==, rather than equal as it is intern'd in setString
return getLastChild().getString() == n.getLastChild().getString()
&& getFirstChild().matchesQualifiedName(n.getFirstChild());

case MEMBER_FUNCTION_DEF:
// These are explicitly *not* qualified name components.
default:
return false;
}
Expand Down
40 changes: 25 additions & 15 deletions test/com/google/javascript/jscomp/Es6RewriteClassTest.java
Expand Up @@ -142,7 +142,7 @@ public void testSimpleClassStatement() {
FunctionType classCConstructorType = classCInstanceType.getConstructor();

// `C` from `let C = function() {};`
Node cName = getNodeMatchingQName(getLastCompiler().getJsRoot(), "C");
Node cName = getNodeWithName(getLastCompiler().getJsRoot(), "C");
assertNode(cName).hasToken(Token.NAME);
assertType(cName.getJSType()).isEqualTo(classCConstructorType);

Expand All @@ -164,7 +164,7 @@ public void testClassStatementWithConstructor() {
FunctionType classCConstructorType = classCInstanceType.getConstructor();

// `C` from `let C = function() {};`
Node cName = getNodeMatchingQName(getLastCompiler().getJsRoot(), "C");
Node cName = getNodeWithName(getLastCompiler().getJsRoot(), "C");
assertNode(cName).hasToken(Token.NAME);
assertType(cName.getJSType()).isEqualTo(classCConstructorType);

Expand All @@ -191,8 +191,7 @@ public void testClassStatementWithMethod() {
JSType methodType = classCPrototypeType.getPropertyType("method");

// `C.prototype.method`
Node cPrototypeMethod =
getNodeMatchingQName(getLastCompiler().getJsRoot(), "C.prototype.method");
Node cPrototypeMethod = getNodeWithName(getLastCompiler().getJsRoot(), "method").getParent();
assertNode(cPrototypeMethod).matchesQualifiedName("C.prototype.method");
assertType(cPrototypeMethod.getJSType()).isEqualTo(methodType);

Expand Down Expand Up @@ -2633,11 +2632,11 @@ public void testSimpleClassStatement_hasCorrectSourceInfo() {

// Get nodes from the original, pre-transpiled AST
// `C` from `class C { }`
Node sourceCName = getNodeMatchingQName(sourceRoot, "C");
Node sourceCName = getNodeWithName(sourceRoot, "C");
Node sourceClass = sourceCName.getParent();

// `C` from `let C = function() {};` matches `C` from `class C { }`
Node expectedCName = getNodeMatchingQName(expectedRoot, "C");
Node expectedCName = getNodeWithName(expectedRoot, "C");
assertNode(expectedCName).matchesQualifiedName("C").hasEqualSourceInfoTo(sourceCName);

// function() {}
Expand All @@ -2656,13 +2655,13 @@ public void testClassStatementWithConstructor_hasCorrectSourceInfo() {

// Get nodes from the original, pre-transpiled AST
// `C` from `class C {`
Node sourceCName = getNodeMatchingQName(sourceRoot, "C");
Node sourceCName = getNodeWithName(sourceRoot, "C");
// `constructor() {}`
Node sourceConstructorFunction = getNodeMatchingQName(sourceRoot, "constructor").getOnlyChild();
Node sourceConstructorFunction = getNodeWithName(sourceRoot, "constructor").getOnlyChild();
assertNode(sourceConstructorFunction).hasToken(Token.FUNCTION);

// `C` from `let C = function() {};` matches `C` from `class C {`
Node expectedCName = getNodeMatchingQName(expectedRoot, "C");
Node expectedCName = getNodeWithName(expectedRoot, "C");
assertNode(expectedCName).matchesQualifiedName("C").hasEqualSourceInfoTo(sourceCName);

// `function() {}` matches `constructor() {}`
Expand All @@ -2687,12 +2686,12 @@ public void testClassStatementWithMethod_hasCorrectSourceInfo() {

// Get nodes from the original, pre-transpiled AST
// The MEMBER_FUNCTION_DEF for `method`
Node sourceMethodMemberDef = getNodeMatchingQName(sourceRoot, "method");
Node sourceMethodMemberDef = getNodeWithName(sourceRoot, "method");
// The FUNCTION node for `method() {}`
Node sourceMethodFunction = sourceMethodMemberDef.getOnlyChild();

// `C.prototype.method` has source info matching `method`
Node cPrototypeMethod = getNodeMatchingQName(expectedRoot, "C.prototype.method");
Node cPrototypeMethod = getNodeWithName(expectedRoot, "method").getParent();
assertNode(cPrototypeMethod).hasEqualSourceInfoTo(sourceMethodMemberDef);

// `C.prototype` has source info matching `method`
Expand Down Expand Up @@ -2925,12 +2924,23 @@ private Node getNodeMatchingLabel(Node root, String label) {
}

/** Returns the first node (preorder) in the given AST that matches the given qualified name */
private Node getNodeMatchingQName(Node root, String qname) {
if (root.matchesQualifiedName(qname)) {
return root;
private Node getNodeWithName(Node root, String name) {
switch (root.getToken()) {
case NAME:
case STRING:
case MEMBER_FUNCTION_DEF:
case STRING_KEY:
if (root.getString().equals(name)) {
return root;
}
break;

default:
break;
}

for (Node child : root.children()) {
Node result = getNodeMatchingQName(child, qname);
Node result = getNodeWithName(child, name);
if (result != null) {
return result;
}
Expand Down

0 comments on commit 1bce7e3

Please sign in to comment.