modules.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. var _expressions = require("./expressions.js");
  21. const {
  22. isClassDeclaration,
  23. isExportDefaultSpecifier,
  24. isExportNamespaceSpecifier,
  25. isImportDefaultSpecifier,
  26. isImportNamespaceSpecifier,
  27. isStatement
  28. } = _t;
  29. function ImportSpecifier(node) {
  30. if (node.importKind === "type" || node.importKind === "typeof") {
  31. this.word(node.importKind);
  32. this.space();
  33. }
  34. this.print(node.imported);
  35. if (node.local && node.local.name !== node.imported.name) {
  36. this.space();
  37. this.word("as");
  38. this.space();
  39. this.print(node.local);
  40. }
  41. }
  42. function ImportDefaultSpecifier(node) {
  43. this.print(node.local);
  44. }
  45. function ExportDefaultSpecifier(node) {
  46. this.print(node.exported);
  47. }
  48. function ExportSpecifier(node) {
  49. if (node.exportKind === "type") {
  50. this.word("type");
  51. this.space();
  52. }
  53. this.print(node.local);
  54. if (node.exported && node.local.name !== node.exported.name) {
  55. this.space();
  56. this.word("as");
  57. this.space();
  58. this.print(node.exported);
  59. }
  60. }
  61. function ExportNamespaceSpecifier(node) {
  62. this.tokenChar(42);
  63. this.space();
  64. this.word("as");
  65. this.space();
  66. this.print(node.exported);
  67. }
  68. let warningShown = false;
  69. function _printAttributes(node, hasPreviousBrace) {
  70. var _node$extra;
  71. const {
  72. attributes
  73. } = node;
  74. var {
  75. assertions
  76. } = node;
  77. const {
  78. importAttributesKeyword
  79. } = this.format;
  80. if (attributes && !importAttributesKeyword && node.extra && (node.extra.deprecatedAssertSyntax || node.extra.deprecatedWithLegacySyntax) && !warningShown) {
  81. warningShown = true;
  82. console.warn(`\
  83. You are using import attributes, without specifying the desired output syntax.
  84. Please specify the "importAttributesKeyword" generator option, whose value can be one of:
  85. - "with" : \`import { a } from "b" with { type: "json" };\`
  86. - "assert" : \`import { a } from "b" assert { type: "json" };\`
  87. - "with-legacy" : \`import { a } from "b" with type: "json";\`
  88. `);
  89. }
  90. const useAssertKeyword = importAttributesKeyword === "assert" || !importAttributesKeyword && assertions;
  91. this.word(useAssertKeyword ? "assert" : "with");
  92. this.space();
  93. if (!useAssertKeyword && (importAttributesKeyword === "with-legacy" || !importAttributesKeyword && (_node$extra = node.extra) != null && _node$extra.deprecatedWithLegacySyntax)) {
  94. this.printList(attributes || assertions);
  95. return;
  96. }
  97. const occurrenceCount = hasPreviousBrace ? 1 : 0;
  98. this.token("{", undefined, occurrenceCount);
  99. this.space();
  100. this.printList(attributes || assertions, this.shouldPrintTrailingComma("}"));
  101. this.space();
  102. this.token("}", undefined, occurrenceCount);
  103. }
  104. function ExportAllDeclaration(node) {
  105. var _node$attributes, _node$assertions;
  106. this.word("export");
  107. this.space();
  108. if (node.exportKind === "type") {
  109. this.word("type");
  110. this.space();
  111. }
  112. this.tokenChar(42);
  113. this.space();
  114. this.word("from");
  115. this.space();
  116. if ((_node$attributes = node.attributes) != null && _node$attributes.length || (_node$assertions = node.assertions) != null && _node$assertions.length) {
  117. this.print(node.source, true);
  118. this.space();
  119. _printAttributes.call(this, node, false);
  120. } else {
  121. this.print(node.source);
  122. }
  123. this.semicolon();
  124. }
  125. function maybePrintDecoratorsBeforeExport(printer, node) {
  126. if (isClassDeclaration(node.declaration) && _expressions._shouldPrintDecoratorsBeforeExport.call(printer, node)) {
  127. printer.printJoin(node.declaration.decorators);
  128. }
  129. }
  130. function ExportNamedDeclaration(node) {
  131. maybePrintDecoratorsBeforeExport(this, node);
  132. this.word("export");
  133. this.space();
  134. if (node.declaration) {
  135. const declar = node.declaration;
  136. this.print(declar);
  137. if (!isStatement(declar)) this.semicolon();
  138. } else {
  139. if (node.exportKind === "type") {
  140. this.word("type");
  141. this.space();
  142. }
  143. const specifiers = node.specifiers.slice(0);
  144. let hasSpecial = false;
  145. for (;;) {
  146. const first = specifiers[0];
  147. if (isExportDefaultSpecifier(first) || isExportNamespaceSpecifier(first)) {
  148. hasSpecial = true;
  149. this.print(specifiers.shift());
  150. if (specifiers.length) {
  151. this.tokenChar(44);
  152. this.space();
  153. }
  154. } else {
  155. break;
  156. }
  157. }
  158. let hasBrace = false;
  159. if (specifiers.length || !specifiers.length && !hasSpecial) {
  160. hasBrace = true;
  161. this.tokenChar(123);
  162. if (specifiers.length) {
  163. this.space();
  164. this.printList(specifiers, this.shouldPrintTrailingComma("}"));
  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. _printAttributes.call(this, 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, this.shouldPrintTrailingComma("}"));
  235. this.space();
  236. this.tokenChar(125);
  237. } else if (isTypeKind && !hasSpecifiers) {
  238. hasBrace = true;
  239. this.tokenChar(123);
  240. this.tokenChar(125);
  241. }
  242. if (hasSpecifiers || isTypeKind) {
  243. this.space();
  244. this.word("from");
  245. this.space();
  246. }
  247. if ((_node$attributes3 = node.attributes) != null && _node$attributes3.length || (_node$assertions3 = node.assertions) != null && _node$assertions3.length) {
  248. this.print(node.source, true);
  249. this.space();
  250. _printAttributes.call(this, node, hasBrace);
  251. } else {
  252. this.print(node.source);
  253. }
  254. this.semicolon();
  255. }
  256. function ImportAttribute(node) {
  257. this.print(node.key);
  258. this.tokenChar(58);
  259. this.space();
  260. this.print(node.value);
  261. }
  262. function ImportNamespaceSpecifier(node) {
  263. this.tokenChar(42);
  264. this.space();
  265. this.word("as");
  266. this.space();
  267. this.print(node.local);
  268. }
  269. function ImportExpression(node) {
  270. this.word("import");
  271. if (node.phase) {
  272. this.tokenChar(46);
  273. this.word(node.phase);
  274. }
  275. this.tokenChar(40);
  276. const shouldPrintTrailingComma = this.shouldPrintTrailingComma(")");
  277. this.print(node.source);
  278. if (node.options != null) {
  279. this.tokenChar(44);
  280. this.space();
  281. this.print(node.options);
  282. }
  283. if (shouldPrintTrailingComma) {
  284. this.tokenChar(44);
  285. }
  286. this.rightParens(node);
  287. }
  288. //# sourceMappingURL=modules.js.map