From 1c4075e091e3800c219ce2ecb1ea6d351adfa1d7 Mon Sep 17 00:00:00 2001 From: 0xGabi Date: Sat, 28 Oct 2023 00:53:01 -0300 Subject: [PATCH] chore: refactor parser types --- .../utils/artifact/generateAragonArtifact.ts | 4 +- .../utils/ast/parseContractFunctions.ts | 65 ++++++++++--------- src/internal/utils/ast/parseContructor.ts | 2 +- .../ast/parseGlobalVariableAssignments.ts | 5 +- src/types/index.ts | 5 +- 5 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/internal/utils/artifact/generateAragonArtifact.ts b/src/internal/utils/artifact/generateAragonArtifact.ts index cf02d2c..24229e6 100644 --- a/src/internal/utils/artifact/generateAragonArtifact.ts +++ b/src/internal/utils/artifact/generateAragonArtifact.ts @@ -29,7 +29,7 @@ export function generateAragonArtifact( flatCode: string ): AragonArtifact { const iface = new ethers.utils.Interface(abi) - + const contractFunctions = parseContractFunctions(flatCode, contractName, { onlyTargetContract: true, }) @@ -40,7 +40,7 @@ export function generateAragonArtifact( roles: parsedFn.roles.map((role) => role.id), notice: parsedFn.notice, abi: - iface.getFunction(parsedFn.sig).format("json") || + iface.getFunction(parsedFn.sig).format('json') || (parsedFn.sig === 'fallback()' ? abiFallback : null), sig: parsedFn.sig, })), diff --git a/src/internal/utils/ast/parseContractFunctions.ts b/src/internal/utils/ast/parseContractFunctions.ts index a93af8d..4768b13 100644 --- a/src/internal/utils/ast/parseContractFunctions.ts +++ b/src/internal/utils/ast/parseContractFunctions.ts @@ -7,7 +7,7 @@ import { FunctionDefinition, ModifierInvocation, VariableDeclaration, -} from '@solidity-parser/parser/dist/ast-types' +} from '@solidity-parser/parser/src/ast-types' import { parseFunctionsNotices } from './parseFunctionNotices' import { AragonContractFunction } from './types' @@ -69,7 +69,7 @@ function parseFunctionSignatureFromNode(node: FunctionDefinition): string { * Using an isolated function to use a switch / return structure */ (nodeParam: VariableDeclaration): string => { - switch (nodeParam.typeName.type) { + switch (nodeParam.typeName?.type) { case 'ElementaryTypeName': return nodeParam.typeName.name case 'ArrayTypeName': @@ -140,7 +140,7 @@ export function parseContractFunctions( ) { const returnParam = node.returnParameters[0] if ( - returnParam.typeName.type === 'ArrayTypeName' && + returnParam.typeName?.type === 'ArrayTypeName' && returnParam.typeName.baseTypeName.type === 'ElementaryTypeName' && returnParam.typeName.baseTypeName.name === 'uint256' ) @@ -162,34 +162,37 @@ export function parseContractFunctions( function parseContract(node: ContractDefinition): void { // Parse functions for (const subNode of node.subNodes) { - if ( - subNode.type === 'FunctionDefinition' && - // Ignore constructors - !subNode.isConstructor && - // Only consider functions that modify state and are public / external - subNode.visibility !== 'internal' && - subNode.visibility !== 'private' && - subNode.stateMutability !== 'view' && - subNode.stateMutability !== 'pure' && - subNode.stateMutability !== 'constant' - ) { - // Check the modifiers - functions.push({ - name: subNode.name || '', - notice: '', - // Parse parameters for signature, some functions may be overloaded - sig: parseFunctionSignatureFromNode(subNode), - // Parse the auth modifiers - roles: subNode.modifiers - .filter((modNode) => ['auth', 'authP'].includes(modNode.name)) - .map((authModNode) => ({ - id: parseRoleIdFromNode(authModNode), - paramCount: parseRoleParamCountFromNode( - authModNode, - authHelperFunctions - ), - })), - }) + if (subNode.type === 'FunctionDefinition') { + const functionNode = subNode as FunctionDefinition + + if ( + // Ignore constructors + !functionNode.isConstructor && + // Only consider functions that modify state and are public / external + functionNode.visibility !== 'internal' && + functionNode.visibility !== 'private' && + functionNode.stateMutability !== 'view' && + functionNode.stateMutability !== 'pure' && + functionNode.stateMutability !== 'constant' + ) { + // Check the modifiers + functions.push({ + name: functionNode.name || '', + notice: '', + // Parse parameters for signature, some functions may be overloaded + sig: parseFunctionSignatureFromNode(functionNode), + // Parse the auth modifiers + roles: functionNode.modifiers + .filter((modNode) => ['auth', 'authP'].includes(modNode.name)) + .map((authModNode) => ({ + id: parseRoleIdFromNode(authModNode), + paramCount: parseRoleParamCountFromNode( + authModNode, + authHelperFunctions + ), + })), + }) + } } } diff --git a/src/internal/utils/ast/parseContructor.ts b/src/internal/utils/ast/parseContructor.ts index ef66e72..a4b64a4 100644 --- a/src/internal/utils/ast/parseContructor.ts +++ b/src/internal/utils/ast/parseContructor.ts @@ -1,5 +1,5 @@ import { parse, visit } from '@solidity-parser/parser' -import { FunctionDefinition } from '@solidity-parser/parser/dist/ast-types' +import { FunctionDefinition } from '@solidity-parser/parser/src/ast-types' /** * Returns true if a contract has a constructor, otherwise false. diff --git a/src/internal/utils/ast/parseGlobalVariableAssignments.ts b/src/internal/utils/ast/parseGlobalVariableAssignments.ts index fa0198c..2483529 100644 --- a/src/internal/utils/ast/parseGlobalVariableAssignments.ts +++ b/src/internal/utils/ast/parseGlobalVariableAssignments.ts @@ -1,5 +1,5 @@ import { parse, visit } from '@solidity-parser/parser' -import { StateVariableDeclaration } from '@solidity-parser/parser/dist/ast-types' +import { StateVariableDeclaration } from '@solidity-parser/parser/src/ast-types' /** * Finds global storage variable declarations with initialized values, e.g 'int a = 1'. @@ -17,7 +17,8 @@ export function parseGlobalVariableAssignments(sourceCode: string): string[] { !variable.isDeclaredConst && variable.expression ) { - variables.push(variable.name) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + variables.push(variable.name!) } }, }) diff --git a/src/types/index.ts b/src/types/index.ts index d1b0ced..19d2ee9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -89,7 +89,10 @@ export interface AragonArtifactFunction { * The function's ABI element is included for convenience of the client * null if ABI is not found for this signature */ - abi: utils.Fragment | null + abi: + | string + | { payable: boolean; stateMutability: string; type: string } + | null } export interface RoleWithBytes extends Role {