Search in sources :

Example 6 with ObjectProperty

use of org.mozilla.javascript.ast.ObjectProperty in project HL4A by HL4A.

the class Parser method plainProperty.

private ObjectProperty plainProperty(AstNode property, int ptt) throws IOException {
    // Support, e.g., |var {x, y} = o| as destructuring shorthand
    // for |var {x: x, y: y} = o|, as implemented in spidermonkey JS 1.8.
    int tt = peekToken();
    if ((tt == Token.COMMA || tt == Token.RC) && ptt == Token.NAME && compilerEnv.getLanguageVersion() >= Context.VERSION_1_8) {
        if (!inDestructuringAssignment) {
            reportError("msg.bad.object.init");
        }
        AstNode nn = new Name(property.getPosition(), property.getString());
        ObjectProperty pn = new ObjectProperty();
        pn.putProp(Node.DESTRUCTURING_SHORTHAND, Boolean.TRUE);
        pn.setLeftAndRight(property, nn);
        return pn;
    }
    mustMatchToken(Token.COLON, "msg.no.colon.prop");
    ObjectProperty pn = new ObjectProperty();
    pn.setOperatorPosition(ts.tokenBeg);
    pn.setLeftAndRight(property, assignExpr());
    return pn;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 7 with ObjectProperty

use of org.mozilla.javascript.ast.ObjectProperty in project HL4A by HL4A.

the class Parser method destructuringObject.

boolean destructuringObject(ObjectLiteral node, int variableType, String tempName, Node parent, List<String> destructuringNames) {
    boolean empty = true;
    int setOp = variableType == Token.CONST ? Token.SETCONST : Token.SETNAME;
    for (ObjectProperty prop : node.getElements()) {
        int lineno = 0;
        // tokenStream isn't set.  Deal with it.
        if (ts != null) {
            lineno = ts.lineno;
        }
        AstNode id = prop.getLeft();
        Node rightElem = null;
        if (id instanceof Name) {
            Node s = Node.newString(((Name) id).getIdentifier());
            rightElem = new Node(Token.GETPROP, createName(tempName), s);
        } else if (id instanceof StringLiteral) {
            Node s = Node.newString(((StringLiteral) id).getValue());
            rightElem = new Node(Token.GETPROP, createName(tempName), s);
        } else if (id instanceof NumberLiteral) {
            Node s = createNumber((int) ((NumberLiteral) id).getNumber());
            rightElem = new Node(Token.GETELEM, createName(tempName), s);
        } else {
            throw codeBug();
        }
        rightElem.setLineno(lineno);
        AstNode value = prop.getRight();
        if (value.getType() == Token.NAME) {
            String name = ((Name) value).getIdentifier();
            parent.addChildToBack(new Node(setOp, createName(Token.BINDNAME, name, null), rightElem));
            if (variableType != -1) {
                defineSymbol(variableType, name, true);
                destructuringNames.add(name);
            }
        } else {
            parent.addChildToBack(destructuringAssignmentHelper(variableType, value, rightElem, currentScriptOrFn.getNextTempName()));
        }
        empty = false;
    }
    return empty;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) StringLiteral(org.mozilla.javascript.ast.StringLiteral) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) ErrorNode(org.mozilla.javascript.ast.ErrorNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) NumberLiteral(org.mozilla.javascript.ast.NumberLiteral) Name(org.mozilla.javascript.ast.Name)

Aggregations

ObjectProperty (org.mozilla.javascript.ast.ObjectProperty)7 AstNode (org.mozilla.javascript.ast.AstNode)4 FunctionNode (org.mozilla.javascript.ast.FunctionNode)3 Name (org.mozilla.javascript.ast.Name)3 LetNode (org.mozilla.javascript.ast.LetNode)2 ScriptNode (org.mozilla.javascript.ast.ScriptNode)2 XmlString (org.mozilla.javascript.ast.XmlString)2 ArrayList (java.util.ArrayList)1 Comment (org.mozilla.javascript.ast.Comment)1 ErrorNode (org.mozilla.javascript.ast.ErrorNode)1 NumberLiteral (org.mozilla.javascript.ast.NumberLiteral)1 ObjectLiteral (org.mozilla.javascript.ast.ObjectLiteral)1 StringLiteral (org.mozilla.javascript.ast.StringLiteral)1