From 47841515518b5fe7fe96597d39feda3fa5b865c8 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sat, 25 Mar 2023 20:19:49 -0700 Subject: [PATCH] Implement strict-mode splitFunction --- index.html | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) 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 -- 2.47.3