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

Function printStatementSequence

lib/printer.ts:2597–2705  ·  view source on GitHub ↗
(path: any, options: any, print: any)

Source from the content-addressed store, hash-verified

2595}
2596
2597function printStatementSequence(path: any, options: any, print: any) {
2598 const filtered: any[] = [];
2599 let sawComment = false;
2600 let sawStatement = false;
2601
2602 path.each(function (stmtPath: any) {
2603 const stmt = stmtPath.getValue();
2604
2605 // Just in case the AST has been modified to contain falsy
2606 // "statements," it's safer simply to skip them.
2607 if (!stmt) {
2608 return;
2609 }
2610
2611 // Skip printing EmptyStatement nodes to avoid leaving stray
2612 // semicolons lying around.
2613 if (
2614 stmt.type === "EmptyStatement" &&
2615 !(stmt.comments && stmt.comments.length > 0)
2616 ) {
2617 return;
2618 }
2619
2620 if (namedTypes.Comment.check(stmt)) {
2621 // The pretty printer allows a dangling Comment node to act as
2622 // a Statement when the Comment can't be attached to any other
2623 // non-Comment node in the tree.
2624 sawComment = true;
2625 } else if (namedTypes.Statement.check(stmt)) {
2626 sawStatement = true;
2627 } else {
2628 // When the pretty printer encounters a string instead of an
2629 // AST node, it just prints the string. This behavior can be
2630 // useful for fine-grained formatting decisions like inserting
2631 // blank lines.
2632 isString.assert(stmt);
2633 }
2634
2635 // We can't hang onto stmtPath outside of this function, because
2636 // it's just a reference to a mutable FastPath object, so we have
2637 // to go ahead and print it here.
2638 filtered.push({
2639 node: stmt,
2640 printed: print(stmtPath),
2641 });
2642 });
2643
2644 if (sawComment) {
2645 assert.strictEqual(
2646 sawStatement,
2647 false,
2648 "Comments may appear as statements in otherwise empty statement " +
2649 "lists, but may not coexist with non-Comment nodes.",
2650 );
2651 }
2652
2653 let prevTrailingSpace: any = null;
2654 const len = filtered.length;

Callers 1

genericPrintNoParensFunction · 0.85

Calls 7

concatFunction · 0.90
maxSpaceFunction · 0.85
eachMethod · 0.80
getValueMethod · 0.80
skipSpacesMethod · 0.80
joinMethod · 0.80
printFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…