From: Ardis Lu Date: Sun, 26 Mar 2023 03:19:49 +0000 (-0700) Subject: Implement strict-mode splitFunction X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=47841515518b5fe7fe96597d39feda3fa5b865c8;p=4-byte-gen Implement strict-mode splitFunction --- diff --git a/index.html b/index.html index db79cd7..8add794 100644 --- a/index.html +++ b/index.html @@ -284,19 +284,24 @@ function setState(code) { const newState = new Map(); - // Assume each function is on its own line - const fns = code.split('\n'); + let strict = false; + const fns = code.split('\n'); // Assume each function is on its own line for (let fn of fns) { // Clean up empty lines fn = fn.trim(); if (fn === '') { continue; } + if (fn.substring(0, 8) === 'contract') { + // Use strict-mode splitFunction for full contracts + strict = true; + continue; + } // Split out the name from the parameters let name, parametersTypes; try { - ({ name, parametersTypes } = splitFunction(fn)); + ({ name, parametersTypes } = splitFunction(fn, strict)); } catch { continue; @@ -366,14 +371,19 @@ /* Business logic */ // Normalize and extract the function name and parameter types - function splitFunction(fn) { - // Clean all characters that definitely do not affect the signature - fn = fn.replaceAll(/[;'"]/g, ''); - + function splitFunction(fn, strict = false) { // Extract the function name and parameters // Original regex is from wagmi-dev/abitype // https://github.com/wagmi-dev/abitype/blob/main/src/human-readable/runtime/signatures.ts#L31 - const functionRegex = /^(function )?(?[a-zA-Z0-9_]+)\((?.*?)\)/; + let functionRegex; + if (strict) { + functionRegex = /^(function )(?[\w]+)\((?.*?)\)/; + } + else { + fn = fn.replaceAll(/[;'"]/g, ''); // Clean all characters that definitely do not affect the signature + functionRegex = /^(function )?(?[\w]+)\((?.*?)\)/; + } + const { name, parameters } = functionRegex.exec(fn).groups; // Drop parameter names and extraneous whitespace, if there are any