modules.js 5.1 KB

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