source: imaps-frontend/node_modules/@babel/generator/lib/printer.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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