modules.js 8.4 KB

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