Java Code Examples for org.mozilla.javascript.ast.FunctionNode#setJsDocNode()

The following examples show how to use org.mozilla.javascript.ast.FunctionNode#setJsDocNode() . 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: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private FunctionNode function(int type)
    throws IOException
{
    int syntheticType = type;
    int baseLineno = ts.lineno;  // line number where source starts
    int functionSourceStart = ts.tokenBeg;  // start of "function" kwd
    Name name = null;
    AstNode memberExprNode = null;

    if (matchToken(Token.NAME)) {
        name = createNameNode(true, Token.NAME);
        if (inUseStrictDirective) {
            String id = name.getIdentifier();
            if ("eval".equals(id)|| "arguments".equals(id)) {
                reportError("msg.bad.id.strict", id);
            }
        }
        if (!matchToken(Token.LP)) {
            if (compilerEnv.isAllowMemberExprAsFunctionName()) {
                AstNode memberExprHead = name;
                name = null;
                memberExprNode = memberExprTail(false, memberExprHead);
            }
            mustMatchToken(Token.LP, "msg.no.paren.parms");
        }
    } else if (matchToken(Token.LP)) {
        // Anonymous function:  leave name as null
    } else {
        if (compilerEnv.isAllowMemberExprAsFunctionName()) {
            // Note that memberExpr can not start with '(' like
            // in function (1+2).toString(), because 'function (' already
            // processed as anonymous function
            memberExprNode = memberExpr(false);
        }
        mustMatchToken(Token.LP, "msg.no.paren.parms");
    }
    int lpPos = currentToken == Token.LP ? ts.tokenBeg : -1;

    if (memberExprNode != null) {
        syntheticType = FunctionNode.FUNCTION_EXPRESSION;
    }

    if (syntheticType != FunctionNode.FUNCTION_EXPRESSION
        && name != null && name.length() > 0) {
        // Function statements define a symbol in the enclosing scope
        defineSymbol(Token.FUNCTION, name.getIdentifier());
    }

    FunctionNode fnNode = new FunctionNode(functionSourceStart, name);
    fnNode.setFunctionType(type);
    if (lpPos != -1)
        fnNode.setLp(lpPos - functionSourceStart);

    fnNode.setJsDocNode(getAndResetJsDoc());

    PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
    try {
        parseFunctionParams(fnNode);
        fnNode.setBody(parseFunctionBody(type, fnNode));
        fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd);
        fnNode.setLength(ts.tokenEnd - functionSourceStart);

        if (compilerEnv.isStrictMode()
            && !fnNode.getBody().hasConsistentReturnUsage()) {
            String msg = (name != null && name.length() > 0)
                       ? "msg.no.return.value"
                       : "msg.anon.no.return.value";
            addStrictWarning(msg, name == null ? "" : name.getIdentifier());
        }
    } finally {
        savedVars.restore();
    }

    if (memberExprNode != null) {
        // TODO(stevey): fix missing functionality
        Kit.codeBug();
        fnNode.setMemberExprNode(memberExprNode);  // rewrite later
        /* old code:
        if (memberExprNode != null) {
            pn = nf.createAssignment(Token.ASSIGN, memberExprNode, pn);
            if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
                // XXX check JScript behavior: should it be createExprStatement?
                pn = nf.createExprStatementNoReturn(pn, baseLineno);
            }
        }
        */
    }

    fnNode.setSourceName(sourceURI);
    fnNode.setBaseLineno(baseLineno);
    fnNode.setEndLineno(ts.lineno);

    // Set the parent scope.  Needed for finding undeclared vars.
    // Have to wait until after parsing the function to set its parent
    // scope, since defineSymbol needs the defining-scope check to stop
    // at the function boundary when checking for redeclarations.
    if (compilerEnv.isIdeMode()) {
        fnNode.setParentScope(currentScope);
    }
    return fnNode;
}
 
Example 2
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private AstNode arrowFunction(AstNode params) throws IOException {
    int baseLineno = ts.lineno;  // line number where source starts
    int functionSourceStart = params != null ? params.getPosition() : -1;  // start of "function" kwd

    FunctionNode fnNode = new FunctionNode(functionSourceStart);
    fnNode.setFunctionType(FunctionNode.ARROW_FUNCTION);
    fnNode.setJsDocNode(getAndResetJsDoc());

    // Would prefer not to call createDestructuringAssignment until codegen,
    // but the symbol definitions have to happen now, before body is parsed.
    Map<String, Node> destructuring = new HashMap<String, Node>();
    Set<String> paramNames = new HashSet<String>();

    PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
    try {
        if (params instanceof ParenthesizedExpression) {
            fnNode.setParens(0, params.getLength());
            AstNode p = ((ParenthesizedExpression)params).getExpression();
            if (!(p instanceof EmptyExpression)) {
                arrowFunctionParams(fnNode, p, destructuring, paramNames);
            }
        } else {
            arrowFunctionParams(fnNode, params, destructuring, paramNames);
        }

        if (!destructuring.isEmpty()) {
            Node destructuringNode = new Node(Token.COMMA);
            // Add assignment helper for each destructuring parameter
            for (Map.Entry<String, Node> param: destructuring.entrySet()) {
                Node assign = createDestructuringAssignment(Token.VAR,
                                                            param.getValue(), createName(param.getKey()));
                destructuringNode.addChildToBack(assign);

            }
            fnNode.putProp(Node.DESTRUCTURING_PARAMS, destructuringNode);
        }
            
        fnNode.setBody(parseFunctionBody(FunctionNode.ARROW_FUNCTION, fnNode));
        fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd);
        fnNode.setLength(ts.tokenEnd - functionSourceStart);
    } finally {
        savedVars.restore();
    }

    if (fnNode.isGenerator()) {
        reportError("msg.arrowfunction.generator");
        return makeErrorNode();
    }

    fnNode.setSourceName(sourceURI);
    fnNode.setBaseLineno(baseLineno);
    fnNode.setEndLineno(ts.lineno);

    return fnNode;
}