From 0b52aaabfe3a6670e142a403c7de57f1357bbe7b Mon Sep 17 00:00:00 2001 From: dbale-altoros Date: Mon, 22 Jul 2024 17:29:33 -0300 Subject: [PATCH] fix: import-order rule --- e2e/08-autofix/imports-order/Foo1AfterFix.sol | 6 +++--- lib/rules/naming/imports-order.js | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/e2e/08-autofix/imports-order/Foo1AfterFix.sol b/e2e/08-autofix/imports-order/Foo1AfterFix.sol index 0e7f38c1..e7ae91c9 100644 --- a/e2e/08-autofix/imports-order/Foo1AfterFix.sol +++ b/e2e/08-autofix/imports-order/Foo1AfterFix.sol @@ -1,6 +1,9 @@ // SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.0; +import '@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol'; +import 'http://github.com/owner/repo/blob/branch/path/to/Contract2.sol'; +import 'https://github.com/owner/repo/blob/branch/path/to/Contract.sol'; import './../../../../token/interfaces/AFakeContract1.sol'; import './../../../../token/interfaces/FakeContract1.sol'; import { FakeContract2 } from './../../../token/interfaces/FakeContract2.sol'; @@ -16,9 +19,6 @@ import { Initializable } from './openzeppelin/contracts-upgradeable/proxy/utils/ import './Ownable.sol'; import './ThisIsAVeryLongFileOnPurposeToTestTheFirstPathShorterThanTheLastOnelooooooooooong.sol'; import { ReentrancyGuardUpgradeable2 } from '@apenzeppelin/ReentrancyGuardUpgradeable2.sol'; -import '@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol'; -import 'http://github.com/owner/repo/blob/branch/path/to/Contract2.sol'; -import 'https://github.com/owner/repo/blob/branch/path/to/Contract.sol'; contract ImportsOrder { constructor() {} diff --git a/lib/rules/naming/imports-order.js b/lib/rules/naming/imports-order.js index 5cc8ec85..6d3a255e 100644 --- a/lib/rules/naming/imports-order.js +++ b/lib/rules/naming/imports-order.js @@ -18,10 +18,10 @@ const meta = { ], notes: [ { - note: 'Order by hierarchy of directories first, e.g. ./../../ comes before ./../, which comes before ./, which comes before ./foo', + note: 'Paths starting with "@" like "@openzeppelin/" and urls ("http" and "https") will go first', }, { - note: 'Paths starting with "@" like "@openzeppelin/" and urls ("http" and "https") will go at last', + note: 'Order by hierarchy of directories first, e.g. ./../../ comes before ./../, which comes before ./, which comes before ./foo', }, { note: 'Order alphabetically for each path at the same level, e.g. ./contract/Zbar.sol comes before ./interface/Ifoo.sol', @@ -71,6 +71,7 @@ class ImportsOrderChecker extends BaseChecker { // order the object to get the ordered and the contract order this.orderedImports = this.sortImports(this.orderedImports) + // console.log('this.orderedImports :>> ', this.fromContractImports) // console.log('NO Order: \n') // this.fromContractImports.forEach((importItem) => console.log(importItem.fullSentence)) // console.log('\n\nOrdered: \n') @@ -132,10 +133,11 @@ class ImportsOrderChecker extends BaseChecker { sortImports(unorderedImports) { // Helper function to determine the hierarchical level of a path function getHierarchyLevel(path) { + // put very large numbers so these comes first in precedence const protocolOrder = { - '@': 1, - 'http://': 2, - 'https://': 3, + '@': -30000, + 'http://': -20000, + 'https://': -10000, } // Check for protocol-specific paths and assign them their respective order levels