1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const generator_1 = require("@babel/generator");
|
4 | class Printer {
|
5 | constructor() {
|
6 | this.printQueue = [];
|
7 | }
|
8 | print() {
|
9 | return (this.printQueue.reduce((document, printable) => {
|
10 | if (typeof printable === 'string') {
|
11 | return document + printable;
|
12 | }
|
13 | else {
|
14 | const documentPart = generator_1.default(printable).code;
|
15 | return document + this.indentComments(documentPart);
|
16 | }
|
17 | }, '') + '\n');
|
18 | }
|
19 | enqueue(printable) {
|
20 | if (this.printQueue.length > 0) {
|
21 | this.printQueue.push('\n');
|
22 | this.printQueue.push('\n');
|
23 | }
|
24 | this.printQueue.push(printable);
|
25 | }
|
26 | printAndClear() {
|
27 | const output = this.print();
|
28 | this.printQueue = [];
|
29 | return output;
|
30 | }
|
31 | indentComments(documentPart) {
|
32 | const lines = documentPart.split('\n').filter(Boolean);
|
33 | let currentLine = 0;
|
34 | const newDocumentParts = [];
|
35 | let maxCommentColumn = 0;
|
36 | while (currentLine !== lines.length) {
|
37 | const currentLineContents = lines[currentLine];
|
38 | const commentColumn = currentLineContents.indexOf('//');
|
39 | if (commentColumn > 0) {
|
40 | if (maxCommentColumn < commentColumn) {
|
41 | maxCommentColumn = commentColumn;
|
42 | }
|
43 | const [contents, comment] = currentLineContents.split('//');
|
44 | newDocumentParts.push({
|
45 | main: contents.replace(/\s+$/g, ''),
|
46 | comment: comment ? comment.trim() : null
|
47 | });
|
48 | }
|
49 | else {
|
50 | newDocumentParts.push({
|
51 | main: currentLineContents,
|
52 | comment: null
|
53 | });
|
54 | }
|
55 | currentLine++;
|
56 | }
|
57 | return newDocumentParts
|
58 | .reduce((memo, part) => {
|
59 | const { main, comment } = part;
|
60 | let line;
|
61 | if (comment !== null) {
|
62 | const spacesBetween = maxCommentColumn - main.length;
|
63 | line = `${main}${' '.repeat(spacesBetween)} // ${comment.trim()}`;
|
64 | }
|
65 | else {
|
66 | line = main;
|
67 | }
|
68 | return [...memo, line];
|
69 | }, [])
|
70 | .join('\n');
|
71 | }
|
72 | }
|
73 | exports.default = Printer;
|
74 |
|
\ | No newline at end of file |