modules.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. {
  81. if (this.format.decoratorsBeforeExport && isClassDeclaration(node.declaration)) {
  82. this.printJoin(node.declaration.decorators, node);
  83. }
  84. }
  85. this.word("export");
  86. this.space();
  87. if (node.declaration) {
  88. const declar = node.declaration;
  89. this.print(declar, node);
  90. if (!isStatement(declar)) this.semicolon();
  91. } else {
  92. if (node.exportKind === "type") {
  93. this.word("type");
  94. this.space();
  95. }
  96. const specifiers = node.specifiers.slice(0);
  97. let hasSpecial = false;
  98. for (;;) {
  99. const first = specifiers[0];
  100. if (isExportDefaultSpecifier(first) || isExportNamespaceSpecifier(first)) {
  101. hasSpecial = true;
  102. this.print(specifiers.shift(), node);
  103. if (specifiers.length) {
  104. this.token(",");
  105. this.space();
  106. }
  107. } else {
  108. break;
  109. }
  110. }
  111. if (specifiers.length || !specifiers.length && !hasSpecial) {
  112. this.token("{");
  113. if (specifiers.length) {
  114. this.space();
  115. this.printList(specifiers, node);
  116. this.space();
  117. }
  118. this.token("}");
  119. }
  120. if (node.source) {
  121. this.space();
  122. this.word("from");
  123. this.space();
  124. this.print(node.source, node);
  125. this.printAssertions(node);
  126. }
  127. this.semicolon();
  128. }
  129. }
  130. function ExportDefaultDeclaration(node) {
  131. {
  132. if (this.format.decoratorsBeforeExport && isClassDeclaration(node.declaration)) {
  133. this.printJoin(node.declaration.decorators, node);
  134. }
  135. }
  136. this.word("export");
  137. this.space();
  138. this.word("default");
  139. this.space();
  140. const declar = node.declaration;
  141. this.print(declar, node);
  142. if (!isStatement(declar)) this.semicolon();
  143. }
  144. function ImportDeclaration(node) {
  145. this.word("import");
  146. this.space();
  147. const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
  148. if (isTypeKind) {
  149. this.word(node.importKind);
  150. this.space();
  151. }
  152. const specifiers = node.specifiers.slice(0);
  153. const hasSpecifiers = !!specifiers.length;
  154. while (hasSpecifiers) {
  155. const first = specifiers[0];
  156. if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) {
  157. this.print(specifiers.shift(), node);
  158. if (specifiers.length) {
  159. this.token(",");
  160. this.space();
  161. }
  162. } else {
  163. break;
  164. }
  165. }
  166. if (specifiers.length) {
  167. this.token("{");
  168. this.space();
  169. this.printList(specifiers, node);
  170. this.space();
  171. this.token("}");
  172. } else if (isTypeKind && !hasSpecifiers) {
  173. this.token("{");
  174. this.token("}");
  175. }
  176. if (hasSpecifiers || isTypeKind) {
  177. this.space();
  178. this.word("from");
  179. this.space();
  180. }
  181. this.print(node.source, node);
  182. this.printAssertions(node);
  183. {
  184. var _node$attributes;
  185. if ((_node$attributes = node.attributes) != null && _node$attributes.length) {
  186. this.space();
  187. this.word("with");
  188. this.space();
  189. this.printList(node.attributes, node);
  190. }
  191. }
  192. this.semicolon();
  193. }
  194. function ImportAttribute(node) {
  195. this.print(node.key);
  196. this.token(":");
  197. this.space();
  198. this.print(node.value);
  199. }
  200. function ImportNamespaceSpecifier(node) {
  201. this.token("*");
  202. this.space();
  203. this.word("as");
  204. this.space();
  205. this.print(node.local, node);
  206. }