Search in sources :

Example 26 with JSTypeExpression

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

the class PolymerClassRewriter method replaceJSDocAndAddNewVars.

/**
 * Replaces JSDoc types in externs with unknown type. For a JSDoc like @type {{ propertyName :
 * string }}, collects all such "propertyName"s, and generates extern vars with an attached
 * {{propertyName: ?}} JsDoc. This is to prevent renaming of vars in source code with the same
 * names as propertyNames.
 */
private JSDocInfo replaceJSDocAndAddNewVars(MemberDefinition prop, JSTypeExpression propType, Node block) {
    JSDocInfo.Builder infoBuilder = JSDocInfo.Builder.maybeCopyFrom(prop.info);
    infoBuilder.recordType(propType);
    JSDocInfo origInfo = infoBuilder.build();
    ImmutableSet<String> propertyNames = propType.getRecordPropertyNames();
    createVarsInExternsBlock(block, propertyNames, propType, prop);
    JSTypeExpression unknown = new JSTypeExpression(new Node(Token.QMARK), propType.getSourceName());
    JSDocInfo.Builder newInfoBuilder = JSDocInfo.Builder.maybeCopyFromWithNewType(origInfo, unknown);
    return newInfoBuilder.build();
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 27 with JSTypeExpression

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

the class PolymerClassRewriter method appendBehaviorPropertiesToBlock.

/**
 * Iterates through all the behaviors of this polymer call, and appends the properties of each
 * behavior to the given block.
 */
private void appendBehaviorPropertiesToBlock(PolymerClassDefinition cls, Node block, String basePath, boolean isExternsBlock) {
    if (cls.behaviors == null || cls.behaviors.isEmpty() || cls.behaviorProps == null) {
        return;
    }
    for (Map.Entry<MemberDefinition, BehaviorDefinition> itr : cls.behaviorProps.entrySet()) {
        BehaviorDefinition behavior = itr.getValue();
        MemberDefinition prop = itr.getKey();
        Node propertyNode = getPropertyNode(prop, basePath);
        if (propertyNode == null) {
            continue;
        }
        JSTypeExpression propType = PolymerPassStaticUtils.getTypeFromProperty(prop, compiler);
        if (propType == null) {
            continue;
        }
        JSDocInfo info = null;
        if (isExternsBlock) {
            info = replaceJSDocAndAddNewVars(prop, propType, block);
        } else {
            JSDocInfo.Builder infoBuilder = getJSDocInfoBuilderForBehavior(behavior, prop);
            infoBuilder.recordType(propType);
            info = infoBuilder.build();
        }
        propertyNode.getFirstChild().setJSDocInfo(info);
        block.addChildToBack(propertyNode);
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MemberDefinition(com.google.javascript.jscomp.PolymerPass.MemberDefinition) BehaviorDefinition(com.google.javascript.jscomp.PolymerBehaviorExtractor.BehaviorDefinition)

Example 28 with JSTypeExpression

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

the class PolymerPass method appendPolymerElementExterns.

/**
 * Duplicates the PolymerElement externs with a different element base class if needed.
 * For example, if the base class is HTMLInputElement, then a class PolymerInputElement will be
 * added. If the element does not extend a native HTML element, this method is a no-op.
 */
private void appendPolymerElementExterns(final PolymerClassDefinition def) {
    if (!nativeExternsAdded.add(def.nativeBaseElement)) {
        return;
    }
    Node block = IR.block();
    Node baseExterns = polymerElementExterns.cloneTree();
    String polymerElementType = PolymerPassStaticUtils.getPolymerElementType(def);
    baseExterns.getFirstChild().setString(polymerElementType);
    String elementType = tagNameMap.get(def.nativeBaseElement);
    if (elementType == null) {
        compiler.report(JSError.make(def.descriptor, POLYMER_INVALID_EXTENDS, def.nativeBaseElement));
        return;
    }
    JSTypeExpression elementBaseType = new JSTypeExpression(new Node(Token.BANG, IR.string(elementType).srcrefTree(polymerElementExterns)), VIRTUAL_FILE);
    JSDocInfo.Builder baseDocs = JSDocInfo.Builder.copyFrom(baseExterns.getJSDocInfo());
    baseDocs.changeBaseType(elementBaseType);
    baseExterns.setJSDocInfo(baseDocs.build());
    block.addChildToBack(baseExterns);
    for (Node baseProp : polymerElementProps) {
        Node newProp = baseProp.cloneTree();
        Node newPropRootName = NodeUtil.getRootOfQualifiedName(newProp.getFirstFirstChild());
        newPropRootName.setString(polymerElementType);
        block.addChildToBack(newProp);
    }
    block.srcrefTreeIfMissing(polymerElementExterns);
    Node parent = polymerElementExterns.getParent();
    Node stmts = block.removeChildren();
    parent.addChildrenAfter(stmts, polymerElementExterns);
    compiler.reportChangeToEnclosingScope(stmts);
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 29 with JSTypeExpression

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

the class PolymerPassStaticUtils method getTypeFromProperty.

/**
 * Gets the JSTypeExpression for a given property using its "type" key.
 * @see https://github.com/Polymer/polymer/blob/0.8-preview/PRIMER.md#configuring-properties
 */
static JSTypeExpression getTypeFromProperty(MemberDefinition property, AbstractCompiler compiler) {
    if (property.info != null && property.info.hasType()) {
        return property.info.getType().copy();
    }
    Node typeValue;
    if (property.value.isObjectLit()) {
        typeValue = NodeUtil.getFirstPropMatchingKey(property.value, "type");
        if (typeValue == null || !typeValue.isName()) {
            compiler.report(JSError.make(property.name, PolymerPassErrors.POLYMER_INVALID_PROPERTY));
            return null;
        }
    } else if (property.value.isName()) {
        typeValue = property.value;
    } else {
        typeValue = null;
    }
    if (typeValue == null) {
        compiler.report(JSError.make(property.value, PolymerPassErrors.POLYMER_INVALID_PROPERTY));
        return null;
    }
    String typeString = typeValue.getString();
    Node typeNode;
    switch(typeString) {
        case "Boolean":
        case "String":
        case "Number":
            typeNode = IR.string(Ascii.toLowerCase(typeString));
            break;
        case "Array":
        case "Function":
        case "Object":
        case "Date":
            typeNode = new Node(Token.BANG, IR.string(typeString));
            break;
        default:
            compiler.report(JSError.make(property.value, PolymerPassErrors.POLYMER_INVALID_PROPERTY));
            typeNode = new Node(Token.QMARK);
    }
    typeNode.srcrefTree(typeValue);
    return new JSTypeExpression(typeNode, VIRTUAL_FILE);
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 30 with JSTypeExpression

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

the class JSDocSerializer method createPlaceholderType.

private static JSTypeExpression createPlaceholderType() {
    // the BANG (!) token makes unit testing easier, as the JSDoc parser implicitly adds "!"
    // to some JSTypeExpressions
    Node name = IR.string(PLACEHOLDER_TYPE_NAME);
    Node bang = new Node(Token.BANG, name);
    name.setStaticSourceFile(SYNTHETIC_SOURCE);
    bang.setStaticSourceFile(SYNTHETIC_SOURCE);
    return new JSTypeExpression(bang, SYNTHETIC_SOURCE.getName());
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

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