@@ -13,6 +13,7 @@ import {
1313 */
1414export abstract class JSONCommentWriterBase < CommentDataNodeType > {
1515 private static readonly defaultConfiguration : IJSONCommentConfiguration = {
16+ emptyLineBeforeComments : true ,
1617 spaceAroundCommentSymbol : true ,
1718 styledBlockComment : true ,
1819 maxLineLength : 80
@@ -185,8 +186,7 @@ ${gap} */`;
185186 return 'null' ;
186187 }
187188 const currGap : string = gap + this . indent ;
188- const lineBreakCurrGap : string =
189- currGap ? '\n' + currGap : '' ;
189+ const lineBreakCurrGap : string = currGap ? '\n' + currGap : '' ;
190190 const lineEndComments : { [ index : number ] : string } = { } ;
191191 const partial : string [ ] = [ ] ;
192192 const fnPartialToLine : ( value : string , i : number ) => string =
@@ -197,15 +197,27 @@ ${gap} */`;
197197 }
198198 for ( let i : number = 0 ; i < value . length ; i ++ ) {
199199 const { comments : parts , childJSON, lineEndComment } = this . getChildJSON ( value , i , currGap , node ) ;
200- parts . push ( childJSON || 'null' ) ;
200+
201201 if ( lineEndComment !== undefined ) {
202202 lineEndComments [ i ] = lineEndComment ;
203203 }
204- partial . push ( parts . join ( lineBreakCurrGap ) ) ;
204+
205+ parts . push ( childJSON || 'null' ) ;
206+ const currentItemWithComments : string = parts . join ( lineBreakCurrGap ) ;
207+
208+ if ( this . configuration . emptyLineBeforeComments && i > 0 && parts . length > 1 && currGap ) {
209+ // Not the first item in array && has comment && spaces != 0
210+ // Add a empty line
211+ partial . push ( lineBreakCurrGap + currentItemWithComments ) ;
212+ } else {
213+ partial . push ( currGap + currentItemWithComments ) ;
214+ }
205215 }
206- return currGap ? `[
207- ${ currGap } ${ partial . map ( fnPartialToLine ) . join ( lineBreakCurrGap ) }
208- ${ gap } ]` : `[${ partial . join ( ',' ) } ]` ;
216+ return currGap
217+ ? `[
218+ ${ partial . map ( fnPartialToLine ) . join ( '\n' ) }
219+ ${ gap } ]`
220+ : `[${ partial . join ( ',' ) } ]` ;
209221 } else {
210222 const keys : ( string | number ) [ ] = Array . isArray ( this . replacer ) ? this . replacer : Object . keys ( value ) ;
211223 if ( keys . length === 0 ) {
@@ -214,16 +226,27 @@ ${gap}]` : `[${partial.join(',')}]`;
214226 for ( const k of keys ) {
215227 const { comments : parts , childJSON, lineEndComment } = this . getChildJSON ( value , k , currGap , node ) ;
216228 if ( childJSON ) {
217- parts . push ( JSON . stringify ( k ) + ( currGap ? ': ' : ':' ) + childJSON ) ;
218229 if ( lineEndComment !== undefined ) {
219230 lineEndComments [ partial . length ] = lineEndComment ;
220231 }
221- partial . push ( parts . join ( lineBreakCurrGap ) ) ;
232+
233+ parts . push ( JSON . stringify ( k ) + ( currGap ? ': ' : ':' ) + childJSON ) ;
234+ const currentKVPairWithComments : string = parts . join ( lineBreakCurrGap ) ;
235+
236+ if ( this . configuration . emptyLineBeforeComments && partial . length > 0 && parts . length > 1 && currGap ) {
237+ // Not the first key-value pair in object && has comment && spaces != 0
238+ // Add a empty line
239+ partial . push ( lineBreakCurrGap + currentKVPairWithComments ) ;
240+ } else {
241+ partial . push ( currGap + currentKVPairWithComments ) ;
242+ }
222243 }
223244 }
224- return currGap ? `{
225- ${ currGap } ${ partial . map ( fnPartialToLine ) . join ( lineBreakCurrGap ) }
226- ${ gap } }` : `{${ partial . join ( ',' ) } }` ;
245+ return currGap
246+ ? `{
247+ ${ partial . map ( fnPartialToLine ) . join ( '\n' ) }
248+ ${ gap } }`
249+ : `{${ partial . join ( ',' ) } }` ;
227250 }
228251 } else {
229252 return JSON . stringify ( value ) ;
0 commit comments