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