modules.js 8.0 KB

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