2023-12-18 13:12:25 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
|
|
|
|
|
|
|
function _isInteger() {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = _interopRequireDefault(require("lodash/isInteger"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_isInteger = function _isInteger() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _repeat() {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = _interopRequireDefault(require("lodash/repeat"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_repeat = function _repeat() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
var _buffer = _interopRequireDefault(require("./buffer"));
|
|
|
|
|
|
|
|
var n = _interopRequireWildcard(require("./node"));
|
|
|
|
|
|
|
|
function t() {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = _interopRequireWildcard(require("@babel/types"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
t = function t() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
var generatorFunctions = _interopRequireWildcard(require("./generators"));
|
|
|
|
|
|
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
|
|
|
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var SCIENTIFIC_NOTATION = /e/i;
|
|
|
|
var ZERO_DECIMAL_INTEGER = /\.0+$/;
|
|
|
|
var NON_DECIMAL_LITERAL = /^0[box]/;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var Printer = function () {
|
|
|
|
function Printer(format, map) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this.inForStatementInitCounter = 0;
|
|
|
|
this._printStack = [];
|
|
|
|
this._indent = 0;
|
|
|
|
this._insideAux = false;
|
|
|
|
this._printedCommentStarts = {};
|
|
|
|
this._parenPushNewlineState = null;
|
|
|
|
this._noLineTerminator = false;
|
|
|
|
this._printAuxAfterOnNextUserNode = false;
|
|
|
|
this._printedComments = new WeakSet();
|
|
|
|
this._endsWithInteger = false;
|
|
|
|
this._endsWithWord = false;
|
|
|
|
this.format = format || {};
|
|
|
|
this._buf = new _buffer.default(map);
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var _proto = Printer.prototype;
|
|
|
|
|
|
|
|
_proto.generate = function generate(ast) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this.print(ast);
|
|
|
|
|
|
|
|
this._maybeAddAuxComment();
|
|
|
|
|
|
|
|
return this._buf.get();
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.indent = function indent() {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this.format.compact || this.format.concise) return;
|
|
|
|
this._indent++;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.dedent = function dedent() {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this.format.compact || this.format.concise) return;
|
|
|
|
this._indent--;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.semicolon = function semicolon(force) {
|
|
|
|
if (force === void 0) {
|
|
|
|
force = false;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
this._maybeAddAuxComment();
|
|
|
|
|
|
|
|
this._append(";", !force);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.rightBrace = function rightBrace() {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this.format.minified) {
|
|
|
|
this._buf.removeLastSemicolon();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.token("}");
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.space = function space(force) {
|
|
|
|
if (force === void 0) {
|
|
|
|
force = false;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (this.format.compact) return;
|
|
|
|
|
|
|
|
if (this._buf.hasContent() && !this.endsWith(" ") && !this.endsWith("\n") || force) {
|
|
|
|
this._space();
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.word = function word(str) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this._endsWithWord || this.endsWith("/") && str.indexOf("/") === 0) {
|
|
|
|
this._space();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._maybeAddAuxComment();
|
|
|
|
|
|
|
|
this._append(str);
|
|
|
|
|
|
|
|
this._endsWithWord = true;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.number = function number(str) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this.word(str);
|
|
|
|
this._endsWithInteger = (0, _isInteger().default)(+str) && !NON_DECIMAL_LITERAL.test(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str[str.length - 1] !== ".";
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.token = function token(str) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (str === "--" && this.endsWith("!") || str[0] === "+" && this.endsWith("+") || str[0] === "-" && this.endsWith("-") || str[0] === "." && this._endsWithInteger) {
|
|
|
|
this._space();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._maybeAddAuxComment();
|
|
|
|
|
|
|
|
this._append(str);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.newline = function newline(i) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this.format.retainLines || this.format.compact) return;
|
|
|
|
|
|
|
|
if (this.format.concise) {
|
|
|
|
this.space();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.endsWith("\n\n")) return;
|
|
|
|
if (typeof i !== "number") i = 1;
|
|
|
|
i = Math.min(2, i);
|
|
|
|
if (this.endsWith("{\n") || this.endsWith(":\n")) i--;
|
|
|
|
if (i <= 0) return;
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var j = 0; j < i; j++) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this._newline();
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.endsWith = function endsWith(str) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return this._buf.endsWith(str);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.removeTrailingNewline = function removeTrailingNewline() {
|
2023-12-18 13:12:25 +08:00
|
|
|
this._buf.removeTrailingNewline();
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.source = function source(prop, loc) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this._catchUp(prop, loc);
|
|
|
|
|
|
|
|
this._buf.source(prop, loc);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.withSource = function withSource(prop, loc, cb) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this._catchUp(prop, loc);
|
|
|
|
|
|
|
|
this._buf.withSource(prop, loc, cb);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._space = function _space() {
|
2023-12-18 13:12:25 +08:00
|
|
|
this._append(" ", true);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._newline = function _newline() {
|
2023-12-18 13:12:25 +08:00
|
|
|
this._append("\n", true);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto._append = function _append(str, queue) {
|
|
|
|
if (queue === void 0) {
|
|
|
|
queue = false;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
this._maybeAddParen(str);
|
|
|
|
|
|
|
|
this._maybeIndent(str);
|
|
|
|
|
|
|
|
if (queue) this._buf.queue(str);else this._buf.append(str);
|
|
|
|
this._endsWithWord = false;
|
|
|
|
this._endsWithInteger = false;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._maybeIndent = function _maybeIndent(str) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this._indent && this.endsWith("\n") && str[0] !== "\n") {
|
|
|
|
this._buf.queue(this._getIndent());
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._maybeAddParen = function _maybeAddParen(str) {
|
|
|
|
var parenPushNewlineState = this._parenPushNewlineState;
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!parenPushNewlineState) return;
|
|
|
|
this._parenPushNewlineState = null;
|
2024-01-16 21:26:16 +08:00
|
|
|
var i;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (i = 0; i < str.length && str[i] === " "; i++) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (i === str.length) return;
|
2024-01-16 21:26:16 +08:00
|
|
|
var cha = str[i];
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (cha !== "\n") {
|
|
|
|
if (cha !== "/") return;
|
|
|
|
if (i + 1 === str.length) return;
|
2024-01-16 21:26:16 +08:00
|
|
|
var chaPost = str[i + 1];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (chaPost !== "/" && chaPost !== "*") return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.token("(");
|
|
|
|
this.indent();
|
|
|
|
parenPushNewlineState.printed = true;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._catchUp = function _catchUp(prop, loc) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!this.format.retainLines) return;
|
2024-01-16 21:26:16 +08:00
|
|
|
var pos = loc ? loc[prop] : null;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (pos && pos.line !== null) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var count = pos.line - this._buf.getCurrentLine();
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var i = 0; i < count; i++) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this._newline();
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._getIndent = function _getIndent() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return (0, _repeat().default)(this.format.indent.style, this._indent);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.startTerminatorless = function startTerminatorless(isLabel) {
|
|
|
|
if (isLabel === void 0) {
|
|
|
|
isLabel = false;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (isLabel) {
|
|
|
|
this._noLineTerminator = true;
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return this._parenPushNewlineState = {
|
|
|
|
printed: false
|
|
|
|
};
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.endTerminatorless = function endTerminatorless(state) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this._noLineTerminator = false;
|
|
|
|
|
|
|
|
if (state && state.printed) {
|
|
|
|
this.dedent();
|
|
|
|
this.newline();
|
|
|
|
this.token(")");
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.print = function print(node, parent) {
|
|
|
|
var _this = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!node) return;
|
2024-01-16 21:26:16 +08:00
|
|
|
var oldConcise = this.format.concise;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (node._compact) {
|
|
|
|
this.format.concise = true;
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var printMethod = this[node.type];
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!printMethod) {
|
2024-01-16 21:26:16 +08:00
|
|
|
throw new ReferenceError("unknown node of type " + JSON.stringify(node.type) + " with constructor " + JSON.stringify(node && node.constructor.name));
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this._printStack.push(node);
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var oldInAux = this._insideAux;
|
2023-12-18 13:12:25 +08:00
|
|
|
this._insideAux = !node.loc;
|
|
|
|
|
|
|
|
this._maybeAddAuxComment(this._insideAux && !oldInAux);
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var needsParens = n.needsParens(node, parent, this._printStack);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (this.format.retainFunctionParens && node.type === "FunctionExpression" && node.extra && node.extra.parenthesized) {
|
|
|
|
needsParens = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsParens) this.token("(");
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
this._printLeadingComments(node, parent);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var loc = t().isProgram(node) || t().isFile(node) ? null : node.loc;
|
|
|
|
this.withSource("start", loc, function () {
|
|
|
|
_this[node.type](node, parent);
|
2023-12-18 13:12:25 +08:00
|
|
|
});
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
this._printTrailingComments(node, parent);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (needsParens) this.token(")");
|
|
|
|
|
|
|
|
this._printStack.pop();
|
|
|
|
|
|
|
|
this.format.concise = oldConcise;
|
|
|
|
this._insideAux = oldInAux;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._maybeAddAuxComment = function _maybeAddAuxComment(enteredPositionlessNode) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (enteredPositionlessNode) this._printAuxBeforeComment();
|
|
|
|
if (!this._insideAux) this._printAuxAfterComment();
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._printAuxBeforeComment = function _printAuxBeforeComment() {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this._printAuxAfterOnNextUserNode) return;
|
|
|
|
this._printAuxAfterOnNextUserNode = true;
|
2024-01-16 21:26:16 +08:00
|
|
|
var comment = this.format.auxiliaryCommentBefore;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (comment) {
|
|
|
|
this._printComment({
|
|
|
|
type: "CommentBlock",
|
|
|
|
value: comment
|
|
|
|
});
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._printAuxAfterComment = function _printAuxAfterComment() {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!this._printAuxAfterOnNextUserNode) return;
|
|
|
|
this._printAuxAfterOnNextUserNode = false;
|
2024-01-16 21:26:16 +08:00
|
|
|
var comment = this.format.auxiliaryCommentAfter;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (comment) {
|
|
|
|
this._printComment({
|
|
|
|
type: "CommentBlock",
|
|
|
|
value: comment
|
|
|
|
});
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getPossibleRaw = function getPossibleRaw(node) {
|
|
|
|
var extra = node.extra;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (extra && extra.raw != null && extra.rawValue != null && node.value === extra.rawValue) {
|
|
|
|
return extra.raw;
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.printJoin = function printJoin(nodes, parent, opts) {
|
|
|
|
if (opts === void 0) {
|
|
|
|
opts = {};
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!nodes || !nodes.length) return;
|
|
|
|
if (opts.indent) this.indent();
|
2024-01-16 21:26:16 +08:00
|
|
|
var newlineOpts = {
|
2023-12-18 13:12:25 +08:00
|
|
|
addNewlines: opts.addNewlines
|
|
|
|
};
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var i = 0; i < nodes.length; i++) {
|
|
|
|
var node = nodes[i];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!node) continue;
|
|
|
|
if (opts.statement) this._printNewline(true, node, parent, newlineOpts);
|
|
|
|
this.print(node, parent);
|
|
|
|
|
|
|
|
if (opts.iterator) {
|
|
|
|
opts.iterator(node, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.separator && i < nodes.length - 1) {
|
|
|
|
opts.separator.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.statement) this._printNewline(false, node, parent, newlineOpts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.indent) this.dedent();
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.printAndIndentOnComments = function printAndIndentOnComments(node, parent) {
|
|
|
|
var indent = node.leadingComments && node.leadingComments.length > 0;
|
2023-12-18 13:12:25 +08:00
|
|
|
if (indent) this.indent();
|
|
|
|
this.print(node, parent);
|
|
|
|
if (indent) this.dedent();
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.printBlock = function printBlock(parent) {
|
|
|
|
var node = parent.body;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!t().isEmptyStatement(node)) {
|
|
|
|
this.space();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.print(node, parent);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._printTrailingComments = function _printTrailingComments(node, parent) {
|
|
|
|
this._printComments(this._getComments(false, node, parent));
|
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._printLeadingComments = function _printLeadingComments(node, parent) {
|
|
|
|
this._printComments(this._getComments(true, node, parent));
|
|
|
|
};
|
|
|
|
|
|
|
|
_proto.printInnerComments = function printInnerComments(node, indent) {
|
|
|
|
if (indent === void 0) {
|
|
|
|
indent = true;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!node.innerComments || !node.innerComments.length) return;
|
|
|
|
if (indent) this.indent();
|
|
|
|
|
|
|
|
this._printComments(node.innerComments);
|
|
|
|
|
|
|
|
if (indent) this.dedent();
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.printSequence = function printSequence(nodes, parent, opts) {
|
|
|
|
if (opts === void 0) {
|
|
|
|
opts = {};
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
opts.statement = true;
|
|
|
|
return this.printJoin(nodes, parent, opts);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.printList = function printList(items, parent, opts) {
|
|
|
|
if (opts === void 0) {
|
|
|
|
opts = {};
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (opts.separator == null) {
|
|
|
|
opts.separator = commaSeparator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.printJoin(items, parent, opts);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._printNewline = function _printNewline(leading, node, parent, opts) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this.format.retainLines || this.format.compact) return;
|
|
|
|
|
|
|
|
if (this.format.concise) {
|
|
|
|
this.space();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var lines = 0;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (this._buf.hasContent()) {
|
|
|
|
if (!leading) lines++;
|
|
|
|
if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
|
2024-01-16 21:26:16 +08:00
|
|
|
var needs = leading ? n.needsWhitespaceBefore : n.needsWhitespaceAfter;
|
2023-12-18 13:12:25 +08:00
|
|
|
if (needs(node, parent)) lines++;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.newline(lines);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._getComments = function _getComments(leading, node) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return node && (leading ? node.leadingComments : node.trailingComments) || [];
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto._printComment = function _printComment(comment) {
|
|
|
|
var _this2 = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!this.format.shouldPrintComment(comment.value)) return;
|
|
|
|
if (comment.ignore) return;
|
|
|
|
if (this._printedComments.has(comment)) return;
|
|
|
|
|
|
|
|
this._printedComments.add(comment);
|
|
|
|
|
|
|
|
if (comment.start != null) {
|
|
|
|
if (this._printedCommentStarts[comment.start]) return;
|
|
|
|
this._printedCommentStarts[comment.start] = true;
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var isBlockComment = comment.type === "CommentBlock";
|
2023-12-18 13:12:25 +08:00
|
|
|
this.newline(this._buf.hasContent() && !this._noLineTerminator && isBlockComment ? 1 : 0);
|
|
|
|
if (!this.endsWith("[") && !this.endsWith("{")) this.space();
|
2024-01-16 21:26:16 +08:00
|
|
|
var val = !isBlockComment && !this._noLineTerminator ? "//" + comment.value + "\n" : "/*" + comment.value + "*/";
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (isBlockComment && this.format.indent.adjustMultilineComment) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var offset = comment.loc && comment.loc.start.column;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (offset) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
|
2023-12-18 13:12:25 +08:00
|
|
|
val = val.replace(newlineRegex, "\n");
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var indentSize = Math.max(this._getIndent().length, this._buf.getCurrentColumn());
|
|
|
|
val = val.replace(/\n(?!$)/g, "\n" + (0, _repeat().default)(" ", indentSize));
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.endsWith("/")) this._space();
|
2024-01-16 21:26:16 +08:00
|
|
|
this.withSource("start", comment.loc, function () {
|
|
|
|
_this2._append(val);
|
2023-12-18 13:12:25 +08:00
|
|
|
});
|
|
|
|
this.newline(isBlockComment && !this._noLineTerminator ? 1 : 0);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._printComments = function _printComments(comments) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!comments || !comments.length) return;
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _iterator = comments, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
|
|
|
var _ref;
|
|
|
|
|
|
|
|
if (_isArray) {
|
|
|
|
if (_i >= _iterator.length) break;
|
|
|
|
_ref = _iterator[_i++];
|
|
|
|
} else {
|
|
|
|
_i = _iterator.next();
|
|
|
|
if (_i.done) break;
|
|
|
|
_ref = _i.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var _comment = _ref;
|
|
|
|
|
|
|
|
this._printComment(_comment);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
return Printer;
|
|
|
|
}();
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
exports.default = Printer;
|
|
|
|
Object.assign(Printer.prototype, generatorFunctions);
|
|
|
|
|
|
|
|
function commaSeparator() {
|
|
|
|
this.token(",");
|
|
|
|
this.space();
|
|
|
|
}
|