44 }
55} = require ( 'prettier/standalone' ) ;
66
7+ const privateUtil = require ( '../prettier-comments/common/util' ) ;
78const printSeparatedList = require ( './print-separated-list' ) ;
9+ const printSeparatedItem = require ( './print-separated-item' ) ;
10+ const printComments = require ( './print-comments' ) ;
811
912const functionName = ( node , options ) => {
1013 if ( node . isConstructor && ! node . name ) return 'constructor' ;
@@ -23,18 +26,37 @@ const functionName = (node, options) => {
2326 return names [ name ] ;
2427} ;
2528
26- const parameters = ( parametersType , node , path , print ) =>
27- node [ parametersType ] && node [ parametersType ] . length > 0
28- ? printSeparatedList ( path . map ( print , parametersType ) , {
29- separator : concat ( [
30- ',' ,
31- // To keep consistency any list of parameters will split if it's longer than 2.
32- // For more information see:
33- // https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
34- node [ parametersType ] . length > 2 ? hardline : line
35- ] )
36- } )
37- : '' ;
29+ const parameters = ( parametersType , node , path , print , options ) => {
30+ if ( node [ parametersType ] && node [ parametersType ] . length > 0 ) {
31+ return printSeparatedList ( path . map ( print , parametersType ) , {
32+ separator : concat ( [
33+ ',' ,
34+ // To keep consistency any list of parameters will split if it's longer than 2.
35+ // For more information see:
36+ // https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
37+ node [ parametersType ] . length > 2 ? hardline : line
38+ ] )
39+ } ) ;
40+ }
41+ if ( node . comments && node . comments . length > 0 ) {
42+ // we add a check to see if the comment is inside the parentheses
43+ const paremeterComments = printComments (
44+ node ,
45+ path ,
46+ options ,
47+ ( comment ) =>
48+ privateUtil . getNextNonSpaceNonCommentCharacter (
49+ options . originalText ,
50+ comment ,
51+ options . locEnd
52+ ) === ')'
53+ ) ;
54+ return paremeterComments . parts . length > 0
55+ ? printSeparatedItem ( paremeterComments )
56+ : '' ;
57+ }
58+ return '' ;
59+ } ;
3860
3961const visibility = ( node ) =>
4062 node . visibility && node . visibility !== 'default'
@@ -64,12 +86,12 @@ const modifiers = (node, path, print) =>
6486 ? concat ( [ line , join ( line , path . map ( print , 'modifiers' ) ) ] )
6587 : '' ;
6688
67- const returnParameters = ( node , path , print ) =>
89+ const returnParameters = ( node , path , print , options ) =>
6890 node . returnParameters
6991 ? concat ( [
7092 line ,
7193 'returns (' ,
72- parameters ( 'returnParameters' , node , path , print ) ,
94+ parameters ( 'returnParameters' , node , path , print , options ) ,
7395 ')'
7496 ] )
7597 : '' ;
@@ -83,17 +105,19 @@ const FunctionDefinition = {
83105 concat ( [
84106 functionName ( node , options ) ,
85107 '(' ,
86- parameters ( 'parameters' , node , path , print ) ,
108+ parameters ( 'parameters' , node , path , print , options ) ,
87109 ')' ,
88110 indent (
89111 group (
90112 concat ( [
113+ // TODO: sort comments for modifiers and return parameters
114+ printComments ( node , path , options ) ,
91115 visibility ( node ) ,
92116 stateMutability ( node ) ,
93117 virtual ( node ) ,
94118 override ( node , path , print ) ,
95119 modifiers ( node , path , print ) ,
96- returnParameters ( node , path , print ) ,
120+ returnParameters ( node , path , print , options ) ,
97121 signatureEnd ( node )
98122 ] )
99123 )
0 commit comments