1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 | var _buffer = require("./buffer.js");
|
---|
8 | var n = require("./node/index.js");
|
---|
9 | var _t = require("@babel/types");
|
---|
10 | var generatorFunctions = require("./generators/index.js");
|
---|
11 | const {
|
---|
12 | isFunction,
|
---|
13 | isStatement,
|
---|
14 | isClassBody,
|
---|
15 | isTSInterfaceBody,
|
---|
16 | isTSEnumDeclaration
|
---|
17 | } = _t;
|
---|
18 | const SCIENTIFIC_NOTATION = /e/i;
|
---|
19 | const ZERO_DECIMAL_INTEGER = /\.0+$/;
|
---|
20 | const HAS_NEWLINE = /[\n\r\u2028\u2029]/;
|
---|
21 | const HAS_NEWLINE_OR_BlOCK_COMMENT_END = /[\n\r\u2028\u2029]|\*\//;
|
---|
22 | const {
|
---|
23 | needsParens
|
---|
24 | } = n;
|
---|
25 | class Printer {
|
---|
26 | constructor(format, map) {
|
---|
27 | this.inForStatementInit = false;
|
---|
28 | this.tokenContext = 0;
|
---|
29 | this._currentNode = null;
|
---|
30 | this._indent = 0;
|
---|
31 | this._indentRepeat = 0;
|
---|
32 | this._insideAux = false;
|
---|
33 | this._parenPushNewlineState = null;
|
---|
34 | this._noLineTerminator = false;
|
---|
35 | this._printAuxAfterOnNextUserNode = false;
|
---|
36 | this._printedComments = new Set();
|
---|
37 | this._endsWithInteger = false;
|
---|
38 | this._endsWithWord = false;
|
---|
39 | this._lastCommentLine = 0;
|
---|
40 | this._endsWithInnerRaw = false;
|
---|
41 | this._indentInnerComments = true;
|
---|
42 | this.format = format;
|
---|
43 | this._indentRepeat = format.indent.style.length;
|
---|
44 | this._inputMap = map == null ? void 0 : map._inputMap;
|
---|
45 | this._buf = new _buffer.default(map, format.indent.style[0]);
|
---|
46 | }
|
---|
47 | enterForStatementInit(val) {
|
---|
48 | const old = this.inForStatementInit;
|
---|
49 | if (old === val) return () => {};
|
---|
50 | this.inForStatementInit = val;
|
---|
51 | return () => {
|
---|
52 | this.inForStatementInit = old;
|
---|
53 | };
|
---|
54 | }
|
---|
55 | generate(ast) {
|
---|
56 | this.print(ast);
|
---|
57 | this._maybeAddAuxComment();
|
---|
58 | return this._buf.get();
|
---|
59 | }
|
---|
60 | indent() {
|
---|
61 | if (this.format.compact || this.format.concise) return;
|
---|
62 | this._indent++;
|
---|
63 | }
|
---|
64 | dedent() {
|
---|
65 | if (this.format.compact || this.format.concise) return;
|
---|
66 | this._indent--;
|
---|
67 | }
|
---|
68 | semicolon(force = false) {
|
---|
69 | this._maybeAddAuxComment();
|
---|
70 | if (force) {
|
---|
71 | this._appendChar(59);
|
---|
72 | } else {
|
---|
73 | this._queue(59);
|
---|
74 | }
|
---|
75 | this._noLineTerminator = false;
|
---|
76 | }
|
---|
77 | rightBrace(node) {
|
---|
78 | if (this.format.minified) {
|
---|
79 | this._buf.removeLastSemicolon();
|
---|
80 | }
|
---|
81 | this.sourceWithOffset("end", node.loc, -1);
|
---|
82 | this.tokenChar(125);
|
---|
83 | }
|
---|
84 | rightParens(node) {
|
---|
85 | this.sourceWithOffset("end", node.loc, -1);
|
---|
86 | this.tokenChar(41);
|
---|
87 | }
|
---|
88 | space(force = false) {
|
---|
89 | if (this.format.compact) return;
|
---|
90 | if (force) {
|
---|
91 | this._space();
|
---|
92 | } else if (this._buf.hasContent()) {
|
---|
93 | const lastCp = this.getLastChar();
|
---|
94 | if (lastCp !== 32 && lastCp !== 10) {
|
---|
95 | this._space();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | word(str, noLineTerminatorAfter = false) {
|
---|
100 | this.tokenContext = 0;
|
---|
101 | this._maybePrintInnerComments();
|
---|
102 | if (this._endsWithWord || str.charCodeAt(0) === 47 && this.endsWith(47)) {
|
---|
103 | this._space();
|
---|
104 | }
|
---|
105 | this._maybeAddAuxComment();
|
---|
106 | this._append(str, false);
|
---|
107 | this._endsWithWord = true;
|
---|
108 | this._noLineTerminator = noLineTerminatorAfter;
|
---|
109 | }
|
---|
110 | number(str, number) {
|
---|
111 | function isNonDecimalLiteral(str) {
|
---|
112 | if (str.length > 2 && str.charCodeAt(0) === 48) {
|
---|
113 | const secondChar = str.charCodeAt(1);
|
---|
114 | return secondChar === 98 || secondChar === 111 || secondChar === 120;
|
---|
115 | }
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 | this.word(str);
|
---|
119 | this._endsWithInteger = Number.isInteger(number) && !isNonDecimalLiteral(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== 46;
|
---|
120 | }
|
---|
121 | token(str, maybeNewline = false) {
|
---|
122 | this.tokenContext = 0;
|
---|
123 | this._maybePrintInnerComments();
|
---|
124 | const lastChar = this.getLastChar();
|
---|
125 | const strFirst = str.charCodeAt(0);
|
---|
126 | if (lastChar === 33 && (str === "--" || strFirst === 61) || strFirst === 43 && lastChar === 43 || strFirst === 45 && lastChar === 45 || strFirst === 46 && this._endsWithInteger) {
|
---|
127 | this._space();
|
---|
128 | }
|
---|
129 | this._maybeAddAuxComment();
|
---|
130 | this._append(str, maybeNewline);
|
---|
131 | this._noLineTerminator = false;
|
---|
132 | }
|
---|
133 | tokenChar(char) {
|
---|
134 | this.tokenContext = 0;
|
---|
135 | this._maybePrintInnerComments();
|
---|
136 | const lastChar = this.getLastChar();
|
---|
137 | if (char === 43 && lastChar === 43 || char === 45 && lastChar === 45 || char === 46 && this._endsWithInteger) {
|
---|
138 | this._space();
|
---|
139 | }
|
---|
140 | this._maybeAddAuxComment();
|
---|
141 | this._appendChar(char);
|
---|
142 | this._noLineTerminator = false;
|
---|
143 | }
|
---|
144 | newline(i = 1, force) {
|
---|
145 | if (i <= 0) return;
|
---|
146 | if (!force) {
|
---|
147 | if (this.format.retainLines || this.format.compact) return;
|
---|
148 | if (this.format.concise) {
|
---|
149 | this.space();
|
---|
150 | return;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | if (i > 2) i = 2;
|
---|
154 | i -= this._buf.getNewlineCount();
|
---|
155 | for (let j = 0; j < i; j++) {
|
---|
156 | this._newline();
|
---|
157 | }
|
---|
158 | return;
|
---|
159 | }
|
---|
160 | endsWith(char) {
|
---|
161 | return this.getLastChar() === char;
|
---|
162 | }
|
---|
163 | getLastChar() {
|
---|
164 | return this._buf.getLastChar();
|
---|
165 | }
|
---|
166 | endsWithCharAndNewline() {
|
---|
167 | return this._buf.endsWithCharAndNewline();
|
---|
168 | }
|
---|
169 | removeTrailingNewline() {
|
---|
170 | this._buf.removeTrailingNewline();
|
---|
171 | }
|
---|
172 | exactSource(loc, cb) {
|
---|
173 | if (!loc) {
|
---|
174 | cb();
|
---|
175 | return;
|
---|
176 | }
|
---|
177 | this._catchUp("start", loc);
|
---|
178 | this._buf.exactSource(loc, cb);
|
---|
179 | }
|
---|
180 | source(prop, loc) {
|
---|
181 | if (!loc) return;
|
---|
182 | this._catchUp(prop, loc);
|
---|
183 | this._buf.source(prop, loc);
|
---|
184 | }
|
---|
185 | sourceWithOffset(prop, loc, columnOffset) {
|
---|
186 | if (!loc) return;
|
---|
187 | this._catchUp(prop, loc);
|
---|
188 | this._buf.sourceWithOffset(prop, loc, columnOffset);
|
---|
189 | }
|
---|
190 | sourceIdentifierName(identifierName, pos) {
|
---|
191 | if (!this._buf._canMarkIdName) return;
|
---|
192 | const sourcePosition = this._buf._sourcePosition;
|
---|
193 | sourcePosition.identifierNamePos = pos;
|
---|
194 | sourcePosition.identifierName = identifierName;
|
---|
195 | }
|
---|
196 | _space() {
|
---|
197 | this._queue(32);
|
---|
198 | }
|
---|
199 | _newline() {
|
---|
200 | this._queue(10);
|
---|
201 | }
|
---|
202 | _append(str, maybeNewline) {
|
---|
203 | this._maybeAddParen(str);
|
---|
204 | this._maybeIndent(str.charCodeAt(0));
|
---|
205 | this._buf.append(str, maybeNewline);
|
---|
206 | this._endsWithWord = false;
|
---|
207 | this._endsWithInteger = false;
|
---|
208 | }
|
---|
209 | _appendChar(char) {
|
---|
210 | this._maybeAddParenChar(char);
|
---|
211 | this._maybeIndent(char);
|
---|
212 | this._buf.appendChar(char);
|
---|
213 | this._endsWithWord = false;
|
---|
214 | this._endsWithInteger = false;
|
---|
215 | }
|
---|
216 | _queue(char) {
|
---|
217 | this._maybeAddParenChar(char);
|
---|
218 | this._maybeIndent(char);
|
---|
219 | this._buf.queue(char);
|
---|
220 | this._endsWithWord = false;
|
---|
221 | this._endsWithInteger = false;
|
---|
222 | }
|
---|
223 | _maybeIndent(firstChar) {
|
---|
224 | if (this._indent && firstChar !== 10 && this.endsWith(10)) {
|
---|
225 | this._buf.queueIndentation(this._getIndent());
|
---|
226 | }
|
---|
227 | }
|
---|
228 | _shouldIndent(firstChar) {
|
---|
229 | if (this._indent && firstChar !== 10 && this.endsWith(10)) {
|
---|
230 | return true;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | _maybeAddParenChar(char) {
|
---|
234 | const parenPushNewlineState = this._parenPushNewlineState;
|
---|
235 | if (!parenPushNewlineState) return;
|
---|
236 | if (char === 32) {
|
---|
237 | return;
|
---|
238 | }
|
---|
239 | if (char !== 10) {
|
---|
240 | this._parenPushNewlineState = null;
|
---|
241 | return;
|
---|
242 | }
|
---|
243 | this.tokenChar(40);
|
---|
244 | this.indent();
|
---|
245 | parenPushNewlineState.printed = true;
|
---|
246 | }
|
---|
247 | _maybeAddParen(str) {
|
---|
248 | const parenPushNewlineState = this._parenPushNewlineState;
|
---|
249 | if (!parenPushNewlineState) return;
|
---|
250 | const len = str.length;
|
---|
251 | let i;
|
---|
252 | for (i = 0; i < len && str.charCodeAt(i) === 32; i++) continue;
|
---|
253 | if (i === len) {
|
---|
254 | return;
|
---|
255 | }
|
---|
256 | const cha = str.charCodeAt(i);
|
---|
257 | if (cha !== 10) {
|
---|
258 | if (cha !== 47 || i + 1 === len) {
|
---|
259 | this._parenPushNewlineState = null;
|
---|
260 | return;
|
---|
261 | }
|
---|
262 | const chaPost = str.charCodeAt(i + 1);
|
---|
263 | if (chaPost === 42) {
|
---|
264 | return;
|
---|
265 | } else if (chaPost !== 47) {
|
---|
266 | this._parenPushNewlineState = null;
|
---|
267 | return;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | this.tokenChar(40);
|
---|
271 | this.indent();
|
---|
272 | parenPushNewlineState.printed = true;
|
---|
273 | }
|
---|
274 | catchUp(line) {
|
---|
275 | if (!this.format.retainLines) return;
|
---|
276 | const count = line - this._buf.getCurrentLine();
|
---|
277 | for (let i = 0; i < count; i++) {
|
---|
278 | this._newline();
|
---|
279 | }
|
---|
280 | }
|
---|
281 | _catchUp(prop, loc) {
|
---|
282 | var _loc$prop;
|
---|
283 | if (!this.format.retainLines) return;
|
---|
284 | const line = loc == null || (_loc$prop = loc[prop]) == null ? void 0 : _loc$prop.line;
|
---|
285 | if (line != null) {
|
---|
286 | const count = line - this._buf.getCurrentLine();
|
---|
287 | for (let i = 0; i < count; i++) {
|
---|
288 | this._newline();
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 | _getIndent() {
|
---|
293 | return this._indentRepeat * this._indent;
|
---|
294 | }
|
---|
295 | printTerminatorless(node, parent, isLabel) {
|
---|
296 | if (isLabel) {
|
---|
297 | this._noLineTerminator = true;
|
---|
298 | this.print(node, parent);
|
---|
299 | } else {
|
---|
300 | const terminatorState = {
|
---|
301 | printed: false
|
---|
302 | };
|
---|
303 | this._parenPushNewlineState = terminatorState;
|
---|
304 | this.print(node, parent);
|
---|
305 | if (terminatorState.printed) {
|
---|
306 | this.dedent();
|
---|
307 | this.newline();
|
---|
308 | this.tokenChar(41);
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 | print(node, parent, noLineTerminatorAfter, trailingCommentsLineOffset, forceParens) {
|
---|
313 | var _node$extra, _node$leadingComments;
|
---|
314 | if (!node) return;
|
---|
315 | this._endsWithInnerRaw = false;
|
---|
316 | const nodeType = node.type;
|
---|
317 | const format = this.format;
|
---|
318 | const oldConcise = format.concise;
|
---|
319 | if (node._compact) {
|
---|
320 | format.concise = true;
|
---|
321 | }
|
---|
322 | const printMethod = this[nodeType];
|
---|
323 | if (printMethod === undefined) {
|
---|
324 | throw new ReferenceError(`unknown node of type ${JSON.stringify(nodeType)} with constructor ${JSON.stringify(node.constructor.name)}`);
|
---|
325 | }
|
---|
326 | const oldNode = this._currentNode;
|
---|
327 | this._currentNode = node;
|
---|
328 | const oldInAux = this._insideAux;
|
---|
329 | this._insideAux = node.loc == null;
|
---|
330 | this._maybeAddAuxComment(this._insideAux && !oldInAux);
|
---|
331 | const parenthesized = (_node$extra = node.extra) == null ? void 0 : _node$extra.parenthesized;
|
---|
332 | let shouldPrintParens = forceParens || parenthesized && format.retainFunctionParens && nodeType === "FunctionExpression" || needsParens(node, parent, this.tokenContext, this.inForStatementInit);
|
---|
333 | if (!shouldPrintParens && parenthesized && (_node$leadingComments = node.leadingComments) != null && _node$leadingComments.length && node.leadingComments[0].type === "CommentBlock") {
|
---|
334 | const parentType = parent == null ? void 0 : parent.type;
|
---|
335 | switch (parentType) {
|
---|
336 | case "ExpressionStatement":
|
---|
337 | case "VariableDeclarator":
|
---|
338 | case "AssignmentExpression":
|
---|
339 | case "ReturnStatement":
|
---|
340 | break;
|
---|
341 | case "CallExpression":
|
---|
342 | case "OptionalCallExpression":
|
---|
343 | case "NewExpression":
|
---|
344 | if (parent.callee !== node) break;
|
---|
345 | default:
|
---|
346 | shouldPrintParens = true;
|
---|
347 | }
|
---|
348 | }
|
---|
349 | let exitInForStatementInit;
|
---|
350 | if (shouldPrintParens) {
|
---|
351 | this.tokenChar(40);
|
---|
352 | this._endsWithInnerRaw = false;
|
---|
353 | exitInForStatementInit = this.enterForStatementInit(false);
|
---|
354 | }
|
---|
355 | this._lastCommentLine = 0;
|
---|
356 | this._printLeadingComments(node, parent);
|
---|
357 | const loc = nodeType === "Program" || nodeType === "File" ? null : node.loc;
|
---|
358 | this.exactSource(loc, printMethod.bind(this, node, parent));
|
---|
359 | if (shouldPrintParens) {
|
---|
360 | this._printTrailingComments(node, parent);
|
---|
361 | this.tokenChar(41);
|
---|
362 | this._noLineTerminator = noLineTerminatorAfter;
|
---|
363 | exitInForStatementInit();
|
---|
364 | } else if (noLineTerminatorAfter && !this._noLineTerminator) {
|
---|
365 | this._noLineTerminator = true;
|
---|
366 | this._printTrailingComments(node, parent);
|
---|
367 | } else {
|
---|
368 | this._printTrailingComments(node, parent, trailingCommentsLineOffset);
|
---|
369 | }
|
---|
370 | this._currentNode = oldNode;
|
---|
371 | format.concise = oldConcise;
|
---|
372 | this._insideAux = oldInAux;
|
---|
373 | this._endsWithInnerRaw = false;
|
---|
374 | }
|
---|
375 | _maybeAddAuxComment(enteredPositionlessNode) {
|
---|
376 | if (enteredPositionlessNode) this._printAuxBeforeComment();
|
---|
377 | if (!this._insideAux) this._printAuxAfterComment();
|
---|
378 | }
|
---|
379 | _printAuxBeforeComment() {
|
---|
380 | if (this._printAuxAfterOnNextUserNode) return;
|
---|
381 | this._printAuxAfterOnNextUserNode = true;
|
---|
382 | const comment = this.format.auxiliaryCommentBefore;
|
---|
383 | if (comment) {
|
---|
384 | this._printComment({
|
---|
385 | type: "CommentBlock",
|
---|
386 | value: comment
|
---|
387 | }, 0);
|
---|
388 | }
|
---|
389 | }
|
---|
390 | _printAuxAfterComment() {
|
---|
391 | if (!this._printAuxAfterOnNextUserNode) return;
|
---|
392 | this._printAuxAfterOnNextUserNode = false;
|
---|
393 | const comment = this.format.auxiliaryCommentAfter;
|
---|
394 | if (comment) {
|
---|
395 | this._printComment({
|
---|
396 | type: "CommentBlock",
|
---|
397 | value: comment
|
---|
398 | }, 0);
|
---|
399 | }
|
---|
400 | }
|
---|
401 | getPossibleRaw(node) {
|
---|
402 | const extra = node.extra;
|
---|
403 | if ((extra == null ? void 0 : extra.raw) != null && extra.rawValue != null && node.value === extra.rawValue) {
|
---|
404 | return extra.raw;
|
---|
405 | }
|
---|
406 | }
|
---|
407 | printJoin(nodes, parent, opts = {}) {
|
---|
408 | if (!(nodes != null && nodes.length)) return;
|
---|
409 | let {
|
---|
410 | indent
|
---|
411 | } = opts;
|
---|
412 | if (indent == null && this.format.retainLines) {
|
---|
413 | var _nodes$0$loc;
|
---|
414 | const startLine = (_nodes$0$loc = nodes[0].loc) == null ? void 0 : _nodes$0$loc.start.line;
|
---|
415 | if (startLine != null && startLine !== this._buf.getCurrentLine()) {
|
---|
416 | indent = true;
|
---|
417 | }
|
---|
418 | }
|
---|
419 | if (indent) this.indent();
|
---|
420 | const newlineOpts = {
|
---|
421 | addNewlines: opts.addNewlines,
|
---|
422 | nextNodeStartLine: 0
|
---|
423 | };
|
---|
424 | const separator = opts.separator ? opts.separator.bind(this) : null;
|
---|
425 | const len = nodes.length;
|
---|
426 | for (let i = 0; i < len; i++) {
|
---|
427 | const node = nodes[i];
|
---|
428 | if (!node) continue;
|
---|
429 | if (opts.statement) this._printNewline(i === 0, newlineOpts);
|
---|
430 | this.print(node, parent, undefined, opts.trailingCommentsLineOffset || 0);
|
---|
431 | opts.iterator == null || opts.iterator(node, i);
|
---|
432 | if (i < len - 1) separator == null || separator();
|
---|
433 | if (opts.statement) {
|
---|
434 | var _node$trailingComment;
|
---|
435 | if (!((_node$trailingComment = node.trailingComments) != null && _node$trailingComment.length)) {
|
---|
436 | this._lastCommentLine = 0;
|
---|
437 | }
|
---|
438 | if (i + 1 === len) {
|
---|
439 | this.newline(1);
|
---|
440 | } else {
|
---|
441 | var _nextNode$loc;
|
---|
442 | const nextNode = nodes[i + 1];
|
---|
443 | newlineOpts.nextNodeStartLine = ((_nextNode$loc = nextNode.loc) == null ? void 0 : _nextNode$loc.start.line) || 0;
|
---|
444 | this._printNewline(true, newlineOpts);
|
---|
445 | }
|
---|
446 | }
|
---|
447 | }
|
---|
448 | if (indent) this.dedent();
|
---|
449 | }
|
---|
450 | printAndIndentOnComments(node, parent) {
|
---|
451 | const indent = node.leadingComments && node.leadingComments.length > 0;
|
---|
452 | if (indent) this.indent();
|
---|
453 | this.print(node, parent);
|
---|
454 | if (indent) this.dedent();
|
---|
455 | }
|
---|
456 | printBlock(parent) {
|
---|
457 | const node = parent.body;
|
---|
458 | if (node.type !== "EmptyStatement") {
|
---|
459 | this.space();
|
---|
460 | }
|
---|
461 | this.print(node, parent);
|
---|
462 | }
|
---|
463 | _printTrailingComments(node, parent, lineOffset) {
|
---|
464 | const {
|
---|
465 | innerComments,
|
---|
466 | trailingComments
|
---|
467 | } = node;
|
---|
468 | if (innerComments != null && innerComments.length) {
|
---|
469 | this._printComments(2, innerComments, node, parent, lineOffset);
|
---|
470 | }
|
---|
471 | if (trailingComments != null && trailingComments.length) {
|
---|
472 | this._printComments(2, trailingComments, node, parent, lineOffset);
|
---|
473 | }
|
---|
474 | }
|
---|
475 | _printLeadingComments(node, parent) {
|
---|
476 | const comments = node.leadingComments;
|
---|
477 | if (!(comments != null && comments.length)) return;
|
---|
478 | this._printComments(0, comments, node, parent);
|
---|
479 | }
|
---|
480 | _maybePrintInnerComments() {
|
---|
481 | if (this._endsWithInnerRaw) this.printInnerComments();
|
---|
482 | this._endsWithInnerRaw = true;
|
---|
483 | this._indentInnerComments = true;
|
---|
484 | }
|
---|
485 | printInnerComments() {
|
---|
486 | const node = this._currentNode;
|
---|
487 | const comments = node.innerComments;
|
---|
488 | if (!(comments != null && comments.length)) return;
|
---|
489 | const hasSpace = this.endsWith(32);
|
---|
490 | const indent = this._indentInnerComments;
|
---|
491 | const printedCommentsCount = this._printedComments.size;
|
---|
492 | if (indent) this.indent();
|
---|
493 | this._printComments(1, comments, node);
|
---|
494 | if (hasSpace && printedCommentsCount !== this._printedComments.size) {
|
---|
495 | this.space();
|
---|
496 | }
|
---|
497 | if (indent) this.dedent();
|
---|
498 | }
|
---|
499 | noIndentInnerCommentsHere() {
|
---|
500 | this._indentInnerComments = false;
|
---|
501 | }
|
---|
502 | printSequence(nodes, parent, opts = {}) {
|
---|
503 | var _opts$indent;
|
---|
504 | opts.statement = true;
|
---|
505 | (_opts$indent = opts.indent) != null ? _opts$indent : opts.indent = false;
|
---|
506 | this.printJoin(nodes, parent, opts);
|
---|
507 | }
|
---|
508 | printList(items, parent, opts = {}) {
|
---|
509 | if (opts.separator == null) {
|
---|
510 | opts.separator = commaSeparator;
|
---|
511 | }
|
---|
512 | this.printJoin(items, parent, opts);
|
---|
513 | }
|
---|
514 | _printNewline(newLine, opts) {
|
---|
515 | const format = this.format;
|
---|
516 | if (format.retainLines || format.compact) return;
|
---|
517 | if (format.concise) {
|
---|
518 | this.space();
|
---|
519 | return;
|
---|
520 | }
|
---|
521 | if (!newLine) {
|
---|
522 | return;
|
---|
523 | }
|
---|
524 | const startLine = opts.nextNodeStartLine;
|
---|
525 | const lastCommentLine = this._lastCommentLine;
|
---|
526 | if (startLine > 0 && lastCommentLine > 0) {
|
---|
527 | const offset = startLine - lastCommentLine;
|
---|
528 | if (offset >= 0) {
|
---|
529 | this.newline(offset || 1);
|
---|
530 | return;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | if (this._buf.hasContent()) {
|
---|
534 | this.newline(1);
|
---|
535 | }
|
---|
536 | }
|
---|
537 | _shouldPrintComment(comment) {
|
---|
538 | if (comment.ignore) return 0;
|
---|
539 | if (this._printedComments.has(comment)) return 0;
|
---|
540 | if (this._noLineTerminator && HAS_NEWLINE_OR_BlOCK_COMMENT_END.test(comment.value)) {
|
---|
541 | return 2;
|
---|
542 | }
|
---|
543 | this._printedComments.add(comment);
|
---|
544 | if (!this.format.shouldPrintComment(comment.value)) {
|
---|
545 | return 0;
|
---|
546 | }
|
---|
547 | return 1;
|
---|
548 | }
|
---|
549 | _printComment(comment, skipNewLines) {
|
---|
550 | const noLineTerminator = this._noLineTerminator;
|
---|
551 | const isBlockComment = comment.type === "CommentBlock";
|
---|
552 | const printNewLines = isBlockComment && skipNewLines !== 1 && !this._noLineTerminator;
|
---|
553 | if (printNewLines && this._buf.hasContent() && skipNewLines !== 2) {
|
---|
554 | this.newline(1);
|
---|
555 | }
|
---|
556 | const lastCharCode = this.getLastChar();
|
---|
557 | if (lastCharCode !== 91 && lastCharCode !== 123) {
|
---|
558 | this.space();
|
---|
559 | }
|
---|
560 | let val;
|
---|
561 | if (isBlockComment) {
|
---|
562 | const {
|
---|
563 | _parenPushNewlineState
|
---|
564 | } = this;
|
---|
565 | if ((_parenPushNewlineState == null ? void 0 : _parenPushNewlineState.printed) === false && HAS_NEWLINE.test(comment.value)) {
|
---|
566 | this.tokenChar(40);
|
---|
567 | this.indent();
|
---|
568 | _parenPushNewlineState.printed = true;
|
---|
569 | }
|
---|
570 | val = `/*${comment.value}*/`;
|
---|
571 | if (this.format.indent.adjustMultilineComment) {
|
---|
572 | var _comment$loc;
|
---|
573 | const offset = (_comment$loc = comment.loc) == null ? void 0 : _comment$loc.start.column;
|
---|
574 | if (offset) {
|
---|
575 | const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
|
---|
576 | val = val.replace(newlineRegex, "\n");
|
---|
577 | }
|
---|
578 | if (this.format.concise) {
|
---|
579 | val = val.replace(/\n(?!$)/g, `\n`);
|
---|
580 | } else {
|
---|
581 | let indentSize = this.format.retainLines ? 0 : this._buf.getCurrentColumn();
|
---|
582 | if (this._shouldIndent(47) || this.format.retainLines) {
|
---|
583 | indentSize += this._getIndent();
|
---|
584 | }
|
---|
585 | val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
|
---|
586 | }
|
---|
587 | }
|
---|
588 | } else if (!noLineTerminator) {
|
---|
589 | val = `//${comment.value}`;
|
---|
590 | } else {
|
---|
591 | val = `/*${comment.value}*/`;
|
---|
592 | }
|
---|
593 | if (this.endsWith(47)) this._space();
|
---|
594 | this.source("start", comment.loc);
|
---|
595 | this._append(val, isBlockComment);
|
---|
596 | if (!isBlockComment && !noLineTerminator) {
|
---|
597 | this.newline(1, true);
|
---|
598 | }
|
---|
599 | if (printNewLines && skipNewLines !== 3) {
|
---|
600 | this.newline(1);
|
---|
601 | }
|
---|
602 | }
|
---|
603 | _printComments(type, comments, node, parent, lineOffset = 0) {
|
---|
604 | const nodeLoc = node.loc;
|
---|
605 | const len = comments.length;
|
---|
606 | let hasLoc = !!nodeLoc;
|
---|
607 | const nodeStartLine = hasLoc ? nodeLoc.start.line : 0;
|
---|
608 | const nodeEndLine = hasLoc ? nodeLoc.end.line : 0;
|
---|
609 | let lastLine = 0;
|
---|
610 | let leadingCommentNewline = 0;
|
---|
611 | const maybeNewline = this._noLineTerminator ? function () {} : this.newline.bind(this);
|
---|
612 | for (let i = 0; i < len; i++) {
|
---|
613 | const comment = comments[i];
|
---|
614 | const shouldPrint = this._shouldPrintComment(comment);
|
---|
615 | if (shouldPrint === 2) {
|
---|
616 | hasLoc = false;
|
---|
617 | break;
|
---|
618 | }
|
---|
619 | if (hasLoc && comment.loc && shouldPrint === 1) {
|
---|
620 | const commentStartLine = comment.loc.start.line;
|
---|
621 | const commentEndLine = comment.loc.end.line;
|
---|
622 | if (type === 0) {
|
---|
623 | let offset = 0;
|
---|
624 | if (i === 0) {
|
---|
625 | if (this._buf.hasContent() && (comment.type === "CommentLine" || commentStartLine !== commentEndLine)) {
|
---|
626 | offset = leadingCommentNewline = 1;
|
---|
627 | }
|
---|
628 | } else {
|
---|
629 | offset = commentStartLine - lastLine;
|
---|
630 | }
|
---|
631 | lastLine = commentEndLine;
|
---|
632 | maybeNewline(offset);
|
---|
633 | this._printComment(comment, 1);
|
---|
634 | if (i + 1 === len) {
|
---|
635 | maybeNewline(Math.max(nodeStartLine - lastLine, leadingCommentNewline));
|
---|
636 | lastLine = nodeStartLine;
|
---|
637 | }
|
---|
638 | } else if (type === 1) {
|
---|
639 | const offset = commentStartLine - (i === 0 ? nodeStartLine : lastLine);
|
---|
640 | lastLine = commentEndLine;
|
---|
641 | maybeNewline(offset);
|
---|
642 | this._printComment(comment, 1);
|
---|
643 | if (i + 1 === len) {
|
---|
644 | maybeNewline(Math.min(1, nodeEndLine - lastLine));
|
---|
645 | lastLine = nodeEndLine;
|
---|
646 | }
|
---|
647 | } else {
|
---|
648 | const offset = commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine);
|
---|
649 | lastLine = commentEndLine;
|
---|
650 | maybeNewline(offset);
|
---|
651 | this._printComment(comment, 1);
|
---|
652 | }
|
---|
653 | } else {
|
---|
654 | hasLoc = false;
|
---|
655 | if (shouldPrint !== 1) {
|
---|
656 | continue;
|
---|
657 | }
|
---|
658 | if (len === 1) {
|
---|
659 | const singleLine = comment.loc ? comment.loc.start.line === comment.loc.end.line : !HAS_NEWLINE.test(comment.value);
|
---|
660 | const shouldSkipNewline = singleLine && !isStatement(node) && !isClassBody(parent) && !isTSInterfaceBody(parent) && !isTSEnumDeclaration(parent);
|
---|
661 | if (type === 0) {
|
---|
662 | this._printComment(comment, shouldSkipNewline && node.type !== "ObjectExpression" || singleLine && isFunction(parent, {
|
---|
663 | body: node
|
---|
664 | }) ? 1 : 0);
|
---|
665 | } else if (shouldSkipNewline && type === 2) {
|
---|
666 | this._printComment(comment, 1);
|
---|
667 | } else {
|
---|
668 | this._printComment(comment, 0);
|
---|
669 | }
|
---|
670 | } else if (type === 1 && !(node.type === "ObjectExpression" && node.properties.length > 1) && node.type !== "ClassBody" && node.type !== "TSInterfaceBody") {
|
---|
671 | this._printComment(comment, i === 0 ? 2 : i === len - 1 ? 3 : 0);
|
---|
672 | } else {
|
---|
673 | this._printComment(comment, 0);
|
---|
674 | }
|
---|
675 | }
|
---|
676 | }
|
---|
677 | if (type === 2 && hasLoc && lastLine) {
|
---|
678 | this._lastCommentLine = lastLine;
|
---|
679 | }
|
---|
680 | }
|
---|
681 | }
|
---|
682 | Object.assign(Printer.prototype, generatorFunctions);
|
---|
683 | {
|
---|
684 | Printer.prototype.Noop = function Noop() {};
|
---|
685 | }
|
---|
686 | var _default = exports.default = Printer;
|
---|
687 | function commaSeparator() {
|
---|
688 | this.tokenChar(44);
|
---|
689 | this.space();
|
---|
690 | }
|
---|
691 |
|
---|
692 | //# sourceMappingURL=printer.js.map
|
---|