flow.js 9.3 KB

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