flow.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.TypeParameterDeclaration = exports.StringLiteralTypeAnnotation = exports.NumericLiteralTypeAnnotation = exports.GenericTypeAnnotation = exports.ClassImplements = undefined;
  4. exports.AnyTypeAnnotation = AnyTypeAnnotation;
  5. exports.ArrayTypeAnnotation = ArrayTypeAnnotation;
  6. exports.BooleanTypeAnnotation = BooleanTypeAnnotation;
  7. exports.BooleanLiteralTypeAnnotation = BooleanLiteralTypeAnnotation;
  8. exports.NullLiteralTypeAnnotation = NullLiteralTypeAnnotation;
  9. exports.DeclareClass = DeclareClass;
  10. exports.DeclareFunction = DeclareFunction;
  11. exports.DeclareInterface = DeclareInterface;
  12. exports.DeclareModule = DeclareModule;
  13. exports.DeclareModuleExports = DeclareModuleExports;
  14. exports.DeclareTypeAlias = DeclareTypeAlias;
  15. exports.DeclareOpaqueType = DeclareOpaqueType;
  16. exports.DeclareVariable = DeclareVariable;
  17. exports.DeclareExportDeclaration = DeclareExportDeclaration;
  18. exports.ExistentialTypeParam = ExistentialTypeParam;
  19. exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
  20. exports.FunctionTypeParam = FunctionTypeParam;
  21. exports.InterfaceExtends = InterfaceExtends;
  22. exports._interfaceish = _interfaceish;
  23. exports._variance = _variance;
  24. exports.InterfaceDeclaration = InterfaceDeclaration;
  25. exports.IntersectionTypeAnnotation = IntersectionTypeAnnotation;
  26. exports.MixedTypeAnnotation = MixedTypeAnnotation;
  27. exports.EmptyTypeAnnotation = EmptyTypeAnnotation;
  28. exports.NullableTypeAnnotation = NullableTypeAnnotation;
  29. var _types = require("./types");
  30. Object.defineProperty(exports, "NumericLiteralTypeAnnotation", {
  31. enumerable: true,
  32. get: function get() {
  33. return _types.NumericLiteral;
  34. }
  35. });
  36. Object.defineProperty(exports, "StringLiteralTypeAnnotation", {
  37. enumerable: true,
  38. get: function get() {
  39. return _types.StringLiteral;
  40. }
  41. });
  42. exports.NumberTypeAnnotation = NumberTypeAnnotation;
  43. exports.StringTypeAnnotation = StringTypeAnnotation;
  44. exports.ThisTypeAnnotation = ThisTypeAnnotation;
  45. exports.TupleTypeAnnotation = TupleTypeAnnotation;
  46. exports.TypeofTypeAnnotation = TypeofTypeAnnotation;
  47. exports.TypeAlias = TypeAlias;
  48. exports.OpaqueType = OpaqueType;
  49. exports.TypeAnnotation = TypeAnnotation;
  50. exports.TypeParameter = TypeParameter;
  51. exports.TypeParameterInstantiation = TypeParameterInstantiation;
  52. exports.ObjectTypeAnnotation = ObjectTypeAnnotation;
  53. exports.ObjectTypeCallProperty = ObjectTypeCallProperty;
  54. exports.ObjectTypeIndexer = ObjectTypeIndexer;
  55. exports.ObjectTypeProperty = ObjectTypeProperty;
  56. exports.ObjectTypeSpreadProperty = ObjectTypeSpreadProperty;
  57. exports.QualifiedTypeIdentifier = QualifiedTypeIdentifier;
  58. exports.UnionTypeAnnotation = UnionTypeAnnotation;
  59. exports.TypeCastExpression = TypeCastExpression;
  60. exports.VoidTypeAnnotation = VoidTypeAnnotation;
  61. var _babelTypes = require("babel-types");
  62. var t = _interopRequireWildcard(_babelTypes);
  63. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  64. function AnyTypeAnnotation() {
  65. this.word("any");
  66. }
  67. function ArrayTypeAnnotation(node) {
  68. this.print(node.elementType, node);
  69. this.token("[");
  70. this.token("]");
  71. }
  72. function BooleanTypeAnnotation() {
  73. this.word("boolean");
  74. }
  75. function BooleanLiteralTypeAnnotation(node) {
  76. this.word(node.value ? "true" : "false");
  77. }
  78. function NullLiteralTypeAnnotation() {
  79. this.word("null");
  80. }
  81. function DeclareClass(node, parent) {
  82. if (!t.isDeclareExportDeclaration(parent)) {
  83. this.word("declare");
  84. this.space();
  85. }
  86. this.word("class");
  87. this.space();
  88. this._interfaceish(node);
  89. }
  90. function DeclareFunction(node, parent) {
  91. if (!t.isDeclareExportDeclaration(parent)) {
  92. this.word("declare");
  93. this.space();
  94. }
  95. this.word("function");
  96. this.space();
  97. this.print(node.id, node);
  98. this.print(node.id.typeAnnotation.typeAnnotation, node);
  99. this.semicolon();
  100. }
  101. function DeclareInterface(node) {
  102. this.word("declare");
  103. this.space();
  104. this.InterfaceDeclaration(node);
  105. }
  106. function DeclareModule(node) {
  107. this.word("declare");
  108. this.space();
  109. this.word("module");
  110. this.space();
  111. this.print(node.id, node);
  112. this.space();
  113. this.print(node.body, node);
  114. }
  115. function DeclareModuleExports(node) {
  116. this.word("declare");
  117. this.space();
  118. this.word("module");
  119. this.token(".");
  120. this.word("exports");
  121. this.print(node.typeAnnotation, node);
  122. }
  123. function DeclareTypeAlias(node) {
  124. this.word("declare");
  125. this.space();
  126. this.TypeAlias(node);
  127. }
  128. function DeclareOpaqueType(node, parent) {
  129. if (!t.isDeclareExportDeclaration(parent)) {
  130. this.word("declare");
  131. this.space();
  132. }
  133. this.OpaqueType(node);
  134. }
  135. function DeclareVariable(node, parent) {
  136. if (!t.isDeclareExportDeclaration(parent)) {
  137. this.word("declare");
  138. this.space();
  139. }
  140. this.word("var");
  141. this.space();
  142. this.print(node.id, node);
  143. this.print(node.id.typeAnnotation, node);
  144. this.semicolon();
  145. }
  146. function DeclareExportDeclaration(node) {
  147. this.word("declare");
  148. this.space();
  149. this.word("export");
  150. this.space();
  151. if (node.default) {
  152. this.word("default");
  153. this.space();
  154. }
  155. FlowExportDeclaration.apply(this, arguments);
  156. }
  157. function FlowExportDeclaration(node) {
  158. if (node.declaration) {
  159. var declar = node.declaration;
  160. this.print(declar, node);
  161. if (!t.isStatement(declar)) this.semicolon();
  162. } else {
  163. this.token("{");
  164. if (node.specifiers.length) {
  165. this.space();
  166. this.printList(node.specifiers, node);
  167. this.space();
  168. }
  169. this.token("}");
  170. if (node.source) {
  171. this.space();
  172. this.word("from");
  173. this.space();
  174. this.print(node.source, node);
  175. }
  176. this.semicolon();
  177. }
  178. }
  179. function ExistentialTypeParam() {
  180. this.token("*");
  181. }
  182. function FunctionTypeAnnotation(node, parent) {
  183. this.print(node.typeParameters, node);
  184. this.token("(");
  185. this.printList(node.params, node);
  186. if (node.rest) {
  187. if (node.params.length) {
  188. this.token(",");
  189. this.space();
  190. }
  191. this.token("...");
  192. this.print(node.rest, node);
  193. }
  194. this.token(")");
  195. if (parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction") {
  196. this.token(":");
  197. } else {
  198. this.space();
  199. this.token("=>");
  200. }
  201. this.space();
  202. this.print(node.returnType, node);
  203. }
  204. function FunctionTypeParam(node) {
  205. this.print(node.name, node);
  206. if (node.optional) this.token("?");
  207. this.token(":");
  208. this.space();
  209. this.print(node.typeAnnotation, node);
  210. }
  211. function InterfaceExtends(node) {
  212. this.print(node.id, node);
  213. this.print(node.typeParameters, node);
  214. }
  215. exports.ClassImplements = InterfaceExtends;
  216. exports.GenericTypeAnnotation = InterfaceExtends;
  217. function _interfaceish(node) {
  218. this.print(node.id, node);
  219. this.print(node.typeParameters, node);
  220. if (node.extends.length) {
  221. this.space();
  222. this.word("extends");
  223. this.space();
  224. this.printList(node.extends, node);
  225. }
  226. if (node.mixins && node.mixins.length) {
  227. this.space();
  228. this.word("mixins");
  229. this.space();
  230. this.printList(node.mixins, node);
  231. }
  232. this.space();
  233. this.print(node.body, node);
  234. }
  235. function _variance(node) {
  236. if (node.variance === "plus") {
  237. this.token("+");
  238. } else if (node.variance === "minus") {
  239. this.token("-");
  240. }
  241. }
  242. function InterfaceDeclaration(node) {
  243. this.word("interface");
  244. this.space();
  245. this._interfaceish(node);
  246. }
  247. function andSeparator() {
  248. this.space();
  249. this.token("&");
  250. this.space();
  251. }
  252. function IntersectionTypeAnnotation(node) {
  253. this.printJoin(node.types, node, { separator: andSeparator });
  254. }
  255. function MixedTypeAnnotation() {
  256. this.word("mixed");
  257. }
  258. function EmptyTypeAnnotation() {
  259. this.word("empty");
  260. }
  261. function NullableTypeAnnotation(node) {
  262. this.token("?");
  263. this.print(node.typeAnnotation, node);
  264. }
  265. function NumberTypeAnnotation() {
  266. this.word("number");
  267. }
  268. function StringTypeAnnotation() {
  269. this.word("string");
  270. }
  271. function ThisTypeAnnotation() {
  272. this.word("this");
  273. }
  274. function TupleTypeAnnotation(node) {
  275. this.token("[");
  276. this.printList(node.types, node);
  277. this.token("]");
  278. }
  279. function TypeofTypeAnnotation(node) {
  280. this.word("typeof");
  281. this.space();
  282. this.print(node.argument, node);
  283. }
  284. function TypeAlias(node) {
  285. this.word("type");
  286. this.space();
  287. this.print(node.id, node);
  288. this.print(node.typeParameters, node);
  289. this.space();
  290. this.token("=");
  291. this.space();
  292. this.print(node.right, node);
  293. this.semicolon();
  294. }
  295. function OpaqueType(node) {
  296. this.word("opaque");
  297. this.space();
  298. this.word("type");
  299. this.space();
  300. this.print(node.id, node);
  301. this.print(node.typeParameters, node);
  302. if (node.supertype) {
  303. this.token(":");
  304. this.space();
  305. this.print(node.supertype, node);
  306. }
  307. if (node.impltype) {
  308. this.space();
  309. this.token("=");
  310. this.space();
  311. this.print(node.impltype, node);
  312. }
  313. this.semicolon();
  314. }
  315. function TypeAnnotation(node) {
  316. this.token(":");
  317. this.space();
  318. if (node.optional) this.token("?");
  319. this.print(node.typeAnnotation, node);
  320. }
  321. function TypeParameter(node) {
  322. this._variance(node);
  323. this.word(node.name);
  324. if (node.bound) {
  325. this.print(node.bound, node);
  326. }
  327. if (node.default) {
  328. this.space();
  329. this.token("=");
  330. this.space();
  331. this.print(node.default, node);
  332. }
  333. }
  334. function TypeParameterInstantiation(node) {
  335. this.token("<");
  336. this.printList(node.params, node, {});
  337. this.token(">");
  338. }
  339. exports.TypeParameterDeclaration = TypeParameterInstantiation;
  340. function ObjectTypeAnnotation(node) {
  341. var _this = this;
  342. if (node.exact) {
  343. this.token("{|");
  344. } else {
  345. this.token("{");
  346. }
  347. var props = node.properties.concat(node.callProperties, node.indexers);
  348. if (props.length) {
  349. this.space();
  350. this.printJoin(props, node, {
  351. addNewlines: function addNewlines(leading) {
  352. if (leading && !props[0]) return 1;
  353. },
  354. indent: true,
  355. statement: true,
  356. iterator: function iterator() {
  357. if (props.length !== 1) {
  358. if (_this.format.flowCommaSeparator) {
  359. _this.token(",");
  360. } else {
  361. _this.semicolon();
  362. }
  363. _this.space();
  364. }
  365. }
  366. });
  367. this.space();
  368. }
  369. if (node.exact) {
  370. this.token("|}");
  371. } else {
  372. this.token("}");
  373. }
  374. }
  375. function ObjectTypeCallProperty(node) {
  376. if (node.static) {
  377. this.word("static");
  378. this.space();
  379. }
  380. this.print(node.value, node);
  381. }
  382. function ObjectTypeIndexer(node) {
  383. if (node.static) {
  384. this.word("static");
  385. this.space();
  386. }
  387. this._variance(node);
  388. this.token("[");
  389. this.print(node.id, node);
  390. this.token(":");
  391. this.space();
  392. this.print(node.key, node);
  393. this.token("]");
  394. this.token(":");
  395. this.space();
  396. this.print(node.value, node);
  397. }
  398. function ObjectTypeProperty(node) {
  399. if (node.static) {
  400. this.word("static");
  401. this.space();
  402. }
  403. this._variance(node);
  404. this.print(node.key, node);
  405. if (node.optional) this.token("?");
  406. this.token(":");
  407. this.space();
  408. this.print(node.value, node);
  409. }
  410. function ObjectTypeSpreadProperty(node) {
  411. this.token("...");
  412. this.print(node.argument, node);
  413. }
  414. function QualifiedTypeIdentifier(node) {
  415. this.print(node.qualification, node);
  416. this.token(".");
  417. this.print(node.id, node);
  418. }
  419. function orSeparator() {
  420. this.space();
  421. this.token("|");
  422. this.space();
  423. }
  424. function UnionTypeAnnotation(node) {
  425. this.printJoin(node.types, node, { separator: orSeparator });
  426. }
  427. function TypeCastExpression(node) {
  428. this.token("(");
  429. this.print(node.expression, node);
  430. this.print(node.typeAnnotation, node);
  431. this.token(")");
  432. }
  433. function VoidTypeAnnotation() {
  434. this.word("void");
  435. }