UNPKG

12.8 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _isInteger = _interopRequireDefault(require("lodash/isInteger"));
9
10var _repeat = _interopRequireDefault(require("lodash/repeat"));
11
12var _buffer = _interopRequireDefault(require("./buffer"));
13
14var n = _interopRequireWildcard(require("./node"));
15
16var t = _interopRequireWildcard(require("@babel/types"));
17
18var generatorFunctions = _interopRequireWildcard(require("./generators"));
19
20function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
21
22function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26const SCIENTIFIC_NOTATION = /e/i;
27const ZERO_DECIMAL_INTEGER = /\.0+$/;
28const NON_DECIMAL_LITERAL = /^0[box]/;
29const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/;
30
31class Printer {
32 constructor(format, map) {
33 this.inForStatementInitCounter = 0;
34 this._printStack = [];
35 this._indent = 0;
36 this._insideAux = false;
37 this._printedCommentStarts = {};
38 this._parenPushNewlineState = null;
39 this._noLineTerminator = false;
40 this._printAuxAfterOnNextUserNode = false;
41 this._printedComments = new WeakSet();
42 this._endsWithInteger = false;
43 this._endsWithWord = false;
44 this.format = format || {};
45 this._buf = new _buffer.default(map);
46 }
47
48 generate(ast) {
49 this.print(ast);
50
51 this._maybeAddAuxComment();
52
53 return this._buf.get();
54 }
55
56 indent() {
57 if (this.format.compact || this.format.concise) return;
58 this._indent++;
59 }
60
61 dedent() {
62 if (this.format.compact || this.format.concise) return;
63 this._indent--;
64 }
65
66 semicolon(force = false) {
67 this._maybeAddAuxComment();
68
69 this._append(";", !force);
70 }
71
72 rightBrace() {
73 if (this.format.minified) {
74 this._buf.removeLastSemicolon();
75 }
76
77 this.token("}");
78 }
79
80 space(force = false) {
81 if (this.format.compact) return;
82
83 if (this._buf.hasContent() && !this.endsWith(" ") && !this.endsWith("\n") || force) {
84 this._space();
85 }
86 }
87
88 word(str) {
89 if (this._endsWithWord || this.endsWith("/") && str.indexOf("/") === 0) {
90 this._space();
91 }
92
93 this._maybeAddAuxComment();
94
95 this._append(str);
96
97 this._endsWithWord = true;
98 }
99
100 number(str) {
101 this.word(str);
102 this._endsWithInteger = (0, _isInteger.default)(+str) && !NON_DECIMAL_LITERAL.test(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str[str.length - 1] !== ".";
103 }
104
105 token(str) {
106 if (str === "--" && this.endsWith("!") || str[0] === "+" && this.endsWith("+") || str[0] === "-" && this.endsWith("-") || str[0] === "." && this._endsWithInteger) {
107 this._space();
108 }
109
110 this._maybeAddAuxComment();
111
112 this._append(str);
113 }
114
115 newline(i) {
116 if (this.format.retainLines || this.format.compact) return;
117
118 if (this.format.concise) {
119 this.space();
120 return;
121 }
122
123 if (this.endsWith("\n\n")) return;
124 if (typeof i !== "number") i = 1;
125 i = Math.min(2, i);
126 if (this.endsWith("{\n") || this.endsWith(":\n")) i--;
127 if (i <= 0) return;
128
129 for (let j = 0; j < i; j++) {
130 this._newline();
131 }
132 }
133
134 endsWith(str) {
135 return this._buf.endsWith(str);
136 }
137
138 removeTrailingNewline() {
139 this._buf.removeTrailingNewline();
140 }
141
142 exactSource(loc, cb) {
143 this._catchUp("start", loc);
144
145 this._buf.exactSource(loc, cb);
146 }
147
148 source(prop, loc) {
149 this._catchUp(prop, loc);
150
151 this._buf.source(prop, loc);
152 }
153
154 withSource(prop, loc, cb) {
155 this._catchUp(prop, loc);
156
157 this._buf.withSource(prop, loc, cb);
158 }
159
160 _space() {
161 this._append(" ", true);
162 }
163
164 _newline() {
165 this._append("\n", true);
166 }
167
168 _append(str, queue = false) {
169 this._maybeAddParen(str);
170
171 this._maybeIndent(str);
172
173 if (queue) this._buf.queue(str);else this._buf.append(str);
174 this._endsWithWord = false;
175 this._endsWithInteger = false;
176 }
177
178 _maybeIndent(str) {
179 if (this._indent && this.endsWith("\n") && str[0] !== "\n") {
180 this._buf.queue(this._getIndent());
181 }
182 }
183
184 _maybeAddParen(str) {
185 const parenPushNewlineState = this._parenPushNewlineState;
186 if (!parenPushNewlineState) return;
187 this._parenPushNewlineState = null;
188 let i;
189
190 for (i = 0; i < str.length && str[i] === " "; i++) continue;
191
192 if (i === str.length) return;
193 const cha = str[i];
194
195 if (cha !== "\n") {
196 if (cha !== "/") return;
197 if (i + 1 === str.length) return;
198 const chaPost = str[i + 1];
199 if (chaPost !== "/" && chaPost !== "*") return;
200 }
201
202 this.token("(");
203 this.indent();
204 parenPushNewlineState.printed = true;
205 }
206
207 _catchUp(prop, loc) {
208 if (!this.format.retainLines) return;
209 const pos = loc ? loc[prop] : null;
210
211 if (pos && pos.line !== null) {
212 const count = pos.line - this._buf.getCurrentLine();
213
214 for (let i = 0; i < count; i++) {
215 this._newline();
216 }
217 }
218 }
219
220 _getIndent() {
221 return (0, _repeat.default)(this.format.indent.style, this._indent);
222 }
223
224 startTerminatorless(isLabel = false) {
225 if (isLabel) {
226 this._noLineTerminator = true;
227 return null;
228 } else {
229 return this._parenPushNewlineState = {
230 printed: false
231 };
232 }
233 }
234
235 endTerminatorless(state) {
236 this._noLineTerminator = false;
237
238 if (state && state.printed) {
239 this.dedent();
240 this.newline();
241 this.token(")");
242 }
243 }
244
245 print(node, parent) {
246 if (!node) return;
247 const oldConcise = this.format.concise;
248
249 if (node._compact) {
250 this.format.concise = true;
251 }
252
253 const printMethod = this[node.type];
254
255 if (!printMethod) {
256 throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node && node.constructor.name)}`);
257 }
258
259 this._printStack.push(node);
260
261 const oldInAux = this._insideAux;
262 this._insideAux = !node.loc;
263
264 this._maybeAddAuxComment(this._insideAux && !oldInAux);
265
266 let needsParens = n.needsParens(node, parent, this._printStack);
267
268 if (this.format.retainFunctionParens && node.type === "FunctionExpression" && node.extra && node.extra.parenthesized) {
269 needsParens = true;
270 }
271
272 if (needsParens) this.token("(");
273
274 this._printLeadingComments(node);
275
276 const loc = t.isProgram(node) || t.isFile(node) ? null : node.loc;
277 this.withSource("start", loc, () => {
278 printMethod.call(this, node, parent);
279 });
280
281 this._printTrailingComments(node);
282
283 if (needsParens) this.token(")");
284
285 this._printStack.pop();
286
287 this.format.concise = oldConcise;
288 this._insideAux = oldInAux;
289 }
290
291 _maybeAddAuxComment(enteredPositionlessNode) {
292 if (enteredPositionlessNode) this._printAuxBeforeComment();
293 if (!this._insideAux) this._printAuxAfterComment();
294 }
295
296 _printAuxBeforeComment() {
297 if (this._printAuxAfterOnNextUserNode) return;
298 this._printAuxAfterOnNextUserNode = true;
299 const comment = this.format.auxiliaryCommentBefore;
300
301 if (comment) {
302 this._printComment({
303 type: "CommentBlock",
304 value: comment
305 });
306 }
307 }
308
309 _printAuxAfterComment() {
310 if (!this._printAuxAfterOnNextUserNode) return;
311 this._printAuxAfterOnNextUserNode = false;
312 const comment = this.format.auxiliaryCommentAfter;
313
314 if (comment) {
315 this._printComment({
316 type: "CommentBlock",
317 value: comment
318 });
319 }
320 }
321
322 getPossibleRaw(node) {
323 const extra = node.extra;
324
325 if (extra && extra.raw != null && extra.rawValue != null && node.value === extra.rawValue) {
326 return extra.raw;
327 }
328 }
329
330 printJoin(nodes, parent, opts = {}) {
331 if (!nodes || !nodes.length) return;
332 if (opts.indent) this.indent();
333 const newlineOpts = {
334 addNewlines: opts.addNewlines
335 };
336
337 for (let i = 0; i < nodes.length; i++) {
338 const node = nodes[i];
339 if (!node) continue;
340 if (opts.statement) this._printNewline(true, node, parent, newlineOpts);
341 this.print(node, parent);
342
343 if (opts.iterator) {
344 opts.iterator(node, i);
345 }
346
347 if (opts.separator && i < nodes.length - 1) {
348 opts.separator.call(this);
349 }
350
351 if (opts.statement) this._printNewline(false, node, parent, newlineOpts);
352 }
353
354 if (opts.indent) this.dedent();
355 }
356
357 printAndIndentOnComments(node, parent) {
358 const indent = node.leadingComments && node.leadingComments.length > 0;
359 if (indent) this.indent();
360 this.print(node, parent);
361 if (indent) this.dedent();
362 }
363
364 printBlock(parent) {
365 const node = parent.body;
366
367 if (!t.isEmptyStatement(node)) {
368 this.space();
369 }
370
371 this.print(node, parent);
372 }
373
374 _printTrailingComments(node) {
375 this._printComments(this._getComments(false, node));
376 }
377
378 _printLeadingComments(node) {
379 this._printComments(this._getComments(true, node), true);
380 }
381
382 printInnerComments(node, indent = true) {
383 if (!node.innerComments || !node.innerComments.length) return;
384 if (indent) this.indent();
385
386 this._printComments(node.innerComments);
387
388 if (indent) this.dedent();
389 }
390
391 printSequence(nodes, parent, opts = {}) {
392 opts.statement = true;
393 return this.printJoin(nodes, parent, opts);
394 }
395
396 printList(items, parent, opts = {}) {
397 if (opts.separator == null) {
398 opts.separator = commaSeparator;
399 }
400
401 return this.printJoin(items, parent, opts);
402 }
403
404 _printNewline(leading, node, parent, opts) {
405 if (this.format.retainLines || this.format.compact) return;
406
407 if (this.format.concise) {
408 this.space();
409 return;
410 }
411
412 let lines = 0;
413
414 if (this._buf.hasContent()) {
415 if (!leading) lines++;
416 if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
417 const needs = leading ? n.needsWhitespaceBefore : n.needsWhitespaceAfter;
418 if (needs(node, parent)) lines++;
419 }
420
421 this.newline(lines);
422 }
423
424 _getComments(leading, node) {
425 return node && (leading ? node.leadingComments : node.trailingComments) || [];
426 }
427
428 _printComment(comment, skipNewLines) {
429 if (!this.format.shouldPrintComment(comment.value)) return;
430 if (comment.ignore) return;
431 if (this._printedComments.has(comment)) return;
432
433 this._printedComments.add(comment);
434
435 if (comment.start != null) {
436 if (this._printedCommentStarts[comment.start]) return;
437 this._printedCommentStarts[comment.start] = true;
438 }
439
440 const isBlockComment = comment.type === "CommentBlock";
441 const printNewLines = isBlockComment && !skipNewLines && !this._noLineTerminator;
442 if (printNewLines && this._buf.hasContent()) this.newline(1);
443 if (!this.endsWith("[") && !this.endsWith("{")) this.space();
444 let val = !isBlockComment && !this._noLineTerminator ? `//${comment.value}\n` : `/*${comment.value}*/`;
445
446 if (isBlockComment && this.format.indent.adjustMultilineComment) {
447 const offset = comment.loc && comment.loc.start.column;
448
449 if (offset) {
450 const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
451 val = val.replace(newlineRegex, "\n");
452 }
453
454 const indentSize = Math.max(this._getIndent().length, this._buf.getCurrentColumn());
455 val = val.replace(/\n(?!$)/g, `\n${(0, _repeat.default)(" ", indentSize)}`);
456 }
457
458 if (this.endsWith("/")) this._space();
459 this.withSource("start", comment.loc, () => {
460 this._append(val);
461 });
462 if (printNewLines) this.newline(1);
463 }
464
465 _printComments(comments, inlinePureAnnotation) {
466 if (!comments || !comments.length) return;
467
468 if (inlinePureAnnotation && comments.length === 1 && PURE_ANNOTATION_RE.test(comments[0].value)) {
469 this._printComment(comments[0], this._buf.hasContent() && !this.endsWith("\n"));
470 } else {
471 for (const comment of comments) {
472 this._printComment(comment);
473 }
474 }
475 }
476
477}
478
479exports.default = Printer;
480Object.assign(Printer.prototype, generatorFunctions);
481
482function commaSeparator() {
483 this.token(",");
484 this.space();
485}
\No newline at end of file