modules.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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, {
  97. printTrailingSeparator: this.shouldPrintTrailingComma("}")
  98. });
  99. this.space();
  100. this.token("}", null, occurrenceCount);
  101. }
  102. function ExportAllDeclaration(node) {
  103. var _node$attributes, _node$assertions;
  104. this.word("export");
  105. this.space();
  106. if (node.exportKind === "type") {
  107. this.word("type");
  108. this.space();
  109. }
  110. this.tokenChar(42);
  111. this.space();
  112. this.word("from");
  113. this.space();
  114. if ((_node$attributes = node.attributes) != null && _node$attributes.length || (_node$assertions = node.assertions) != null && _node$assertions.length) {
  115. this.print(node.source, true);
  116. this.space();
  117. this._printAttributes(node, false);
  118. } else {
  119. this.print(node.source);
  120. }
  121. this.semicolon();
  122. }
  123. function maybePrintDecoratorsBeforeExport(printer, node) {
  124. if (isClassDeclaration(node.declaration) && printer._shouldPrintDecoratorsBeforeExport(node)) {
  125. printer.printJoin(node.declaration.decorators);
  126. }
  127. }
  128. function ExportNamedDeclaration(node) {
  129. maybePrintDecoratorsBeforeExport(this, node);
  130. this.word("export");
  131. this.space();
  132. if (node.declaration) {
  133. const declar = node.declaration;
  134. this.print(declar);
  135. if (!isStatement(declar)) this.semicolon();
  136. } else {
  137. if (node.exportKind === "type") {
  138. this.word("type");
  139. this.space();
  140. }
  141. const specifiers = node.specifiers.slice(0);
  142. let hasSpecial = false;
  143. for (;;) {
  144. const first = specifiers[0];
  145. if (isExportDefaultSpecifier(first) || isExportNamespaceSpecifier(first)) {
  146. hasSpecial = true;
  147. this.print(specifiers.shift());
  148. if (specifiers.length) {
  149. this.tokenChar(44);
  150. this.space();
  151. }
  152. } else {
  153. break;
  154. }
  155. }
  156. let hasBrace = false;
  157. if (specifiers.length || !specifiers.length && !hasSpecial) {
  158. hasBrace = true;
  159. this.tokenChar(123);
  160. if (specifiers.length) {
  161. this.space();
  162. this.printList(specifiers, {
  163. printTrailingSeparator: this.shouldPrintTrailingComma("}")
  164. });
  165. this.space();
  166. }
  167. this.tokenChar(125);
  168. }
  169. if (node.source) {
  170. var _node$attributes2, _node$assertions2;
  171. this.space();
  172. this.word("from");
  173. this.space();
  174. if ((_node$attributes2 = node.attributes) != null && _node$attributes2.length || (_node$assertions2 = node.assertions) != null && _node$assertions2.length) {
  175. this.print(node.source, true);
  176. this.space();
  177. this._printAttributes(node, hasBrace);
  178. } else {
  179. this.print(node.source);
  180. }
  181. }
  182. this.semicolon();
  183. }
  184. }
  185. function ExportDefaultDeclaration(node) {
  186. maybePrintDecoratorsBeforeExport(this, node);
  187. this.word("export");
  188. this.noIndentInnerCommentsHere();
  189. this.space();
  190. this.word("default");
  191. this.space();
  192. this.tokenContext |= _index.TokenContext.exportDefault;
  193. const declar = node.declaration;
  194. this.print(declar);
  195. if (!isStatement(declar)) this.semicolon();
  196. }
  197. function ImportDeclaration(node) {
  198. var _node$attributes3, _node$assertions3;
  199. this.word("import");
  200. this.space();
  201. const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
  202. if (isTypeKind) {
  203. this.noIndentInnerCommentsHere();
  204. this.word(node.importKind);
  205. this.space();
  206. } else if (node.module) {
  207. this.noIndentInnerCommentsHere();
  208. this.word("module");
  209. this.space();
  210. } else if (node.phase) {
  211. this.noIndentInnerCommentsHere();
  212. this.word(node.phase);
  213. this.space();
  214. }
  215. const specifiers = node.specifiers.slice(0);
  216. const hasSpecifiers = !!specifiers.length;
  217. while (hasSpecifiers) {
  218. const first = specifiers[0];
  219. if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) {
  220. this.print(specifiers.shift());
  221. if (specifiers.length) {
  222. this.tokenChar(44);
  223. this.space();
  224. }
  225. } else {
  226. break;
  227. }
  228. }
  229. let hasBrace = false;
  230. if (specifiers.length) {
  231. hasBrace = true;
  232. this.tokenChar(123);
  233. this.space();
  234. this.printList(specifiers, {
  235. printTrailingSeparator: this.shouldPrintTrailingComma("}")
  236. });
  237. this.space();
  238. this.tokenChar(125);
  239. } else if (isTypeKind && !hasSpecifiers) {
  240. hasBrace = true;
  241. this.tokenChar(123);
  242. this.tokenChar(125);
  243. }
  244. if (hasSpecifiers || isTypeKind) {
  245. this.space();
  246. this.word("from");
  247. this.space();
  248. }
  249. if ((_node$attributes3 = node.attributes) != null && _node$attributes3.length || (_node$assertions3 = node.assertions) != null && _node$assertions3.length) {
  250. this.print(node.source, true);
  251. this.space();
  252. this._printAttributes(node, hasBrace);
  253. } else {
  254. this.print(node.source);
  255. }
  256. this.semicolon();
  257. }
  258. function ImportAttribute(node) {
  259. this.print(node.key);
  260. this.tokenChar(58);
  261. this.space();
  262. this.print(node.value);
  263. }
  264. function ImportNamespaceSpecifier(node) {
  265. this.tokenChar(42);
  266. this.space();
  267. this.word("as");
  268. this.space();
  269. this.print(node.local);
  270. }
  271. function ImportExpression(node) {
  272. this.word("import");
  273. if (node.phase) {
  274. this.tokenChar(46);
  275. this.word(node.phase);
  276. }
  277. this.tokenChar(40);
  278. this.print(node.source);
  279. if (node.options != null) {
  280. this.tokenChar(44);
  281. this.space();
  282. this.print(node.options);
  283. }
  284. this.tokenChar(41);
  285. }
  286. //# sourceMappingURL=modules.js.map