@@ -25,8 +25,8 @@ const {
2525} = builders ;
2626
2727export default {
28- block ( path , print ) {
29- return printBlock ( path , printBlockStatements ( path , print ) ) ;
28+ block ( path , print , options ) {
29+ return printBlock ( path , printBlockStatements ( path , print ) , options ) ;
3030 } ,
3131
3232 local_variable_declaration : printVariableDeclaration ,
@@ -47,7 +47,7 @@ export default {
4747 : [ expression , ";" ] ;
4848 } ,
4949
50- if_statement ( path , print ) {
50+ if_statement ( path , print , options ) {
5151 const statement = [ "if " , path . call ( print , "conditionNode" ) ] ;
5252
5353 if ( path . node . consequenceNode . type === ";" ) {
@@ -63,6 +63,8 @@ export default {
6363 const danglingComments = printDanglingComments ( path ) ;
6464 if ( danglingComments . length ) {
6565 statement . push ( hardline , ...danglingComments , hardline ) ;
66+ } else if ( options ?. braceStyle === "next-line" ) {
67+ statement . push ( hardline ) ;
6668 } else {
6769 const ifHasBlock = path . node . consequenceNode . type === SyntaxType . Block ;
6870 statement . push ( ifHasBlock ? " " : hardline ) ;
@@ -91,8 +93,8 @@ export default {
9193 ] ) ;
9294 } ,
9395
94- switch_block ( path , print ) {
95- return printBlock ( path , path . map ( print , "namedChildren" ) ) ;
96+ switch_block ( path , print , options ) {
97+ return printBlock ( path , path . map ( print , "namedChildren" ) , options ) ;
9698 } ,
9799
98100 switch_block_statement_group ( path , print ) {
@@ -192,15 +194,16 @@ export default {
192194 }
193195 } ,
194196
195- do_statement ( path , print ) {
197+ do_statement ( path , print , options ) {
196198 const hasEmptyStatement = path . node . bodyNode . type === ";" ;
197- return [
198- "do" ,
199- hasEmptyStatement ? ";" : [ " " , path . call ( print , "bodyNode" ) ] ,
200- " while " ,
201- path . call ( print , "conditionNode" ) ,
202- ";"
203- ] ;
199+ const isNextLine = options ?. braceStyle === "next-line" ;
200+ const whilePrefix = isNextLine ? [ hardline , "while " ] : " while " ;
201+ const body = hasEmptyStatement
202+ ? ";"
203+ : isNextLine
204+ ? path . call ( print , "bodyNode" )
205+ : [ " " , path . call ( print , "bodyNode" ) ] ;
206+ return [ "do" , body , whilePrefix , path . call ( print , "conditionNode" ) , ";" ] ;
204207 } ,
205208
206209 for_statement ( path , print ) {
@@ -330,7 +333,7 @@ export default {
330333 ] ;
331334 } ,
332335
333- try_statement ( path , print ) {
336+ try_statement ( path , print , options ) {
334337 const parts = [ "try" , path . call ( print , "bodyNode" ) ] ;
335338
336339 path . each ( child => {
@@ -342,23 +345,26 @@ export default {
342345 }
343346 } , "namedChildren" ) ;
344347
348+ if ( options ?. braceStyle === "next-line" ) {
349+ return parts ;
350+ }
345351 return join ( " " , parts ) ;
346352 } ,
347353
348- catch_clause ( path , print ) {
354+ catch_clause ( path , print , options ) {
349355 const catchFormalParameterIndex = path . node . namedChildren . findIndex (
350356 ( { type } ) => type === SyntaxType . CatchFormalParameter
351357 ) ;
352- return [
353- "catch " ,
354- group (
355- indentInParentheses (
356- path . call ( print , "namedChildren" , catchFormalParameterIndex )
357- )
358- ) ,
359- " ",
360- path . call ( print , "bodyNode" )
361- ] ;
358+ const params = group (
359+ indentInParentheses (
360+ path . call ( print , "namedChildren" , catchFormalParameterIndex )
361+ )
362+ ) ;
363+ const body = path . call ( print , "bodyNode" ) ;
364+ if ( options ?. braceStyle === "next-line" ) {
365+ return [ hardline , "catch ", params , body ] ;
366+ }
367+ return [ "catch " , params , " " , body ] ;
362368 } ,
363369
364370 catch_formal_parameter ( path , print ) {
@@ -383,27 +389,31 @@ export default {
383389 return join ( [ line , "| " ] , path . map ( print , "namedChildren" ) ) ;
384390 } ,
385391
386- finally_clause ( path , print ) {
392+ finally_clause ( path , print , options ) {
393+ if ( options ?. braceStyle === "next-line" ) {
394+ return [ hardline , "finally" , path . call ( print , "namedChildren" , 0 ) ] ;
395+ }
387396 return [ "finally " , path . call ( print , "namedChildren" , 0 ) ] ;
388397 } ,
389398
390- try_with_resources_statement ( path , print ) {
391- const parts = [
392- "try" ,
393- path . call ( print , "resourcesNode" ) ,
394- path . call ( print , "bodyNode" )
395- ] ;
399+ try_with_resources_statement ( path , print , options ) {
400+ const resources = path . call ( print , "resourcesNode" ) ;
401+ const body = path . call ( print , "bodyNode" ) ;
402+ const clauses : Doc [ ] = [ ] ;
396403
397404 path . each ( child => {
398405 if (
399406 child . node . type === SyntaxType . CatchClause ||
400407 child . node . type === SyntaxType . FinallyClause
401408 ) {
402- parts . push ( print ( child ) ) ;
409+ clauses . push ( print ( child ) ) ;
403410 }
404411 } , "namedChildren" ) ;
405412
406- return join ( " " , parts ) ;
413+ if ( options ?. braceStyle === "next-line" ) {
414+ return [ "try " , resources , body , ...clauses ] ;
415+ }
416+ return join ( " " , [ "try" , resources , body , ...clauses ] ) ;
407417 } ,
408418
409419 resource_specification ( path , print ) {
0 commit comments