| 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 | var _opts$recordAndTupleS;
|
|---|
| 12 | if (opts.experimental_preserveFormat) {
|
|---|
| 13 | if (typeof code !== "string") {
|
|---|
| 14 | throw new Error("`experimental_preserveFormat` requires the original `code` to be passed to @babel/generator as a string");
|
|---|
| 15 | }
|
|---|
| 16 | if (!opts.retainLines) {
|
|---|
| 17 | throw new Error("`experimental_preserveFormat` requires `retainLines` to be set to `true`");
|
|---|
| 18 | }
|
|---|
| 19 | if (opts.compact && opts.compact !== "auto") {
|
|---|
| 20 | throw new Error("`experimental_preserveFormat` is not compatible with the `compact` option");
|
|---|
| 21 | }
|
|---|
| 22 | if (opts.minified) {
|
|---|
| 23 | throw new Error("`experimental_preserveFormat` is not compatible with the `minified` option");
|
|---|
| 24 | }
|
|---|
| 25 | if (opts.jsescOption) {
|
|---|
| 26 | throw new Error("`experimental_preserveFormat` is not compatible with the `jsescOption` option");
|
|---|
| 27 | }
|
|---|
| 28 | if (!Array.isArray(ast.tokens)) {
|
|---|
| 29 | 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.");
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 | const format = {
|
|---|
| 33 | auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
|
|---|
| 34 | auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
|
|---|
| 35 | shouldPrintComment: opts.shouldPrintComment,
|
|---|
| 36 | preserveFormat: opts.experimental_preserveFormat,
|
|---|
| 37 | retainLines: opts.retainLines,
|
|---|
| 38 | retainFunctionParens: opts.retainFunctionParens,
|
|---|
| 39 | comments: opts.comments == null || opts.comments,
|
|---|
| 40 | compact: opts.compact,
|
|---|
| 41 | minified: opts.minified,
|
|---|
| 42 | concise: opts.concise,
|
|---|
| 43 | indent: {
|
|---|
| 44 | adjustMultilineComment: true,
|
|---|
| 45 | style: " "
|
|---|
| 46 | },
|
|---|
| 47 | jsescOption: Object.assign({
|
|---|
| 48 | quotes: "double",
|
|---|
| 49 | wrap: true,
|
|---|
| 50 | minimal: false
|
|---|
| 51 | }, opts.jsescOption),
|
|---|
| 52 | topicToken: opts.topicToken
|
|---|
| 53 | };
|
|---|
| 54 | format.decoratorsBeforeExport = opts.decoratorsBeforeExport;
|
|---|
| 55 | format.jsescOption.json = opts.jsonCompatibleStrings;
|
|---|
| 56 | format.recordAndTupleSyntaxType = (_opts$recordAndTupleS = opts.recordAndTupleSyntaxType) != null ? _opts$recordAndTupleS : "hash";
|
|---|
| 57 | format.importAttributesKeyword = opts.importAttributesKeyword;
|
|---|
| 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 | exports.CodeGenerator = class CodeGenerator {
|
|---|
| 87 | constructor(ast, opts = {}, code) {
|
|---|
| 88 | this._ast = void 0;
|
|---|
| 89 | this._format = void 0;
|
|---|
| 90 | this._map = void 0;
|
|---|
| 91 | this._ast = ast;
|
|---|
| 92 | this._format = normalizeOptions(code, opts, ast);
|
|---|
| 93 | this._map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
|
|---|
| 94 | }
|
|---|
| 95 | generate() {
|
|---|
| 96 | const printer = new _printer.default(this._format, this._map);
|
|---|
| 97 | return printer.generate(this._ast);
|
|---|
| 98 | }
|
|---|
| 99 | };
|
|---|
| 100 | function generate(ast, opts = {}, code) {
|
|---|
| 101 | const format = normalizeOptions(code, opts, ast);
|
|---|
| 102 | const map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
|
|---|
| 103 | const printer = new _printer.default(format, map, ast.tokens, typeof code === "string" ? code : null);
|
|---|
| 104 | return printer.generate(ast);
|
|---|
| 105 | }
|
|---|
| 106 | var _default = exports.default = generate;
|
|---|
| 107 |
|
|---|
| 108 | //# sourceMappingURL=index.js.map
|
|---|