flow.js 13 KB

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