You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

742 lines
16 KiB

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