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();
}
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);
}
}
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);
}
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);
}
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());
}
Aggregations