From eadaa8b1b1c6e2bdf7fdac13396585e0d051cc49 Mon Sep 17 00:00:00 2001 From: zoeyTM Date: Tue, 4 Jun 2024 00:49:11 -0400 Subject: [PATCH] adjust regex for function and event names --- .../internal/utils/identifier-validators.ts | 4 +++- .../test/validations/identifier-validators.ts | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 packages/core/test/validations/identifier-validators.ts diff --git a/packages/core/src/internal/utils/identifier-validators.ts b/packages/core/src/internal/utils/identifier-validators.ts index 28399c786..7d006f464 100644 --- a/packages/core/src/internal/utils/identifier-validators.ts +++ b/packages/core/src/internal/utils/identifier-validators.ts @@ -13,8 +13,10 @@ const solidityIdentifierRegex = /^[a-zA-Z$_][a-zA-Z0-9$_]*$/; /** * A regex capturing the solidity identifier rule but extended to support * the `myfun(uint256,bool)` parameter syntax + * + * This *could* be even stricter, but it works for now and covers obvious mistakes */ -const functionNameRegex = /^[a-zA-Z$_][a-zA-Z0-9$_,()]*$/; +const functionNameRegex = /^[a-zA-Z0-9$_]*(\([a-zA-Z0-9$_,\[\]]*\))?$/; /** * Does the identifier match Ignition's rules for ids. Specifically that they diff --git a/packages/core/test/validations/identifier-validators.ts b/packages/core/test/validations/identifier-validators.ts new file mode 100644 index 000000000..cef0a5fa6 --- /dev/null +++ b/packages/core/test/validations/identifier-validators.ts @@ -0,0 +1,21 @@ +import { assert } from "chai"; + +import { isValidFunctionOrEventName } from "../../src/internal/utils/identifier-validators"; + +describe("isValidFunctionOrEventName", () => { + it("should return true for valid solidity function names", () => { + assert.isTrue(isValidFunctionOrEventName("myFunction")); + assert.isTrue(isValidFunctionOrEventName("myFunction()")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256)")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256)")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256,bool)")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256[],bool)")); + }); + + it("should return false for invalid solidity function names", () => { + assert.isFalse(isValidFunctionOrEventName("myFunction(")); + assert.isFalse(isValidFunctionOrEventName("myFunction(uint)256")); + assert.isFalse(isValidFunctionOrEventName("myFunction(uint256")); + assert.isFalse(isValidFunctionOrEventName("myFunctionuint256)")); + }); +});