Java Code Examples for com.google.javascript.rhino.Node#getQualifiedName()

The following examples show how to use com.google.javascript.rhino.Node#getQualifiedName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cardumen_0087_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets the function's name. This method recognizes five forms:
 * <ul>
 * <li>{@code function name() ...}</li>
 * <li>{@code var name = function() ...}</li>
 * <li>{@code qualified.name = function() ...}</li>
 * <li>{@code var name2 = function name1() ...}</li>
 * <li>{@code qualified.name2 = function name1() ...}</li>
 * </ul>
 * In two last cases with named function expressions, the second name is
 * returned (the variable of qualified name).
 *
 * @param n a node whose type is {@link Token#FUNCTION}
 * @return the function's name, or {@code null} if it has no name
 */
static String getFunctionName(Node n) {
  Preconditions.checkState(n.isFunction());
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.NAME:
      // var name = function() ...
      // var name2 = function name1() ...
      return parent.getQualifiedName();

    case Token.ASSIGN:
      // qualified.name = function() ...
      // qualified.name2 = function name1() ...
      return parent.getFirstChild().getQualifiedName();

    default:
      // function name() ...
      String name = n.getFirstChild().getQualifiedName();
      return name;
  }
}
 
Example 2
Source File: Cardumen_00200_t.java    From coming with MIT License 6 votes vote down vote up
/** Get the name of the given l-value node. */
static String getBestLValueName(@Nullable Node lValue) {
  if (lValue == null || lValue.getParent() == null) {
    return null;
  }
  if (isObjectLitKey(lValue, lValue.getParent())) {
    Node owner = getBestLValue(lValue.getParent());
    if (owner != null) {
      String ownerName = getBestLValueName(owner);
      if (ownerName != null) {
        return ownerName + "." + getObjectLitKeyName(lValue);
      }
    }
    return null;
  }
  return lValue.getQualifiedName();
}
 
Example 3
Source File: ClosureCodingConvention.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private static String extractClassNameIfGoog(Node node, Node parent,
    String functionName){
  String className = null;
  if (NodeUtil.isExprCall(parent)) {
    Node callee = node.getFirstChild();
    if (callee != null && callee.isGetProp()) {
      String qualifiedName = callee.getQualifiedName();
      if (functionName.equals(qualifiedName)) {
        Node target = callee.getNext();
        if (target != null && target.isString()) {
          className = target.getString();
        }
      }
    }
  }
  return className;
}
 
Example 4
Source File: jMutRepair_003_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets the function's name. This method recognizes five forms:
 * <ul>
 * <li>{@code function name() ...}</li>
 * <li>{@code var name = function() ...}</li>
 * <li>{@code qualified.name = function() ...}</li>
 * <li>{@code var name2 = function name1() ...}</li>
 * <li>{@code qualified.name2 = function name1() ...}</li>
 * </ul>
 * In two last cases with named function expressions, the second name is
 * returned (the variable of qualified name).
 *
 * @param n a node whose type is {@link Token#FUNCTION}
 * @return the function's name, or {@code null} if it has no name
 */
static String getFunctionName(Node n) {
  Preconditions.checkState(n.isFunction());
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.NAME:
      // var name = function() ...
      // var name2 = function name1() ...
      return parent.getQualifiedName();

    case Token.ASSIGN:
      // qualified.name = function() ...
      // qualified.name2 = function name1() ...
      return parent.getFirstChild().getQualifiedName();

    default:
      // function name() ...
      String name = n.getFirstChild().getQualifiedName();
      return name;
  }
}
 
Example 5
Source File: CheckProvides.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void visitFunctionNode(Node n, Node parent) {
  Node name = null;
  JSDocInfo info = parent.getJSDocInfo();
  if (info != null && info.isConstructor()) {
    name = parent.getFirstChild();
  } else {
    // look to the child, maybe it's a named function
    info = n.getJSDocInfo();
    if (info != null && info.isConstructor()) {
      name = n.getFirstChild();
    }
  }
  if (name != null && name.isQualifiedName()) {
    String qualifiedName = name.getQualifiedName();
    if (!this.convention.isPrivate(qualifiedName)) {
      Visibility visibility = info.getVisibility();
      if (!visibility.equals(JSDocInfo.Visibility.PRIVATE)) {
        ctors.put(qualifiedName, name);
      }
    }
  }
}
 
Example 6
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the type specified in a JSDoc annotation near a GETPROP or NAME.
 *
 * Extracts type information from either the {@code @type} tag or from
 * the {@code @return} and {@code @param} tags.
 */
