UNPKG

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