MCPcopy
hub / github.com/benjamn/recast / printLeadingComment

Function printLeadingComment

lib/comments.ts:266–298  ·  view source on GitHub ↗
(commentPath: any, print: any)

Source from the content-addressed store, hash-verified

264}
265
266function printLeadingComment(commentPath: any, print: any) {
267 const comment = commentPath.getValue();
268 n.Comment.assert(comment);
269
270 const loc = comment.loc;
271 const lines = loc && loc.lines;
272 const parts = [print(commentPath)];
273
274 if (comment.trailing) {
275 // When we print trailing comments as leading comments, we don't
276 // want to bring any trailing spaces along.
277 parts.push("\n");
278 } else if (lines instanceof Lines) {
279 const trailingSpace = lines.slice(
280 loc.end,
281 lines.skipSpaces(loc.end) || lines.lastPos(),
282 );
283
284 if (trailingSpace.length === 1) {
285 // If the trailing space contains no newlines, then we want to
286 // preserve it exactly as we found it.
287 parts.push(trailingSpace);
288 } else {
289 // If the trailing space contains newlines, then replace it
290 // with just that many newlines, with all other spaces removed.
291 parts.push(new Array(trailingSpace.length).join("\n"));
292 }
293 } else {
294 parts.push("\n");
295 }
296
297 return concat(parts);
298}
299
300function printTrailingComment(commentPath: any, print: any) {
301 const comment = commentPath.getValue(commentPath);

Callers 1

printCommentsFunction · 0.85

Calls 7

concatFunction · 0.90
getValueMethod · 0.80
skipSpacesMethod · 0.80
lastPosMethod · 0.80
joinMethod · 0.80
printFunction · 0.70
sliceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…