Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust regex to handle square brackets #774

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/core/src/internal/utils/identifier-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/validations/identifier-validators.ts
Original file line number Diff line number Diff line change
@@ -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)"));
});
});
Loading