flow.js 16 KB

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