babel-generator: Add TypeScript support (#5896)
* babel-generator: Add TypeScript support * Remove type declarations; not published from babylon * Remove TODOs * Consistently use `this.word` for tokens that are words
This commit is contained in:
@@ -3,20 +3,31 @@ import * as t from "babel-types";
|
||||
export function _params(node: Object) {
|
||||
this.print(node.typeParameters, node);
|
||||
this.token("(");
|
||||
this.printList(node.params, node, {
|
||||
iterator: node => {
|
||||
if (node.optional) this.token("?");
|
||||
this.print(node.typeAnnotation, node);
|
||||
},
|
||||
});
|
||||
this._parameters(node.params, node);
|
||||
this.token(")");
|
||||
|
||||
if (node.returnType) {
|
||||
this.print(node.returnType, node);
|
||||
this.print(node.returnType, node);
|
||||
}
|
||||
|
||||
export function _parameters(parameters, parent) {
|
||||
for (let i = 0; i < parameters.length; i++) {
|
||||
this._param(parameters[i], parent);
|
||||
|
||||
if (i < parameters.length - 1) {
|
||||
this.token(",");
|
||||
this.space();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function _method(node: Object) {
|
||||
export function _param(parameter, parent) {
|
||||
this.printJoin(parameter.decorators, parameter);
|
||||
this.print(parameter, parent);
|
||||
if (parameter.optional) this.token("?");
|
||||
this.print(parameter.typeAnnotation, parameter);
|
||||
}
|
||||
|
||||
export function _methodHead(node: Object) {
|
||||
const kind = node.kind;
|
||||
const key = node.key;
|
||||
|
||||
@@ -44,9 +55,11 @@ export function _method(node: Object) {
|
||||
this.print(key, node);
|
||||
}
|
||||
|
||||
if (node.optional) {
|
||||
this.token("?");
|
||||
}
|
||||
|
||||
this._params(node);
|
||||
this.space();
|
||||
this.print(node.body, node);
|
||||
}
|
||||
|
||||
export function _predicate(node: Object) {
|
||||
@@ -59,7 +72,7 @@ export function _predicate(node: Object) {
|
||||
}
|
||||
}
|
||||
|
||||
export function FunctionExpression(node: Object) {
|
||||
export function _functionHead(node: Object) {
|
||||
if (node.async) {
|
||||
this.word("async");
|
||||
this.space();
|
||||
@@ -67,16 +80,17 @@ export function FunctionExpression(node: Object) {
|
||||
this.word("function");
|
||||
if (node.generator) this.token("*");
|
||||
|
||||
this.space();
|
||||
if (node.id) {
|
||||
this.space();
|
||||
this.print(node.id, node);
|
||||
} else {
|
||||
this.space();
|
||||
}
|
||||
|
||||
this._params(node);
|
||||
this._predicate(node);
|
||||
}
|
||||
|
||||
export function FunctionExpression(node: Object) {
|
||||
this._functionHead(node);
|
||||
this.space();
|
||||
this.print(node.body, node);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user