modules.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ExportAllDeclaration = ExportAllDeclaration;
  6. exports.ExportDefaultDeclaration = ExportDefaultDeclaration;
  7. exports.ExportDefaultSpecifier = ExportDefaultSpecifier;
  8. exports.ExportNamedDeclaration = ExportNamedDeclaration;
  9. exports.ExportNamespaceSpecifier = ExportNamespaceSpecifier;
  10. exports.ExportSpecifier = ExportSpecifier;
  11. exports.ImportAttribute = ImportAttribute;
  12. exports.ImportDeclaration = ImportDeclaration;
  13. exports.ImportDefaultSpecifier = ImportDefaultSpecifier;
  14. exports.ImportNamespaceSpecifier = ImportNamespaceSpecifier;
  15. exports.ImportSpecifier = ImportSpecifier;
  16. var _t = require("@babel/types");
  17. const {
  18. isClassDeclaration,
  19. isExportDefaultSpecifier,
  20. isExportNamespaceSpecifier,
  21. isImportDefaultSpecifier,
  22. isImportNamespaceSpecifier,
  23. isStatement
  24. } = _t;
  25. function ImportSpecifier(node) {
  26. if (node.importKind === "type" || node.importKind === "typeof") {
  27. this.word(node.importKind);
  28. this.space();
  29. }
  30. this.print(node.imported, node);
  31. if (node.local && node.local.name !== node.imported.name) {
  32. this.space();
  33. this.word("as");
  34. this.space();
  35. this.print(node.local, node);
  36. }
  37. }
  38. function ImportDefaultSpecifier(node) {
  39. this.print(node.local, node);
  40. }
  41. function ExportDefaultSpecifier(node) {
  42. this.print(node.exported, node);
  43. }
  44. function ExportSpecifier(node) {
  45. if (node.exportKind === "type") {
  46. this.word("type");
  47. this.space();
  48. }
  49. this.print(node.local, node);
  50. if (node.exported && node.local.name !== node.exported.name) {
  51. this.space();
  52. this.word("as");
  53. this.space();
  54. this.print(node.exported, node);
  55. }
  56. }
  57. function ExportNamespaceSpecifier(node) {
  58. this.token("*");
  59. this.space();
  60. this.word("as");
  61. this.space();
  62. this.print(node.exported, node);
  63. }
  64. function ExportAllDeclaration(node) {
  65. this.word("export");
  66. this.space();
  67. if (node.exportKind === "type") {
  68. this.word("type");
  69. this.space();
  70. }
  71. this.token("*");
  72. this.space();
  73. this.word("from");
  74. this.space();
  75. this.print(node.source, node);
  76. this.printAssertions(node);
  77. this.semicolon();
  78. }
  79. function ExportNamedDeclaration(node) {
  80. if (this.format.decoratorsBeforeExport && isClassDeclaration(node.declaration)) {
  81. this.printJoin(node.declaration.decorators, node);
  82. }
  83. this.word("export");
  84. this.space();
  85. ExportDeclaration.apply(this, arguments);
  86. }
  87. function ExportDefaultDeclaration(node) {
  88. if (this.format.decoratorsBeforeExport && isClassDeclaration(node.declaration)) {
  89. this.printJoin(node.declaration.decorators, node);
  90. }
  91. this.word("export");
  92. this.space();
  93. this.word("default");
  94. this.space();
  95. ExportDeclaration.apply(this, arguments);
  96. }
  97. function ExportDeclaration(node) {
  98. if (node.declaration) {
  99. const declar = node.declaration;
  100. this.print(declar, node);
  101. if (!isStatement(declar)) this.semicolon();
  102. } else {
  103. if (node.exportKind === "type") {
  104. this.word("type");
  105. this.space();
  106. }
  107. const specifiers = node.specifiers.slice(0);
  108. let hasSpecial = false;
  109. for (;;) {
  110. const first = specifiers[0];
  111. if (isExportDefaultSpecifier(first) || isExportNamespaceSpecifier(first)) {
  112. hasSpecial = true;
  113. this.print(specifiers.shift(), node);
  114. if (specifiers.length) {
  115. this.token(",");
  116. this.space();
  117. }
  118. } else {
  119. break;
  120. }
  121. }
  122. if (specifiers.length || !specifiers.length && !hasSpecial) {
  123. this.token("{");
  124. if (specifiers.length) {
  125. this.space();
  126. this.printList(specifiers, node);
  127. this.space();
  128. }
  129. this.token("}");
  130. }
  131. if (node.source) {
  132. this.space();
  133. this.word("from");
  134. this.space();
  135. this.print(node.source, node);
  136. this.printAssertions(node);
  137. }
  138. this.semicolon();
  139. }
  140. }
  141. function ImportDeclaration(node) {
  142. this.word("import");
  143. this.space();
  144. const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
  145. if (isTypeKind) {
  146. this.word(node.importKind);
  147. this.space();
  148. }
  149. const specifiers = node.specifiers.slice(0);
  150. const hasSpecifiers = !!specifiers.length;
  151. while (hasSpecifiers) {
  152. const first = specifiers[0];
  153. if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) {
  154. this.print(specifiers.shift(), node);
  155. if (specifiers.length) {
  156. this.token(",");
  157. this.space();
  158. }
  159. } else {
  160. break;
  161. }
  162. }
  163. if (specifiers.length) {
  164. this.token("{");
  165. this.space();
  166. this.printList(specifiers, node);
  167. this.space();
  168. this.token("}");
  169. } else if (isTypeKind && !hasSpecifiers) {
  170. this.token("{");
  171. this.token("}");
  172. }
  173. if (hasSpecifiers || isTypeKind) {
  174. this.space();
  175. this.word("from");
  176. this.space();
  177. }
  178. this.print(node.source, node);
  179. this.printAssertions(node);
  180. {
  181. var _node$attributes;
  182. if ((_node$attributes = node.attributes) != null && _node$attributes.length) {
  183. this.space();
  184. this.word("with");
  185. this.space();
  186. this.printList(node.attributes, node);
  187. }
  188. }
  189. this.semicolon();
  190. }
  191. function ImportAttribute(node) {
  192. this.print(node.key);
  193. this.token(":");
  194. this.space();
  195. this.print(node.value);
  196. }
  197. function ImportNamespaceSpecifier(node) {
  198. this.token("*");
  199. this.space();
  200. this.word("as");
  201. this.space();
  202. this.print(node.local, node);
  203. }