Search in sources :

Example 11 with JSTypeExpression

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

the class Es6RewriteModules method createExportsObject.

private Node createExportsObject(NodeTraversal t, Node script) {
    String moduleName = t.getInput().getPath().toModuleName();
    Set<String> exportedNames = new HashSet<>();
    Node objLit = IR.objectlit();
    // Going to get renamed by rename global vars, doesn't matter
    Node moduleVar = IR.var(IR.name("exports"), objLit);
    moduleVar.getFirstChild().putBooleanProp(Node.MODULE_EXPORT, true);
    JSDocInfoBuilder infoBuilder = new JSDocInfoBuilder(false);
    infoBuilder.recordConstancy();
    moduleVar.setJSDocInfo(infoBuilder.build());
    script.addChildToBack(moduleVar.useSourceInfoIfMissingFromForTree(script));
    for (Map.Entry<String, NameNodePair> entry : exportsByLocalName.entries()) {
        NameNodePair pair = entry.getValue();
        String exportedName = pair.exportedName;
        Node nodeForSourceInfo = pair.nodeForSourceInfo;
        if (!exportedNames.add(exportedName)) {
            t.report(nodeForSourceInfo, DUPLICATE_EXPORT, exportedName);
            continue;
        }
        String withSuffix = entry.getKey();
        boolean mutated = pair.mutated;
        Node getProp = IR.getprop(IR.name(moduleName), IR.string(exportedName));
        getProp.putBooleanProp(Node.MODULE_EXPORT, true);
        if (typedefs.contains(exportedName)) {
            // /** @typedef {foo} */
            // moduleName.foo;
            JSDocInfoBuilder builder = new JSDocInfoBuilder(true);
            JSTypeExpression typeExpr = new JSTypeExpression(IR.string(exportedName), script.getSourceFileName());
            builder.recordTypedef(typeExpr);
            JSDocInfo info = builder.build();
            getProp.setJSDocInfo(info);
            Node exprResult = IR.exprResult(getProp).useSourceInfoIfMissingFromForTree(nodeForSourceInfo);
            script.addChildToBack(exprResult);
        } else if (mutated || importMap.containsKey(withSuffix)) {
            addGetterExport(script, nodeForSourceInfo, objLit, exportedName, withSuffix);
        } else {
            // This step is done before type checking and the type checker doesn't understand getters.
            // However it does understand aliases. So if an export isn't mutated use an alias to make it
            // actually type checkable.
            // exports.foo = foo;
            Node assign = IR.assign(getProp, NodeUtil.newQName(compiler, withSuffix));
            if (classes.contains(exportedName)) {
                JSDocInfoBuilder builder = new JSDocInfoBuilder(true);
                builder.recordConstancy();
                JSDocInfo info = builder.build();
                assign.setJSDocInfo(info);
            }
            script.addChildToBack(IR.exprResult(assign).useSourceInfoIfMissingFromForTree(nodeForSourceInfo));
        }
    }
    exportsByLocalName.clear();
    return moduleVar;
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder) JSDocInfo(com.google.javascript.rhino.JSDocInfo) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 12 with JSTypeExpression

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

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

the class Es6TypedToEs6Converter method createIObject.

private JSTypeExpression createIObject(NodeTraversal t, Node indexSignature) {
    Node indexType = convertWithLocation(indexSignature.getFirstChild().getDeclaredTypeExpression());
    Node declaredType = convertWithLocation(indexSignature.getDeclaredTypeExpression());
    Node block = new Node(Token.BLOCK, indexType, declaredType);
    Node iObject = IR.string("IObject");
    iObject.addChildToFront(block);
    JSTypeExpression bang = new JSTypeExpression(new Node(Token.BANG, iObject).useSourceInfoIfMissingFromForTree(indexSignature), indexSignature.getSourceFileName());
    indexSignature.detach();
    t.reportCodeChange();
    return bang;
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 14 with JSTypeExpression

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

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

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