index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. var _opts$recordAndTupleS;
  23. const format = {
  24. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  25. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  26. shouldPrintComment: opts.shouldPrintComment,
  27. retainLines: opts.retainLines,
  28. retainFunctionParens: opts.retainFunctionParens,
  29. comments: opts.comments == null || opts.comments,
  30. compact: opts.compact,
  31. minified: opts.minified,
  32. concise: opts.concise,
  33. indent: {
  34. adjustMultilineComment: true,
  35. style: " "
  36. },
  37. jsescOption: Object.assign({
  38. quotes: "double",
  39. wrap: true,
  40. minimal: false
  41. }, opts.jsescOption),
  42. recordAndTupleSyntaxType: (_opts$recordAndTupleS = opts.recordAndTupleSyntaxType) != null ? _opts$recordAndTupleS : "hash",
  43. topicToken: opts.topicToken,
  44. importAttributesKeyword: opts.importAttributesKeyword
  45. };
  46. {
  47. format.decoratorsBeforeExport = opts.decoratorsBeforeExport;
  48. format.jsescOption.json = opts.jsonCompatibleStrings;
  49. }
  50. if (format.minified) {
  51. format.compact = true;
  52. format.shouldPrintComment = format.shouldPrintComment || (() => format.comments);
  53. } else {
  54. format.shouldPrintComment = format.shouldPrintComment || (value => format.comments || value.includes("@license") || value.includes("@preserve"));
  55. }
  56. if (format.compact === "auto") {
  57. format.compact = typeof code === "string" && code.length > 500000;
  58. if (format.compact) {
  59. console.error("[BABEL] Note: The code generator has deoptimised the styling of " + `${opts.filename} as it exceeds the max of ${"500KB"}.`);
  60. }
  61. }
  62. if (format.compact) {
  63. format.indent.adjustMultilineComment = false;
  64. }
  65. const {
  66. auxiliaryCommentBefore,
  67. auxiliaryCommentAfter,
  68. shouldPrintComment
  69. } = format;
  70. if (auxiliaryCommentBefore && !shouldPrintComment(auxiliaryCommentBefore)) {
  71. format.auxiliaryCommentBefore = undefined;
  72. }
  73. if (auxiliaryCommentAfter && !shouldPrintComment(auxiliaryCommentAfter)) {
  74. format.auxiliaryCommentAfter = undefined;
  75. }
  76. return format;
  77. }
  78. class CodeGenerator {
  79. constructor(ast, opts, code) {
  80. this._generator = void 0;
  81. this._generator = new Generator(ast, opts, code);
  82. }
  83. generate() {
  84. return this._generator.generate();
  85. }
  86. }
  87. exports.CodeGenerator = CodeGenerator;
  88. function generate(ast, opts, code) {
  89. const gen = new Generator(ast, opts, code);
  90. return gen.generate();
  91. }
  92. //# sourceMappingURL=index.js.map