Skip to content

Commit

Permalink
ordering rule: added support for file level statements
Browse files Browse the repository at this point in the history
  • Loading branch information
dbale-altoros committed Feb 14, 2023
1 parent 97a4fdf commit 0b4b278
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/rules/order/ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class OrderingChecker extends BaseChecker {

SourceUnit(node) {
const children = node.children

this.checkOrder(children, sourceUnitPartOrder)
}

Expand Down Expand Up @@ -90,10 +89,22 @@ function sourceUnitPartOrder(node) {
return [10, 'import directive']
}

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

if (node.type === 'EnumDefinition' || node.type === 'StructDefinition') {
return [20, 'type definition']
}

if (node.type === 'FileLevelConstant') {
return [23, 'file level constant']
}

if (node.type === 'FunctionDefinition') {
return [26, 'free function definition']
}

if (node.type === 'ContractDefinition') {
if (node.kind === 'interface') {
return [30, 'interface']
Expand Down
138 changes: 138 additions & 0 deletions test/rules/order/ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,142 @@ describe('Linter - ordering', () => {

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

it('should raise error when custom error is before import', () => {
const code = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
error Unauthorized();
import "@openzeppelin/contracts/ownership/Ownable.sol";
contract OneNiceContract {}
`
const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

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

it('should not raise error when custom error is defined in correct order', () => {
const code = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/ownership/Ownable.sol";
error Unauthorized();
contract OneNiceContract {}
`
const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

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

it('should raise error when free function is before custom error', () => {
const code = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/ownership/Ownable.sol";
function freeFunction() pure returns (uint256) {
return 1;
}
error Unauthorized();
contract OneNiceContract {}
`
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 free function definition'
)
)
})

it('should not raise error when free function is defined in correct order', () => {
const code = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/ownership/Ownable.sol";
error Unauthorized();
function freeFunction() pure returns (uint256) {
return 1;
}
contract OneNiceContract {}
`
const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

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

it('should raise error when file level constant is defined after free function', () => {
const code = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/ownership/Ownable.sol";
function freeFunction() pure returns (uint256) {
return 1;
}
uint256 constant oneNiceConstant = 1;
contract OneNiceContract {}
`
const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

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

it('should not raise error when file level constant is defined in correct order', () => {
const code = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/ownership/Ownable.sol";
uint256 constant oneNiceConstant = 1;
function freeFunction() pure returns (uint256) {
return 1;
}
contract OneNiceContract {}
`
const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

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

it('should not raise error when all top level code is well placed', () => {
const code = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/ownership/Ownable.sol";
error Unauthorized();
enum MyEnum { A, B }
struct OneNiceStruct { uint256 a; uint256 b; }
uint256 constant oneNiceConstant = 1;
function freeFunction() pure returns (uint256) {
return 1;
}
contract OneNiceContract {}
`
const report = linter.processStr(code, {
rules: { ordering: 'error' },
})

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

0 comments on commit 0b4b278

Please sign in to comment.