index.js 4.1 KB

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