@@ -50,6 +50,7 @@ const {
5050 dedent,
5151 ifBreak,
5252 hardline,
53+ hardlineWithoutBreakParent,
5354 softline,
5455 literalline,
5556 align,
@@ -114,7 +115,15 @@ function genericPrint(path, options, print) {
114115 }
115116
116117 if ( lineShouldEndWithSemicolon ( path ) ) {
117- parts . push ( ";" ) ;
118+ const isPipeChainStatement =
119+ node . kind === "expressionstatement" &&
120+ ( node . expression . kind === "bin" && node . expression . type === "|>" ||
121+ ( node . expression . kind === "assign" &&
122+ node . expression . right . kind === "bin" &&
123+ node . expression . right . type === "|>" ) ) ;
124+ if ( ! isPipeChainStatement ) {
125+ parts . push ( ";" ) ;
126+ }
118127 }
119128
120129 if ( fileShouldEndWithHardline ( path ) ) {
@@ -741,6 +750,28 @@ function shouldInlineLogicalExpression(node) {
741750// precedence level and the AST is structured based on precedence
742751// level, things are naturally broken up correctly, i.e. `&&` is
743752// broken before `+`.
753+ function printPipeChain ( path , print , trailingSemicolon = false ) {
754+ const elements = [ ] ;
755+
756+ function collect ( ) {
757+ const { node } = path ;
758+ if ( node . kind === "bin" && node . type === "|>" ) {
759+ elements . push ( path . call ( print , "left" ) ) ;
760+ path . call ( collect , "right" ) ;
761+ } else {
762+ elements . push ( print ( ) ) ;
763+ }
764+ }
765+
766+ collect ( ) ;
767+
768+ const [ first , ...rest ] = elements ;
769+ const semicolon = trailingSemicolon
770+ ? ifBreak ( [ hardlineWithoutBreakParent , ";" ] , ";" )
771+ : "" ;
772+ return group ( [ first , indent ( rest . flatMap ( ( el ) => [ line , "|> " , el ] ) ) , semicolon ] ) ;
773+ }
774+
744775function printBinaryExpression (
745776 path ,
746777 print ,
@@ -1524,6 +1555,7 @@ function printAssignmentRight(
15241555
15251556 const canBreak =
15261557 ( pureRightNode . kind === "bin" &&
1558+ pureRightNode . type !== "|>" &&
15271559 ! shouldInlineLogicalExpression ( pureRightNode ) ) ||
15281560 ( pureRightNode . kind === "retif" &&
15291561 ( ( ! pureRightNode . trueExpr &&
@@ -2487,6 +2519,16 @@ function printNode(path, options, print) {
24872519 ) ;
24882520 }
24892521 case "bin" : {
2522+ if ( node . type === "|>" ) {
2523+ const { parent, grandparent } = path ;
2524+ const isStatementLevel =
2525+ parent . kind === "expressionstatement" ||
2526+ ( parent . kind === "assign" &&
2527+ grandparent &&
2528+ grandparent . kind === "expressionstatement" ) ;
2529+ return printPipeChain ( path , print , isStatementLevel ) ;
2530+ }
2531+
24902532 const { parent, grandparent : parentParent } = path ;
24912533 const isInsideParenthesis =
24922534 node !== parent . body &&
0 commit comments