use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class PolymerClassRewriter method appendPropertiesToBlock.
/**
* Appends all properties in the ClassDefinition to the prototype of the custom element.
*/
private void appendPropertiesToBlock(final PolymerClassDefinition cls, Node block, String basePath) {
for (MemberDefinition prop : cls.props) {
Node propertyNode = IR.exprResult(NodeUtil.newQName(compiler, basePath + prop.name.getString()));
// If a property string is quoted, make sure the added prototype properties are also quoted
if (prop.name.isQuotedString()) {
continue;
}
propertyNode.useSourceInfoIfMissingFromForTree(prop.name);
JSDocInfoBuilder info = JSDocInfoBuilder.maybeCopyFrom(prop.info);
JSTypeExpression propType = PolymerPassStaticUtils.getTypeFromProperty(prop, compiler);
if (propType == null) {
return;
}
info.recordType(propType);
propertyNode.getFirstChild().setJSDocInfo(info.build());
block.addChildToBack(propertyNode);
}
}
use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class NodeUtilTest method testGetDeclaredTypeExpression4.
public void testGetDeclaredTypeExpression4() {
Node ast = parse("/** @param {number=} x */ function f(x = -1) {}");
Node x = getNameNode(ast, "x");
JSTypeExpression typeExpr = NodeUtil.getDeclaredTypeExpression(x);
assertNode(typeExpr.getRoot()).hasType(Token.EQUALS);
assertThat(typeExpr.getRoot().getFirstChild().getString()).isEqualTo("number");
}
use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class NodeUtilTest method testGetDeclaredTypeExpression1.
public void testGetDeclaredTypeExpression1() {
Node ast = parse("function f(/** string */ x) {}");
Node x = getNameNode(ast, "x");
JSTypeExpression typeExpr = NodeUtil.getDeclaredTypeExpression(x);
assertThat(typeExpr.getRoot().getString()).isEqualTo("string");
}
use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class JSTypeCreatorFromJSDoc method fillInFormalParameterTypes.
private void fillInFormalParameterTypes(JSDocInfo jsdoc, Node funNode, ImmutableList<String> typeParameters, DeclaredTypeRegistry registry, FunctionTypeBuilder builder, boolean ignoreJsdoc) /* for when the jsdoc is malformed */
{
boolean ignoreFunNode = !funNode.isFunction();
Node params = ignoreFunNode ? null : funNode.getSecondChild();
ParamIterator iterator = new ParamIterator(params, jsdoc);
while (iterator.hasNext()) {
String pname = iterator.nextString();
Node param = iterator.getNode();
ParameterKind p = ParameterKind.REQUIRED;
if (param != null && convention.isOptionalParameter(param)) {
p = ParameterKind.OPTIONAL;
} else if (param != null && convention.isVarArgsParameter(param)) {
p = ParameterKind.REST;
}
ParameterType inlineParamType = (ignoreJsdoc || ignoreFunNode || param.getJSDocInfo() == null) ? null : parseParameter(param.getJSDocInfo().getType(), p, registry, typeParameters);
ParameterType fnParamType = inlineParamType;
JSTypeExpression jsdocExp = jsdoc == null ? null : jsdoc.getParameterType(pname);
if (jsdocExp != null) {
if (inlineParamType == null) {
fnParamType = parseParameter(jsdocExp, p, registry, typeParameters);
} else {
warnings.add(JSError.make(param, TWO_JSDOCS, "formal parameter " + pname));
}
}
JSType t = null;
if (fnParamType != null) {
p = fnParamType.kind;
t = fnParamType.type;
}
switch(p) {
case REQUIRED:
builder.addReqFormal(t);
break;
case OPTIONAL:
builder.addOptFormal(t);
break;
case REST:
builder.addRestFormals(t != null ? t : this.commonTypes.UNKNOWN);
break;
}
}
}
use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class JSTypeCreatorFromJSDoc method resolveTypedef.
public void resolveTypedef(Typedef td, DeclaredTypeRegistry registry) {
checkState(td != null, "getTypedef should only be called when we know that the typedef is defined");
if (td.isResolved()) {
return;
}
JSTypeExpression texp = td.getTypeExpr();
JSType tdType;
if (texp == null) {
warnings.add(JSError.make(td.getTypeExprForErrorReporting().getRoot(), CIRCULAR_TYPEDEF_ENUM));
tdType = this.commonTypes.UNKNOWN;
} else {
tdType = getTypeFromJSTypeExpression(texp, registry, null);
// If the typedef is an object-literal type, record the names of the properties.
if (tdType.isSingletonObj()) {
Node texpRoot = texp.getRoot();
if (texpRoot.getToken() == Token.LC) {
for (Node propNode : texpRoot.getFirstChild().children()) {
Node propNameNode = propNode.hasChildren() ? propNode.getFirstChild() : propNode;
this.recordPropertyName.apply(propNameNode);
}
}
}
}
td.resolveTypedef(tdType);
}
Aggregations