Skip to content

Commit

Permalink
Merge pull request #593 from protofire/fix-imports-order-v2
Browse files Browse the repository at this point in the history
Fix imports order v2
  • Loading branch information
dbale-altoros authored Aug 3, 2024
2 parents 8012672 + 534b6dd commit 018c459
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [5.0.3] - 2024-08-03
### Fixed
- New Rule: Imports order [#593](https://github.com/protofire/solhint/pull/593)

<br><br>

## [5.0.2] - 2024-07-25
### Fixed
- `func-named-parameters` exclude abi.encodeX from the rule [#583](https://github.com/protofire/solhint/pull/583) (Thanks to [@0xCLARITY](https://github.com/0xCLARITY))
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM node:20-alpine
LABEL maintainer="[email protected]"
ENV VERSION=5.0.2
ENV VERSION=5.0.3

RUN npm install -g solhint@"$VERSION"
6 changes: 6 additions & 0 deletions docs/rules/naming/func-named-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ functionName({ sender: '0xA81705c8C247C413a19A244938ae7f4A0393944e', amount: 1e1
functionName({ sender: _senderAddress, amount: 1e18, token: _tokenAddress, receiver: _receiverAddress })
```

#### abi.encodeX call with four UNNAMED parameters

```solidity
abi.encodePacked(_senderAddress, 1e18, _tokenAddress, _receiverAddress )
```

### 👎 Examples of **incorrect** code for this rule

#### Function call with four UNNAMED parameters (default 4)
Expand Down
3 changes: 2 additions & 1 deletion docs/rules/naming/imports-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This rule accepts a string option of rule severity. Must be one of "error", "war
### Notes
- Paths starting with "@" like "@openzeppelin/" and urls ("http" and "https") will go first
- Order by hierarchy of directories first, e.g. ./../../ comes before ./../, which comes before ./, which comes before ./foo
- Direct imports come before relative imports
- Order alphabetically for each path at the same level, e.g. ./contract/Zbar.sol comes before ./interface/Ifoo.sol
- Rule does NOT support this kind of import "import * as Alias from "./filename.sol"
- When "--fix", rule will re-write this notation "../folder/file.sol" or this one "../file.sol" to "./../folder/file.sol" or this one "./../file.sol"
Expand All @@ -34,7 +35,7 @@ This rule accepts a string option of rule severity. Must be one of "error", "war
This rule does not have examples.

## Version
This rule is introduced in the latest version.
This rule was introduced in [Solhint 5.0.2](https://github.com/protofire/solhint/tree/v5.0.2)

## Resources
- [Rule source](https://github.com/protofire/solhint/tree/master/lib/rules/naming/imports-order.js)
Expand Down
18 changes: 14 additions & 4 deletions lib/rules/naming/imports-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const meta = {
{
note: 'Order by hierarchy of directories first, e.g. ./../../ comes before ./../, which comes before ./, which comes before ./foo',
},
{
note: 'Direct imports come before relative imports',
},
{
note: 'Order alphabetically for each path at the same level, e.g. ./contract/Zbar.sol comes before ./interface/Ifoo.sol',
},
Expand Down Expand Up @@ -135,18 +138,25 @@ class ImportsOrderChecker extends BaseChecker {
function getHierarchyLevel(path) {
// put very large numbers so these comes first in precedence
const protocolOrder = {
'@': -30000,
'http://': -20000,
'https://': -10000,
'@': -40000,
'http://': -30000,
'https://': -20000,
// eslint-disable-next-line prettier/prettier
folderPath: -10000,
}

// Check for protocol-specific paths and assign them their respective order levels
for (const protocol in protocolOrder) {
if (path.startsWith(protocol)) {
if (protocol !== 'folderPath' && path.startsWith(protocol)) {
return protocolOrder[protocol]
}
}

// Handling for paths that are likely folder names without a leading './'
if (!path.startsWith('./') && /^[a-zA-Z0-9]/.test(path)) {
return protocolOrder.folderPath
}

// Relative path handling
if (path.startsWith('./')) {
// Count the number of '../' sequences to determine depth
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solhint",
"version": "5.0.2",
"version": "5.0.3",
"description": "Solidity Code Linter",
"main": "lib/index.js",
"keywords": [
Expand Down

0 comments on commit 018c459

Please sign in to comment.