| 1 | "use strict"; |
| 2 | |
| 3 | Object.defineProperty(exports, "__esModule", { |
| 4 | value: true |
| 5 | }); |
| 6 | exports.default = void 0; |
| 7 | exports.generate = generate; |
| 8 | var _sourceMap = require("./source-map.js"); |
| 9 | var _printer = require("./printer.js"); |
| 10 | function normalizeOptions(code, opts, ast) { |
| 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 | importAttributesKeyword: opts.importAttributesKeyword |
| 53 | }; |
| 54 | { |
| 55 | var _opts$recordAndTupleS; |
| 56 | format.decoratorsBeforeExport = opts.decoratorsBeforeExport; |
| 57 | format.jsescOption.json = opts.jsonCompatibleStrings; |
| 58 | format.recordAndTupleSyntaxType = (_opts$recordAndTupleS = opts.recordAndTupleSyntaxType) != null ? _opts$recordAndTupleS : "hash"; |
| 59 | } |
| 60 | if (format.minified) { |
| 61 | format.compact = true; |
| 62 | format.shouldPrintComment = format.shouldPrintComment || (() => format.comments); |
| 63 | } else { |
| 64 | format.shouldPrintComment = format.shouldPrintComment || (value => format.comments || value.includes("@license") || value.includes("@preserve")); |
| 65 | } |
| 66 | if (format.compact === "auto") { |
| 67 | format.compact = typeof code === "string" && code.length > 500000; |
| 68 | if (format.compact) { |
| 69 | console.error("[BABEL] Note: The code generator has deoptimised the styling of " + `${opts.filename} as it exceeds the max of ${"500KB"}.`); |
| 70 | } |
| 71 | } |
| 72 | if (format.compact || format.preserveFormat) { |
| 73 | format.indent.adjustMultilineComment = false; |
| 74 | } |
| 75 | const { |
| 76 | auxiliaryCommentBefore, |
| 77 | auxiliaryCommentAfter, |
| 78 | shouldPrintComment |
| 79 | } = format; |
| 80 | if (auxiliaryCommentBefore && !shouldPrintComment(auxiliaryCommentBefore)) { |
| 81 | format.auxiliaryCommentBefore = undefined; |
| 82 | } |
| 83 | if (auxiliaryCommentAfter && !shouldPrintComment(auxiliaryCommentAfter)) { |
| 84 | format.auxiliaryCommentAfter = undefined; |
| 85 | } |
| 86 | return format; |
| 87 | } |
| 88 | { |
| 89 | exports.CodeGenerator = class CodeGenerator { |
| 90 | constructor(ast, opts = {}, code) { |
| 91 | this._ast = void 0; |
| 92 | this._format = void 0; |
| 93 | this._map = void 0; |
| 94 | this._ast = ast; |
| 95 | this._format = normalizeOptions(code, opts, ast); |
| 96 | this._map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null; |
| 97 | } |
| 98 | generate() { |
| 99 | const printer = new _printer.default(this._format, this._map); |
| 100 | return printer.generate(this._ast); |
| 101 | } |
| 102 | }; |
| 103 | } |
| 104 | function generate(ast, opts = {}, code) { |
| 105 | const format = normalizeOptions(code, opts, ast); |
| 106 | const map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null; |
| 107 | const printer = new _printer.default(format, map, ast.tokens, typeof code === "string" ? code : null); |
| 108 | return printer.generate(ast); |
| 109 | } |
| 110 | var _default = exports.default = generate; |
| 111 | |
| 112 | //# sourceMappingURL=index.js.map |
| 113 |