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