Skip to content

Commit

Permalink
ordering-rule custom error support
Browse files Browse the repository at this point in the history
  • Loading branch information
dbale-altoros committed Jan 27, 2023
1 parent a2b8ea3 commit 97a4fdf
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/rules/order/ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ function contractPartOrder(node) {
return [30, 'event definition']
}

if (node.type === 'CustomErrorDefinition') {
return [35, 'custom error definition']
}

if (node.type === 'ModifierDefinition') {
return [40, 'modifier definition']
}
Expand Down
106 changes: 105 additions & 1 deletion test/rules/order/ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('Linter - ordering', () => {
assert.ok(report.messages[0].message.includes('Function order is incorrect'))
})

it('should not raise error when external const goes before public ', () => {
it('should not raise error when external function goes before public ', () => {
const code = contractWith(`
function a() external view {}
function b() public {}
Expand All @@ -158,6 +158,110 @@ describe('Linter - ordering', () => {
assert.equal(report.errorCount, 0)
})

it('should raise an error when custom error is after external function', () => {
const code = contractWith(`
function a() external view {}
error Unauthorized();
function b() public {}
`)

const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(
report.messages[0].message.includes(
'Function order is incorrect, custom error definition can not go after external view function'
)
)
})

it('should raise an error when custom error is after public function', () => {
const code = contractWith(`
function b() public {}
error Unauthorized();
function a() external view {}
`)

const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(
report.messages[0].message.includes(
'Function order is incorrect, custom error definition can not go after public function'
)
)
})

it('should raise an error when custom error is after constructor', () => {
const code = contractWith(`
constructor() {}
error Unauthorized();
`)

const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(
report.messages[0].message.includes(
'Function order is incorrect, custom error definition can not go after constructor'
)
)
})

it('should raise an error when custom error is before event definition', () => {
const code = contractWith(`
error Unauthorized();
event WithdrawRegistered(uint256 receiver);
constructor() {}
`)

const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(
report.messages[0].message.includes(
'Function order is incorrect, event definition can not go after custom error'
)
)
})

it('should not raise an error when custom error is well placed after event and before modifier', () => {
const code = contractWith(`
event WithdrawRegistered(uint256 receiver);
error Unauthorized();
modifier onlyOwner() {}
constructor() {}
`)

const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

assert.equal(report.errorCount, 0)
})

it('should not raise an error when custom error is well placed after state variables and before constructor', () => {
const code = contractWith(`
uint256 public stateVariable;
error Unauthorized();
constructor() {}
`)

const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

assert.equal(report.errorCount, 0)
})

it('should raise error for enum after contract', () => {
const code = `
contract Foo {}
Expand Down

0 comments on commit 97a4fdf

Please sign in to comment.