private JSType getDeclaredTypeInAnnotation(String sourceName,
    Node node, JSDocInfo info) {
  JSType jsType = null;
  Node objNode =
      node.isGetProp() ? node.getFirstChild() :
      NodeUtil.isObjectLitKey(node, node.getParent()) ? node.getParent() :
      null;
  if (info != null) {
    if (info.hasType()) {
      jsType = info.getType().evaluate(scope, typeRegistry);
    } else if (FunctionTypeBuilder.isFunctionTypeDeclaration(info)) {
      String fnName = node.getQualifiedName();
      jsType = createFunctionTypeFromNodes(
          null, fnName, info, node);
    }
  }
  return jsType;
}
 
Example 7
Source File: Closure_19_ChainableReverseAbstractInterpreter_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares a refined type in {@code scope} for the name represented by
 * {@code node}. It must be possible to refine the type of the given node in
 * the given scope, as determined by {@link #getTypeIfRefinable}.
 */
protected void declareNameInScope(FlowScope scope, Node node, JSType type) {
  switch (node.getType()) {
    case Token.NAME:
      scope.inferSlotType(node.getString(), type);
      break;

    case Token.GETPROP:
      String qualifiedName = node.getQualifiedName();
      Preconditions.checkNotNull(qualifiedName);

      JSType origType = node.getJSType();
      origType = origType == null ? getNativeType(UNKNOWN_TYPE) : origType;
      scope.inferQualifiedSlot(node, qualifiedName, origType, type);
      break;

    case Token.THIS:
      // "this" references aren't currently modeled in the CFG.
      break;

    default:
      throw new IllegalArgumentException("Node cannot be refined. \n" +
          node.toStringTree());
  }
}
 
Example 8
Source File: Closure_25_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSTypeNative assertedType = assertionFunctionSpec.getAssertedType();
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(getNativeType(assertedType));
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  if (getJSType(callNode).differsFrom(narrowed)) {
    callNode.setJSType(narrowed);
  }
  return scope;
}
 
Example 9
Source File: Closure_70_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Handle typedefs.
 * @param t The current traversal.
 * @param candidate A qualified name node.
 * @param info JSDoc comments.
 */
private void checkForTypedef(
    NodeTraversal t, Node candidate, JSDocInfo info) {
  if (info == null || !info.hasTypedefType()) {
    return;
  }

  String typedef = candidate.getQualifiedName();
  if (typedef == null) {
    return;
  }

  // TODO(nicksantos|user): This is a terrible, terrible hack
  // to bail out on recusive typedefs. We'll eventually need
  // to handle these properly.
  typeRegistry.declareType(typedef, getNativeType(UNKNOWN_TYPE));

  JSType realType = info.getTypedefType().evaluate(scope, typeRegistry);
  if (realType == null) {
    compiler.report(
        JSError.make(
            t.getSourceName(), candidate, MALFORMED_TYPEDEF, typedef));
  }

  typeRegistry.overwriteDeclaredType(typedef, realType);
  if (candidate.getType() == Token.GETPROP) {
    defineSlot(candidate, candidate.getParent(),
        getNativeType(NO_TYPE), false);
  }
}
 
Example 10
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 11
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, isExtern);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 12
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
static boolean isPrototypeProperty(Node n) {
  String lhsString = n.getQualifiedName();
  if (lhsString == null) {
    return false;
  }
  int prototypeIdx = lhsString.indexOf(".prototype.");
  return prototypeIdx != -1;
}
 
Example 13
Source File: NameReferenceGraphConstruction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return true if n MUST be a static name reference.
 */
private boolean isStaticNameReference(Node n, Scope scope) {
  Preconditions.checkArgument(n.isName() || n.isGetProp());
  if (n.isName()) {
    return true;
  }
  String qName = n.getQualifiedName();
  if (qName == null) {
    return false;
  }
  // TODO(user): This does not always work due to type system bugs.
  return scope.isDeclared(qName, true);
}
 
Example 14
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return Whether the node represents a qualified prototype property.
 */
static boolean isPrototypeProperty(Node n) {
  String lhsString = n.getQualifiedName();
  if (lhsString == null) {
    return false;
  }
  int prototypeIdx = lhsString.indexOf(".prototype.");
  return prototypeIdx != -1;
}
 
