flow.js 12 KB

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