feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -6,11 +6,32 @@ Object.defineProperty(exports, "__esModule", {
exports.default = generate;
var _sourceMap = require("./source-map.js");
var _printer = require("./printer.js");
function normalizeOptions(code, opts) {
function normalizeOptions(code, opts, ast) {
if (opts.experimental_preserveFormat) {
if (typeof code !== "string") {
throw new Error("`experimental_preserveFormat` requires the original `code` to be passed to @babel/generator as a string");
}
if (!opts.retainLines) {
throw new Error("`experimental_preserveFormat` requires `retainLines` to be set to `true`");
}
if (opts.compact && opts.compact !== "auto") {
throw new Error("`experimental_preserveFormat` is not compatible with the `compact` option");
}
if (opts.minified) {
throw new Error("`experimental_preserveFormat` is not compatible with the `minified` option");
}
if (opts.jsescOption) {
throw new Error("`experimental_preserveFormat` is not compatible with the `jsescOption` option");
}
if (!Array.isArray(ast.tokens)) {
throw new Error("`experimental_preserveFormat` requires the AST to have attatched the token of the input code. Make sure to enable the `tokens: true` parser option.");
}
}
const format = {
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
shouldPrintComment: opts.shouldPrintComment,
preserveFormat: opts.experimental_preserveFormat,
retainLines: opts.retainLines,
retainFunctionParens: opts.retainFunctionParens,
comments: opts.comments == null || opts.comments,
@@ -47,7 +68,7 @@ function normalizeOptions(code, opts) {
console.error("[BABEL] Note: The code generator has deoptimised the styling of " + `${opts.filename} as it exceeds the max of ${"500KB"}.`);
}
}
if (format.compact) {
if (format.compact || format.preserveFormat) {
format.indent.adjustMultilineComment = false;
}
const {
@@ -70,7 +91,7 @@ function normalizeOptions(code, opts) {
this._format = void 0;
this._map = void 0;
this._ast = ast;
this._format = normalizeOptions(code, opts);
this._format = normalizeOptions(code, opts, ast);
this._map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
}
generate() {
@@ -80,9 +101,9 @@ function normalizeOptions(code, opts) {
};
}
function generate(ast, opts = {}, code) {
const format = normalizeOptions(code, opts);
const format = normalizeOptions(code, opts, ast);
const map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
const printer = new _printer.default(format, map);
const printer = new _printer.default(format, map, ast.tokens, typeof code === "string" ? code : null);
return printer.generate(ast);
}