diff --git a/packages/core/src/internal/utils/identifier-validators.ts b/packages/core/src/internal/utils/identifier-validators.ts index 28399c786..0ae43a557 100644 --- a/packages/core/src/internal/utils/identifier-validators.ts +++ b/packages/core/src/internal/utils/identifier-validators.ts @@ -13,8 +13,11 @@ 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-Z$_][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..8fbc3de3d --- /dev/null +++ b/packages/core/test/validations/identifier-validators.ts @@ -0,0 +1,31 @@ +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("a")); + assert.isTrue(isValidFunctionOrEventName("aa")); + assert.isTrue(isValidFunctionOrEventName("a1")); + assert.isTrue(isValidFunctionOrEventName("myFunction")); + assert.isTrue(isValidFunctionOrEventName("myFunction()")); + assert.isTrue(isValidFunctionOrEventName("myFunction123()")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256)")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256)")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256,bool)")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256[])")); + assert.isTrue(isValidFunctionOrEventName("myFunction(uint256[],bool)")); + }); + + it("should return false for invalid solidity function names", () => { + assert.isFalse(isValidFunctionOrEventName("1")); + assert.isFalse(isValidFunctionOrEventName("11")); + assert.isFalse(isValidFunctionOrEventName("123myFunction")); + assert.isFalse(isValidFunctionOrEventName("myFunction(")); + assert.isFalse(isValidFunctionOrEventName("myFunction(uint)256")); + assert.isFalse(isValidFunctionOrEventName("myFunction(uint256")); + assert.isFalse(isValidFunctionOrEventName("myFunctionuint256)")); + assert.isFalse(isValidFunctionOrEventName("(uint256)")); + assert.isFalse(isValidFunctionOrEventName("123(uint256)")); + }); +});