Search in sources :

Example 56 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression 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 57 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression 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 58 with JSTypeExpression

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

the class Es6TypedToEs6Converter method visitTypeAlias.

private void visitTypeAlias(NodeTraversal t, Node n, Node parent) {
    String alias = n.getString();
    if (t.getScope().isDeclared(alias, true)) {
        compiler.report(JSError.make(n, TYPE_ALIAS_ALREADY_DECLARED, alias));
    }
    JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(n.getJSDocInfo());
    builder.recordTypedef(new JSTypeExpression(convertWithLocation(n.getFirstChild()), n.getSourceFileName()));
    Node newName = maybeGetQualifiedNameNode(IR.name(n.getString())).useSourceInfoIfMissingFromForTree(n);
    Node newDec1 = NodeUtil.newQNameDeclaration(compiler, newName.getQualifiedName(), null, builder.build()).useSourceInfoFromForTree(n);
    parent.replaceChild(n, newDec1);
    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 59 with JSTypeExpression

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

the class Es6TypedToEs6Converter method visitFunction.

private void visitFunction(NodeTraversal t, Node n, Node parent) {
    // For member functions (eg. class Foo<T> { f() {} }), the JSDocInfo
    // needs to go on the synthetic MEMBER_FUNCTION_DEF node.
    boolean isMemberFunctionDef = parent.isMemberFunctionDef();
    // Currently, we remove the overloading signature and drop the type information on the original
    // signature.
    String name = isMemberFunctionDef ? parent.getString() : n.getFirstChild().getString();
    if (!name.isEmpty() && overloadStack.peek().containsKey(name)) {
        compiler.report(JSError.make(n, OVERLOAD_NOT_SUPPORTED));
        if (isMemberFunctionDef) {
            t.reportCodeChange(parent.getParent());
            parent.detach();
            NodeUtil.markFunctionsDeleted(parent, compiler);
        } else {
            t.reportCodeChange(parent);
            n.detach();
            NodeUtil.markFunctionsDeleted(n, compiler);
        }
        Node original = overloadStack.peek().get(name);
        processedOverloads.add(original);
        Node paramList = original.getSecondChild();
        paramList.removeChildren();
        Node originalParent = original.getParent();
        Node originalJsDocNode = originalParent.isMemberFunctionDef() || originalParent.isAssign() ? originalParent : original;
        JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
        builder.recordType(new JSTypeExpression(convertWithLocation(TypeDeclarationsIR.namedType("Function")), n.getSourceFileName()));
        originalJsDocNode.setJSDocInfo(builder.build());
        return;
    }
    overloadStack.peek().put(name, n);
    Node jsDocNode = isMemberFunctionDef ? parent : n;
    maybeAddGenerics(n, jsDocNode);
    // separately.
    if (!(isMemberFunctionDef && n.isOptionalEs6Typed())) {
        maybeVisitColonType(t, n, jsDocNode);
    }
    if (n.getLastChild().isEmpty()) {
        n.replaceChild(n.getLastChild(), IR.block().useSourceInfoFrom(n));
    }
    if (!isMemberFunctionDef) {
        maybeCreateQualifiedDeclaration(t, n, parent);
    }
}
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 60 with JSTypeExpression

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

the class Es6ToEs3ClassSideInheritance method copyDeclarations.

/**
 * When static get/set properties are transpiled, in addition to the Object.defineProperties, they
 * are declared with stub GETPROP declarations so that the type checker understands that these
 * properties exist on the class.
 * When subclassing, we also need to declare these properties on the subclass so that the type
 * checker knows they exist.
 */
private void copyDeclarations(JavascriptClass superClass, JavascriptClass subClass, Node inheritsCall) {
    for (Node staticGetProp : superClass.staticFieldAccess) {
        checkState(staticGetProp.isGetProp());
        String memberName = staticGetProp.getLastChild().getString();
        // We only copy declarations that have corresponding Object.defineProperties
        if (!superClass.definedProperties.contains(memberName)) {
            continue;
        }
        // If the subclass already declares the property no need to redeclare it.
        if (isOverriden(subClass, memberName)) {
            continue;
        }
        Node subclassNameNode = inheritsCall.getSecondChild();
        Node getprop = IR.getprop(subclassNameNode.cloneTree(), IR.string(memberName));
        JSDocInfoBuilder info = JSDocInfoBuilder.maybeCopyFrom(staticGetProp.getJSDocInfo());
        JSTypeExpression unknown = new JSTypeExpression(new Node(Token.QMARK), "<synthetic>");
        // In case there wasn't a type specified on the base class.
        info.recordType(unknown);
        info.addSuppression("visibility");
        getprop.setJSDocInfo(info.build());
        Node declaration = IR.exprResult(getprop);
        declaration.useSourceInfoIfMissingFromForTree(inheritsCall);
        Node parent = inheritsCall.getParent();
        parent.getParent().addChildBefore(declaration, parent);
        compiler.reportChangeToEnclosingScope(parent);
        // Copy over field access so that subclasses of this subclass can also make the declarations
        if (!subClass.definedProperties.contains(memberName)) {
            subClass.staticFieldAccess.add(getprop);
            subClass.definedProperties.add(memberName);
        }
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Aggregations

JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)101 Node (com.google.javascript.rhino.Node)67 JSDocInfo (com.google.javascript.rhino.JSDocInfo)58 Test (org.junit.Test)26 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)18 MemberDefinition (com.google.javascript.jscomp.PolymerPass.MemberDefinition)9 JSType (com.google.javascript.rhino.jstype.JSType)9 ArrayList (java.util.ArrayList)8 TypeDeclarationNode (com.google.javascript.rhino.Node.TypeDeclarationNode)7 Map (java.util.Map)6 NodeSubject.assertNode (com.google.javascript.jscomp.testing.NodeSubject.assertNode)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)3 LinkedHashMap (java.util.LinkedHashMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Name (com.google.javascript.jscomp.GlobalNamespace.Name)2 Ref (com.google.javascript.jscomp.GlobalNamespace.Ref)2