Example 15
Source File: ClosureCodingConvention.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ObjectLiteralCast getObjectLiteralCast(Node callNode) {
  Preconditions.checkArgument(callNode.isCall());
  ObjectLiteralCast proxyCast = super.getObjectLiteralCast(callNode);
  if (proxyCast != null) {
    return proxyCast;
  }

  Node callName = callNode.getFirstChild();
  if (!"goog.reflect.object".equals(callName.getQualifiedName()) ||
      callNode.getChildCount() != 3) {
    return null;
  }

  Node typeNode = callName.getNext();
  if (!typeNode.isQualifiedName()) {
    return null;
  }

  Node objectNode = typeNode.getNext();
  if (!objectNode.isObjectLit()) {
    return new ObjectLiteralCast(null, null, OBJECTLIT_EXPECTED);
  }

  return new ObjectLiteralCast(
      typeNode.getQualifiedName(), typeNode.getNext(), null);
}
 
Example 16
Source File: Closure_75_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return The string property name part of a qualified prototype name.
 */
static String getPrototypePropertyName(Node qName) {
  String qNameStr = qName.getQualifiedName();
  int prototypeIdx = qNameStr.lastIndexOf(".prototype.");
  int memberIndex = prototypeIdx + ".prototype".length() + 1;
  return qNameStr.substring(memberIndex);
}
 
Example 17
Source File: Closure_48_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Handle typedefs.
 * @param t The current traversal.
 * @param candidate A qualified name node.
 * @param info JSDoc comments.
 */
private void checkForTypedef(
    NodeTraversal t, Node candidate, JSDocInfo info) {
  if (info == null || !info.hasTypedefType()) {
    return;
  }

  String typedef = candidate.getQualifiedName();
  if (typedef == null) {
    return;
  }

  // TODO(nicksantos|user): This is a terrible, terrible hack
  // to bail out on recusive typedefs. We'll eventually need
  // to handle these properly.
  typeRegistry.declareType(typedef, getNativeType(UNKNOWN_TYPE));

  JSType realType = info.getTypedefType().evaluate(scope, typeRegistry);
  if (realType == null) {
    compiler.report(
        JSError.make(
            t.getSourceName(), candidate, MALFORMED_TYPEDEF, typedef));
  }

  typeRegistry.overwriteDeclaredType(typedef, realType);
  if (candidate.isGetProp()) {
    defineSlot(candidate, candidate.getParent(),
        getNativeType(NO_TYPE), false);
  }
}
 
Example 18
Source File: Closure_54_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 19
Source File: Cardumen_0014_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node represents a qualified prototype property.
 */
static boolean isPrototypeProperty(Node n) {
  String lhsString = n.getQualifiedName();
  if (lhsString == null) {
    return false;
  }
  int prototypeIdx = lhsString.indexOf(".prototype.");
  return prototypeIdx != -1;
}
 
Example 20
Source File: Closure_114_NameAnalyzer_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (!(n.isName() || (NodeUtil.isGet(n) && !parent.isGetProp()))) {
    // This is not a simple or qualified name.
    return;
  }

  NameInformation nameInfo = createNameInformation(t, n);
  if (nameInfo == null) {
    // The name is not a global name
    return;
  }

  if (nameInfo.onlyAffectsClassDef) {
    if (nameInfo.superclass != null) {
      recordReference(
          nameInfo.name, nameInfo.superclass, RefType.INHERITANCE);
    }

    // Make sure that we record a reference to the function that does
    // the inheritance, so that the inherits() function itself does
    // not get stripped.
    String nodeName = n.getQualifiedName();
    if (nodeName != null) {
      recordReference(
          nameInfo.name, nodeName, RefType.REGULAR);
    }

    return;
  }

  if (parent.isInstanceOf() &&
      parent.getLastChild() == n &&
      // Don't cover GETELEMs with a global root node.
      n.isQualifiedName()) {
    JsName checkedClass = getName(nameInfo.name, true);
    refNodes.add(new InstanceOfCheckNode(checkedClass, n));
    checkedClass.hasInstanceOfReference = true;
    return;
  }

  // Determine which name might be potentially referring to this one by
  // looking up the nearest enclosing dependency scope. It's unnecessary to
  // determine all enclosing dependency scopes because this callback should
  // create a chain of references between them.
  List<NameInformation> referers = getDependencyScope(n);
  if (referers.isEmpty()) {
    maybeRecordReferenceOrAlias(t, n, parent, nameInfo, null);
  } else {
    for (NameInformation referring : referers) {
      maybeRecordReferenceOrAlias(t, n, parent, nameInfo, referring);
    }
    recordAliases(referers);
  }
}