index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.CodeGenerator = void 0;
  6. exports.default = generate;
  7. var _sourceMap = require("./source-map");
  8. var _printer = require("./printer");
  9. class Generator extends _printer.default {
  10. constructor(ast, opts = {}, code) {
  11. const format = normalizeOptions(code, opts);
  12. const map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
  13. super(format, map);
  14. this.ast = void 0;
  15. this.ast = ast;
  16. }
  17. generate() {
  18. return super.generate(this.ast);
  19. }
  20. }
  21. function normalizeOptions(code, opts) {
  22. const format = {
  23. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  24. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  25. shouldPrintComment: opts.shouldPrintComment,
  26. retainLines: opts.retainLines,
  27. retainFunctionParens: opts.retainFunctionParens,
  28. comments: opts.comments == null || opts.comments,
  29. compact: opts.compact,
  30. minified: opts.minified,
  31. concise: opts.concise,
  32. indent: {
  33. adjustMultilineComment: true,
  34. style: " "
  35. },
  36. jsescOption: Object.assign({
  37. quotes: "double",
  38. wrap: true,
  39. minimal: false
  40. }, opts.jsescOption),
  41. recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType,
  42. topicToken: opts.topicToken
  43. };
  44. {
  45. format.decoratorsBeforeExport = !!opts.decoratorsBeforeExport;
  46. format.jsonCompatibleStrings = opts.jsonCompatibleStrings;
  47. }
  48. if (format.minified) {
  49. format.compact = true;
  50. format.shouldPrintComment = format.shouldPrintComment || (() => format.comments);
  51. } else {
  52. format.shouldPrintComment = format.shouldPrintComment || (value => format.comments || value.includes("@license") || value.includes("@preserve"));
  53. }
  54. if (format.compact === "auto") {
  55. format.compact = code.length > 500000;
  56. if (format.compact) {
  57. console.error("[BABEL] Note: The code generator has deoptimised the styling of " + `${opts.filename} as it exceeds the max of ${"500KB"}.`);
  58. }
  59. }
  60. if (format.compact) {
  61. format.indent.adjustMultilineComment = false;
  62. }
  63. const {
  64. auxiliaryCommentBefore,
  65. auxiliaryCommentAfter,
  66. shouldPrintComment
  67. } = format;
  68. if (auxiliaryCommentBefore && !shouldPrintComment(auxiliaryCommentBefore)) {
  69. format.auxiliaryCommentBefore = undefined;
  70. }
  71. if (auxiliaryCommentAfter && !shouldPrintComment(auxiliaryCommentAfter)) {
  72. format.auxiliaryCommentAfter = undefined;
  73. }
  74. return format;
  75. }
  76. class CodeGenerator {
  77. constructor(ast, opts, code) {
  78. this._generator = void 0;
  79. this._generator = new Generator(ast, opts, code);
  80. }
  81. generate() {
  82. return this._generator.generate();
  83. }
  84. }
  85. exports.CodeGenerator = CodeGenerator;
  86. function generate(ast, opts, code) {
  87. const gen = new Generator(ast, opts, code);
  88. return gen.generate();
  89. }
  90. //# sourceMappingURL=index.js.map