flow.js 16 KB

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