modules.js 8.3 KB

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