11import { doc } from 'prettier' ;
22import {
3- isFirst ,
43 isLast ,
54 isNextLineEmpty ,
6- isPrettier2 ,
7- next ,
8- previous
5+ isPrettier2
96} from './backward-compatibility.js' ;
107
118const { group, indent, join, line, softline, hardline } = doc . builders ;
@@ -24,7 +21,7 @@ export const printComments = (node, path, options, filter = () => true) => {
2421 return null ;
2522 }
2623 comment . printed = true ;
27- return options . printer . printComment ( commentPath ) ;
24+ return options . printer . printComment ( commentPath , options ) ;
2825 } , 'comments' )
2926 . filter ( Boolean )
3027 ) ;
@@ -39,22 +36,6 @@ export const printComments = (node, path, options, filter = () => true) => {
3936 /* c8 ignore stop */
4037} ;
4138
42- const shouldHaveEmptyLine = ( node , checkForLeading ) =>
43- Boolean (
44- // if node is not FunctionDefinition, it should have an empty line
45- node . type !== 'FunctionDefinition' ||
46- // if FunctionDefinition is not abstract, it should have an empty line
47- node . body ||
48- // if FunctionDefinition has the comment we are looking for (trailing or
49- // leading), it should have an empty line
50- node . comments ?. some ( ( comment ) => checkForLeading && comment . leading )
51- ) ;
52-
53- const separatingLine = ( firstNode , secondNode ) =>
54- shouldHaveEmptyLine ( firstNode , false ) || shouldHaveEmptyLine ( secondNode , true )
55- ? hardline
56- : '' ;
57-
5839export function printPreservingEmptyLines ( path , key , options , print ) {
5940 const parts = [ ] ;
6041 path . each ( ( childPath , index ) => {
@@ -71,38 +52,16 @@ export function printPreservingEmptyLines(path, key, options, print) {
7152 parts . push ( hardline ) ;
7253 }
7354
74- // Only attempt to prepend an empty line if `node` is not the first item
75- // and an empty line hasn't already been appended after the previous `node`
76- if (
77- ! isFirst ( childPath , key , index ) &&
78- parts [ parts . length - 2 ] !== hardline
79- ) {
80- if ( nodeType === 'FunctionDefinition' ) {
81- // Prepend FunctionDefinition with an empty line if there should be a
82- // separation with the previous `node`
83- parts . push ( separatingLine ( previous ( childPath , key , index ) , node ) ) ;
84- } else if ( nodeType === 'ContractDefinition' ) {
85- // Prepend ContractDefinition with an empty line
86- parts . push ( hardline ) ;
87- }
88- }
89-
9055 parts . push ( print ( childPath ) ) ;
9156
9257 // Only attempt to append an empty line if `node` is not the last item
93- if ( ! isLast ( childPath , key , index ) ) {
94- if ( isNextLineEmpty ( options . originalText , options . locEnd ( node ) + 1 ) ) {
95- // Append an empty line if the original text already had an one after
96- // the current `node`
97- parts . push ( hardline ) ;
98- } else if ( nodeType === 'FunctionDefinition' ) {
99- // Append FunctionDefinition with an empty line if there should be a
100- // separation with the next `node`
101- parts . push ( separatingLine ( node , next ( childPath , key , index ) ) ) ;
102- } else if ( nodeType === 'ContractDefinition' ) {
103- // Append ContractDefinition with an empty line
104- parts . push ( hardline ) ;
105- }
58+ if (
59+ ! isLast ( childPath , key , index ) &&
60+ isNextLineEmpty ( options . originalText , options . locEnd ( node ) + 1 )
61+ ) {
62+ // Append an empty line if the original text already had an one after
63+ // the current `node`
64+ parts . push ( hardline ) ;
10665 }
10766 } , key ) ;
10867
0 commit comments