add support for outputting flow types - fixes #665

This commit is contained in:
Sebastian McKenzie
2015-03-01 00:32:36 +11:00
parent fb04b2561f
commit e945f0d10f
61 changed files with 965 additions and 98 deletions

View File

@@ -1,5 +1,4 @@
exports.ClassExpression =
exports.ClassDeclaration = function (node, print) {
export function ClassDeclaration(node, print) {
this.push("class");
if (node.id) {
@@ -7,14 +6,24 @@ exports.ClassDeclaration = function (node, print) {
print(node.id);
}
print(node.typeParameters);
if (node.superClass) {
this.push(" extends ");
print(node.superClass);
print(node.superTypeParameters);
}
if (node.implements) {
this.push(" implements ");
print.join(node.implements, { separator: ", " });
}
this.space();
print(node.body);
};
}
export { ClassDeclaration as ClassExpression };
export function ClassBody(node, print) {
if (node.body.length === 0) {

View File

@@ -1,34 +1,226 @@
exports.AnyTypeAnnotation =
exports.ArrayTypeAnnotation =
exports.BooleanTypeAnnotation =
exports.ClassProperty =
exports.DeclareClass =
exports.DeclareFunction =
exports.DeclareModule =
exports.DeclareVariable =
exports.FunctionTypeAnnotation =
exports.FunctionTypeParam =
exports.GenericTypeAnnotation =
exports.InterfaceExtends =
exports.InterfaceDeclaration =
exports.IntersectionTypeAnnotation =
exports.NullableTypeAnnotation =
exports.NumberTypeAnnotation =
exports.StringLiteralTypeAnnotation =
exports.StringTypeAnnotation =
exports.TupleTypeAnnotation =
exports.TypeofTypeAnnotation =
exports.TypeAlias =
exports.TypeAnnotation =
exports.TypeParameterDeclaration =
exports.TypeParameterInstantiation =
exports.ObjectTypeAnnotation =
exports.ObjectTypeCallProperty =
exports.ObjectTypeIndexer =
exports.ObjectTypeProperty =
exports.QualifiedTypeIdentifier =
exports.UnionTypeAnnotation =
exports.TypeCastExpression =
exports.VoidTypeAnnotation = function () {
// todo: implement these once we have a `--keep-types` option
};
import t from "../../types";
export function AnyTypeAnnotation() {
this.push("any");
}
export function ArrayTypeAnnotation(node, print) {
print(node.elementType);
this.push("[");
this.push("]");
}
export function BooleanTypeAnnotation(node) {
this.push("bool");
}
export function ClassProperty(node, print) {
if (node.static) this.push("static ");
print(node.key);
print(node.typeAnnotation);
this.semicolon();
}
export function DeclareClass(node, print) {
this.push("declare class ");
this._interfaceish(node, print);
}
export function DeclareFunction(node, print) {
this.push("declare function ");
print(node.id);
print(node.id.typeAnnotation.typeAnnotation);
this.semicolon();
}
export function DeclareModule(node, print) {
this.push("declare module ");
print(node.id);
this.space();
print(node.body);
}
export function DeclareVariable(node, print) {
this.push("declare var ");
print(node.id);
print(node.id.typeAnnotation);
this.semicolon();
}
export function FunctionTypeAnnotation(node, print, parent) {
print(node.typeParameters);
this.push("(");
print.list(node.params);
if (node.rest) {
if (node.params.length) {
this.push(",");
this.space();
}
this.push("...");
print(node.rest);
}
this.push(")");
// this node type is overloaded, not sure why but it makes it EXTREMELY annoying
if (parent.type === "ObjectTypeProperty" || parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction") {
this.push(":");
} else {
this.space();
this.push("=>");
}
this.space();
print(node.returnType);
}
export function FunctionTypeParam(node, print) {
print(node.name);
if (node.optional) this.push("?");
this.push(":");
this.space();
print(node.typeAnnotation);
}
export function InterfaceExtends(node, print) {
print(node.id);
print(node.typeParameters);
}
export { InterfaceExtends as ClassImplements, InterfaceExtends as GenericTypeAnnotation };
export function _interfaceish(node, print) {
print(node.id);
print(node.typeParameters);
if (node.extends.length) {
this.push(" extends ");
print.join(node.extends, { separator: ", " });
}
this.space();
print(node.body);
}
export function InterfaceDeclaration(node, print) {
this.push("interface ");
this._interfaceish(node, print);
}
export function IntersectionTypeAnnotation(node, print) {
print.join(node.types, { separator: " & " });
}
export function NullableTypeAnnotation(node, print) {
this.push("?");
print(node.typeAnnotation);
}
export function NumberTypeAnnotation() {
this.push("number");
}
export function StringLiteralTypeAnnotation(node) {
this._stringLiteral(node.value);
}
export function StringTypeAnnotation() {
this.push("string");
}
export function TupleTypeAnnotation(node, print) {
this.push("[");
print.join(node.types, { separator: ", " });
this.push("]");
}
export function TypeofTypeAnnotation(node, print) {
this.push("typeof ");
print(node.argument);
}
export function TypeAlias(node, print) {
this.push("type ");
print(node.id);
print(node.typeParameters);
this.space();
this.push("=");
this.space();
print(node.right);
this.semicolon();
}
export function TypeAnnotation(node, print) {
this.push(":");
this.space();
if (node.optional) this.push("?");
print(node.typeAnnotation);
}
export function TypeParameterInstantiation(node, print) {
this.push("<");
print.join(node.params, { separator: ", " });
this.push(">");
}
export { TypeParameterInstantiation as TypeParameterDeclaration };
export function ObjectTypeAnnotation(node, print) {
this.push("{");
var props = node.properties.concat(node.callProperties, node.indexers);
if (props.length) {
this.space();
print.list(props, { indent: true, separator: "; " });
this.space();
}
this.push("}");
}
export function ObjectTypeCallProperty(node, print) {
if (node.static) this.push("static ");
print(node.value);
}
export function ObjectTypeIndexer(node, print) {
if (node.static) this.push("static ");
this.push("[");
print(node.id);
this.push(":");
this.space();
print(node.key);
this.push("]");
this.push(":");
this.space();
print(node.value);
}
export function ObjectTypeProperty(node, print) {
if (node.static) this.push("static ");
print(node.key);
if (node.optional) this.push("?");
if (!t.isFunctionTypeAnnotation(node.value)) {
this.push(":");
this.space();
}
print(node.value);
}
export function QualifiedTypeIdentifier(node, print) {
print(node.qualification);
this.push(".");
print(node.id);
}
export function UnionTypeAnnotation(node, print) {
print.join(node.types, { separator: " | " });
}
export function TypeCastExpression(node, print) {
this.push("(");
print(node.expression);
print(node.typeAnnotation);
this.push(")");
}
export function VoidTypeAnnotation(node) {
this.push("void");
}

View File

@@ -1,9 +1,19 @@
import t from "../../types";
export function _params(node, print) {
print(node.typeParameters);
this.push("(");
print.list(node.params);
print.list(node.params, {
iterator: (node) =>{
if (node.optional) this.push("?");
print(node.typeAnnotation);
}
});
this.push(")");
if (node.returnType) {
print(node.returnType);
}
}
export function _method(node, print) {

View File

@@ -211,13 +211,12 @@ export function PrivateDeclaration(node, print) {
}
export function VariableDeclarator(node, print) {
print(node.id);
print(node.id.typeAnnotation);
if (node.init) {
print(node.id);
this.space();
this.push("=");
this.space();
print(node.init);
} else {
print(node.id);
}
}

View File

@@ -83,14 +83,7 @@ export function Literal(node) {
var type = typeof val;
if (type === "string") {
val = JSON.stringify(val);
// escape illegal js but valid json unicode characters
val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
});
this.push(val);
this._stringLiteral(val);
} else if (type === "number") {
this.push(val + "");
} else if (type === "boolean") {
@@ -101,3 +94,14 @@ export function Literal(node) {
this.push("null");
}
}
export function _stringLiteral(val) {
val = JSON.stringify(val);
// escape illegal js but valid json unicode characters
val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
});
this.push(val);
}

View File

@@ -117,7 +117,7 @@ class CodeGenerator {
}
print(node, parent, opts = {}) {
if (!node) return "";
if (!node) return;
if (parent && parent._compact) {
node._compact = true;

View File

@@ -21,6 +21,12 @@ each([
});
});
export function NullableTypeAnnotation(node, parent) {
return t.isArrayTypeAnnotation(parent);
}
export { NullableTypeAnnotation as FunctionTypeAnnotation };
export function UpdateExpression(node, parent) {
if (t.isMemberExpression(parent) && parent.object === node) {
// (foo++).test()
@@ -139,8 +145,7 @@ export function FunctionExpression(node, parent) {
}
}
exports.AssignmentExpression =
exports.ConditionalExpression = function (node, parent) {
export function ConditionalExpression(node, parent) {
if (t.isUnaryLike(parent)) {
return true;
}
@@ -164,4 +169,6 @@ exports.ConditionalExpression = function (node, parent) {
}
return false;
};
}
export { ConditionalExpression as AssignmentExpression };

View File

@@ -1,5 +1,15 @@
export function SequenceExpression(node) {
if (node.expressions.length === 1) {
return node.expressions[0];
export var SequenceExpression = {
exit(node) {
if (node.expressions.length === 1) {
return node.expressions[0];
} else if (!node.expressions.length) {
this.remove();
}
}
}
};
export var ExpressionStatement = {
exit(node) {
if (!node.expression) this.remove();
}
};

View File

@@ -1,5 +1,18 @@
import t from "../../../types";
export function Flow(node) {
this.remove();
}
export function ClassProperty(node) {
node.typeAnnotation = null;
if (!node.value) this.remove();
}
export function Class(node) {
node.implements = null;
}
export function TypeCastExpression(node) {
return node.expression;
}

View File

@@ -43,7 +43,7 @@ export default class TraversalConext {
if (this.shouldFlatten) {
node[key] = flatten(node[key]);
if (key === "body") {
if (key === "body" || key === "expressions") {
// we can safely compact this
node[key] = compact(node[key]);
}

View File

@@ -84,7 +84,7 @@ traverse.explode = function (obj) {
var aliases = t.FLIPPED_ALIAS_KEYS[type];
if (aliases) {
for (var i = 0; i < aliases.length; i++) {
obj[aliases[i]] = fns;
obj[aliases[i]] ||= fns;
}
}
}

View File

@@ -299,7 +299,7 @@ export default class Scope {
var info = this.getBindingInfo(name);
if (!info) return;
info.identifier.typeAnnotation = info.typeAnnotation = type;
info.typeAnnotation = type;
}
getTypeAnnotation(name, id, node) {

View File

@@ -71,6 +71,39 @@
"JSXMemberExpression": ["Expression"],
"YieldExpression": ["Expression"],
"AnyTypeAnnotation": ["Flow"],
"ArrayTypeAnnotation": ["Flow"],
"BooleanTypeAnnotation": ["Flow"],
"ClassImplements": ["Flow"],
"DeclareClass": ["Flow"],
"DeclareFunction": ["Flow"],
"DeclareModule": ["Flow"],
"DeclareVariable": ["Flow"],
"FunctionTypeAnnotation": ["Flow"],
"FunctionTypeParam": ["Flow"],
"GenericTypeAnnotation": ["Flow"],
"InterfaceExtends": ["Flow"],
"InterfaceDeclaration": ["Flow"],
"IntersectionTypeAnnotation": ["Flow"],
"NullableTypeAnnotation": ["Flow"],
"NumberTypeAnnotation": ["Flow"],
"StringLiteralTypeAnnotation": ["Flow"],
"StringTypeAnnotation": ["Flow"],
"TupleTypeAnnotation": ["Flow"],
"TypeofTypeAnnotation": ["Flow"],
"TypeAlias": ["Flow"],
"TypeAnnotation": ["Flow"],
"TypeCastExpression": ["Flow"],
"TypeParameterDeclaration": ["Flow"],
"TypeParameterInstantiation": ["Flow"],
"ObjectTypeAnnotation": ["Flow"],
"ObjectTypeCallProperty": ["Flow"],
"ObjectTypeIndexer": ["Flow"],
"ObjectTypeProperty": ["Flow"],
"QualifiedTypeIdentifier": ["Flow"],
"UnionTypeAnnotation": ["Flow"],
"VoidTypeAnnotation": ["Flow"],
"JSXAttribute": ["JSX"],
"JSXClosingElement": ["JSX"],
"JSXElement": ["JSX", "Expression"],

View File

@@ -709,6 +709,10 @@ t.inherits = function (child, parent) {
child.start = parent.start;
child.loc = parent.loc;
child.end = parent.end;
child.typeAnnotation = parent.typeAnnotation;
child.returnType = parent.returnType;
t.inheritsComments(child, parent);
return child;
};

View File

@@ -1,7 +1,7 @@
{
"ArrayExpression": ["elements"],
"ArrayPattern": ["elements"],
"ArrowFunctionExpression": ["params", "defaults", "rest", "body"],
"ArrayPattern": ["elements", "typeAnnotation"],
"ArrowFunctionExpression": ["params", "defaults", "rest", "body", "returnType"],
"AssignmentExpression": ["left", "right"],
"AssignmentPattern": ["left", "right"],
"AwaitExpression": ["argument"],
@@ -13,8 +13,8 @@
"CallExpression": ["callee", "arguments"],
"CatchClause": ["param", "body"],
"ClassBody": ["body"],
"ClassDeclaration": ["id", "body", "superClass"],
"ClassExpression": ["id", "body", "superClass"],
"ClassDeclaration": ["id", "body", "superClass", "typeParameters", "superTypeParameters", "implements"],
"ClassExpression": ["id", "body", "superClass", "typeParameters", "superTypeParameters", "implements"],
"ComprehensionBlock": ["left", "right", "body"],
"ComprehensionExpression": ["filter", "blocks", "body"],
"ConditionalExpression": ["test", "consequent", "alternate"],
@@ -30,9 +30,9 @@
"ForInStatement": ["left", "right", "body"],
"ForOfStatement": ["left", "right", "body"],
"ForStatement": ["init", "test", "update", "body"],
"FunctionDeclaration": ["id", "params", "defaults", "rest", "body"],
"FunctionExpression": ["id", "params", "defaults", "rest", "body"],
"Identifier": [],
"FunctionDeclaration": ["id", "params", "defaults", "rest", "body", "returnType", "typeParameters"],
"FunctionExpression": ["id", "params", "defaults", "rest", "body", "returnType", "typeParameters"],
"Identifier": ["typeAnnotation"],
"IfStatement": ["test", "consequent", "alternate"],
"ImportBatchSpecifier": ["id"],
"ImportDeclaration": ["specifiers", "source"],
@@ -44,11 +44,11 @@
"MethodDefinition": ["key", "value"],
"NewExpression": ["callee", "arguments"],
"ObjectExpression": ["properties"],
"ObjectPattern": ["properties"],
"ObjectPattern": ["properties", "typeAnnotation"],
"PrivateDeclaration": ["declarations"],
"Program": ["body"],
"Property": ["key", "value"],
"RestElement": ["argument"],
"RestElement": ["argument", "typeAnnotation"],
"ReturnStatement": ["argument"],
"SequenceExpression": ["expressions"],
"SpreadElement": ["argument"],
@@ -71,36 +71,37 @@
"YieldExpression": ["argument"],
"AnyTypeAnnotation": [],
"ArrayTypeAnnotation": [],
"ArrayTypeAnnotation": ["elementType"],
"BooleanTypeAnnotation": [],
"ClassProperty": ["key", "value"],
"DeclareClass": [],
"DeclareFunction": [],
"DeclareModule": [],
"DeclareVariable": [],
"FunctionTypeAnnotation": [],
"FunctionTypeParam": [],
"GenericTypeAnnotation": [],
"InterfaceExtends": [],
"InterfaceDeclaration": [],
"IntersectionTypeAnnotation": [],
"NullableTypeAnnotation": [],
"ClassImplements": ["id", "typeParameters"],
"ClassProperty": ["key", "value", "typeAnnotation"],
"DeclareClass": ["id", "typeParameters", "extends", "body"],
"DeclareFunction": ["id"],
"DeclareModule": ["id", "body"],
"DeclareVariable": ["id"],
"FunctionTypeAnnotation": ["typeParameters", "params", "rest", "returnType"],
"FunctionTypeParam": ["name", "typeAnnotation"],
"GenericTypeAnnotation": ["id", "typeParameters"],
"InterfaceExtends": ["id", "typeParameters"],
"InterfaceDeclaration": ["id", "typeParameters", "extends", "body"],
"IntersectionTypeAnnotation": ["types"],
"NullableTypeAnnotation": ["typeAnnotation"],
"NumberTypeAnnotation": [],
"StringLiteralTypeAnnotation": [],
"StringTypeAnnotation": [],
"TupleTypeAnnotation": [],
"TypeofTypeAnnotation": [],
"TypeAlias": [],
"TypeAnnotation": [],
"TupleTypeAnnotation": ["types"],
"TypeofTypeAnnotation": ["argument"],
"TypeAlias": ["id", "typeParameters", "right"],
"TypeAnnotation": ["typeAnnotation"],
"TypeCastExpression": ["expression"],
"TypeParameterDeclaration": [],
"TypeParameterInstantiation": [],
"ObjectTypeAnnotation": [],
"ObjectTypeCallProperty": [],
"ObjectTypeIndexer": [],
"ObjectTypeProperty": [],
"QualifiedTypeIdentifier": [],
"UnionTypeAnnotation": [],
"TypeParameterDeclaration": ["params"],
"TypeParameterInstantiation": ["params"],
"ObjectTypeAnnotation": ["key", "value"],
"ObjectTypeCallProperty": ["value"],
"ObjectTypeIndexer": ["id", "key", "value"],
"ObjectTypeProperty": ["key", "value"],
"QualifiedTypeIdentifier": ["id", "qualification"],
"UnionTypeAnnotation": ["types"],
"VoidTypeAnnotation": [],
"JSXAttribute": ["name", "value"],

View File

@@ -15,7 +15,7 @@ import has from "lodash/object/has";
import fs from "fs";
import t from "./types";
export { inherits } from "util";
export { inherits, inspect } from "util";
export var debug = buildDebug("babel");