1- import { builders } from "prettier/doc" ;
2- import { concat , dedent , group , indent , join } from "./prettier-builder.js" ;
3- import { printTokenWithComments } from "./comments/format-comments.js" ;
4- import {
5- hasLeadingLineComments ,
6- hasTrailingLineComments
7- } from "./comments/comments-utils.js" ;
8- import {
9- displaySemicolon ,
10- getBlankLinesSeparator ,
11- isStatementEmptyStatement ,
12- putIntoBraces ,
13- rejectAndConcat ,
14- rejectAndJoin ,
15- rejectAndJoinSeps ,
16- rejectSeparators ,
17- sortModifiers
18- } from "./printer-utils.js" ;
19- import { BaseCstPrettierPrinter } from "../base-cst-printer.js" ;
201import {
212 AssertStatementCtx ,
223 BasicForStatementCtx ,
@@ -49,6 +30,7 @@ import {
4930 ResourceListCtx ,
5031 ResourceSpecificationCtx ,
5132 ReturnStatementCtx ,
33+ StatementCstNode ,
5234 StatementCtx ,
5335 StatementExpressionCtx ,
5436 StatementExpressionListCtx ,
@@ -67,6 +49,25 @@ import {
6749 YieldStatementCtx
6850} from "java-parser" ;
6951import { Doc } from "prettier" ;
52+ import { builders } from "prettier/doc" ;
53+ import { BaseCstPrettierPrinter } from "../base-cst-printer.js" ;
54+ import {
55+ hasLeadingLineComments ,
56+ hasTrailingLineComments
57+ } from "./comments/comments-utils.js" ;
58+ import { printTokenWithComments } from "./comments/format-comments.js" ;
59+ import { concat , group , indent , join } from "./prettier-builder.js" ;
60+ import {
61+ displaySemicolon ,
62+ getBlankLinesSeparator ,
63+ isStatementEmptyStatement ,
64+ putIntoBraces ,
65+ rejectAndConcat ,
66+ rejectAndJoin ,
67+ rejectAndJoinSeps ,
68+ rejectSeparators ,
69+ sortModifiers
70+ } from "./printer-utils.js" ;
7071
7172const { line, softline, hardline } = builders ;
7273
@@ -189,18 +190,11 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
189190
190191 ifStatement ( ctx : IfStatementCtx ) {
191192 const expression = this . visit ( ctx . expression ) ;
192-
193- const ifStatement = this . visit ( ctx . statement [ 0 ] , {
194- allowEmptyStatement : true
195- } ) ;
196- const ifSeparator = isStatementEmptyStatement ( ifStatement ) ? "" : " " ;
193+ const ifStatement = this . subStatement ( ctx . statement [ 0 ] ) ;
197194
198195 let elsePart : Doc = "" ;
199196 if ( ctx . Else !== undefined ) {
200- const elseStatement = this . visit ( ctx . statement [ 1 ] , {
201- allowEmptyStatement : true
202- } ) ;
203- const elseSeparator = isStatementEmptyStatement ( elseStatement ) ? "" : " " ;
197+ const elseStatement = this . subStatement ( ctx . statement [ 1 ] , true ) ;
204198
205199 const elseOnSameLine =
206200 hasTrailingLineComments ( ctx . statement [ 0 ] ) ||
@@ -210,20 +204,13 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
210204 ? hardline
211205 : " " ;
212206
213- elsePart = rejectAndJoin ( elseSeparator , [
214- concat ( [ elseOnSameLine , ctx . Else [ 0 ] ] ) ,
215- elseStatement
216- ] ) ;
207+ elsePart = concat ( [ elseOnSameLine , ctx . Else [ 0 ] , elseStatement ] ) ;
217208 }
218209
219- return rejectAndConcat ( [
220- rejectAndJoin ( " " , [
221- ctx . If [ 0 ] ,
222- concat ( [
223- putIntoBraces ( expression , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] ) ,
224- ifSeparator
225- ] )
226- ] ) ,
210+ return concat ( [
211+ ctx . If [ 0 ] ,
212+ " " ,
213+ putIntoBraces ( expression , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] ) ,
227214 ifStatement ,
228215 elsePart
229216 ] ) ;
@@ -337,32 +324,24 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
337324
338325 whileStatement ( ctx : WhileStatementCtx ) {
339326 const expression = this . visit ( ctx . expression ) ;
340- const statement = this . visit ( ctx . statement [ 0 ] , {
341- allowEmptyStatement : true
342- } ) ;
343- const statementSeparator = isStatementEmptyStatement ( statement ) ? "" : " " ;
327+ const statement = this . subStatement ( ctx . statement [ 0 ] ) ;
344328
345- return rejectAndJoin ( " " , [
329+ return concat ( [
346330 ctx . While [ 0 ] ,
347- rejectAndJoin ( statementSeparator , [
348- putIntoBraces ( expression , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] ) ,
349- statement
350- ] )
331+ " " ,
332+ putIntoBraces ( expression , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] ) ,
333+ statement
351334 ] ) ;
352335 }
353336
354337 doStatement ( ctx : DoStatementCtx ) {
355- const statement = this . visit ( ctx . statement [ 0 ] , {
356- allowEmptyStatement : true
357- } ) ;
358- const statementSeparator = isStatementEmptyStatement ( statement ) ? "" : " " ;
359-
338+ const statement = this . subStatement ( ctx . statement [ 0 ] ) ;
360339 const expression = this . visit ( ctx . expression ) ;
361340
362- return rejectAndJoin ( " " , [
363- rejectAndJoin ( statementSeparator , [ ctx . Do [ 0 ] , statement ] ) ,
341+ return join ( " " , [
342+ concat ( [ ctx . Do [ 0 ] , statement ] ) ,
364343 ctx . While [ 0 ] ,
365- rejectAndConcat ( [
344+ concat ( [
366345 putIntoBraces ( expression , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] ) ,
367346 ctx . Semicolon [ 0 ]
368347 ] )
@@ -377,26 +356,21 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
377356 const forInit = this . visit ( ctx . forInit ) ;
378357 const expression = this . visit ( ctx . expression ) ;
379358 const forUpdate = this . visit ( ctx . forUpdate ) ;
380- const statement = this . visit ( ctx . statement [ 0 ] , {
381- allowEmptyStatement : true
382- } ) ;
383- const statementSeparator = isStatementEmptyStatement ( statement ) ? "" : " " ;
359+ const statement = this . subStatement ( ctx . statement [ 0 ] ) ;
384360
385- return rejectAndConcat ( [
386- rejectAndJoin ( " " , [
387- ctx . For [ 0 ] ,
388- putIntoBraces (
389- rejectAndConcat ( [
390- forInit ,
391- rejectAndJoin ( line , [ ctx . Semicolon [ 0 ] , expression ] ) ,
392- rejectAndJoin ( line , [ ctx . Semicolon [ 1 ] , forUpdate ] )
393- ] ) ,
394- softline ,
395- ctx . LBrace [ 0 ] ,
396- ctx . RBrace [ 0 ]
397- )
398- ] ) ,
399- statementSeparator ,
361+ return concat ( [
362+ ctx . For [ 0 ] ,
363+ " " ,
364+ putIntoBraces (
365+ [
366+ forInit ,
367+ rejectAndJoin ( line , [ ctx . Semicolon [ 0 ] , expression ] ) ,
368+ rejectAndJoin ( line , [ ctx . Semicolon [ 1 ] , forUpdate ] )
369+ ] ,
370+ softline ,
371+ ctx . LBrace [ 0 ] ,
372+ ctx . RBrace [ 0 ]
373+ ) ,
400374 statement
401375 ] ) ;
402376 }
@@ -422,17 +396,14 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
422396 enhancedForStatement ( ctx : EnhancedForStatementCtx ) {
423397 const localVariableDeclaration = this . visit ( ctx . localVariableDeclaration ) ;
424398 const expression = this . visit ( ctx . expression ) ;
425- const statement = this . visit ( ctx . statement [ 0 ] , {
426- allowEmptyStatement : true
427- } ) ;
428- const statementSeparator = isStatementEmptyStatement ( statement ) ? "" : " " ;
399+ const statement = this . subStatement ( ctx . statement [ 0 ] ) ;
429400
430- return rejectAndConcat ( [
431- rejectAndJoin ( " " , [ ctx . For [ 0 ] , ctx . LBrace [ 0 ] ] ) ,
432- localVariableDeclaration ,
433- concat ( [ " " , ctx . Colon [ 0 ] , " " ] ) ,
434- expression ,
435- concat ( [ ctx . RBrace [ 0 ] , statementSeparator ] ) ,
401+ return concat ( [
402+ ctx . For [ 0 ] ,
403+ " " ,
404+ ctx . LBrace [ 0 ] ,
405+ join ( " " , [ localVariableDeclaration , ctx . Colon [ 0 ] , expression ] ) ,
406+ ctx . RBrace [ 0 ] ,
436407 statement
437408 ] ) ;
438409 }
@@ -615,4 +586,18 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
615586 variableAccess ( ctx : VariableAccessCtx ) {
616587 return this . visitSingle ( ctx ) ;
617588 }
589+
590+ private subStatement ( subStatement : StatementCstNode , isElse = false ) {
591+ const statement = this . visit ( subStatement , { allowEmptyStatement : true } ) ;
592+ const isBlock =
593+ subStatement . children . statementWithoutTrailingSubstatement ?. [ 0 ] . children
594+ . block !== undefined ;
595+ const isElseIf = isElse && subStatement . children . ifStatement !== undefined ;
596+ if ( isBlock || isElseIf ) {
597+ return [ " " , statement ] ;
598+ } else if ( isStatementEmptyStatement ( statement ) ) {
599+ return statement ;
600+ }
601+ return indent ( [ line , statement ] ) ;
602+ }
618603}
0 commit comments