Skip to content

Commit 0cbe3d8

Browse files
docs: add modifier ordering rule
1 parent 1b4311d commit 0cbe3d8

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ wheels/
99
# Virtual environments
1010
.venv
1111
.idea
12+
13+
site

docs/rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This section documents the available rules in Natrix.
1515
| [NTX9](implicit-export.md) | Implicit Export | Important | Detects when a module exposes all its functions using `__interface__` |
1616
| [NTX10](unused-argument.md) | Unused Argument | Warning | Detects function arguments that are declared but never used |
1717
| [NTX11](argument-naming.md) | Argument Naming Convention | Warning | Ensures function arguments follow a specified naming convention |
18+
| [NTX12](modifiers-ordering.md) | Modifiers Ordering | Style | Enforces consistent ordering of function modifiers |
1819

1920
## Rule Categories
2021

docs/rules/modifiers-ordering.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Modifiers Ordering
2+
3+
| Property | Value |
4+
|----------|-------|
5+
| Rule Code | `NTX12` |
6+
| Severity | Style |
7+
8+
## Background
9+
10+
Vyper allows multiple decorators on functions to specify visibility (@external, @internal, @deploy), mutability (@pure, @view, @nonpayable, @payable), and security properties (@nonreentrant). While the compiler accepts these decorators in any order, maintaining a consistent ordering improves code readability and makes the function's properties immediately clear to readers.
11+
12+
## Purpose
13+
14+
Enforces a consistent ordering of function modifiers in Vyper contracts. The recommended order is:
15+
16+
1. **Visibility** decorators first (@external, @internal, @deploy)
17+
2. **Mutability** decorators second (@pure, @view, @nonpayable, @payable)
18+
3. **Security** decorators last (@nonreentrant)
19+
20+
## Example
21+
22+
```vyper
23+
# Non-compliant - incorrect ordering
24+
@view
25+
@external # visibility should come before mutability
26+
def get_balance() -> uint256:
27+
return self.balance
28+
29+
@nonreentrant
30+
@external # visibility should come before security
31+
@payable
32+
def deposit():
33+
self.balance += msg.value
34+
35+
# Compliant - correct ordering
36+
@external # visibility first
37+
@view # mutability second
38+
def get_balance_correct() -> uint256:
39+
return self.balance
40+
41+
@external # visibility first
42+
@payable # mutability second
43+
@nonreentrant # security last
44+
def deposit_correct():
45+
self.balance += msg.value
46+
47+
# Functions without explicit visibility (defaults to internal)
48+
# Non-compliant
49+
@nonreentrant
50+
@view # mutability should come before security
51+
def internal_func() -> uint256:
52+
return self.balance
53+
54+
# Compliant
55+
@view # mutability first
56+
@nonreentrant # security second
57+
def internal_func_correct() -> uint256:
58+
return self.balance
59+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ nav:
5757
- Implicit Export (NTX9): rules/implicit-export.md
5858
- Unused Argument (NTX10): rules/unused-argument.md
5959
- Argument Naming Convention (NTX11): rules/argument-naming.md
60+
- Modifiers Ordering (NTX12): rules/modifiers-ordering.md
6061
- API Reference: api/index.md
6162
- Development: development/index.md

0 commit comments

Comments
 (0)