@@ -26,6 +26,8 @@ import {
2626 STRUCTS_BLOCK_NAME ,
2727} from "./constants" ;
2828import { ContractInfo , DocumentationBlock , NatSpecDocumentation } from "./types" ;
29+
30+ import pluginSolidity from "prettier-plugin-solidity" ;
2931import prettier = require( "prettier" ) ;
3032
3133export class Parser {
@@ -38,10 +40,10 @@ export class Parser {
3840 this . deref = astDereferencer ( contractBuildInfo . output ) ;
3941 }
4042
41- parseContractInfo ( source : string , name : string ) : ContractInfo {
43+ async parseContractInfo ( source : string , name : string ) : Promise < ContractInfo > {
4244 const sourceUnit : SourceUnit = this . contractBuildInfo . output . sources [ source ] . ast ;
4345 const contractNode : ContractDefinition = sourceUnit . nodes . find (
44- ( node ) => isNodeType ( "ContractDefinition" , node ) && node . name === name
46+ ( node ) => isNodeType ( "ContractDefinition" , node ) && node . name === name ,
4547 ) as ContractDefinition ;
4648
4749 if ( ! contractNode ) {
@@ -58,35 +60,33 @@ export class Parser {
5860 functions = allFunctions . filter ( ( node ) => this . isPublicOrExternal ( node ) ) ;
5961 }
6062
61- const contractInfo : ContractInfo = {
63+ return {
6264 name : contractNode . name ,
6365 isAbstract : contractNode . abstract ,
6466 contractKind : contractNode . contractKind ,
6567 license : this . parseLicense ( sourceUnit ) ,
66- documentations : [
68+ documentations : await Promise . all ( [
6769 this . parseDocumentation ( [ contractNode ] , "" ) ,
6870 this . parseDocumentation ( [ ...findAll ( "EnumDefinition" , contractNode ) ] , ENUMS_BLOCK_NAME ) ,
6971 this . parseDocumentation ( [ ...findAll ( "StructDefinition" , contractNode ) ] , STRUCTS_BLOCK_NAME ) ,
7072 this . parseDocumentation ( [ ...findAll ( "EventDefinition" , contractNode ) ] , EVENTS_BLOCK_NAME ) ,
7173 this . parseDocumentation ( [ ...findAll ( "ErrorDefinition" , contractNode ) ] , ERRORS_BLOCK_NAME ) ,
7274 this . parseDocumentation (
7375 [ ...findAll ( "VariableDeclaration" , contractNode ) ] . filter (
74- ( node ) => node . constant && this . isPublicOrExternal ( node )
76+ ( node ) => node . constant && this . isPublicOrExternal ( node ) ,
7577 ) ,
76- CONSTANTS_BLOCK_NAME
78+ CONSTANTS_BLOCK_NAME ,
7779 ) ,
7880 this . parseDocumentation (
7981 [ ...findAll ( "VariableDeclaration" , contractNode ) ] . filter (
80- ( node ) => ! node . constant && this . isPublicOrExternal ( node )
82+ ( node ) => ! node . constant && this . isPublicOrExternal ( node ) ,
8183 ) ,
82- STATE_VARIABLES_BLOCK_NAME
84+ STATE_VARIABLES_BLOCK_NAME ,
8385 ) ,
8486 this . parseDocumentation ( [ ...findAll ( "ModifierDefinition" , contractNode ) ] , MODIFIERS_BLOCK_NAME ) ,
8587 this . parseDocumentation ( functions , FUNCTIONS_BLOCK_NAME ) ,
86- ] ,
88+ ] ) ,
8789 } ;
88-
89- return contractInfo ;
9090 }
9191
9292 isInternal ( node : FunctionDefinition | VariableDeclaration ) : boolean {
@@ -105,8 +105,8 @@ export class Parser {
105105 return node . functionSelector ? ` (0x${ node . functionSelector } )` : "" ;
106106 }
107107
108- applyPrettier ( text : string ) : string {
109- return prettier . format ( text , { parser : "solidity-parse" } ) . trim ( ) ;
108+ async applyPrettier ( text : string ) : Promise < string > {
109+ return ( await prettier . format ( text , { parser : "solidity-parse" , plugins : [ pluginSolidity ] } ) ) . trim ( ) ;
110110 }
111111
112112 parseHeader ( node : any ) : string {
@@ -126,7 +126,7 @@ export class Parser {
126126 }
127127 }
128128
129- parseFullSign ( node : any ) : string {
129+ async parseFullSign ( node : any ) : Promise < string > {
130130 switch ( node . nodeType ) {
131131 case "FunctionDefinition" : {
132132 return this . parseFullFunctionSign ( node ) ;
@@ -158,14 +158,16 @@ export class Parser {
158158 }
159159 }
160160
161- parseDocumentation ( nodes : Node [ ] , name : string ) : DocumentationBlock {
161+ async parseDocumentation ( nodes : Node [ ] , name : string ) : Promise < DocumentationBlock > {
162162 return {
163163 blockName : name ,
164- documentation : nodes . map ( ( node ) => ( {
165- fullSign : this . parseFullSign ( node ) ,
166- header : this . parseHeader ( node ) ,
167- natSpecDocumentation : this . parseNatSpecDocumentation ( node ) ,
168- } ) ) ,
164+ documentation : await Promise . all (
165+ nodes . map ( async ( node ) => ( {
166+ fullSign : await this . parseFullSign ( node ) ,
167+ header : this . parseHeader ( node ) ,
168+ natSpecDocumentation : this . parseNatSpecDocumentation ( node ) ,
169+ } ) ) ,
170+ ) ,
169171 } ;
170172 }
171173
@@ -185,7 +187,7 @@ export class Parser {
185187 parameters : VariableDeclaration [ ] ,
186188 delimiter : string = ", " ,
187189 beginning : string = "" ,
188- ending : string = ""
190+ ending : string = "" ,
189191 ) : string {
190192 return parameters
191193 . map ( ( variableDeclaration ) => {
@@ -210,12 +212,12 @@ export class Parser {
210212 return this . parseStringFromSourceCode ( expression . src ) ;
211213 } )
212214 . join ( ", " ) } )`
213- : "" )
215+ : "" ) ,
214216 )
215217 . join ( " " ) ;
216218 }
217219
218- parseFullFunctionSign ( functionDefinition : FunctionDefinition ) : string {
220+ async parseFullFunctionSign ( functionDefinition : FunctionDefinition ) : Promise < string > {
219221 const kind = functionDefinition . kind ;
220222 const functionName = functionDefinition . name . length === 0 ? "" : ` ${ functionDefinition . name } ` ;
221223 const parameters = this . buildParameterString ( functionDefinition . parameters . parameters ) ;
@@ -232,8 +234,8 @@ export class Parser {
232234 ? ""
233235 : ` returns (${ this . buildParameterString ( functionDefinition . returnParameters . parameters ) } )` ;
234236
235- const formattedRes = this . applyPrettier (
236- `${ kind } ${ functionName } (${ parameters } )${ visibility } ${ stateMutability } ${ modifiers } ${ virtual } ${ overrides } ${ returns } ;`
237+ const formattedRes = await this . applyPrettier (
238+ `${ kind } ${ functionName } (${ parameters } )${ visibility } ${ stateMutability } ${ modifiers } ${ virtual } ${ overrides } ${ returns } ;` ,
237239 ) ;
238240
239241 return formattedRes . substring ( 0 , formattedRes . length - 1 ) ;
@@ -280,7 +282,7 @@ export class Parser {
280282 structDefinition . members ,
281283 "\n" ,
282284 "\t" ,
283- ";"
285+ ";" ,
284286 ) } \n}`;
285287 }
286288
@@ -292,7 +294,7 @@ export class Parser {
292294
293295 parseFullModifierSign ( modifierDefinition : ModifierDefinition ) : string {
294296 return `modifier ${ modifierDefinition . name } (${ this . buildParameterString (
295- modifierDefinition . parameters . parameters
297+ modifierDefinition . parameters . parameters ,
296298 ) } )`;
297299 }
298300
@@ -344,7 +346,7 @@ export class Parser {
344346 }
345347
346348 getValidParentNodeToInheritDocumentation (
347- baseNode : FunctionDefinition | VariableDeclaration
349+ baseNode : FunctionDefinition | VariableDeclaration ,
348350 ) : FunctionDefinition | null {
349351 if ( ! baseNode . baseFunctions || baseNode . baseFunctions . length !== 1 ) {
350352 return null ;
@@ -371,7 +373,7 @@ export class Parser {
371373
372374 findFunctionDefinitionByContractName (
373375 node : FunctionDefinition | VariableDeclaration ,
374- contractName : string
376+ contractName : string ,
375377 ) : FunctionDefinition | undefined {
376378 if ( this . deref ( "ContractDefinition" , node . scope ) . canonicalName === contractName ) {
377379 return node as FunctionDefinition ;
0 commit comments