sort-members
Enforces a specific order for top-level elements and contract/interface/library members.
Rule details
Section titled “Rule details”For top-level elements, the enforced order is:
- Pragma directives
- Import directives
- User-defined value type definitions
- Using for directives
- Constant definitions
- Enum definitions
- Struct definitions
- Event definitions
- Error definitions
- Function definitions
- Interface definitions
- Library definitions
- Contract definitions
For contract members, the enforced order is:
- User-defined value type definitions
- Using for directives
- Enum definitions
- Struct definitions
- Event definitions
- Error definitions
- State variable definitions
- Constructor definitions
- Modifier definitions
- Function definitions
- Receive function definitions
- Fallback function definitions
Examples of correct code for this rule:
// pragma directivespragma solidity ^0.8.0;
// import directivesimport { exampleImport } from "./Example.sol";
// user-defined value typestype ExampleType is uint256;
// using for directivesusing { exampleTypeExtension } for ExampleType global;
// constantsuint constant EXAMPLE_CONSTANT = 3141592;
// enumsenum ExampleEnum { FirstValue, SecondValue}
// structsstruct ExampleStruct { uint x; uint y;}
// eventsevent ExampleEvent();
// errorserror ExampleError();
// functionsfunction exampleFunction() pure returns (bool) { return true;}
// interfacesinterface ExampleInterface {}
// librarieslibrary ExampleLibrary {}
// contractscontract ExampleContract { // user-defined value types type ExampleContractType is uint8;
// using for directives using { exampleContractTypeZero } for ExampleContractType;
// enums enum ExampleContractEnum { FirstValue, SecondValue }
// structs struct ExampleContractStruct { uint x; uint y; }
// events event ExampleContractEvent();
// errors error ExampleContractError();
// state variables uint public x;
// constructor constructor () {}
// modifiers modifier m() { _; }
// functions function f() public {}
// receive receive() external payable {}
// fallback fallback() external {}}Options
Section titled “Options”This rule can receive an object option with two optional fields:
file: An array of strings representing the custom order for top-level elements.contract: An array of strings representing the custom order for members of contracts, interfaces, and libraries.
The default value for file is:
[ "pragma", "import", "userDefinedValueType", "usingFor", "constant", "enum", "struct", "event", "error", "function", "interface", "library", "contract"]The default value for contract is:
[ "userDefinedValueType", "usingFor", "enum", "struct", "event", "error", "stateVariable", "constructor", "modifier", "function", "receive", "fallback"]Custom orders don’t have to be exhaustive. For example, if you only care about having state variable before functions, you can define your custom order like this:
"sort-members": [ "error", { "contract": ["stateVariable", "function"] }]