modules.js 7.7 KB

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