Repository: protofire/solhint
Pull request #483 (2023-08-11 14:25:03)

Fix and Bump compiler-version default


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5a3e90b..92070ed 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,9 @@ You should add this rule manually:
 ```
 If not explicitly added, this rule will not be executed.
 
+### SPECIAL ATTENTION
+- RULE: `compiler-version` default was updated from ^0.5.2 to ^0.8.0
+
 ### Added
 - New Rule: Enforces the use of Custom Errors over Require and Revert statements [#475](https://github.com/protofire/solhint/pull/475)
 - New Rule: Enforces the test_ prefix on a file for Foundry users [#476](https://github.com/protofire/solhint/pull/476)
@@ -26,6 +29,12 @@ If not explicitly added, this rule will not be executed.
 - `func-named-parameters` - false positives on builtin functions [#472](https://github.com/protofire/solhint/pull/472)
 - `ordering` - treat initializer weight same as constructor [#474](https://github.com/protofire/solhint/pull/474)
 - `check-send-result` - false positive on `erc777.send()`` function [#477](https://github.com/protofire/solhint/pull/477)
+- `explicit-types` - default value is now taking into account when no value is specified in config [#481](https://github.com/protofire/solhint/pull/481)
+- `compiler-version` - default value is now taking into account when no value is specified in config [#483](https://github.com/protofire/solhint/pull/483)
+
+### Updates
+- Rule: `check-send-result` added config clarification in the new `Notes` section [#482](https://github.com/protofire/solhint/pull/482)
+- Rule: `compiler-version` default was updated from ^0.5.2 to ^0.8.0 [#483](https://github.com/protofire/solhint/pull/483)
 
 
 
diff --git a/conf/rulesets/solhint-all.js b/conf/rulesets/solhint-all.js
index efec741..eab50c5 100644
--- a/conf/rulesets/solhint-all.js
+++ b/conf/rulesets/solhint-all.js
@@ -60,7 +60,7 @@ module.exports = Object.freeze({
     'avoid-throw': 'warn',
     'avoid-tx-origin': 'warn',
     'check-send-result': 'warn',
-    'compiler-version': ['error', '^0.5.8'],
+    'compiler-version': ['error', '^0.8.0'],
     'func-visibility': [
       'warn',
       {
diff --git a/conf/rulesets/solhint-recommended.js b/conf/rulesets/solhint-recommended.js
index e33a36d..072ea68 100644
--- a/conf/rulesets/solhint-recommended.js
+++ b/conf/rulesets/solhint-recommended.js
@@ -42,7 +42,7 @@ module.exports = Object.freeze({
     'avoid-throw': 'warn',
     'avoid-tx-origin': 'warn',
     'check-send-result': 'warn',
-    'compiler-version': ['error', '^0.5.8'],
+    'compiler-version': ['error', '^0.8.0'],
     'func-visibility': [
       'warn',
       {
diff --git a/docs/rules/security/compiler-version.md b/docs/rules/security/compiler-version.md
index e930e93..d7bc39d 100644
--- a/docs/rules/security/compiler-version.md
+++ b/docs/rules/security/compiler-version.md
@@ -20,14 +20,14 @@ This rule accepts an array of options:
 | Index | Description                                           | Default Value |
 | ----- | ----------------------------------------------------- | ------------- |
 | 0     | Rule severity. Must be one of "error", "warn", "off". | error         |
-| 1     | Semver requirement                                    | ^0.5.8        |
+| 1     | Semver requirement                                    | ^0.8.0        |
 
 
 ### Example Config
 ```json
 {
   "rules": {
-    "compiler-version": ["error","^0.5.8"]
+    "compiler-version": ["error","^0.8.0"]
   }
 }
 ```
diff --git a/lib/config.js b/lib/config.js
index f67fe5a..69b3dad 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -27,7 +27,7 @@ module.exports = {
     return this.getBooleanByPath(`rules["${ruleName}"][1][${ruleProperty}]`, defaultValue)
   },
 
-  getString(ruleName, defaultValue) {
+  getStringFromArray(ruleName, defaultValue) {
     if (this.rules && this.rules[ruleName]) {
       const ruleValue = this.rules[ruleName]
       return Array.isArray(ruleValue) ? ruleValue[1] : defaultValue
diff --git a/lib/rules/best-practises/explicit-types.js b/lib/rules/best-practises/explicit-types.js
index c3bc55f..ebf7b60 100644
--- a/lib/rules/best-practises/explicit-types.js
+++ b/lib/rules/best-practises/explicit-types.js
@@ -62,7 +62,8 @@ const meta = {
 class ExplicitTypesChecker extends BaseChecker {
   constructor(reporter, config) {
     super(reporter, ruleId, meta)
-    this.configOption = (config && config.getString(ruleId, DEFAULT_OPTION)) || DEFAULT_OPTION
+    this.configOption =
+      (config && config.getStringFromArray(ruleId, DEFAULT_OPTION)) || DEFAULT_OPTION
     this.isExplicit = this.configOption === 'explicit'
   }
 
diff --git a/lib/rules/security/compiler-version.js b/lib/rules/security/compiler-version.js
index 3bb5132..955a53b 100644
--- a/lib/rules/security/compiler-version.js
+++ b/lib/rules/security/compiler-version.js
@@ -4,7 +4,7 @@ const { severityDescription } = require('../../doc/utils')
 
 const ruleId = 'compiler-version'
 const DEFAULT_SEVERITY = 'error'
-const DEFAULT_SEMVER = '^0.5.8'
+const DEFAULT_SEMVER = '^0.8.0'
 const meta = {
   type: 'security',
 
@@ -36,7 +36,8 @@ class CompilerVersionChecker extends BaseChecker {
   constructor(reporter, config) {
     super(reporter, ruleId, meta)
 
-    this.requirement = (config && config.getString(ruleId, DEFAULT_SEMVER)) || DEFAULT_SEMVER
+    this.requirement =
+      (config && config.getStringFromArray(ruleId, DEFAULT_SEMVER)) || DEFAULT_SEMVER
   }
 
   SourceUnit(node) {


Test patch:
diff --git a/e2e/05-max-warnings/contracts/Foo.sol b/e2e/05-max-warnings/contracts/Foo.sol
index 659755d..1c04574 100644
--- a/e2e/05-max-warnings/contracts/Foo.sol
+++ b/e2e/05-max-warnings/contracts/Foo.sol
@@ -1,5 +1,5 @@
 // SPDX-License-Identifier: MIT
-pragma solidity >=0.5.8;
+pragma solidity >=0.8.0;
 
 contract Foo {
     uint256 public constant test1 = 1;
diff --git a/e2e/06-formatters/contracts/Foo2.sol b/e2e/06-formatters/contracts/Foo2.sol
index 9fb9801..4a5c2af 100644
--- a/e2e/06-formatters/contracts/Foo2.sol
+++ b/e2e/06-formatters/contracts/Foo2.sol
@@ -1,5 +1,5 @@
 // SPDX-License-Identifier: MIT
-pragma solidity >=0.5.8;
+pragma solidity >=0.8.0;
 
 contract Foo {
     uint256 public constant test1 = 1;
diff --git a/e2e/06-formatters/contracts/Foo3.sol b/e2e/06-formatters/contracts/Foo3.sol
index f89c691..8c1c1e0 100644
--- a/e2e/06-formatters/contracts/Foo3.sol
+++ b/e2e/06-formatters/contracts/Foo3.sol
@@ -1,5 +1,5 @@
 // SPDX-License-Identifier: MIT
-pragma solidity >=0.5.8;
+pragma solidity >=0.8.0;
 
 contract Foo {
     uint256 public constant TEST1 = 1;
diff --git a/e2e/06-formatters/helpers/helpers.js b/e2e/06-formatters/helpers/helpers.js
index 0508991..85d43c6 100644
--- a/e2e/06-formatters/helpers/helpers.js
+++ b/e2e/06-formatters/helpers/helpers.js
@@ -3,7 +3,7 @@ const foo1Output = [
     line: 2,
     column: 1,
     severity: 'Error',
-    message: 'Compiler version >=0.6.0 does not satisfy the ^0.5.8 semver requirement',
+    message: 'Compiler version >=0.6.0 does not satisfy the ^0.8.0 semver requirement',
     ruleId: 'compiler-version',
     fix: null,
     filePath: 'contracts/Foo.sol',
diff --git a/e2e/07-foundry-test/contracts/Foo.sol b/e2e/07-foundry-test/contracts/Foo.sol
index d5a6ce1..3ffecf6 100644
--- a/e2e/07-foundry-test/contracts/Foo.sol
+++ b/e2e/07-foundry-test/contracts/Foo.sol
@@ -1,5 +1,5 @@
 // SPDX-License-Identifier: MIT
-pragma solidity >=0.6.0;
+pragma solidity >=0.8.0;
 
 contract Foo {
     uint256 public constant TEST = 1;
diff --git a/test/rules/best-practises/no-global-import.js b/test/rules/best-practises/no-global-import.js
index 3ab26a1..7a357e1 100644
--- a/test/rules/best-practises/no-global-import.js
+++ b/test/rules/best-practises/no-global-import.js
@@ -28,7 +28,7 @@ describe('Linter - no-global-import', () => {
     assertErrorMessage(report, 'Specify names to import individually')
   })
   it('should raise warning when using solhint:recommended', () => {
-    const code = `pragma solidity ^0.5.8; import "./A.sol";`
+    const code = `pragma solidity ^0.8.0; import "./A.sol";`
 
     const report = linter.processStr(code, {
       extends: 'solhint:recommended',
diff --git a/test/rules/best-practises/no-unused-import.js b/test/rules/best-practises/no-unused-import.js
index ff1e332..f366689 100644
--- a/test/rules/best-practises/no-unused-import.js
+++ b/test/rules/best-practises/no-unused-import.js
@@ -48,7 +48,7 @@ describe('Linter - no-unused-import', () => {
   })
 
   it('should raise error when using solhint:recommended', () => {
-    const code = `pragma solidity ^0.5.8; import {A} from "./A.sol";`
+    const code = `pragma solidity ^0.8.0; import {A} from "./A.sol";`
 
     const report = linter.processStr(code, {
       extends: 'solhint:recommended',
diff --git a/test/rules/security/compiler-version.js b/test/rules/security/compiler-version.js
index 3f18d6f..b3c51fe 100644
--- a/test/rules/security/compiler-version.js
+++ b/test/rules/security/compiler-version.js
@@ -2,6 +2,8 @@ const assert = require('assert')
 const { assertNoErrors, assertErrorCount, assertErrorMessage } = require('../../common/asserts')
 const linter = require('../../../lib/index')
 
+const DEFAULT_SEMVER = '^0.8.0'
+
 describe('Linter - compiler-version', () => {
   it('should disable only one compiler error on next line', () => {
     const report = linter.processStr(
@@ -11,7 +13,7 @@ describe('Linter - compiler-version', () => {
                 pragma solidity 0.3.4;
             `,
       {
-        rules: { 'compiler-version': ['error', '^0.5.2'] },
+        rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
       }
     )
 
@@ -26,7 +28,7 @@ describe('Linter - compiler-version', () => {
               pragma solidity 0.3.4;
           `,
       {
-        rules: { 'compiler-version': ['error', '^0.5.2'] },
+        rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
       }
     )
 
@@ -41,7 +43,7 @@ describe('Linter - compiler-version', () => {
                 pragma solidity 0.3.4;
             `,
       {
-        rules: { 'compiler-version': ['error', '^0.5.2'] },
+        rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
       }
     )
 
@@ -56,7 +58,7 @@ describe('Linter - compiler-version', () => {
                 pragma solidity 0.3.4;
             `,
       {
-        rules: { 'compiler-version': ['error', '^0.5.2'] },
+        rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
       }
     )
 
@@ -72,12 +74,12 @@ describe('Linter - compiler-version', () => {
                 pragma solidity 0.3.4;
             `,
       {
-        rules: { 'compiler-version': ['error', '^0.5.2'] },
+        rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
       }
     )
 
     assertErrorCount(report, 1)
-    assertErrorMessage(report, '0.5.2')
+    assertErrorMessage(report, DEFAULT_SEMVER)
   })
 
   it('should disable all errors', () => {
@@ -88,7 +90,7 @@ describe('Linter - compiler-version', () => {
                 pragma solidity 0.3.4;
             `,
       {
-        rules: { 'compiler-version': ['error', '^0.5.2'] },
+        rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
       }
     )
     assertNoErrors(report)
@@ -103,58 +105,58 @@ describe('Linter - compiler-version', () => {
                 pragma solidity ^0.4.4;
             `,
       {
-        rules: { 'compiler-version': ['error', '^0.5.2'] },
+        rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
       }
     )
 
     assertErrorCount(report, 1)
-    assertErrorMessage(report, '0.5.2')
+    assertErrorMessage(report, DEFAULT_SEMVER)
   })
 
   it('should return compiler version error', () => {
     const report = linter.processStr('pragma solidity 0.3.4;', {
-      rules: { 'compiler-version': ['error', '^0.5.2'] },
+      rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
     })
 
     assert.equal(report.errorCount, 1)
-    assert.ok(report.reports[0].message.includes('0.5.2'))
+    assert.ok(report.reports[0].message.includes(DEFAULT_SEMVER))
   })
 
   it('should not report compiler version error on exact match', () => {
-    const report = linter.processStr('pragma solidity 0.5.2;', {
-      rules: { 'compiler-version': ['error', '0.5.2'] },
+    const report = linter.processStr('pragma solidity 0.8.0;', {
+      rules: { 'compiler-version': ['error', '0.8.0'] },
     })
 
     assert.equal(report.errorCount, 0)
   })
 
   it('should not report compiler version error on range match', () => {
-    const report = linter.processStr('pragma solidity ^0.5.2;', {
-      rules: { 'compiler-version': ['error', '^0.5.2'] },
+    const report = linter.processStr('pragma solidity ^0.8.0;', {
+      rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
     })
 
     assert.equal(report.errorCount, 0)
   })
 
   it('should not report compiler version error on patch bump', () => {
-    const report = linter.processStr('pragma solidity 0.5.3;', {
-      rules: { 'compiler-version': ['error', '^0.5.2'] },
+    const report = linter.processStr('pragma solidity 0.8.1;', {
+      rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
     })
 
     assert.equal(report.errorCount, 0)
   })
 
   it('should not report compiler version error on range match', () => {
-    const report = linter.processStr('pragma solidity ^0.5.3;', {
-      rules: { 'compiler-version': ['error', '^0.5.2'] },
+    const report = linter.processStr('pragma solidity ^0.8.2;', {
+      rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
     })
 
     assert.equal(report.errorCount, 0)
   })
 
   it('should report compiler version error on range not matching', () => {
-    const report = linter.processStr('pragma solidity ^0.5.2;', {
-      rules: { 'compiler-version': ['error', '^0.5.3'] },
+    const report = linter.processStr('pragma solidity ^0.8.1;', {
+      rules: { 'compiler-version': ['error', '^0.8.3'] },
     })
 
     assert.equal(report.errorCount, 1)
@@ -162,7 +164,7 @@ describe('Linter - compiler-version', () => {
 
   it('should report compiler version error on minor bump', () => {
     const report = linter.processStr('pragma solidity 0.6.0;', {
-      rules: { 'compiler-version': ['error', '^0.5.2'] },
+      rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
     })
 
     assert.equal(report.errorCount, 1)
@@ -170,7 +172,7 @@ describe('Linter - compiler-version', () => {
 
   it(`should report compiler version error if pragma doesn't exist`, () => {
     const report = linter.processStr('contract Foo {}', {
-      rules: { 'compiler-version': ['error', '^0.5.2'] },
+      rules: { 'compiler-version': ['error', DEFAULT_SEMVER] },
     })
 
     assert.equal(report.errorCount, 1)
@@ -178,10 +180,10 @@ describe('Linter - compiler-version', () => {
 
   it(`should not report compiler version error if pragma exist`, () => {
     const report = linter.processStr(
-      `pragma solidity 0.6.0;
+      `pragma solidity 0.8.2;
       contract Foo {}`,
       {
-        rules: { 'compiler-version': ['error', '^0.6.0'] },
+        rules: { 'compiler-version': ['error', '^0.8.2'] },
       }
     )
 
@@ -202,4 +204,62 @@ describe('Linter - compiler-version', () => {
 
     assert.equal(report.errorCount, 0)
   })
+
+  it(`should not report compiler version error using default and correct pragma`, () => {
+    const report = linter.processStr(
+      `pragma solidity ^0.8.1;
+       pragma experimental ABIEncoderV2;
+       
+       contract Main {
+       }`,
+      {
+        rules: { 'compiler-version': 'error' },
+      }
+    )
+
+    assert.equal(report.errorCount, 0)
+  })
+
+  it(`should report compiler version error using default and lower pragma`, () => {
+    const report = linter.processStr(
+      `pragma solidity ^0.7.4;
+       
+       contract Main {
+       }`,
+      {
+        rules: { 'compiler-version': 'error' },
+      }
+    )
+
+    assert.equal(report.errorCount, 1)
+    assertErrorMessage(report, DEFAULT_SEMVER)
+  })
+
+  it(`should not report compiler version error using >= and default and correct pragma`, () => {
+    const report = linter.processStr(
+      `pragma solidity >=0.8.0;
+       
+       contract Main {
+       }`,
+      {
+        rules: { 'compiler-version': 'error' },
+      }
+    )
+
+    assert.equal(report.errorCount, 0)
+  })
+
+  it(`should report compiler version error using >= and default and lower pragma`, () => {
+    const report = linter.processStr(
+      `pragma solidity >=0.7.4;
+       
+       contract Main {
+       }`,
+      {
+        rules: { 'compiler-version': 'error' },
+      }
+    )
+    assert.equal(report.errorCount, 1)
+    assertErrorMessage(report, DEFAULT_SEMVER)
+  })
 })