Search in sources :

Example 11 with JSDocInfoBuilder

use of com.google.javascript.rhino.JSDocInfoBuilder in project closure-compiler by google.

the class Es6RewriteArrowFunction method addVarDecls.

private void addVarDecls(ThisContext thisContext) {
    Node scopeBody = thisContext.scopeBody;
    if (thisContext.needsArgumentsVar) {
        Node name = IR.name(ARGUMENTS_VAR);
        Node argumentsVar = IR.constNode(name, IR.name("arguments"));
        JSDocInfoBuilder jsdoc = new JSDocInfoBuilder(false);
        jsdoc.recordType(new JSTypeExpression(new Node(Token.BANG, IR.string("Arguments")), "<Es6RewriteArrowFunction>"));
        argumentsVar.setJSDocInfo(jsdoc.build());
        argumentsVar.useSourceInfoIfMissingFromForTree(scopeBody);
        scopeBody.addChildToFront(argumentsVar);
        compiler.reportChangeToEnclosingScope(argumentsVar);
    }
    if (thisContext.needsThisVar) {
        Node name = IR.name(THIS_VAR);
        Node thisVar = IR.constNode(name, IR.thisNode());
        thisVar.useSourceInfoIfMissingFromForTree(scopeBody);
        makeTreeNonIndexable(thisVar);
        if (thisContext.lastSuperStatement == null) {
            scopeBody.addChildToFront(thisVar);
        } else {
            // Not safe to reference `this` until after super() has been called.
            // TODO(bradfordcsmith): Some complex cases still aren't covered, like
            // if (...) { super(); arrow function } else { super(); }
            scopeBody.addChildAfter(thisVar, thisContext.lastSuperStatement);
        }
        compiler.reportChangeToEnclosingScope(thisVar);
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 12 with JSDocInfoBuilder

use of com.google.javascript.rhino.JSDocInfoBuilder in project closure-compiler by google.

the class Es6TemplateLiterals method visitTaggedTemplateLiteral.

/**
 * Converts tag`a\tb${bar}` to:
 *   // A global (module) scoped variable
 *   var $jscomp$templatelit$0 = ["a\tb"];   // cooked string array
 *   $jscomp$templatelit$0.raw = ["a\\tb"];  // raw string array
 *   ...
 *   // A call to the tagging function
 *   tag($jscomp$templatelit$0, bar);
 *
 *   See template_literal_test.js for more examples.
 *
 * @param n A TAGGED_TEMPLATELIT node
 */
static void visitTaggedTemplateLiteral(NodeTraversal t, Node n, boolean addTypes) {
    TypeIRegistry registry = t.getCompiler().getTypeIRegistry();
    TypeI stringType = createType(addTypes, registry, JSTypeNative.STRING_TYPE);
    TypeI arrayType = createGenericType(addTypes, registry, JSTypeNative.ARRAY_TYPE, stringType);
    TypeI templateArrayType = createType(addTypes, registry, JSTypeNative.I_TEMPLATE_ARRAY_TYPE);
    Node templateLit = n.getLastChild();
    // Prepare the raw and cooked string arrays.
    Node raw = createRawStringArray(templateLit, arrayType, stringType);
    Node cooked = createCookedStringArray(templateLit, templateArrayType, stringType);
    // Specify the type of the first argument to be ITemplateArray.
    JSTypeExpression nonNullSiteObject = new JSTypeExpression(JsDocInfoParser.parseTypeString("!ITemplateArray"), "<Es6TemplateLiterals.java>");
    JSDocInfoBuilder info = new JSDocInfoBuilder(false);
    info.recordType(nonNullSiteObject);
    Node siteObject = withType(IR.cast(cooked, info.build()), templateArrayType);
    // Create a variable representing the template literal.
    Node callsiteId = withType(IR.name(TEMPLATELIT_VAR + t.getCompiler().getUniqueNameIdSupplier().get()), templateArrayType);
    Node var = IR.var(callsiteId, siteObject).useSourceInfoIfMissingFromForTree(n);
    Node script = NodeUtil.getEnclosingScript(n);
    script.addChildToFront(var);
    t.reportCodeChange(var);
    // Define the "raw" property on the introduced variable.
    Node defineRaw = IR.exprResult(withType(IR.assign(withType(IR.getprop(callsiteId.cloneNode(), withType(IR.string("raw"), stringType)), arrayType), raw), arrayType)).useSourceInfoIfMissingFromForTree(n);
    script.addChildAfter(defineRaw, var);
    // Generate the call expression.
    Node call = withType(IR.call(n.removeFirstChild(), callsiteId.cloneNode()), n.getTypeI());
    for (Node child = templateLit.getFirstChild(); child != null; child = child.getNext()) {
        if (!child.isString()) {
            call.addChildToBack(child.removeFirstChild());
        }
    }
    call.useSourceInfoIfMissingFromForTree(templateLit);
    call.putBooleanProp(Node.FREE_CALL, !call.getFirstChild().isGetProp());
    n.replaceWith(call);
    t.reportCodeChange();
}
Also used : TypeI(com.google.javascript.rhino.TypeI) Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) TypeIRegistry(com.google.javascript.rhino.TypeIRegistry) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 13 with JSDocInfoBuilder

use of com.google.javascript.rhino.JSDocInfoBuilder in project closure-compiler by google.

the class Es6TypedToEs6Converter method maybeVisitColonType.

private void maybeVisitColonType(NodeTraversal t, Node n, Node jsDocNode) {
    Node type = n.getDeclaredTypeExpression();
    boolean hasColonType = type != null;
    if (n.isRest() && hasColonType) {
        type = new Node(Token.ELLIPSIS, convertWithLocation(type.removeFirstChild()));
    } else if (n.isMemberVariableDef()) {
        if (type != null) {
            type = maybeProcessOptionalProperty(n, type);
        }
    } else {
        type = maybeProcessOptionalParameter(n, type);
    }
    if (type == null) {
        return;
    }
    JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(jsDocNode.getJSDocInfo());
    JSTypeExpression typeExpression = new JSTypeExpression(type, n.getSourceFileName());
    switch(n.getToken()) {
        case FUNCTION:
            builder.recordReturnType(typeExpression);
            break;
        case MEMBER_VARIABLE_DEF:
            builder.recordType(typeExpression);
            break;
        default:
            builder.recordType(typeExpression);
            builder.recordInlineType();
    }
    jsDocNode.setJSDocInfo(builder.build());
    if (hasColonType) {
        n.setDeclaredTypeExpression(null);
        t.reportCodeChange();
    }
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 14 with JSDocInfoBuilder

use of com.google.javascript.rhino.JSDocInfoBuilder in project closure-compiler by google.

the class Es6TypedToEs6Converter method visitInterface.

private void visitInterface(NodeTraversal t, Node n, Node parent) {
    maybeAddGenerics(n, n);
    Node name = n.getFirstChild();
    Node superTypes = name.getNext();
    JSDocInfoBuilder doc = JSDocInfoBuilder.maybeCopyFrom(n.getJSDocInfo());
    doc.recordInterface();
    if (!superTypes.isEmpty()) {
        for (Node child : superTypes.children()) {
            Node type = convertWithLocation(child);
            doc.recordExtendedInterface(new JSTypeExpression(type, n.getSourceFileName()));
        }
    }
    Node insertionPoint = n;
    Node members = n.getLastChild();
    for (Node member : members.children()) {
        if (member.isCallSignature()) {
            compiler.report(JSError.make(n, CALL_SIGNATURE_NOT_SUPPORTED));
        }
        if (member.isIndexSignature()) {
            doc.recordExtendedInterface(createIObject(t, member));
        }
        // Synthesize a block for method signatures, or convert it to a member variable if optional.
        if (member.isMemberFunctionDef()) {
            Node function = member.getFirstChild();
            if (function.isOptionalEs6Typed()) {
                member = convertMemberFunctionToMemberVariable(member);
            } else {
                function.getLastChild().setToken(Token.BLOCK);
            }
        }
        if (member.isMemberVariableDef()) {
            Node newNode = createPropertyDefinition(t, member, name.getString());
            insertionPoint.getParent().addChildAfter(newNode, insertionPoint);
            insertionPoint = newNode;
        }
    }
    n.setJSDocInfo(doc.build());
    // Convert interface to class
    n.setToken(Token.CLASS);
    Node empty = new Node(Token.EMPTY).useSourceInfoIfMissingFrom(n);
    n.replaceChild(superTypes, empty);
    members.setToken(Token.CLASS_MEMBERS);
    maybeCreateQualifiedDeclaration(t, n, parent);
    t.reportCodeChange();
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 15 with JSDocInfoBuilder

use of com.google.javascript.rhino.JSDocInfoBuilder in project closure-compiler by google.

the class Es6TypedToEs6Converter method visitVarInsideNamespace.

private void visitVarInsideNamespace(NodeTraversal t, Node n, Node parent) {
    if (currNamespace != null) {
        Node insertPoint = n;
        for (Node child : n.children()) {
            Node name = child;
            String oldName = name.getString();
            String qName = maybePrependCurrNamespace(oldName);
            JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(child.getJSDocInfo());
            if (n.isConst()) {
                builder.recordConstancy();
            }
            Node newDec = NodeUtil.newQNameDeclaration(compiler, qName, child.removeFirstChild(), builder.build()).useSourceInfoFromForTree(n);
            parent.addChildAfter(newDec, insertPoint);
            insertPoint = newDec;
        }
        n.detach();
        t.reportCodeChange();
    }
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) Node(com.google.javascript.rhino.Node) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Aggregations

JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)40 Node (com.google.javascript.rhino.Node)27 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)18 TypeDeclarationNode (com.google.javascript.rhino.Node.TypeDeclarationNode)10 JSDocInfo (com.google.javascript.rhino.JSDocInfo)8 MemberDefinition (com.google.javascript.jscomp.PolymerPass.MemberDefinition)4 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClassDeclarationMetadata (com.google.javascript.jscomp.Es6RewriteClass.ClassDeclarationMetadata)1 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 TypeI (com.google.javascript.rhino.TypeI)1 TypeIRegistry (com.google.javascript.rhino.TypeIRegistry)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1