flow.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.AnyTypeAnnotation = AnyTypeAnnotation;
  4. exports.ArrayTypeAnnotation = ArrayTypeAnnotation;
  5. exports.BooleanTypeAnnotation = BooleanTypeAnnotation;
  6. exports.BooleanLiteralTypeAnnotation = BooleanLiteralTypeAnnotation;
  7. exports.NullLiteralTypeAnnotation = NullLiteralTypeAnnotation;
  8. exports.DeclareClass = DeclareClass;
  9. exports.DeclareFunction = DeclareFunction;
  10. exports.DeclareInterface = DeclareInterface;
  11. exports.DeclareModule = DeclareModule;
  12. exports.DeclareModuleExports = DeclareModuleExports;
  13. exports.DeclareTypeAlias = DeclareTypeAlias;
  14. exports.DeclareVariable = DeclareVariable;
  15. exports.ExistentialTypeParam = ExistentialTypeParam;
  16. exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
  17. exports.FunctionTypeParam = FunctionTypeParam;
  18. exports.InterfaceExtends = InterfaceExtends;
  19. exports._interfaceish = _interfaceish;
  20. exports.InterfaceDeclaration = InterfaceDeclaration;
  21. exports.IntersectionTypeAnnotation = IntersectionTypeAnnotation;
  22. exports.MixedTypeAnnotation = MixedTypeAnnotation;
  23. exports.NullableTypeAnnotation = NullableTypeAnnotation;
  24. var _types = require("./types");
  25. Object.defineProperty(exports, "NumericLiteralTypeAnnotation", {
  26. enumerable: true,
  27. get: function get() {
  28. return _types.NumericLiteral;
  29. }
  30. });
  31. Object.defineProperty(exports, "StringLiteralTypeAnnotation", {
  32. enumerable: true,
  33. get: function get() {
  34. return _types.StringLiteral;
  35. }
  36. });
  37. exports.NumberTypeAnnotation = NumberTypeAnnotation;
  38. exports.StringTypeAnnotation = StringTypeAnnotation;
  39. exports.ThisTypeAnnotation = ThisTypeAnnotation;
  40. exports.TupleTypeAnnotation = TupleTypeAnnotation;
  41. exports.TypeofTypeAnnotation = TypeofTypeAnnotation;
  42. exports.TypeAlias = TypeAlias;
  43. exports.TypeAnnotation = TypeAnnotation;
  44. exports.TypeParameter = TypeParameter;
  45. exports.TypeParameterInstantiation = TypeParameterInstantiation;
  46. exports.ObjectTypeAnnotation = ObjectTypeAnnotation;
  47. exports.ObjectTypeCallProperty = ObjectTypeCallProperty;
  48. exports.ObjectTypeIndexer = ObjectTypeIndexer;
  49. exports.ObjectTypeProperty = ObjectTypeProperty;
  50. exports.QualifiedTypeIdentifier = QualifiedTypeIdentifier;
  51. exports.UnionTypeAnnotation = UnionTypeAnnotation;
  52. exports.TypeCastExpression = TypeCastExpression;
  53. exports.VoidTypeAnnotation = VoidTypeAnnotation;
  54. function AnyTypeAnnotation() {
  55. this.word("any");
  56. }
  57. function ArrayTypeAnnotation(node) {
  58. this.print(node.elementType, node);
  59. this.token("[");
  60. this.token("]");
  61. }
  62. function BooleanTypeAnnotation() {
  63. this.word("bool");
  64. }
  65. function BooleanLiteralTypeAnnotation(node) {
  66. this.word(node.value ? "true" : "false");
  67. }
  68. function NullLiteralTypeAnnotation() {
  69. this.word("null");
  70. }
  71. function DeclareClass(node) {
  72. this.word("declare");
  73. this.space();
  74. this.word("class");
  75. this.space();
  76. this._interfaceish(node);
  77. }
  78. function DeclareFunction(node) {
  79. this.word("declare");
  80. this.space();
  81. this.word("function");
  82. this.space();
  83. this.print(node.id, node);
  84. this.print(node.id.typeAnnotation.typeAnnotation, node);
  85. this.semicolon();
  86. }
  87. function DeclareInterface(node) {
  88. this.word("declare");
  89. this.space();
  90. this.InterfaceDeclaration(node);
  91. }
  92. function DeclareModule(node) {
  93. this.word("declare");
  94. this.space();
  95. this.word("module");
  96. this.space();
  97. this.print(node.id, node);
  98. this.space();
  99. this.print(node.body, node);
  100. }
  101. function DeclareModuleExports(node) {
  102. this.word("declare");
  103. this.space();
  104. this.word("module");
  105. this.token(".");
  106. this.word("exports");
  107. this.print(node.typeAnnotation, node);
  108. }
  109. function DeclareTypeAlias(node) {
  110. this.word("declare");
  111. this.space();
  112. this.TypeAlias(node);
  113. }
  114. function DeclareVariable(node) {
  115. this.word("declare");
  116. this.space();
  117. this.word("var");
  118. this.space();
  119. this.print(node.id, node);
  120. this.print(node.id.typeAnnotation, node);
  121. this.semicolon();
  122. }
  123. function ExistentialTypeParam() {
  124. this.token("*");
  125. }
  126. function FunctionTypeAnnotation(node, parent) {
  127. this.print(node.typeParameters, node);
  128. this.token("(");
  129. this.printList(node.params, node);
  130. if (node.rest) {
  131. if (node.params.length) {
  132. this.token(",");
  133. this.space();
  134. }
  135. this.token("...");
  136. this.print(node.rest, node);
  137. }
  138. this.token(")");
  139. if (parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction") {
  140. this.token(":");
  141. } else {
  142. this.space();
  143. this.token("=>");
  144. }
  145. this.space();
  146. this.print(node.returnType, node);
  147. }
  148. function FunctionTypeParam(node) {
  149. this.print(node.name, node);
  150. if (node.optional) this.token("?");
  151. this.token(":");
  152. this.space();
  153. this.print(node.typeAnnotation, node);
  154. }
  155. function InterfaceExtends(node) {
  156. this.print(node.id, node);
  157. this.print(node.typeParameters, node);
  158. }
  159. exports.ClassImplements = InterfaceExtends;
  160. exports.GenericTypeAnnotation = InterfaceExtends;
  161. function _interfaceish(node) {
  162. this.print(node.id, node);
  163. this.print(node.typeParameters, node);
  164. if (node.extends.length) {
  165. this.space();
  166. this.word("extends");
  167. this.space();
  168. this.printList(node.extends, node);
  169. }
  170. if (node.mixins && node.mixins.length) {
  171. this.space();
  172. this.word("mixins");
  173. this.space();
  174. this.printList(node.mixins, node);
  175. }
  176. this.space();
  177. this.print(node.body, node);
  178. }
  179. function InterfaceDeclaration(node) {
  180. this.word("interface");
  181. this.space();
  182. this._interfaceish(node);
  183. }
  184. function andSeparator() {
  185. this.space();
  186. this.token("&");
  187. this.space();
  188. }
  189. function IntersectionTypeAnnotation(node) {
  190. this.printJoin(node.types, node, { separator: andSeparator });
  191. }
  192. function MixedTypeAnnotation() {
  193. this.word("mixed");
  194. }
  195. function NullableTypeAnnotation(node) {
  196. this.token("?");
  197. this.print(node.typeAnnotation, node);
  198. }
  199. function NumberTypeAnnotation() {
  200. this.word("number");
  201. }
  202. function StringTypeAnnotation() {
  203. this.word("string");
  204. }
  205. function ThisTypeAnnotation() {
  206. this.word("this");
  207. }
  208. function TupleTypeAnnotation(node) {
  209. this.token("[");
  210. this.printList(node.types, node);
  211. this.token("]");
  212. }
  213. function TypeofTypeAnnotation(node) {
  214. this.word("typeof");
  215. this.space();
  216. this.print(node.argument, node);
  217. }
  218. function TypeAlias(node) {
  219. this.word("type");
  220. this.space();
  221. this.print(node.id, node);
  222. this.print(node.typeParameters, node);
  223. this.space();
  224. this.token("=");
  225. this.space();
  226. this.print(node.right, node);
  227. this.semicolon();
  228. }
  229. function TypeAnnotation(node) {
  230. this.token(":");
  231. this.space();
  232. if (node.optional) this.token("?");
  233. this.print(node.typeAnnotation, node);
  234. }
  235. function TypeParameter(node) {
  236. if (node.variance === "plus") {
  237. this.token("+");
  238. } else if (node.variance === "minus") {
  239. this.token("-");
  240. }
  241. this.word(node.name);
  242. if (node.bound) {
  243. this.print(node.bound, node);
  244. }
  245. if (node.default) {
  246. this.space();
  247. this.token("=");
  248. this.space();
  249. this.print(node.default, node);
  250. }
  251. }
  252. function TypeParameterInstantiation(node) {
  253. this.token("<");
  254. this.printList(node.params, node, {});
  255. this.token(">");
  256. }
  257. exports.TypeParameterDeclaration = TypeParameterInstantiation;
  258. function ObjectTypeAnnotation(node) {
  259. var _this = this;
  260. this.token("{");
  261. var props = node.properties.concat(node.callProperties, node.indexers);
  262. if (props.length) {
  263. this.space();
  264. this.printJoin(props, node, {
  265. indent: true,
  266. statement: true,
  267. iterator: function iterator() {
  268. if (props.length !== 1) {
  269. _this.semicolon();
  270. _this.space();
  271. }
  272. }
  273. });
  274. this.space();
  275. }
  276. this.token("}");
  277. }
  278. function ObjectTypeCallProperty(node) {
  279. if (node.static) {
  280. this.word("static");
  281. this.space();
  282. }
  283. this.print(node.value, node);
  284. }
  285. function ObjectTypeIndexer(node) {
  286. if (node.static) {
  287. this.word("static");
  288. this.space();
  289. }
  290. this.token("[");
  291. this.print(node.id, node);
  292. this.token(":");
  293. this.space();
  294. this.print(node.key, node);
  295. this.token("]");
  296. this.token(":");
  297. this.space();
  298. this.print(node.value, node);
  299. }
  300. function ObjectTypeProperty(node) {
  301. if (node.static) {
  302. this.word("static");
  303. this.space();
  304. }
  305. this.print(node.key, node);
  306. if (node.optional) this.token("?");
  307. this.token(":");
  308. this.space();
  309. this.print(node.value, node);
  310. }
  311. function QualifiedTypeIdentifier(node) {
  312. this.print(node.qualification, node);
  313. this.token(".");
  314. this.print(node.id, node);
  315. }
  316. function orSeparator() {
  317. this.space();
  318. this.token("|");
  319. this.space();
  320. }
  321. function UnionTypeAnnotation(node) {
  322. this.printJoin(node.types, node, { separator: orSeparator });
  323. }
  324. function TypeCastExpression(node) {
  325. this.token("(");
  326. this.print(node.expression, node);
  327. this.print(node.typeAnnotation, node);
  328. this.token(")");
  329. }
  330. function VoidTypeAnnotation() {
  331. this.word("void");
  332. }