flow.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.AnyTypeAnnotation = AnyTypeAnnotation;
  6. exports.ArrayTypeAnnotation = ArrayTypeAnnotation;
  7. exports.BooleanLiteralTypeAnnotation = BooleanLiteralTypeAnnotation;
  8. exports.BooleanTypeAnnotation = BooleanTypeAnnotation;
  9. exports.DeclareClass = DeclareClass;
  10. exports.DeclareExportAllDeclaration = DeclareExportAllDeclaration;
  11. exports.DeclareExportDeclaration = DeclareExportDeclaration;
  12. exports.DeclareFunction = DeclareFunction;
  13. exports.DeclareInterface = DeclareInterface;
  14. exports.DeclareModule = DeclareModule;
  15. exports.DeclareModuleExports = DeclareModuleExports;
  16. exports.DeclareOpaqueType = DeclareOpaqueType;
  17. exports.DeclareTypeAlias = DeclareTypeAlias;
  18. exports.DeclareVariable = DeclareVariable;
  19. exports.DeclaredPredicate = DeclaredPredicate;
  20. exports.EmptyTypeAnnotation = EmptyTypeAnnotation;
  21. exports.EnumBooleanBody = EnumBooleanBody;
  22. exports.EnumBooleanMember = EnumBooleanMember;
  23. exports.EnumDeclaration = EnumDeclaration;
  24. exports.EnumDefaultedMember = EnumDefaultedMember;
  25. exports.EnumNumberBody = EnumNumberBody;
  26. exports.EnumNumberMember = EnumNumberMember;
  27. exports.EnumStringBody = EnumStringBody;
  28. exports.EnumStringMember = EnumStringMember;
  29. exports.EnumSymbolBody = EnumSymbolBody;
  30. exports.ExistsTypeAnnotation = ExistsTypeAnnotation;
  31. exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
  32. exports.FunctionTypeParam = FunctionTypeParam;
  33. exports.IndexedAccessType = IndexedAccessType;
  34. exports.InferredPredicate = InferredPredicate;
  35. exports.InterfaceDeclaration = InterfaceDeclaration;
  36. exports.GenericTypeAnnotation = exports.ClassImplements = exports.InterfaceExtends = InterfaceExtends;
  37. exports.InterfaceTypeAnnotation = InterfaceTypeAnnotation;
  38. exports.IntersectionTypeAnnotation = IntersectionTypeAnnotation;
  39. exports.MixedTypeAnnotation = MixedTypeAnnotation;
  40. exports.NullLiteralTypeAnnotation = NullLiteralTypeAnnotation;
  41. exports.NullableTypeAnnotation = NullableTypeAnnotation;
  42. Object.defineProperty(exports, "NumberLiteralTypeAnnotation", {
  43. enumerable: true,
  44. get: function () {
  45. return _types2.NumericLiteral;
  46. }
  47. });
  48. exports.NumberTypeAnnotation = NumberTypeAnnotation;
  49. exports.ObjectTypeAnnotation = ObjectTypeAnnotation;
  50. exports.ObjectTypeCallProperty = ObjectTypeCallProperty;
  51. exports.ObjectTypeIndexer = ObjectTypeIndexer;
  52. exports.ObjectTypeInternalSlot = ObjectTypeInternalSlot;
  53. exports.ObjectTypeProperty = ObjectTypeProperty;
  54. exports.ObjectTypeSpreadProperty = ObjectTypeSpreadProperty;
  55. exports.OpaqueType = OpaqueType;
  56. exports.OptionalIndexedAccessType = OptionalIndexedAccessType;
  57. exports.QualifiedTypeIdentifier = QualifiedTypeIdentifier;
  58. Object.defineProperty(exports, "StringLiteralTypeAnnotation", {
  59. enumerable: true,
  60. get: function () {
  61. return _types2.StringLiteral;
  62. }
  63. });
  64. exports.StringTypeAnnotation = StringTypeAnnotation;
  65. exports.SymbolTypeAnnotation = SymbolTypeAnnotation;
  66. exports.ThisTypeAnnotation = ThisTypeAnnotation;
  67. exports.TupleTypeAnnotation = TupleTypeAnnotation;
  68. exports.TypeAlias = TypeAlias;
  69. exports.TypeAnnotation = TypeAnnotation;
  70. exports.TypeCastExpression = TypeCastExpression;
  71. exports.TypeParameter = TypeParameter;
  72. exports.TypeParameterDeclaration = exports.TypeParameterInstantiation = TypeParameterInstantiation;
  73. exports.TypeofTypeAnnotation = TypeofTypeAnnotation;
  74. exports.UnionTypeAnnotation = UnionTypeAnnotation;
  75. exports.Variance = Variance;
  76. exports.VoidTypeAnnotation = VoidTypeAnnotation;
  77. exports._interfaceish = _interfaceish;
  78. exports._variance = _variance;
  79. var _t = require("@babel/types");
  80. var _modules = require("./modules");
  81. var _types2 = require("./types");
  82. const {
  83. isDeclareExportDeclaration,
  84. isStatement
  85. } = _t;
  86. function AnyTypeAnnotation() {
  87. this.word("any");
  88. }
  89. function ArrayTypeAnnotation(node) {
  90. this.print(node.elementType, node);
  91. this.token("[");
  92. this.token("]");
  93. }
  94. function BooleanTypeAnnotation() {
  95. this.word("boolean");
  96. }
  97. function BooleanLiteralTypeAnnotation(node) {
  98. this.word(node.value ? "true" : "false");
  99. }
  100. function NullLiteralTypeAnnotation() {
  101. this.word("null");
  102. }
  103. function DeclareClass(node, parent) {
  104. if (!isDeclareExportDeclaration(parent)) {
  105. this.word("declare");
  106. this.space();
  107. }
  108. this.word("class");
  109. this.space();
  110. this._interfaceish(node);
  111. }
  112. function DeclareFunction(node, parent) {
  113. if (!isDeclareExportDeclaration(parent)) {
  114. this.word("declare");
  115. this.space();
  116. }
  117. this.word("function");
  118. this.space();
  119. this.print(node.id, node);
  120. this.print(node.id.typeAnnotation.typeAnnotation, node);
  121. if (node.predicate) {
  122. this.space();
  123. this.print(node.predicate, node);
  124. }
  125. this.semicolon();
  126. }
  127. function InferredPredicate() {
  128. this.token("%");
  129. this.word("checks");
  130. }
  131. function DeclaredPredicate(node) {
  132. this.token("%");
  133. this.word("checks");
  134. this.token("(");
  135. this.print(node.value, node);
  136. this.token(")");
  137. }
  138. function DeclareInterface(node) {
  139. this.word("declare");
  140. this.space();
  141. this.InterfaceDeclaration(node);
  142. }
  143. function DeclareModule(node) {
  144. this.word("declare");
  145. this.space();
  146. this.word("module");
  147. this.space();
  148. this.print(node.id, node);
  149. this.space();
  150. this.print(node.body, node);
  151. }
  152. function DeclareModuleExports(node) {
  153. this.word("declare");
  154. this.space();
  155. this.word("module");
  156. this.token(".");
  157. this.word("exports");
  158. this.print(node.typeAnnotation, node);
  159. }
  160. function DeclareTypeAlias(node) {
  161. this.word("declare");
  162. this.space();
  163. this.TypeAlias(node);
  164. }
  165. function DeclareOpaqueType(node, parent) {
  166. if (!isDeclareExportDeclaration(parent)) {
  167. this.word("declare");
  168. this.space();
  169. }
  170. this.OpaqueType(node);
  171. }
  172. function DeclareVariable(node, parent) {
  173. if (!isDeclareExportDeclaration(parent)) {
  174. this.word("declare");
  175. this.space();
  176. }
  177. this.word("var");
  178. this.space();
  179. this.print(node.id, node);
  180. this.print(node.id.typeAnnotation, node);
  181. this.semicolon();
  182. }
  183. function DeclareExportDeclaration(node) {
  184. this.word("declare");
  185. this.space();
  186. this.word("export");
  187. this.space();
  188. if (node.default) {
  189. this.word("default");
  190. this.space();
  191. }
  192. FlowExportDeclaration.apply(this, arguments);
  193. }
  194. function DeclareExportAllDeclaration() {
  195. this.word("declare");
  196. this.space();
  197. _modules.ExportAllDeclaration.apply(this, arguments);
  198. }
  199. function EnumDeclaration(node) {
  200. const {
  201. id,
  202. body
  203. } = node;
  204. this.word("enum");
  205. this.space();
  206. this.print(id, node);
  207. this.print(body, node);
  208. }
  209. function enumExplicitType(context, name, hasExplicitType) {
  210. if (hasExplicitType) {
  211. context.space();
  212. context.word("of");
  213. context.space();
  214. context.word(name);
  215. }
  216. context.space();
  217. }
  218. function enumBody(context, node) {
  219. const {
  220. members
  221. } = node;
  222. context.token("{");
  223. context.indent();
  224. context.newline();
  225. for (const member of members) {
  226. context.print(member, node);
  227. context.newline();
  228. }
  229. if (node.hasUnknownMembers) {
  230. context.token("...");
  231. context.newline();
  232. }
  233. context.dedent();
  234. context.token("}");
  235. }
  236. function EnumBooleanBody(node) {
  237. const {
  238. explicitType
  239. } = node;
  240. enumExplicitType(this, "boolean", explicitType);
  241. enumBody(this, node);
  242. }
  243. function EnumNumberBody(node) {
  244. const {
  245. explicitType
  246. } = node;
  247. enumExplicitType(this, "number", explicitType);
  248. enumBody(this, node);
  249. }
  250. function EnumStringBody(node) {
  251. const {
  252. explicitType
  253. } = node;
  254. enumExplicitType(this, "string", explicitType);
  255. enumBody(this, node);
  256. }
  257. function EnumSymbolBody(node) {
  258. enumExplicitType(this, "symbol", true);
  259. enumBody(this, node);
  260. }
  261. function EnumDefaultedMember(node) {
  262. const {
  263. id
  264. } = node;
  265. this.print(id, node);
  266. this.token(",");
  267. }
  268. function enumInitializedMember(context, node) {
  269. const {
  270. id,
  271. init
  272. } = node;
  273. context.print(id, node);
  274. context.space();
  275. context.token("=");
  276. context.space();
  277. context.print(init, node);
  278. context.token(",");
  279. }
  280. function EnumBooleanMember(node) {
  281. enumInitializedMember(this, node);
  282. }
  283. function EnumNumberMember(node) {
  284. enumInitializedMember(this, node);
  285. }
  286. function EnumStringMember(node) {
  287. enumInitializedMember(this, node);
  288. }
  289. function FlowExportDeclaration(node) {
  290. if (node.declaration) {
  291. const declar = node.declaration;
  292. this.print(declar, node);
  293. if (!isStatement(declar)) this.semicolon();
  294. } else {
  295. this.token("{");
  296. if (node.specifiers.length) {
  297. this.space();
  298. this.printList(node.specifiers, node);
  299. this.space();
  300. }
  301. this.token("}");
  302. if (node.source) {
  303. this.space();
  304. this.word("from");
  305. this.space();
  306. this.print(node.source, node);
  307. }
  308. this.semicolon();
  309. }
  310. }
  311. function ExistsTypeAnnotation() {
  312. this.token("*");
  313. }
  314. function FunctionTypeAnnotation(node, parent) {
  315. this.print(node.typeParameters, node);
  316. this.token("(");
  317. if (node.this) {
  318. this.word("this");
  319. this.token(":");
  320. this.space();
  321. this.print(node.this.typeAnnotation, node);
  322. if (node.params.length || node.rest) {
  323. this.token(",");
  324. this.space();
  325. }
  326. }
  327. this.printList(node.params, node);
  328. if (node.rest) {
  329. if (node.params.length) {
  330. this.token(",");
  331. this.space();
  332. }
  333. this.token("...");
  334. this.print(node.rest, node);
  335. }
  336. this.token(")");
  337. if (parent && (parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction" || parent.type === "ObjectTypeProperty" && parent.method)) {
  338. this.token(":");
  339. } else {
  340. this.space();
  341. this.token("=>");
  342. }
  343. this.space();
  344. this.print(node.returnType, node);
  345. }
  346. function FunctionTypeParam(node) {
  347. this.print(node.name, node);
  348. if (node.optional) this.token("?");
  349. if (node.name) {
  350. this.token(":");
  351. this.space();
  352. }
  353. this.print(node.typeAnnotation, node);
  354. }
  355. function InterfaceExtends(node) {
  356. this.print(node.id, node);
  357. this.print(node.typeParameters, node);
  358. }
  359. function _interfaceish(node) {
  360. var _node$extends;
  361. this.print(node.id, node);
  362. this.print(node.typeParameters, node);
  363. if ((_node$extends = node.extends) != null && _node$extends.length) {
  364. this.space();
  365. this.word("extends");
  366. this.space();
  367. this.printList(node.extends, node);
  368. }
  369. if (node.mixins && node.mixins.length) {
  370. this.space();
  371. this.word("mixins");
  372. this.space();
  373. this.printList(node.mixins, node);
  374. }
  375. if (node.implements && node.implements.length) {
  376. this.space();
  377. this.word("implements");
  378. this.space();
  379. this.printList(node.implements, node);
  380. }
  381. this.space();
  382. this.print(node.body, node);
  383. }
  384. function _variance(node) {
  385. if (node.variance) {
  386. if (node.variance.kind === "plus") {
  387. this.token("+");
  388. } else if (node.variance.kind === "minus") {
  389. this.token("-");
  390. }
  391. }
  392. }
  393. function InterfaceDeclaration(node) {
  394. this.word("interface");
  395. this.space();
  396. this._interfaceish(node);
  397. }
  398. function andSeparator() {
  399. this.space();
  400. this.token("&");
  401. this.space();
  402. }
  403. function InterfaceTypeAnnotation(node) {
  404. this.word("interface");
  405. if (node.extends && node.extends.length) {
  406. this.space();
  407. this.word("extends");
  408. this.space();
  409. this.printList(node.extends, node);
  410. }
  411. this.space();
  412. this.print(node.body, node);
  413. }
  414. function IntersectionTypeAnnotation(node) {
  415. this.printJoin(node.types, node, {
  416. separator: andSeparator
  417. });
  418. }
  419. function MixedTypeAnnotation() {
  420. this.word("mixed");
  421. }
  422. function EmptyTypeAnnotation() {
  423. this.word("empty");
  424. }
  425. function NullableTypeAnnotation(node) {
  426. this.token("?");
  427. this.print(node.typeAnnotation, node);
  428. }
  429. function NumberTypeAnnotation() {
  430. this.word("number");
  431. }
  432. function StringTypeAnnotation() {
  433. this.word("string");
  434. }
  435. function ThisTypeAnnotation() {
  436. this.word("this");
  437. }
  438. function TupleTypeAnnotation(node) {
  439. this.token("[");
  440. this.printList(node.types, node);
  441. this.token("]");
  442. }
  443. function TypeofTypeAnnotation(node) {
  444. this.word("typeof");
  445. this.space();
  446. this.print(node.argument, node);
  447. }
  448. function TypeAlias(node) {
  449. this.word("type");
  450. this.space();
  451. this.print(node.id, node);
  452. this.print(node.typeParameters, node);
  453. this.space();
  454. this.token("=");
  455. this.space();
  456. this.print(node.right, node);
  457. this.semicolon();
  458. }
  459. function TypeAnnotation(node) {
  460. this.token(":");
  461. this.space();
  462. if (node.optional) this.token("?");
  463. this.print(node.typeAnnotation, node);
  464. }
  465. function TypeParameterInstantiation(node) {
  466. this.token("<");
  467. this.printList(node.params, node, {});
  468. this.token(">");
  469. }
  470. function TypeParameter(node) {
  471. this._variance(node);
  472. this.word(node.name);
  473. if (node.bound) {
  474. this.print(node.bound, node);
  475. }
  476. if (node.default) {
  477. this.space();
  478. this.token("=");
  479. this.space();
  480. this.print(node.default, node);
  481. }
  482. }
  483. function OpaqueType(node) {
  484. this.word("opaque");
  485. this.space();
  486. this.word("type");
  487. this.space();
  488. this.print(node.id, node);
  489. this.print(node.typeParameters, node);
  490. if (node.supertype) {
  491. this.token(":");
  492. this.space();
  493. this.print(node.supertype, node);
  494. }
  495. if (node.impltype) {
  496. this.space();
  497. this.token("=");
  498. this.space();
  499. this.print(node.impltype, node);
  500. }
  501. this.semicolon();
  502. }
  503. function ObjectTypeAnnotation(node) {
  504. if (node.exact) {
  505. this.token("{|");
  506. } else {
  507. this.token("{");
  508. }
  509. const props = [...node.properties, ...(node.callProperties || []), ...(node.indexers || []), ...(node.internalSlots || [])];
  510. if (props.length) {
  511. this.space();
  512. this.printJoin(props, node, {
  513. addNewlines(leading) {
  514. if (leading && !props[0]) return 1;
  515. },
  516. indent: true,
  517. statement: true,
  518. iterator: () => {
  519. if (props.length !== 1 || node.inexact) {
  520. this.token(",");
  521. this.space();
  522. }
  523. }
  524. });
  525. this.space();
  526. }
  527. if (node.inexact) {
  528. this.indent();
  529. this.token("...");
  530. if (props.length) {
  531. this.newline();
  532. }
  533. this.dedent();
  534. }
  535. if (node.exact) {
  536. this.token("|}");
  537. } else {
  538. this.token("}");
  539. }
  540. }
  541. function ObjectTypeInternalSlot(node) {
  542. if (node.static) {
  543. this.word("static");
  544. this.space();
  545. }
  546. this.token("[");
  547. this.token("[");
  548. this.print(node.id, node);
  549. this.token("]");
  550. this.token("]");
  551. if (node.optional) this.token("?");
  552. if (!node.method) {
  553. this.token(":");
  554. this.space();
  555. }
  556. this.print(node.value, node);
  557. }
  558. function ObjectTypeCallProperty(node) {
  559. if (node.static) {
  560. this.word("static");
  561. this.space();
  562. }
  563. this.print(node.value, node);
  564. }
  565. function ObjectTypeIndexer(node) {
  566. if (node.static) {
  567. this.word("static");
  568. this.space();
  569. }
  570. this._variance(node);
  571. this.token("[");
  572. if (node.id) {
  573. this.print(node.id, node);
  574. this.token(":");
  575. this.space();
  576. }
  577. this.print(node.key, node);
  578. this.token("]");
  579. this.token(":");
  580. this.space();
  581. this.print(node.value, node);
  582. }
  583. function ObjectTypeProperty(node) {
  584. if (node.proto) {
  585. this.word("proto");
  586. this.space();
  587. }
  588. if (node.static) {
  589. this.word("static");
  590. this.space();
  591. }
  592. if (node.kind === "get" || node.kind === "set") {
  593. this.word(node.kind);
  594. this.space();
  595. }
  596. this._variance(node);
  597. this.print(node.key, node);
  598. if (node.optional) this.token("?");
  599. if (!node.method) {
  600. this.token(":");
  601. this.space();
  602. }
  603. this.print(node.value, node);
  604. }
  605. function ObjectTypeSpreadProperty(node) {
  606. this.token("...");
  607. this.print(node.argument, node);
  608. }
  609. function QualifiedTypeIdentifier(node) {
  610. this.print(node.qualification, node);
  611. this.token(".");
  612. this.print(node.id, node);
  613. }
  614. function SymbolTypeAnnotation() {
  615. this.word("symbol");
  616. }
  617. function orSeparator() {
  618. this.space();
  619. this.token("|");
  620. this.space();
  621. }
  622. function UnionTypeAnnotation(node) {
  623. this.printJoin(node.types, node, {
  624. separator: orSeparator
  625. });
  626. }
  627. function TypeCastExpression(node) {
  628. this.token("(");
  629. this.print(node.expression, node);
  630. this.print(node.typeAnnotation, node);
  631. this.token(")");
  632. }
  633. function Variance(node) {
  634. if (node.kind === "plus") {
  635. this.token("+");
  636. } else {
  637. this.token("-");
  638. }
  639. }
  640. function VoidTypeAnnotation() {
  641. this.word("void");
  642. }
  643. function IndexedAccessType(node) {
  644. this.print(node.objectType, node);
  645. this.token("[");
  646. this.print(node.indexType, node);
  647. this.token("]");
  648. }
  649. function OptionalIndexedAccessType(node) {
  650. this.print(node.objectType, node);
  651. if (node.optional) {
  652. this.token("?.");
  653. }
  654. this.token("[");
  655. this.print(node.indexType, node);
  656. this.token("]");
  657. }