flow.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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.js");
  81. var _types2 = require("./types.js");
  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, true);
  91. this.tokenChar(91);
  92. this.tokenChar(93);
  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.tokenChar(37);
  129. this.word("checks");
  130. }
  131. function DeclaredPredicate(node) {
  132. this.tokenChar(37);
  133. this.word("checks");
  134. this.tokenChar(40);
  135. this.print(node.value, node);
  136. this.tokenChar(41);
  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.tokenChar(46);
  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.call(this, node);
  193. }
  194. function DeclareExportAllDeclaration(node) {
  195. this.word("declare");
  196. this.space();
  197. _modules.ExportAllDeclaration.call(this, node);
  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.tokenChar(44);
  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.tokenChar(123);
  296. if (node.specifiers.length) {
  297. this.space();
  298. this.printList(node.specifiers, node);
  299. this.space();
  300. }
  301. this.tokenChar(125);
  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.tokenChar(42);
  313. }
  314. function FunctionTypeAnnotation(node, parent) {
  315. this.print(node.typeParameters, node);
  316. this.tokenChar(40);
  317. if (node.this) {
  318. this.word("this");
  319. this.tokenChar(58);
  320. this.space();
  321. this.print(node.this.typeAnnotation, node);
  322. if (node.params.length || node.rest) {
  323. this.tokenChar(44);
  324. this.space();
  325. }
  326. }
  327. this.printList(node.params, node);
  328. if (node.rest) {
  329. if (node.params.length) {
  330. this.tokenChar(44);
  331. this.space();
  332. }
  333. this.token("...");
  334. this.print(node.rest, node);
  335. }
  336. this.tokenChar(41);
  337. const type = parent == null ? void 0 : parent.type;
  338. if (type != null && (type === "ObjectTypeCallProperty" || type === "ObjectTypeInternalSlot" || type === "DeclareFunction" || type === "ObjectTypeProperty" && parent.method)) {
  339. this.tokenChar(58);
  340. } else {
  341. this.space();
  342. this.token("=>");
  343. }
  344. this.space();
  345. this.print(node.returnType, node);
  346. }
  347. function FunctionTypeParam(node) {
  348. this.print(node.name, node);
  349. if (node.optional) this.tokenChar(63);
  350. if (node.name) {
  351. this.tokenChar(58);
  352. this.space();
  353. }
  354. this.print(node.typeAnnotation, node);
  355. }
  356. function InterfaceExtends(node) {
  357. this.print(node.id, node);
  358. this.print(node.typeParameters, node, true);
  359. }
  360. function _interfaceish(node) {
  361. var _node$extends;
  362. this.print(node.id, node);
  363. this.print(node.typeParameters, node);
  364. if ((_node$extends = node.extends) != null && _node$extends.length) {
  365. this.space();
  366. this.word("extends");
  367. this.space();
  368. this.printList(node.extends, node);
  369. }
  370. if (node.type === "DeclareClass") {
  371. var _node$mixins, _node$implements;
  372. if ((_node$mixins = node.mixins) != null && _node$mixins.length) {
  373. this.space();
  374. this.word("mixins");
  375. this.space();
  376. this.printList(node.mixins, node);
  377. }
  378. if ((_node$implements = node.implements) != null && _node$implements.length) {
  379. this.space();
  380. this.word("implements");
  381. this.space();
  382. this.printList(node.implements, node);
  383. }
  384. }
  385. this.space();
  386. this.print(node.body, node);
  387. }
  388. function _variance(node) {
  389. var _node$variance;
  390. const kind = (_node$variance = node.variance) == null ? void 0 : _node$variance.kind;
  391. if (kind != null) {
  392. if (kind === "plus") {
  393. this.tokenChar(43);
  394. } else if (kind === "minus") {
  395. this.tokenChar(45);
  396. }
  397. }
  398. }
  399. function InterfaceDeclaration(node) {
  400. this.word("interface");
  401. this.space();
  402. this._interfaceish(node);
  403. }
  404. function andSeparator() {
  405. this.space();
  406. this.tokenChar(38);
  407. this.space();
  408. }
  409. function InterfaceTypeAnnotation(node) {
  410. var _node$extends2;
  411. this.word("interface");
  412. if ((_node$extends2 = node.extends) != null && _node$extends2.length) {
  413. this.space();
  414. this.word("extends");
  415. this.space();
  416. this.printList(node.extends, node);
  417. }
  418. this.space();
  419. this.print(node.body, node);
  420. }
  421. function IntersectionTypeAnnotation(node) {
  422. this.printJoin(node.types, node, {
  423. separator: andSeparator
  424. });
  425. }
  426. function MixedTypeAnnotation() {
  427. this.word("mixed");
  428. }
  429. function EmptyTypeAnnotation() {
  430. this.word("empty");
  431. }
  432. function NullableTypeAnnotation(node) {
  433. this.tokenChar(63);
  434. this.print(node.typeAnnotation, node);
  435. }
  436. function NumberTypeAnnotation() {
  437. this.word("number");
  438. }
  439. function StringTypeAnnotation() {
  440. this.word("string");
  441. }
  442. function ThisTypeAnnotation() {
  443. this.word("this");
  444. }
  445. function TupleTypeAnnotation(node) {
  446. this.tokenChar(91);
  447. this.printList(node.types, node);
  448. this.tokenChar(93);
  449. }
  450. function TypeofTypeAnnotation(node) {
  451. this.word("typeof");
  452. this.space();
  453. this.print(node.argument, node);
  454. }
  455. function TypeAlias(node) {
  456. this.word("type");
  457. this.space();
  458. this.print(node.id, node);
  459. this.print(node.typeParameters, node);
  460. this.space();
  461. this.tokenChar(61);
  462. this.space();
  463. this.print(node.right, node);
  464. this.semicolon();
  465. }
  466. function TypeAnnotation(node) {
  467. this.tokenChar(58);
  468. this.space();
  469. if (node.optional) this.tokenChar(63);
  470. this.print(node.typeAnnotation, node);
  471. }
  472. function TypeParameterInstantiation(node) {
  473. this.tokenChar(60);
  474. this.printList(node.params, node, {});
  475. this.tokenChar(62);
  476. }
  477. function TypeParameter(node) {
  478. this._variance(node);
  479. this.word(node.name);
  480. if (node.bound) {
  481. this.print(node.bound, node);
  482. }
  483. if (node.default) {
  484. this.space();
  485. this.tokenChar(61);
  486. this.space();
  487. this.print(node.default, node);
  488. }
  489. }
  490. function OpaqueType(node) {
  491. this.word("opaque");
  492. this.space();
  493. this.word("type");
  494. this.space();
  495. this.print(node.id, node);
  496. this.print(node.typeParameters, node);
  497. if (node.supertype) {
  498. this.tokenChar(58);
  499. this.space();
  500. this.print(node.supertype, node);
  501. }
  502. if (node.impltype) {
  503. this.space();
  504. this.tokenChar(61);
  505. this.space();
  506. this.print(node.impltype, node);
  507. }
  508. this.semicolon();
  509. }
  510. function ObjectTypeAnnotation(node) {
  511. if (node.exact) {
  512. this.token("{|");
  513. } else {
  514. this.tokenChar(123);
  515. }
  516. const props = [...node.properties, ...(node.callProperties || []), ...(node.indexers || []), ...(node.internalSlots || [])];
  517. if (props.length) {
  518. this.newline();
  519. this.space();
  520. this.printJoin(props, node, {
  521. addNewlines(leading) {
  522. if (leading && !props[0]) return 1;
  523. },
  524. indent: true,
  525. statement: true,
  526. iterator: () => {
  527. if (props.length !== 1 || node.inexact) {
  528. this.tokenChar(44);
  529. this.space();
  530. }
  531. }
  532. });
  533. this.space();
  534. }
  535. if (node.inexact) {
  536. this.indent();
  537. this.token("...");
  538. if (props.length) {
  539. this.newline();
  540. }
  541. this.dedent();
  542. }
  543. if (node.exact) {
  544. this.token("|}");
  545. } else {
  546. this.tokenChar(125);
  547. }
  548. }
  549. function ObjectTypeInternalSlot(node) {
  550. if (node.static) {
  551. this.word("static");
  552. this.space();
  553. }
  554. this.tokenChar(91);
  555. this.tokenChar(91);
  556. this.print(node.id, node);
  557. this.tokenChar(93);
  558. this.tokenChar(93);
  559. if (node.optional) this.tokenChar(63);
  560. if (!node.method) {
  561. this.tokenChar(58);
  562. this.space();
  563. }
  564. this.print(node.value, node);
  565. }
  566. function ObjectTypeCallProperty(node) {
  567. if (node.static) {
  568. this.word("static");
  569. this.space();
  570. }
  571. this.print(node.value, node);
  572. }
  573. function ObjectTypeIndexer(node) {
  574. if (node.static) {
  575. this.word("static");
  576. this.space();
  577. }
  578. this._variance(node);
  579. this.tokenChar(91);
  580. if (node.id) {
  581. this.print(node.id, node);
  582. this.tokenChar(58);
  583. this.space();
  584. }
  585. this.print(node.key, node);
  586. this.tokenChar(93);
  587. this.tokenChar(58);
  588. this.space();
  589. this.print(node.value, node);
  590. }
  591. function ObjectTypeProperty(node) {
  592. if (node.proto) {
  593. this.word("proto");
  594. this.space();
  595. }
  596. if (node.static) {
  597. this.word("static");
  598. this.space();
  599. }
  600. if (node.kind === "get" || node.kind === "set") {
  601. this.word(node.kind);
  602. this.space();
  603. }
  604. this._variance(node);
  605. this.print(node.key, node);
  606. if (node.optional) this.tokenChar(63);
  607. if (!node.method) {
  608. this.tokenChar(58);
  609. this.space();
  610. }
  611. this.print(node.value, node);
  612. }
  613. function ObjectTypeSpreadProperty(node) {
  614. this.token("...");
  615. this.print(node.argument, node);
  616. }
  617. function QualifiedTypeIdentifier(node) {
  618. this.print(node.qualification, node);
  619. this.tokenChar(46);
  620. this.print(node.id, node);
  621. }
  622. function SymbolTypeAnnotation() {
  623. this.word("symbol");
  624. }
  625. function orSeparator() {
  626. this.space();
  627. this.tokenChar(124);
  628. this.space();
  629. }
  630. function UnionTypeAnnotation(node) {
  631. this.printJoin(node.types, node, {
  632. separator: orSeparator
  633. });
  634. }
  635. function TypeCastExpression(node) {
  636. this.tokenChar(40);
  637. this.print(node.expression, node);
  638. this.print(node.typeAnnotation, node);
  639. this.tokenChar(41);
  640. }
  641. function Variance(node) {
  642. if (node.kind === "plus") {
  643. this.tokenChar(43);
  644. } else {
  645. this.tokenChar(45);
  646. }
  647. }
  648. function VoidTypeAnnotation() {
  649. this.word("void");
  650. }
  651. function IndexedAccessType(node) {
  652. this.print(node.objectType, node, true);
  653. this.tokenChar(91);
  654. this.print(node.indexType, node);
  655. this.tokenChar(93);
  656. }
  657. function OptionalIndexedAccessType(node) {
  658. this.print(node.objectType, node);
  659. if (node.optional) {
  660. this.token("?.");
  661. }
  662. this.tokenChar(91);
  663. this.print(node.indexType, node);
  664. this.tokenChar(93);
  665. }
  666. //# sourceMappingURL=flow.js.map