Search in sources :

Example 21 with JSDocInfoBuilder

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

the class Es6TypedToEs6Converter method visitClass.

private void visitClass(NodeTraversal t, Node n, Node parent) {
    maybeAddGenerics(n, n);
    JSDocInfoBuilder doc = JSDocInfoBuilder.maybeCopyFrom(n.getJSDocInfo());
    Node interfaces = (Node) n.getProp(Node.IMPLEMENTS);
    if (interfaces != null) {
        for (Node child : interfaces.children()) {
            Node type = convertWithLocation(child);
            doc.recordImplementedInterface(new JSTypeExpression(type, n.getSourceFileName()));
        }
        n.removeProp(Node.IMPLEMENTS);
    }
    Node superType = n.getSecondChild();
    Node newSuperType = maybeGetQualifiedNameNode(superType);
    if (newSuperType != superType) {
        n.replaceChild(superType, newSuperType);
    }
    Node classMembers = n.getLastChild();
    ClassDeclarationMetadata metadata = ClassDeclarationMetadata.create(n, parent);
    for (Node member : classMembers.children()) {
        if (member.isCallSignature()) {
            compiler.report(JSError.make(n, CALL_SIGNATURE_NOT_SUPPORTED));
            continue;
        }
        if (member.isIndexSignature()) {
            doc.recordImplementedInterface(createIObject(t, member));
            continue;
        }
        // Functions are handled by the regular Es6ToEs3Converter
        if (!member.isMemberVariableDef() && !member.getBooleanProp(Node.COMPUTED_PROP_VARIABLE)) {
            maybeAddVisibility(member);
            continue;
        }
        if (metadata == null) {
            compiler.report(JSError.make(n, CANNOT_CONVERT_MEMBER_VARIABLES));
            return;
        }
        metadata.insertNodeAndAdvance(createPropertyDefinition(t, member, metadata.fullClassName));
        t.reportCodeChange();
    }
    n.setJSDocInfo(doc.build());
    maybeCreateQualifiedDeclaration(t, n, parent);
}
Also used : ClassDeclarationMetadata(com.google.javascript.jscomp.Es6RewriteClass.ClassDeclarationMetadata) 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 22 with JSDocInfoBuilder

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

the class Es6TypedToEs6Converter method visitAmbientDeclaration.

private void visitAmbientDeclaration(NodeTraversal t, Node n, Node parent) {
    if (!n.isFromExterns()) {
        compiler.report(JSError.make(n, DECLARE_IN_NON_EXTERNS));
    }
    Node insertionPoint = n;
    Node topLevel = parent;
    boolean insideExport = parent.isExport();
    if (insideExport) {
        insertionPoint = parent;
        topLevel = parent.getParent();
    }
    // The node can have multiple children if transformed from an ambient namespace declaration.
    for (Node c : n.children()) {
        if (c.isConst()) {
            JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(c.getJSDocInfo());
            builder.recordConstancy();
            c.setToken(Token.VAR);
            c.setJSDocInfo(builder.build());
        }
        Node toAdd = c.detach();
        if (insideExport && !toAdd.isExprResult()) {
            // We want to keep the "export" declaration in externs
            toAdd = new Node(Token.EXPORT, toAdd).srcref(parent);
        }
        topLevel.addChildBefore(toAdd, insertionPoint);
    }
    insertionPoint.detach();
    t.reportCodeChange();
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) Node(com.google.javascript.rhino.Node) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 23 with JSDocInfoBuilder

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

the class Es6TypedToEs6Converter method maybeAddVisibility.

private void maybeAddVisibility(Node n) {
    Visibility access = (Visibility) n.getProp(Node.ACCESS_MODIFIER);
    if (access != null) {
        if (n.isComputedProp()) {
            compiler.report(JSError.make(n, COMPUTED_PROP_ACCESS_MODIFIER));
        }
        JSDocInfoBuilder memberDoc = JSDocInfoBuilder.maybeCopyFrom(n.getJSDocInfo());
        memberDoc.recordVisibility(access);
        n.setJSDocInfo(memberDoc.build());
        n.removeProp(Node.ACCESS_MODIFIER);
    }
}
Also used : Visibility(com.google.javascript.rhino.JSDocInfo.Visibility) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 24 with JSDocInfoBuilder

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

the class Es6TypedToEs6Converter method visitEnum.

private void visitEnum(NodeTraversal t, Node n, Node parent) {
    Node name = n.getFirstChild();
    Node members = n.getLastChild();
    double nextValue = 0;
    Node[] stringKeys = new Node[members.getChildCount()];
    int i = 0;
    for (Node child = members.getFirstChild(); child != null; child = child.getNext(), i++) {
        if (child.hasChildren()) {
            nextValue = child.getFirstChild().getDouble() + 1;
        } else {
            child.addChildToFront(IR.number(nextValue++));
        }
        stringKeys[i] = child;
    }
    members.detachChildren();
    String oldName = name.getString();
    String qName = maybePrependCurrNamespace(oldName);
    JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(n.getJSDocInfo());
    builder.recordEnumParameterType(new JSTypeExpression(IR.string("number"), n.getSourceFileName()));
    Node newDec = NodeUtil.newQNameDeclaration(compiler, qName, IR.objectlit(stringKeys), builder.build()).useSourceInfoFromForTree(n);
    n.setJSDocInfo(null);
    parent.replaceChild(n, newDec);
    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 25 with JSDocInfoBuilder

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

the class Es6TypedToEs6Converter method maybeAddGenerics.

private void maybeAddGenerics(Node n, Node jsDocNode) {
    Node name = n.getFirstChild();
    Node generics = (Node) name.getProp(Node.GENERIC_TYPE_LIST);
    if (generics != null) {
        JSDocInfoBuilder doc = JSDocInfoBuilder.maybeCopyFrom(jsDocNode.getJSDocInfo());
        // Discard the type bound (the "extends" part) for now
        for (Node typeName : generics.children()) {
            doc.recordTemplateTypeName(typeName.getString());
            if (typeName.hasChildren()) {
                compiler.report(JSError.make(name, CANNOT_CONVERT_BOUNDED_GENERICS));
                typeName.removeChildren();
            }
        }
        name.removeProp(Node.GENERIC_TYPE_LIST);
        jsDocNode.setJSDocInfo(doc.build());
    }
}
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