Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LF-12515 Improved Solhint #1025

Merged
merged 3 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,41 @@
}
],
"code-complexity": ["error", 20],
"compiler-version": ["error", "^0.8.17"],
"const-name-snakecase": "off",
"func-name-mixedcase": "off",
"explicit-types": ["error", "explicit"],
"max-states-count": ["error", 15],
"no-empty-blocks": "error",
"no-global-import": "error",
"no-unused-import": "error",
"no-unused-vars": "error",
"one-contract-per-file": "off",
"payable-fallback": "error",
"reason-string": ["warn", { "maxLength": 64 }],
"interface-starts-with-i": "error",
"const-name-snakecase": "error",
"constructor-syntax": "error",
"func-visibility": ["error", { "ignoreConstructors": true }],
"not-rely-on-time": "off",
"reason-string": ["warn", { "maxLength": 64 }]
"contract-name-capwords": "error",
"event-name-capwords": "error",
"func-name-mixedcase": "error",
"immutable-vars-naming": ["error", { "immutablesAsConstants": true }],
"use-forbidden-name": "error",
"var-name-mixedcase": "error",
"imports-on-top": "error",
"visibility-modifier-order": "error",
"gas-custom-errors": "error",
"quotes": ["error","double"],
"avoid-call-value": "off",
"avoid-low-level-calls": "off",
"avoid-sha3": "error",
"avoid-suicide": "error",
"avoid-throw": "error",
"avoid-tx-origin": "error",
"check-send-result": "error",
"compiler-version": ["error", "^0.8.17"],
"func-visibility": ["error", { "ignoreConstructors": true }],
"no-inline-assembly": "off",
"not-rely-on-block-hash": "error",
"reentrancy": "error",
"state-visibility": "error"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.6.0",
"prettier-plugin-solidity": "^1.0.0-beta.19",
"solhint": "^3.3.7",
"solhint": "^5.0.5",
"solhint-plugin-prettier": "^0.0.5",
"solidity-coverage": "^0.8.14",
"ts-generator": "^0.1.1",
Expand Down Expand Up @@ -110,7 +110,7 @@
],
"*.sol": [
"prettier --write",
"solhint --fix"
"bun run solhint-filtered.ts"
]
}
}
120 changes: 120 additions & 0 deletions solhint-filtered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import * as fs from 'fs'
import { execSync } from 'child_process'
import * as path from 'path'

// load stored versions from contracts.json
const contractVersions = {
'src/LiFiDiamond.sol': '1.0.0',
'src/Facets/CalldataVerificationFacet.sol': '1.2.0',
'src/Facets/StargateFacet.sol': '2.2.0',
'src/Facets/HopFacet.sol': '2.0.0',
'src/Facets/EmergencyPauseFacet.sol': '1.0.1',
'src/Facets/HopFacetPacked.sol': '1.0.6',
'src/Facets/GasZipFacet.sol': '2.0.2',
'src/Facets/OmniBridgeFacet.sol': '1.0.0',
'src/Facets/AllBridgeFacet.sol': '2.0.0',
'src/Facets/AmarokFacetPacked.sol': '1.0.0',
'src/Facets/PolygonBridgeFacet.sol': '1.0.0',
'src/Facets/AcrossFacet.sol': '2.0.0',
'src/Facets/SquidFacet.sol': '1.0.0',
'src/Facets/DeBridgeDlnFacet.sol': '1.0.0',
'src/Facets/CBridgeFacet.sol': '1.0.0',
'src/Facets/AcrossFacetV3.sol': '1.1.0',
'src/Facets/StargateFacetV2.sol': '1.0.1',
'src/Facets/ThorSwapFacet.sol': '1.2.1',
'src/Facets/MayanFacet.sol': '1.0.0',
'src/Facets/CelerIMFacetMutable.sol': '2.0.0',
'src/Facets/CelerCircleBridgeFacet.sol': '1.0.1',
'src/Facets/AcrossFacetPacked.sol': '1.0.0',
'src/Facets/ArbitrumBridgeFacet.sol': '1.0.0',
'src/Facets/CBridgeFacetPacked.sol': '1.0.3',
'src/Facets/RelayFacet.sol': '1.0.0',
'src/Facets/AcrossFacetPackedV3.sol': '1.2.0',
'src/Facets/AmarokFacet.sol': '3.0.0',
'src/Facets/GnosisBridgeFacet.sol': '1.0.0',
'src/Facets/GenericSwapFacetV3.sol': '1.0.1',
'src/Facets/SymbiosisFacet.sol': '1.0.0',
'src/Libraries/LibUtil.sol': '1.0.0',
'src/Libraries/LibAsset.sol': '1.0.2',
'src/Libraries/LibBytes.sol': '1.0.0',
'src/Periphery/ReceiverStargateV2.sol': '1.1.0',
'src/Periphery/TokenWrapper.sol': '1.1.0',
'src/Periphery/RelayerCelerIM.sol': '2.1.1',
'src/Periphery/Receiver.sol': '2.1.0',
'src/Periphery/Permit2Proxy.sol': '1.0.2',
'src/Periphery/LiFiDEXAggregator.sol': '1.6.0',
'src/Periphery/GasZipPeriphery.sol': '1.0.1',
'src/Periphery/ReceiverAcrossV3.sol': '1.1.0',
'src/Helpers/CelerIMFacetBase.sol': '1.0.0',
'src/Helpers/Validatable.sol': '1.0.0',
'src/Interfaces/IStargateRouter.sol': '1.0.0',
'src/Security/LiFiTimelockController.sol': '1.0.0',
}

// get staged .sol files from lint-staged
const files: string[] = process.argv.slice(2)
const filesToLint: string[] = []

files.forEach((file) => {
// convert absolute path to relative path (relative to your contracts directory)
const relativeFile = path.relative(process.cwd(), file)

if (fs.existsSync(file) && file.endsWith('.sol')) {
const content = fs.readFileSync(file, 'utf8')

// extract @custom:version from the file
const versionMatch = content.match(/@custom:version\s+([\d.]+)/)
const currentVersion = versionMatch ? versionMatch[1] : null

// compare with stored version using the relative path
const storedVersion = contractVersions[relativeFile] || null

if (!currentVersion) {
console.log(
`⚠️ ${relativeFile} has no @custom:version. Including for linting.`
)
filesToLint.push(file)
} else if (
!storedVersion ||
isVersionNewer(currentVersion, storedVersion)
) {
console.log(
`✅ ${relativeFile} has a newer version (${currentVersion}). Linting.`
)
filesToLint.push(file)
} else {
console.log(
`⏭️ ${relativeFile} is unchanged (version ${currentVersion}). Skipping.`
)
}
}
})

// run Solhint only on relevant files
if (filesToLint.length > 0) {
try {
console.log(`Running Solhint on: ${filesToLint.join(', ')}`)
execSync(`npx solhint --noPrompt --fix ${filesToLint.join(' ')}`, {
stdio: 'inherit',
})
} catch (error: any) {
// in case of deeper solhint error you print just error.message if needed:
// console.error(error.message)
process.exit(error.status || 1)
}
} else {
console.log('No Solidity files need linting.')
}

// function to compare versions
function isVersionNewer(current: string, stored: string): boolean {
const parseVersion = (v: string) => v.split('.').map(Number)
const [cMajor, cMinor, cPatch] = parseVersion(current)
const [sMajor, sMinor, sPatch] = parseVersion(stored)

return (
cMajor > sMajor ||
(cMajor === sMajor && cMinor > sMinor) ||
(cMajor === sMajor && cMinor === sMinor && cPatch > sPatch)
)
}
Loading