naming-convention
Enforces a naming convention across the codebase.
Rule details
Section titled “Rule details”By default, this rule enforces the following naming convention:
- Variables, parameters and function names should be in
camelCase. - Names of type-like entities like contracts, structs, custom errors, etc., should be in
PascalCase. - Enum members should be in
PascalCase.
Examples of correct code for this rule:
contract MyContract { error MyCustomError(uint256 myErrorParam);
uint256 myVariable;
function myFunction(uint256 myFunctionParam) public { uint myLocalVariable; }}
enum MyEnum { FirstValue, SecondValue}Options
Section titled “Options”This rule accepts the same configuration array as typescript-eslint’s naming-convention rule, with some differences:
- Selectors and modifiers are adapted for Solidity (see below).
- The
prefix,suffixandtypesoptions are not supported.
The following selectors are available:
contractinterfacelibrarystateVariablefunctionvariablestructstructMemberenumenumMemberparametermodifiereventeventParameteruserDefinedValueTypeerrorerrorParametermappingParameter
The following group selectors are available:
default: matches all identifiers.typeLike: matchescontract,interface,library,struct,enum,error,event, anduserDefinedValueType.variableLike: matchesstateVariable,function,variable,structMember,parameter,modifier,eventParameter,errorParameter, andmappingParameter
The following modifiers are available:
constant: the variable is marked as constantimmutable: the variable is marked as immutablepublic: the variable or function is marked as publicinternal: the variable or function is marked as internalprivate: the variable or function is marked as privateexternal: the function is marked as externalview: the function is marked as viewpure: the function is marked as purepayable: the function is marked as payablevirtual: the function is marked as virtualoverride: the function overrides a function in a parent contractabstract: the contract is marked as abstractnoParameters: the function has no parametershasParameters: the function has at least one parametercontract: the function is defined in a contractinterface: the function is defined in an interfacelibrary: the function is defined in a library
For example, the following configuration enforces camelCase for everything except for type-like entities, which should be in PascalCase:
[ { "selector": "default", "format": ["camelCase"], "leadingUnderscore": "allow", "trailingUnderscore": "allow" }, { "selector": "typeLike", "format": ["PascalCase"] }]
```jsexport default { rules: { "naming-convention": [ "error", [ { selector: "default", format: ["camelCase"], leadingUnderscore: "allow", trailingUnderscore: "allow", }, { selector: "typeLike", format: ["PascalCase"], }, { selector: "enumMember", format: ["PascalCase"], }, ], ], },};Example configs
Section titled “Example configs”Simple config
Section titled “Simple config”[ { selector: "default", format: ["camelCase"], leadingUnderscore: "allow", trailingUnderscore: "allow", }, { selector: "typeLike", format: ["PascalCase"], }, { selector: "enumMember", format: ["PascalCase"], },]Interfaces should start with I
Section titled “Interfaces should start with I”[ { "selector": "interface", "format": ["PascalCase"], "custom": { "match": true, "regex": "^I[A-Z]" } }]Constants and immutables should be in UPPER_CASE
Section titled “Constants and immutables should be in UPPER_CASE”[ { "selector": "stateVariable", "format": ["UPPER_CASE"], "modifiers": ["constant"] }, { "selector": "stateVariable", "format": ["UPPER_CASE"], "modifiers": ["immutable"] }]⚠️ A name must match all modifiers in an entry, that’s why we need two separate entries for
constantandimmutablein the example above.
Unit tests start with test_ and fuzz tests start with testFuzz_
Section titled “Unit tests start with test_ and fuzz tests start with testFuzz_”[ { selector: 'function', filter: '^test', format: null, custom: { match: true, regex: '^test_', }, modifiers: ['noParameters'], }, { selector: 'function', filter: '^test', format: null, custom: { match: true, regex: '^testFuzz_', }, modifiers: ['hasParameters'], },],