index.js 4.1 KB

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