modules.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.ImportSpecifier = ImportSpecifier;
  4. exports.ImportDefaultSpecifier = ImportDefaultSpecifier;
  5. exports.ExportDefaultSpecifier = ExportDefaultSpecifier;
  6. exports.ExportSpecifier = ExportSpecifier;
  7. exports.ExportNamespaceSpecifier = ExportNamespaceSpecifier;
  8. exports.ExportAllDeclaration = ExportAllDeclaration;
  9. exports.ExportNamedDeclaration = ExportNamedDeclaration;
  10. exports.ExportDefaultDeclaration = ExportDefaultDeclaration;
  11. exports.ImportDeclaration = ImportDeclaration;
  12. exports.ImportNamespaceSpecifier = ImportNamespaceSpecifier;
  13. var _babelTypes = require("babel-types");
  14. var t = _interopRequireWildcard(_babelTypes);
  15. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  16. function ImportSpecifier(node) {
  17. if (node.importKind === "type" || node.importKind === "typeof") {
  18. this.word(node.importKind);
  19. this.space();
  20. }
  21. this.print(node.imported, node);
  22. if (node.local && node.local.name !== node.imported.name) {
  23. this.space();
  24. this.word("as");
  25. this.space();
  26. this.print(node.local, node);
  27. }
  28. }
  29. function ImportDefaultSpecifier(node) {
  30. this.print(node.local, node);
  31. }
  32. function ExportDefaultSpecifier(node) {
  33. this.print(node.exported, node);
  34. }
  35. function ExportSpecifier(node) {
  36. this.print(node.local, node);
  37. if (node.exported && node.local.name !== node.exported.name) {
  38. this.space();
  39. this.word("as");
  40. this.space();
  41. this.print(node.exported, node);
  42. }
  43. }
  44. function ExportNamespaceSpecifier(node) {
  45. this.token("*");
  46. this.space();
  47. this.word("as");
  48. this.space();
  49. this.print(node.exported, node);
  50. }
  51. function ExportAllDeclaration(node) {
  52. this.word("export");
  53. this.space();
  54. this.token("*");
  55. this.space();
  56. this.word("from");
  57. this.space();
  58. this.print(node.source, node);
  59. this.semicolon();
  60. }
  61. function ExportNamedDeclaration() {
  62. this.word("export");
  63. this.space();
  64. ExportDeclaration.apply(this, arguments);
  65. }
  66. function ExportDefaultDeclaration() {
  67. this.word("export");
  68. this.space();
  69. this.word("default");
  70. this.space();
  71. ExportDeclaration.apply(this, arguments);
  72. }
  73. function ExportDeclaration(node) {
  74. if (node.declaration) {
  75. var declar = node.declaration;
  76. this.print(declar, node);
  77. if (!t.isStatement(declar)) this.semicolon();
  78. } else {
  79. if (node.exportKind === "type") {
  80. this.word("type");
  81. this.space();
  82. }
  83. var specifiers = node.specifiers.slice(0);
  84. var hasSpecial = false;
  85. while (true) {
  86. var first = specifiers[0];
  87. if (t.isExportDefaultSpecifier(first) || t.isExportNamespaceSpecifier(first)) {
  88. hasSpecial = true;
  89. this.print(specifiers.shift(), node);
  90. if (specifiers.length) {
  91. this.token(",");
  92. this.space();
  93. }
  94. } else {
  95. break;
  96. }
  97. }
  98. if (specifiers.length || !specifiers.length && !hasSpecial) {
  99. this.token("{");
  100. if (specifiers.length) {
  101. this.space();
  102. this.printList(specifiers, node);
  103. this.space();
  104. }
  105. this.token("}");
  106. }
  107. if (node.source) {
  108. this.space();
  109. this.word("from");
  110. this.space();
  111. this.print(node.source, node);
  112. }
  113. this.semicolon();
  114. }
  115. }
  116. function ImportDeclaration(node) {
  117. this.word("import");
  118. this.space();
  119. if (node.importKind === "type" || node.importKind === "typeof") {
  120. this.word(node.importKind);
  121. this.space();
  122. }
  123. var specifiers = node.specifiers.slice(0);
  124. if (specifiers && specifiers.length) {
  125. while (true) {
  126. var first = specifiers[0];
  127. if (t.isImportDefaultSpecifier(first) || t.isImportNamespaceSpecifier(first)) {
  128. this.print(specifiers.shift(), node);
  129. if (specifiers.length) {
  130. this.token(",");
  131. this.space();
  132. }
  133. } else {
  134. break;
  135. }
  136. }
  137. if (specifiers.length) {
  138. this.token("{");
  139. this.space();
  140. this.printList(specifiers, node);
  141. this.space();
  142. this.token("}");
  143. }
  144. this.space();
  145. this.word("from");
  146. this.space();
  147. }
  148. this.print(node.source, node);
  149. this.semicolon();
  150. }
  151. function ImportNamespaceSpecifier(node) {
  152. this.token("*");
  153. this.space();
  154. this.word("as");
  155. this.space();
  156. this.print(node.local, node);
  157. }