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