index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.generate = generate;
  7. var _sourceMap = require("./source-map.js");
  8. var _printer = require("./printer.js");
  9. function normalizeOptions(code, opts, ast) {
  10. var _opts$recordAndTupleS;
  11. if (opts.experimental_preserveFormat) {
  12. if (typeof code !== "string") {
  13. throw new Error("`experimental_preserveFormat` requires the original `code` to be passed to @babel/generator as a string");
  14. }
  15. if (!opts.retainLines) {
  16. throw new Error("`experimental_preserveFormat` requires `retainLines` to be set to `true`");
  17. }
  18. if (opts.compact && opts.compact !== "auto") {
  19. throw new Error("`experimental_preserveFormat` is not compatible with the `compact` option");
  20. }
  21. if (opts.minified) {
  22. throw new Error("`experimental_preserveFormat` is not compatible with the `minified` option");
  23. }
  24. if (opts.jsescOption) {
  25. throw new Error("`experimental_preserveFormat` is not compatible with the `jsescOption` option");
  26. }
  27. if (!Array.isArray(ast.tokens)) {
  28. throw new Error("`experimental_preserveFormat` requires the AST to have attached the token of the input code. Make sure to enable the `tokens: true` parser option.");
  29. }
  30. }
  31. const format = {
  32. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  33. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  34. shouldPrintComment: opts.shouldPrintComment,
  35. preserveFormat: opts.experimental_preserveFormat,
  36. retainLines: opts.retainLines,
  37. retainFunctionParens: opts.retainFunctionParens,
  38. comments: opts.comments == null || opts.comments,
  39. compact: opts.compact,
  40. minified: opts.minified,
  41. concise: opts.concise,
  42. indent: {
  43. adjustMultilineComment: true,
  44. style: " "
  45. },
  46. jsescOption: Object.assign({
  47. quotes: "double",
  48. wrap: true,
  49. minimal: false
  50. }, opts.jsescOption),
  51. topicToken: opts.topicToken
  52. };
  53. format.decoratorsBeforeExport = opts.decoratorsBeforeExport;
  54. format.jsescOption.json = opts.jsonCompatibleStrings;
  55. format.recordAndTupleSyntaxType = (_opts$recordAndTupleS = opts.recordAndTupleSyntaxType) != null ? _opts$recordAndTupleS : "hash";
  56. format.importAttributesKeyword = opts.importAttributesKeyword;
  57. if (format.minified) {
  58. format.compact = true;
  59. format.shouldPrintComment = format.shouldPrintComment || (() => format.comments);
  60. } else {
  61. format.shouldPrintComment = format.shouldPrintComment || (value => format.comments || value.includes("@license") || value.includes("@preserve"));
  62. }
  63. if (format.compact === "auto") {
  64. format.compact = typeof code === "string" && code.length > 500000;
  65. if (format.compact) {
  66. console.error("[BABEL] Note: The code generator has deoptimised the styling of " + `${opts.filename} as it exceeds the max of ${"500KB"}.`);
  67. }
  68. }
  69. if (format.compact || format.preserveFormat) {
  70. format.indent.adjustMultilineComment = false;
  71. }
  72. const {
  73. auxiliaryCommentBefore,
  74. auxiliaryCommentAfter,
  75. shouldPrintComment
  76. } = format;
  77. if (auxiliaryCommentBefore && !shouldPrintComment(auxiliaryCommentBefore)) {
  78. format.auxiliaryCommentBefore = undefined;
  79. }
  80. if (auxiliaryCommentAfter && !shouldPrintComment(auxiliaryCommentAfter)) {
  81. format.auxiliaryCommentAfter = undefined;
  82. }
  83. return format;
  84. }
  85. exports.CodeGenerator = class CodeGenerator {
  86. constructor(ast, opts = {}, code) {
  87. this._ast = void 0;
  88. this._format = void 0;
  89. this._map = void 0;
  90. this._ast = ast;
  91. this._format = normalizeOptions(code, opts, ast);
  92. this._map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
  93. }
  94. generate() {
  95. const printer = new _printer.default(this._format, this._map);
  96. return printer.generate(this._ast);
  97. }
  98. };
  99. function generate(ast, opts = {}, code) {
  100. const format = normalizeOptions(code, opts, ast);
  101. const map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
  102. const printer = new _printer.default(format, map, ast.tokens, typeof code === "string" ? code : null);
  103. return printer.generate(ast);
  104. }
  105. var _default = exports.default = generate;
  106. //# sourceMappingURL=index.js.map