solhint
Advanced tools
Comparing version 4.5.2 to 4.5.3
@@ -0,1 +1,2 @@ | ||
const semver = require('semver') | ||
const BaseChecker = require('../base-checker') | ||
@@ -5,3 +6,3 @@ const { severityDescription } = require('../../doc/utils') | ||
const DEFAULT_SEVERITY = 'warn' | ||
const BASE_VERSION = [0, 8, 4] | ||
const BASE_VERSION = '>=0.8.4' | ||
const ruleId = 'gas-custom-errors' | ||
@@ -66,31 +67,6 @@ const meta = { | ||
parseVersion(version) { | ||
// Remove any leading ^ or ~ and other non-numeric characters before the first digit | ||
const cleanVersion = version.replace(/^[^\d]+/, '') | ||
return cleanVersion.split('.').map((num) => parseInt(num, 10)) | ||
isVersionGreater() { | ||
return semver.intersects(this.solidityVersion, BASE_VERSION) | ||
} | ||
isVersionGreater(node) { | ||
const currentVersion = this.parseVersion(this.solidityVersion) | ||
if (currentVersion.length !== 3) { | ||
this.error(node, `GC: Invalid Solidity version`) | ||
return | ||
} | ||
for (let i = 0; i < BASE_VERSION.length; i++) { | ||
if (currentVersion[i] < BASE_VERSION[i]) { | ||
// If any part of the current version is less than the base version, return false | ||
return false | ||
} else if (currentVersion[i] > BASE_VERSION[i]) { | ||
// If any part of the current version is greater, no need to check further, return true | ||
return true | ||
} | ||
// If parts are equal, continue to the next part | ||
} | ||
// Reaching here means all parts compared are equal, or the base version parts have been exhausted, | ||
// so the current version is at least equal, return true | ||
return true | ||
} | ||
PragmaDirective(node) { | ||
@@ -97,0 +73,0 @@ if (node.type === 'PragmaDirective' && node.name === 'solidity') { |
@@ -43,3 +43,3 @@ const BaseChecker = require('../base-checker') | ||
for (const type of suggestForTypes) { | ||
if (argumentType.startsWith(type)) { | ||
if (argumentType.startsWith(type) && !node.parameters[i].isIndexed) { | ||
// push the position into an array to come back later if there's room for another indexed argument | ||
@@ -46,0 +46,0 @@ positionsOfPossibleSuggestions.push(i) |
{ | ||
"name": "solhint", | ||
"version": "4.5.2", | ||
"version": "4.5.3", | ||
"description": "Solidity Code Linter", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -146,3 +146,3 @@ const assert = require('assert') | ||
it('should NOT raise error for lower versions 0.8.3', () => { | ||
it('should raise error for higher versions 0.8', () => { | ||
let code = funcWith(`revert();`) | ||
@@ -156,4 +156,60 @@ code = replaceSolidityVersion(code, '0.8') | ||
assertErrorCount(report, 1) | ||
assertErrorMessage(report, 'GC: Invalid Solidity version') | ||
assert.equal(report.reports[0].message, 'GC: Use Custom Errors instead of revert statements') | ||
}) | ||
it('should raise error for higher versions 0.9', () => { | ||
let code = funcWith(`revert();`) | ||
code = replaceSolidityVersion(code, '0.9') | ||
const report = linter.processStr(code, { | ||
rules: { 'gas-custom-errors': 'error' }, | ||
}) | ||
assertErrorCount(report, 1) | ||
assert.equal(report.reports[0].message, 'GC: Use Custom Errors instead of revert statements') | ||
}) | ||
it('should NOT raise error for lower versions 0.7', () => { | ||
let code = funcWith(`revert();`) | ||
code = replaceSolidityVersion(code, '0.7') | ||
const report = linter.processStr(code, { | ||
rules: { 'gas-custom-errors': 'error' }, | ||
}) | ||
assertErrorCount(report, 0) | ||
}) | ||
it('should NOT raise error for range versions lower than 0.8.4', () => { | ||
let code = funcWith(`revert();`) | ||
code = replaceSolidityVersion(code, '>= 0.8.1 <= 0.8.3') | ||
const report = linter.processStr(code, { | ||
rules: { 'gas-custom-errors': 'error' }, | ||
}) | ||
assertErrorCount(report, 0) | ||
}) | ||
it('should raise error for range versions higher than 0.8.4', () => { | ||
let code = funcWith(`revert();`) | ||
code = replaceSolidityVersion(code, '> 0.8.4 <= 0.9') | ||
const report = linter.processStr(code, { | ||
rules: { 'gas-custom-errors': 'error' }, | ||
}) | ||
assertErrorCount(report, 1) | ||
}) | ||
it('should raise error for range versions containing 0.8.4', () => { | ||
let code = funcWith(`revert();`) | ||
code = replaceSolidityVersion(code, '> 0.8.1 <= 0.8.6') | ||
const report = linter.processStr(code, { | ||
rules: { 'gas-custom-errors': 'error' }, | ||
}) | ||
assertErrorCount(report, 1) | ||
}) | ||
}) |
@@ -82,2 +82,22 @@ const assert = require('assert') | ||
}) | ||
it('should NOT raise error, all variables are indexed', () => { | ||
const code = contractWith('event Increment (address indexed sender, uint256 indexed value);') | ||
const report = linter.processStr(code, { | ||
rules: { 'gas-indexed-events': 'error' }, | ||
}) | ||
assert.equal(report.errorCount, 0) | ||
}) | ||
it('should NOT raise error, all variables are indexed or not for suggestion', () => { | ||
const code = contractWith('event Increment (address indexed sender, string whatever);') | ||
const report = linter.processStr(code, { | ||
rules: { 'gas-indexed-events': 'error' }, | ||
}) | ||
assert.equal(report.errorCount, 0) | ||
}) | ||
}) |
457264
13280