Repository navigation
Expand file tree
/
Copy pathprint.js
More file actions
229 lines (195 loc) · 5.46 KB
/
Copy pathprint.js
File metadata and controls
229 lines (195 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import {
breakParent,
hardline,
indent,
join,
line,
lineSuffix,
} from "../../document/builders.js";
import { inheritLabel } from "../../document/utils.js";
import hasNewline from "../../utils/has-newline.js";
import isNonEmptyArray from "../../utils/is-non-empty-array.js";
import isPreviousLineEmpty from "../../utils/is-previous-line-empty.js";
import { skipSpaces } from "../../utils/skip.js";
import skipNewline from "../../utils/skip-newline.js";
/**
* @typedef {import("../../common/ast-path.js").default} AstPath
* @typedef {import("../../document/builders.js").Doc} Doc
*/
const returnTrue = () => true;
function printComment(path, options) {
const comment = path.node;
comment.printed = true;
return options.printer.printComment(path, options);
}
function printLeadingComment(path, options) {
const comment = path.node;
const parts = [printComment(path, options)];
const { printer, originalText, locStart, locEnd } = options;
const isBlock = printer.isBlockComment?.(comment);
// Leading block comments should see if they need to stay on the
// same line or not.
if (isBlock) {
const lineBreak = hasNewline(originalText, locEnd(comment))
? hasNewline(originalText, locStart(comment), {
backwards: true,
})
? hardline
: line
: " ";
parts.push(lineBreak);
} else {
parts.push(hardline);
}
const index = skipNewline(
originalText,
skipSpaces(originalText, locEnd(comment)),
);
if (index !== false && hasNewline(originalText, index)) {
parts.push(hardline);
}
return parts;
}
function printTrailingComment(path, options, previousComment) {
const comment = path.node;
const printed = printComment(path, options);
const { printer, originalText, locStart } = options;
const isBlock = printer.isBlockComment?.(comment);
if (
(previousComment?.hasLineSuffix && !previousComment?.isBlock) ||
hasNewline(originalText, locStart(comment), { backwards: true })
) {
// This allows comments at the end of nested structures:
// {
// x: 1,
// y: 2
// // A comment
// }
// Those kinds of comments are almost always leading comments, but
// here it doesn't go "outside" the block and turns it into a
// trailing comment for `2`. We can simulate the above by checking
// if this a comment on its own line; normal trailing comments are
// always at the end of another expression.
const isLineBeforeEmpty = isPreviousLineEmpty(
originalText,
locStart(comment),
);
return {
doc: lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", printed]),
isBlock,
hasLineSuffix: true,
};
}
if (!isBlock || previousComment?.hasLineSuffix) {
return {
doc: [lineSuffix([" ", printed]), breakParent],
isBlock,
hasLineSuffix: true,
};
}
return { doc: [" ", printed], isBlock, hasLineSuffix: false };
}
/**
* @param {AstPath} path
* @param {{
* indent?: boolean,
* marker?: symbol,
* filter?: (comment) => boolean,
* }} [danglingCommentsPrintOptions]
* @returns {Doc}
*/
function printDanglingComments(
path,
options,
danglingCommentsPrintOptions = {},
) {
const { node } = path;
if (!isNonEmptyArray(node?.comments)) {
return "";
}
const {
indent: shouldIndent = false,
marker,
filter = returnTrue,
} = danglingCommentsPrintOptions;
const parts = [];
path.each(({ node: comment }) => {
if (
comment.leading ||
comment.trailing ||
comment.marker !== marker ||
!filter(comment)
) {
return;
}
parts.push(printComment(path, options));
}, "comments");
if (parts.length === 0) {
return "";
}
const doc = join(hardline, parts);
return shouldIndent ? indent([hardline, doc]) : doc;
}
function printCommentsSeparately(path, options) {
const value = path.node;
if (!value) {
return {};
}
const ignored = options[Symbol.for("printedComments")];
const comments = (value.comments || []).filter(
(comment) => !ignored.has(comment),
);
if (comments.length === 0) {
return { leading: "", trailing: "" };
}
const leadingParts = [];
const trailingParts = [];
let printedTrailingComment;
path.each(() => {
const comment = path.node;
if (ignored?.has(comment)) {
return;
}
const { leading, trailing } = comment;
if (leading) {
leadingParts.push(printLeadingComment(path, options));
} else if (trailing) {
printedTrailingComment = printTrailingComment(
path,
options,
printedTrailingComment,
);
trailingParts.push(printedTrailingComment.doc);
}
}, "comments");
return { leading: leadingParts, trailing: trailingParts };
}
function printComments(path, doc, options) {
const { leading, trailing } = printCommentsSeparately(path, options);
if (!leading && !trailing) {
return doc;
}
return inheritLabel(doc, (doc) => [leading, doc, trailing]);
}
function ensureAllCommentsPrinted(options) {
const {
[Symbol.for("comments")]: comments,
[Symbol.for("printedComments")]: printedComments,
} = options;
for (const comment of comments) {
if (!comment.printed && !printedComments.has(comment)) {
throw new Error(
'Comment "' +
comment.value.trim() +
'" was not printed. Please report this error!',
);
}
delete comment.printed;
}
}
export {
ensureAllCommentsPrinted,
printComments,
printCommentsSeparately,
printDanglingComments,
};