Ejemplo n.º 1
0
  /*
   * (non-Javadoc)
   * @see com.aptana.editor.js.parsing.ast.JSTreeWalker#visit(com.aptana.editor.js.parsing.ast.JSInvokeNode)
   */
  @Override
  public void visit(JSInvokeNode node) {
    IParseNode child = node.getExpression();

    if (child instanceof JSNode) {
      // TODO hang the "require" string as a constant somewhere!
      if (child instanceof JSIdentifierNode
          && "require".equals(child.getNameNode().getName())) // $NON-NLS-1$
      {
        // it's a requires!
        JSArgumentsNode args = (JSArgumentsNode) node.getArguments();
        IParseNode[] children = args.getChildren();
        for (IParseNode arg : children) {
          if (arg instanceof JSStringNode) {
            JSStringNode string = (JSStringNode) arg;
            String text = string.getText();
            // strip quotes TODO Use util method to strip quotes!
            if (text.startsWith("'") || text.startsWith("\"")) // $NON-NLS-1$ //$NON-NLS-2$
            {
              text = text.substring(1, text.length() - 1);
            }

            // Handle resolving absolute versus relative module ids!
            URI resolved = _location.resolve(text);
            URI relative = _index.getRelativeDocumentPath(resolved);
            this.addType(relative.getPath() + ".exports"); // $NON-NLS-1$
          }
        }
      }

      List<String> types = this.getTypes(child);

      // NOTE: This is a special case for functions used as a RHS of assignments or as part of a
      // property chain.
      // If the invocation returns undefined, we change that to Object.
      // TODO: As a refinement, we want to check that the function being called is not defined in
      // the current
      // scope
      if (types.isEmpty()) {
        IParseNode parent = node.getParent();

        if (parent != null) {
          switch (parent.getNodeType()) {
            case IJSNodeTypes.ASSIGN:
              if (node.getIndex() == 1) {
                this.addType(JSTypeConstants.OBJECT_TYPE);
              }
              break;

            case IJSNodeTypes.GET_PROPERTY:
              this.addType(JSTypeConstants.OBJECT_TYPE);
              break;

            default:
              break;
          }
        }
      }

      for (String typeName : types) {
        if (JSTypeUtil.isFunctionPrefix(typeName)) {
          List<String> returnTypes = JSTypeUtil.getFunctionSignatureReturnTypeNames(typeName);
          for (String returnTypeName : returnTypes) {
            this.addType(returnTypeName);
          }
        }
      }
    }
  }