modules.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.ImportExpression = ImportExpression;
  15. exports.ImportNamespaceSpecifier = ImportNamespaceSpecifier;
  16. exports.ImportSpecifier = ImportSpecifier;
  17. exports._printAttributes = _printAttributes;
  18. var _t = require("@babel/types");
  19. const {
  20. isClassDeclaration,
  21. isExportDefaultSpecifier,
  22. isExportNamespaceSpecifier,
  23. isImportDefaultSpecifier,
  24. isImportNamespaceSpecifier,
  25. isStatement
  26. } = _t;
  27. function ImportSpecifier(node) {
  28. if (node.importKind === "type" || node.importKind === "typeof") {
  29. this.word(node.importKind);
  30. this.space();
  31. }
  32. this.print(node.imported, node);
  33. if (node.local && node.local.name !== node.imported.name) {
  34. this.space();
  35. this.word("as");
  36. this.space();
  37. this.print(node.local, node);
  38. }
  39. }
  40. function ImportDefaultSpecifier(node) {
  41. this.print(node.local, node);
  42. }
  43. function ExportDefaultSpecifier(node) {
  44. this.print(node.exported, node);
  45. }
  46. function ExportSpecifier(node) {
  47. if (node.exportKind === "type") {
  48. this.word("type");
  49. this.space();
  50. }
  51. this.print(node.local, node);
  52. if (node.exported && node.local.name !== node.exported.name) {
  53. this.space();
  54. this.word("as");
  55. this.space();
  56. this.print(node.exported, node);
  57. }
  58. }
  59. function ExportNamespaceSpecifier(node) {
  60. this.tokenChar(42);
  61. this.space();
  62. this.word("as");
  63. this.space();
  64. this.print(node.exported, node);
  65. }
  66. let warningShown = false;
  67. function _printAttributes(node) {
  68. const {
  69. importAttributesKeyword
  70. } = this.format;
  71. const {
  72. attributes,
  73. assertions
  74. } = node;
  75. if (attributes && !importAttributesKeyword && !warningShown) {
  76. warningShown = true;
  77. console.warn(`\
  78. You are using import attributes, without specifying the desired output syntax.
  79. Please specify the "importAttributesKeyword" generator option, whose value can be one of:
  80. - "with" : \`import { a } from "b" with { type: "json" };\`
  81. - "assert" : \`import { a } from "b" assert { type: "json" };\`
  82. - "with-legacy" : \`import { a } from "b" with type: "json";\`
  83. `);
  84. }
  85. const useAssertKeyword = importAttributesKeyword === "assert" || !importAttributesKeyword && assertions;
  86. this.word(useAssertKeyword ? "assert" : "with");
  87. this.space();
  88. if (!useAssertKeyword && importAttributesKeyword !== "with") {
  89. this.printList(attributes || assertions, node);
  90. return;
  91. }
  92. this.tokenChar(123);
  93. this.space();
  94. this.printList(attributes || assertions, node);
  95. this.space();
  96. this.tokenChar(125);
  97. }
  98. function ExportAllDeclaration(node) {
  99. var _node$attributes, _node$assertions;
  100. this.word("export");
  101. this.space();
  102. if (node.exportKind === "type") {
  103. this.word("type");
  104. this.space();
  105. }
  106. this.tokenChar(42);
  107. this.space();
  108. this.word("from");
  109. this.space();
  110. if ((_node$attributes = node.attributes) != null && _node$attributes.length || (_node$assertions = node.assertions) != null && _node$assertions.length) {
  111. this.print(node.source, node, true);
  112. this.space();
  113. this._printAttributes(node);
  114. } else {
  115. this.print(node.source, node);
  116. }
  117. this.semicolon();
  118. }
  119. function maybePrintDecoratorsBeforeExport(printer, node) {
  120. if (isClassDeclaration(node.declaration) && printer._shouldPrintDecoratorsBeforeExport(node)) {
  121. printer.printJoin(node.declaration.decorators, node);
  122. }
  123. }
  124. function ExportNamedDeclaration(node) {
  125. maybePrintDecoratorsBeforeExport(this, node);
  126. this.word("export");
  127. this.space();
  128. if (node.declaration) {
  129. const declar = node.declaration;
  130. this.print(declar, node);
  131. if (!isStatement(declar)) this.semicolon();
  132. } else {
  133. if (node.exportKind === "type") {
  134. this.word("type");
  135. this.space();
  136. }
  137. const specifiers = node.specifiers.slice(0);
  138. let hasSpecial = false;
  139. for (;;) {
  140. const first = specifiers[0];
  141. if (isExportDefaultSpecifier(first) || isExportNamespaceSpecifier(first)) {
  142. hasSpecial = true;
  143. this.print(specifiers.shift(), node);
  144. if (specifiers.length) {
  145. this.tokenChar(44);
  146. this.space();
  147. }
  148. } else {
  149. break;
  150. }
  151. }
  152. if (specifiers.length || !specifiers.length && !hasSpecial) {
  153. this.tokenChar(123);
  154. if (specifiers.length) {
  155. this.space();
  156. this.printList(specifiers, node);
  157. this.space();
  158. }
  159. this.tokenChar(125);
  160. }
  161. if (node.source) {
  162. var _node$attributes2, _node$assertions2;
  163. this.space();
  164. this.word("from");
  165. this.space();
  166. if ((_node$attributes2 = node.attributes) != null && _node$attributes2.length || (_node$assertions2 = node.assertions) != null && _node$assertions2.length) {
  167. this.print(node.source, node, true);
  168. this.space();
  169. this._printAttributes(node);
  170. } else {
  171. this.print(node.source, node);
  172. }
  173. }
  174. this.semicolon();
  175. }
  176. }
  177. function ExportDefaultDeclaration(node) {
  178. maybePrintDecoratorsBeforeExport(this, node);
  179. this.word("export");
  180. this.noIndentInnerCommentsHere();
  181. this.space();
  182. this.word("default");
  183. this.space();
  184. const declar = node.declaration;
  185. this.print(declar, node);
  186. if (!isStatement(declar)) this.semicolon();
  187. }
  188. function ImportDeclaration(node) {
  189. var _node$attributes3, _node$assertions3;
  190. this.word("import");
  191. this.space();
  192. const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
  193. if (isTypeKind) {
  194. this.noIndentInnerCommentsHere();
  195. this.word(node.importKind);
  196. this.space();
  197. } else if (node.module) {
  198. this.noIndentInnerCommentsHere();
  199. this.word("module");
  200. this.space();
  201. } else if (node.phase) {
  202. this.noIndentInnerCommentsHere();
  203. this.word(node.phase);
  204. this.space();
  205. }
  206. const specifiers = node.specifiers.slice(0);
  207. const hasSpecifiers = !!specifiers.length;
  208. while (hasSpecifiers) {
  209. const first = specifiers[0];
  210. if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) {
  211. this.print(specifiers.shift(), node);
  212. if (specifiers.length) {
  213. this.tokenChar(44);
  214. this.space();
  215. }
  216. } else {
  217. break;
  218. }
  219. }
  220. if (specifiers.length) {
  221. this.tokenChar(123);
  222. this.space();
  223. this.printList(specifiers, node);
  224. this.space();
  225. this.tokenChar(125);
  226. } else if (isTypeKind && !hasSpecifiers) {
  227. this.tokenChar(123);
  228. this.tokenChar(125);
  229. }
  230. if (hasSpecifiers || isTypeKind) {
  231. this.space();
  232. this.word("from");
  233. this.space();
  234. }
  235. if ((_node$attributes3 = node.attributes) != null && _node$attributes3.length || (_node$assertions3 = node.assertions) != null && _node$assertions3.length) {
  236. this.print(node.source, node, true);
  237. this.space();
  238. this._printAttributes(node);
  239. } else {
  240. this.print(node.source, node);
  241. }
  242. this.semicolon();
  243. }
  244. function ImportAttribute(node) {
  245. this.print(node.key);
  246. this.tokenChar(58);
  247. this.space();
  248. this.print(node.value);
  249. }
  250. function ImportNamespaceSpecifier(node) {
  251. this.tokenChar(42);
  252. this.space();
  253. this.word("as");
  254. this.space();
  255. this.print(node.local, node);
  256. }
  257. function ImportExpression(node) {
  258. this.word("import");
  259. if (node.phase) {
  260. this.tokenChar(46);
  261. this.word(node.phase);
  262. }
  263. this.tokenChar(40);
  264. this.print(node.source, node);
  265. if (node.options != null) {
  266. this.tokenChar(44);
  267. this.space();
  268. this.print(node.options, node);
  269. }
  270. this.tokenChar(41);
  271. }
  272. //# sourceMappingURL=modules.js.map