From 2a0ce5f7ab3b1a15c27e301e2f0b7390acfdfd7b Mon Sep 17 00:00:00 2001 From: sdh Date: Fri, 9 Nov 2018 19:25:06 -0800 Subject: [PATCH] Factor out a QualifiedName class This provides a lighter-weight alternative to using Node to pass around qualified names, without the extra string-parsing required to deal with Strings. QualifiedName objects can be built from Nodes, strings, or by adding additional property accesses on top of an existing QualifiedName object. This should be used anywhere we're currently writing `owner + "." + prop`, as well as replace most usages of Node.matchesQualifiedName(String). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=220899304 --- .../javascript/jscomp/TypedScopeCreator.java | 75 ++--- src/com/google/javascript/rhino/Node.java | 5 + .../javascript/rhino/QualifiedName.java | 293 ++++++++++++++++++ .../javascript/rhino/QualifiedNameTest.java | 204 ++++++++++++ 4 files changed, 529 insertions(+), 48 deletions(-) create mode 100644 src/com/google/javascript/rhino/QualifiedName.java create mode 100644 test/com/google/javascript/rhino/QualifiedNameTest.java diff --git a/src/com/google/javascript/jscomp/TypedScopeCreator.java b/src/com/google/javascript/jscomp/TypedScopeCreator.java index 5b0c0e99ccf..ed8fbcc8284 100644 --- a/src/com/google/javascript/jscomp/TypedScopeCreator.java +++ b/src/com/google/javascript/jscomp/TypedScopeCreator.java @@ -63,11 +63,11 @@ import com.google.javascript.jscomp.NodeTraversal.AbstractScopedCallback; import com.google.javascript.jscomp.NodeTraversal.AbstractShallowStatementCallback; import com.google.javascript.rhino.ErrorReporter; -import com.google.javascript.rhino.IR; import com.google.javascript.rhino.InputId; import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.NominalTypeBuilder; +import com.google.javascript.rhino.QualifiedName; import com.google.javascript.rhino.StaticSymbolTable; import com.google.javascript.rhino.Token; import com.google.javascript.rhino.jstype.EnumType; @@ -225,19 +225,12 @@ void resolve() { } } - /** - * Stores the type and qualified name for a destructuring rvalue, which has no actual AST node. - * The {@code qualifiedName} is therefore a detached Node cloned out of the source tree with extra - * getprops added onto it. Cloning should generally be pretty cheap (since it's always just a - * qualified name). Nevertheless, we may pull out a QualifiedName class abstraction at some point - * in the future, which would allow avoiding the clone in the first place. Using a Node here is - * necessary to avoid duplicating logic for non-destructured qualified name aliasing. - */ + /** Stores the type and qualified name for a destructuring rvalue. */ private static class RValueInfo { @Nullable final JSType type; - @Nullable final Node qualifiedName; + @Nullable final QualifiedName qualifiedName; - RValueInfo(JSType type, Node qualifiedName) { + RValueInfo(JSType type, QualifiedName qualifiedName) { this.type = type; this.qualifiedName = qualifiedName; } @@ -245,17 +238,6 @@ private static class RValueInfo { static RValueInfo empty() { return new RValueInfo(null, null); } - - /** Returns a new RValueInfo whose qualified name has an extra property. */ - RValueInfo forProperty(JSType type, String property) { - if (qualifiedName == null) { - return new RValueInfo(type, null); - } else if (qualifiedName.getParent() == null) { - return new RValueInfo(type, IR.getprop(qualifiedName, property)); - } - // qualified name is already in the tree, so we need to clone it first. - return new RValueInfo(type, IR.getprop(qualifiedName.cloneTree(), property)); - } } TypedScopeCreator(AbstractCompiler compiler) { @@ -901,7 +883,7 @@ void defineVarChild(JSDocInfo declarationInfo, Node child, TypedScope scope) { value != null ? new RValueInfo( getDeclaredRValueType(/* lValue= */ null, value), - /* qualifiedName= */ value) + value.getQualifiedNameObject()) : new RValueInfo(unknownType, /* qualifiedName= */ null)); } } @@ -928,14 +910,16 @@ private RValueInfo inferTypeForDestructuredTarget( RValueInfo rvalue = patternTypeSupplier.get(); JSType patternType = rvalue.type; String propertyName = target.getStringKey().getString(); + QualifiedName qname = + rvalue.qualifiedName != null ? rvalue.qualifiedName.getprop(propertyName) : null; if (patternType == null || patternType.isUnknownType()) { - return rvalue.forProperty(null, propertyName); + return new RValueInfo(null, qname); } if (patternType.hasProperty(propertyName)) { JSType type = patternType.findPropertyType(propertyName); - return rvalue.forProperty(type, propertyName); + return new RValueInfo(type, qname); } - return rvalue.forProperty(null, propertyName); + return new RValueInfo(null, qname); } void defineDestructuringPatternInVarDeclaration( @@ -1890,7 +1874,7 @@ && shouldUseFunctionLiteralType( if (NodeUtil.isConstantDeclaration(compiler.getCodingConvention(), info, lValue)) { if (rValue != null) { JSType rValueType = getDeclaredRValueType(lValue, rValue); - maybeDeclareAliasType(lValue, rValue, rValueType); + maybeDeclareAliasType(lValue, rValue.getQualifiedNameObject(), rValueType); if (rValueType != null) { return rValueType; } @@ -1920,7 +1904,7 @@ && shouldUseFunctionLiteralType( * @param rvalueName the rvalue's qualified name if it exists, null otherwise */ private void maybeDeclareAliasType( - Node lValue, @Nullable Node rValue, @Nullable JSType rValueType) { + Node lValue, @Nullable QualifiedName rValue, @Nullable JSType rValueType) { // NOTE: this allows some strange patterns such allowing instance properties // to be aliases of constructors, and then creating a local alias of that to be // used as a type name. Consider restricting this. @@ -1949,9 +1933,9 @@ private void maybeDeclareAliasType( FunctionType functionType = rValueType.toMaybeFunctionType(); typeRegistry.declareType( currentScope, lValue.getQualifiedName(), functionType.getInstanceType()); - } else if (rValue.isQualifiedName()) { + } else if (rValue != null) { // Also infer a type name for aliased @typedef - JSType rhsNamedType = typeRegistry.getType(currentScope, rValue.getQualifiedName()); + JSType rhsNamedType = typeRegistry.getType(currentScope, rValue.join()); if (rhsNamedType != null) { typeRegistry.declareType(currentScope, lValue.getQualifiedName(), rhsNamedType); } @@ -1959,15 +1943,13 @@ private void maybeDeclareAliasType( } /** Returns the AST node associated with the definition, if any. */ - private Node getDefinitionNode(Node qnameNode) { - if (!qnameNode.isGetProp()) { - TypedVar var = currentScope.getVar(qnameNode.getQualifiedName()); + private Node getDefinitionNode(QualifiedName qname) { + if (qname.isSimple()) { + TypedVar var = currentScope.getVar(qname.getComponent()); return var != null ? var.getNameNode() : null; } - ObjectType parent = ObjectType.cast(lookupQualifiedName(qnameNode.getFirstChild())); - return parent != null - ? parent.getPropertyDefSite(qnameNode.getLastChild().getString()) - : null; + ObjectType parent = ObjectType.cast(lookupQualifiedName(qname.getOwner())); + return parent != null ? parent.getPropertyDefSite(qname.getComponent()) : null; } /** @@ -1996,7 +1978,7 @@ private JSType getDeclaredRValueType(@Nullable Node lValue, Node rValue) { // If rValue is a name, try looking it up in the current scope. if (rValue.isQualifiedName()) { - return lookupQualifiedName(rValue); + return lookupQualifiedName(rValue.getQualifiedNameObject()); } // Check for simple invariant operations, such as "!x" or "+x" or "''+x" @@ -2013,7 +1995,7 @@ private JSType getDeclaredRValueType(@Nullable Node lValue, Node rValue) { } if (rValue.isNew() && rValue.getFirstChild().isQualifiedName()) { - JSType targetType = lookupQualifiedName(rValue.getFirstChild()); + JSType targetType = lookupQualifiedName(rValue.getFirstChild().getQualifiedNameObject()); if (targetType != null) { FunctionType fnType = targetType .restrictByNotNullOrUndefined() @@ -2047,23 +2029,20 @@ private JSType getDeclaredRValueType(@Nullable Node lValue, Node rValue) { return null; } - private JSType lookupQualifiedName(Node n) { - String name = n.getQualifiedName(); - TypedVar slot = currentScope.getVar(name); + private JSType lookupQualifiedName(QualifiedName qname) { + TypedVar slot = currentScope.getVar(qname.join()); if (slot != null && !slot.isTypeInferred()) { JSType type = slot.getType(); if (type != null && !type.isUnknownType()) { return type; } - } else if (n.isGetProp()) { - JSType type = lookupQualifiedName(n.getFirstChild()); + } else if (!qname.isSimple()) { + JSType type = lookupQualifiedName(qname.getOwner()); // NOTE: The scope only contains declared types at this // point so any property we find is a value type // to look up properties on. if (type != null) { - JSType propType = type.findPropertyType( - n.getLastChild().getString()); - return propType; + return type.findPropertyType(qname.getComponent()); } } return null; @@ -2504,7 +2483,7 @@ void checkForTypedef(NodeTraversal t, Node candidate, JSDocInfo info) { .withType(getNativeType(NO_TYPE)) .allowLaterTypeInference(false) .defineSlot(); - Node definitionNode = getDefinitionNode(candidate.getFirstChild()); + Node definitionNode = getDefinitionNode(candidate.getFirstChild().getQualifiedNameObject()); if (definitionNode != null) { ObjectType parent = ObjectType.cast(definitionNode.getJSType()); if (parent != null) { diff --git a/src/com/google/javascript/rhino/Node.java b/src/com/google/javascript/rhino/Node.java index 7c251c7e190..937a75fa8c1 100644 --- a/src/com/google/javascript/rhino/Node.java +++ b/src/com/google/javascript/rhino/Node.java @@ -2111,6 +2111,11 @@ public final String getQualifiedName() { } } + @Nullable + public final QualifiedName getQualifiedNameObject() { + return isQualifiedName() ? new QualifiedName.NodeQname(this) : null; + } + /** * Helper method for {@link #getQualifiedName} to handle GETPROP nodes. * diff --git a/src/com/google/javascript/rhino/QualifiedName.java b/src/com/google/javascript/rhino/QualifiedName.java new file mode 100644 index 00000000000..1a48c18ad2f --- /dev/null +++ b/src/com/google/javascript/rhino/QualifiedName.java @@ -0,0 +1,293 @@ +/* + * + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Rhino code, released + * May 6, 1999. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1997-1999 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Norris Boyd + * Roger Lawrence + * Mike McCabe + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License Version 2 or later (the "GPL"), in which + * case the provisions of the GPL are applicable instead of those above. If + * you wish to allow use of your version of this file only under the terms of + * the GPL and not to allow others to use your version of this file under the + * MPL, indicate your decision by deleting the provisions above and replacing + * them with the notice and other provisions required by the GPL. If you do + * not delete the provisions above, a recipient may use your version of this + * file under either the MPL or the GPL. + * + * ***** END LICENSE BLOCK ***** */ + +package com.google.javascript.rhino; + +import com.google.common.collect.ImmutableList; +import javax.annotation.Nullable; + +/** + * Abstraction over a qualified name. Unifies Node-based qualified names and string-based names, + * allowing to lazily parse strings and represent a pre-parsed qualified name without the overhead + * of a whole Node. Essentially, a qualified name is a linked list of {@linkplain #getComponent + * components}, starting from the outermost property access and ending with the root of the name, + * which is a {@linkplain #isSimple simple name} with no {@linkplain #getOwner owner}. + */ +public abstract class QualifiedName { + + // All subclasses must be defined in this file. + private QualifiedName() {} + + public static QualifiedName of(String string) { + int lastIndex = 0; + int index; + ImmutableList.Builder builder = ImmutableList.builder(); + do { + index = string.indexOf('.', lastIndex); + builder.add(string.substring(lastIndex, index < 0 ? string.length() : index).intern()); + lastIndex = index + 1; + } while (index >= 0); + ImmutableList terms = builder.build(); + return new StringListQname(terms, terms.size()); + } + + /** + * Returns the qualified name of the owner, or null for simple names. For the name "foo.bar.baz", + * this returns an object representing "foo.bar". + */ + @Nullable + public abstract QualifiedName getOwner(); + + /** + * Returns outer-most term of this qualified name, or the entire name for simple names. For the + * name "foo.bar.baz", this returns "baz". + */ + public abstract String getComponent(); + + /** Returns true if this is a simple name. */ + public abstract boolean isSimple(); + + // TODO(sdh): We'll probably want a getRoot() method, which would be useful once this is + // integrated + // further into TypedScopeCreator. + + /** Appends the joined qualified name to the given StringBuilder. */ + abstract void appendTo(StringBuilder sb); + + /** Checks whether the given node matches this name. */ + public abstract boolean matches(Node n); + + /** + * Returns the components of this name as an iterable of strings, starting at the root. For the + * qualified name foo.bar.baz, this returns ["foo", "bar", "baz"]. + */ + public Iterable components() { + ImmutableList.Builder components = ImmutableList.builder(); + buildComponents(components); + return components.build(); + } + + private void buildComponents(ImmutableList.Builder builder) { + QualifiedName owner = getOwner(); + if (owner != null) { + owner.buildComponents(builder); + } + builder.add(getComponent()); + } + + /** Returns the qualified name as a string. */ + public String join() { + StringBuilder sb = new StringBuilder(); + appendTo(sb); + return sb.toString(); + } + + /** + * Returns a new qualified name object with {@code this} name as the owner and the given string as + * the property name. + */ + public QualifiedName getprop(String propertyName) { + return new GetpropQname(this, propertyName); + } + + /** A qualified name based on a list of string terms. */ + private static class StringListQname extends QualifiedName { + final ImmutableList terms; + final int size; + + StringListQname(ImmutableList terms, int size) { + this.terms = terms; + this.size = size; + } + + @Override + public QualifiedName getOwner() { + return size > 1 ? new StringListQname(terms, size - 1) : null; + } + + @Override + public String getComponent() { + return terms.get(size - 1); + } + + @Override + public boolean isSimple() { + return size == 1; + } + + @Override + void appendTo(StringBuilder sb) { + for (int i = 0; i < size; i++) { + if (i > 0) { + sb.append('.'); + } + sb.append(terms.get(i)); + } + } + + @Override + public Iterable components() { + return terms.subList(0, size); + } + + @Override + public boolean matches(Node n) { + int pos = size - 1; + while (pos > 0 && n.isGetProp()) { + // NOTE: these strings are all interned, so we can do identity comparison. + if (n.getLastChild().getString() != terms.get(pos)) { + return false; + } + pos--; + n = n.getFirstChild(); + } + if (pos > 0) { + return false; + } + switch (n.getToken()) { + case NAME: + case MEMBER_FUNCTION_DEF: + return terms.get(0) == n.getString(); + case THIS: + return terms.get(0) == THIS; + case SUPER: + return terms.get(0) == SUPER; + default: + return false; + } + } + } + + /** A qualified name built with an extra property access on an existing qualified name. */ + private static class GetpropQname extends QualifiedName { + final QualifiedName owner; + final String prop; + + GetpropQname(QualifiedName owner, String prop) { + this.owner = owner; + this.prop = prop.intern(); + } + + @Override + public QualifiedName getOwner() { + return owner; + } + + @Override + public String getComponent() { + return prop; + } + + @Override + public boolean isSimple() { + return false; + } + + @Override + void appendTo(StringBuilder sb) { + owner.appendTo(sb); + sb.append('.').append(prop); + } + + @Override + public boolean matches(Node n) { + return n.isGetProp() + && n.getLastChild().getString() == prop + && owner.matches(n.getFirstChild()); + } + } + + /** + * A qualified name from a node. The precondition that the node is actually a qualified name is + * not actually checked here, though it will throw IllegalStateException eventually if it is not. + */ + static final class NodeQname extends QualifiedName { + private final Node node; + + NodeQname(Node n) { + this.node = n; + } + + @Override + public QualifiedName getOwner() { + return node.isGetProp() ? new NodeQname(node.getFirstChild()) : null; + } + + @Override + public String getComponent() { + switch (node.getToken()) { + case GETPROP: + return node.getLastChild().getString(); + case THIS: + return THIS; + case SUPER: + return SUPER; + case NAME: + case MEMBER_FUNCTION_DEF: + return node.getString(); + default: + throw new IllegalStateException("Not a qualified name: " + node); + } + } + + @Override + public boolean isSimple() { + return !node.isGetProp(); + } + + @Override + void appendTo(StringBuilder sb) { + sb.append(join()); + } + + @Override + public String join() { + return node.getQualifiedName(); + } + + @Override + public boolean matches(Node n) { + return n.matchesQualifiedName(node); + } + } + + private static final String THIS = "this".intern(); + private static final String SUPER = "super".intern(); +} diff --git a/test/com/google/javascript/rhino/QualifiedNameTest.java b/test/com/google/javascript/rhino/QualifiedNameTest.java new file mode 100644 index 00000000000..02b4153a4ca --- /dev/null +++ b/test/com/google/javascript/rhino/QualifiedNameTest.java @@ -0,0 +1,204 @@ +/* + * + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Rhino code, released + * May 6, 1999. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1997-1999 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Nick Santos + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License Version 2 or later (the "GPL"), in which + * case the provisions of the GPL are applicable instead of those above. If + * you wish to allow use of your version of this file only under the terms of + * the GPL and not to allow others to use your version of this file under the + * MPL, indicate your decision by deleting the provisions above and replacing + * them with the notice and other provisions required by the GPL. If you do + * not delete the provisions above, a recipient may use your version of this + * file under either the MPL or the GPL. + * + * ***** END LICENSE BLOCK ***** */ + +package com.google.javascript.rhino; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class QualifiedNameTest { + + // All of these qualified names are "foo.bar.baz" + private static final QualifiedName FROM_STRING = QualifiedName.of("foo.bar.baz"); + private static final QualifiedName FROM_NODE = + IR.getprop(IR.getprop(IR.name("foo"), IR.string("bar")), IR.string("baz")) + .getQualifiedNameObject(); + private static final QualifiedName FROM_GETPROP = + QualifiedName.of("foo").getprop("bar").getprop("baz"); + + private static Node qname(Node root, String... props) { + Node n = root; + for (String p : props) { + n = IR.getprop(n, IR.string(p)); + } + return n; + } + + @Test + public void testJoin_fromString() { + QualifiedName n = QualifiedName.of("foo.bar.baz"); + assertThat(n.join()).isEqualTo("foo.bar.baz"); + } + + @Test + public void testJoin_fromNode() { + QualifiedName n = qname(IR.name("foo"), "bar", "baz").getQualifiedNameObject(); + assertThat(n.join()).isEqualTo("foo.bar.baz"); + + n = qname(IR.thisNode(), "bar", "baz").getQualifiedNameObject(); + assertThat(n.join()).isEqualTo("this.bar.baz"); + + n = qname(IR.superNode(), "bar", "baz").getQualifiedNameObject(); + assertThat(n.join()).isEqualTo("super.bar.baz"); + } + + @Test + public void testJoin_fromGetprop() { + QualifiedName n = QualifiedName.of("foo").getprop("bar").getprop("baz"); + assertThat(n.join()).isEqualTo("foo.bar.baz"); + } + + @Test + public void testComponents_fromString() { + QualifiedName n = QualifiedName.of("foo.bar.baz"); + assertThat(n.components()).containsExactly("foo", "bar", "baz").inOrder(); + } + + @Test + public void testComponents_fromNode() { + QualifiedName n = qname(IR.name("foo"), "bar", "baz").getQualifiedNameObject(); + assertThat(n.components()).containsExactly("foo", "bar", "baz").inOrder(); + } + + @Test + public void testComponents_fromGetprop() { + QualifiedName n = QualifiedName.of("foo").getprop("bar").getprop("baz"); + assertThat(n.components()).containsExactly("foo", "bar", "baz").inOrder(); + } + + @Test + public void testGetOwner_fromString() { + assertThat(QualifiedName.of("foo.bar.baz").getOwner().join()).isEqualTo("foo.bar"); + assertThat(QualifiedName.of("foo").getOwner()).isNull(); + } + + @Test + public void testGetOwner_fromNode() { + QualifiedName n = qname(IR.name("foo"), "bar", "baz").getQualifiedNameObject(); + assertThat(n.getOwner().join()).isEqualTo("foo.bar"); + assertThat(IR.name("foo").getQualifiedNameObject().getOwner()).isNull(); + assertThat(IR.thisNode().getQualifiedNameObject().getOwner()).isNull(); + assertThat(IR.superNode().getQualifiedNameObject().getOwner()).isNull(); + } + + @Test + public void testGetOwner_fromGetprop() { + QualifiedName foo = QualifiedName.of("foo"); + QualifiedName fooBar = foo.getprop("bar"); + assertThat(fooBar.getOwner()).isEqualTo(foo); + assertThat(fooBar.getprop("baz").getOwner()).isEqualTo(fooBar); + } + + @Test + public void testGetComponent_fromString() { + assertThat(QualifiedName.of("foo.bar.baz").getComponent()).isEqualTo("baz"); + assertThat(QualifiedName.of("foo").getComponent()).isEqualTo("foo"); + } + + @Test + public void testGetComponent_fromNode() { + QualifiedName n = qname(IR.name("foo"), "bar", "baz").getQualifiedNameObject(); + assertThat(n.getComponent()).isEqualTo("baz"); + assertThat(IR.name("foo").getQualifiedNameObject().getComponent()).isEqualTo("foo"); + assertThat(IR.thisNode().getQualifiedNameObject().getComponent()).isEqualTo("this"); + assertThat(IR.superNode().getQualifiedNameObject().getComponent()).isEqualTo("super"); + } + + @Test + public void testGetComponent_fromGetprop() { + QualifiedName foo = QualifiedName.of("foo"); + QualifiedName fooBar = foo.getprop("bar"); + assertThat(fooBar.getComponent()).isEqualTo("bar"); + assertThat(fooBar.getprop("baz").getComponent()).isEqualTo("baz"); + } + + @Test + public void testMatch_fromString() { + QualifiedName n = QualifiedName.of("foo.bar.baz"); + assertThat(n.matches(qname(IR.name("foo"), "bar", "baz"))).isTrue(); + assertThat(n.matches(qname(IR.thisNode(), "bar", "baz"))).isFalse(); + assertThat(n.matches(qname(IR.superNode(), "bar", "baz"))).isFalse(); + assertThat(n.matches(qname(IR.name("foo"), "baz", "bar"))).isFalse(); + + n = QualifiedName.of("this.qux"); + assertThat(n.matches(qname(IR.thisNode(), "qux"))).isTrue(); + assertThat(n.matches(qname(IR.name("x"), "qux"))).isFalse(); + + n = QualifiedName.of("super.qux"); + assertThat(n.matches(qname(IR.superNode(), "qux"))).isTrue(); + assertThat(n.matches(qname(IR.name("x"), "qux"))).isFalse(); + } + + @Test + public void testMatch_fromNode() { + QualifiedName n = qname(IR.name("foo"), "bar", "baz").getQualifiedNameObject(); + assertThat(n.matches(qname(IR.name("foo"), "bar", "baz"))).isTrue(); + assertThat(n.matches(qname(IR.thisNode(), "bar", "baz"))).isFalse(); + assertThat(n.matches(qname(IR.superNode(), "bar", "baz"))).isFalse(); + assertThat(n.matches(qname(IR.name("foo"), "baz", "bar"))).isFalse(); + + n = qname(IR.thisNode(), "qux").getQualifiedNameObject(); + assertThat(n.matches(qname(IR.thisNode(), "qux"))).isTrue(); + assertThat(n.matches(qname(IR.name("x"), "qux"))).isFalse(); + + n = qname(IR.superNode(), "qux").getQualifiedNameObject(); + assertThat(n.matches(qname(IR.superNode(), "qux"))).isTrue(); + assertThat(n.matches(qname(IR.name("x"), "qux"))).isFalse(); + } + + @Test + public void testMatch_fromGetprop() { + QualifiedName n = QualifiedName.of("foo").getprop("bar").getprop("baz"); + assertThat(n.matches(qname(IR.name("foo"), "bar", "baz"))).isTrue(); + assertThat(n.matches(qname(IR.thisNode(), "bar", "baz"))).isFalse(); + assertThat(n.matches(qname(IR.superNode(), "bar", "baz"))).isFalse(); + assertThat(n.matches(qname(IR.name("foo"), "baz", "bar"))).isFalse(); + + n = QualifiedName.of("this").getprop("qux"); + assertThat(n.matches(qname(IR.thisNode(), "qux"))).isTrue(); + assertThat(n.matches(qname(IR.name("x"), "qux"))).isFalse(); + + n = QualifiedName.of("super").getprop("qux"); + assertThat(n.matches(qname(IR.superNode(), "qux"))).isTrue(); + assertThat(n.matches(qname(IR.name("x"), "qux"))).isFalse(); + } +}