Skip to content

Commit

Permalink
Merge pull request #481 from protofire/fix-explicit-types
Browse files Browse the repository at this point in the history
Fix explicit-types default value
  • Loading branch information
dbale-altoros committed Aug 10, 2023
2 parents 726fba9 + 6bb75a7 commit 3822e4a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
12 changes: 6 additions & 6 deletions lib/config.js
Expand Up @@ -15,11 +15,6 @@ module.exports = {
return _.isBoolean(configVal) ? configVal : defaultValue
},

getStringByPath(path, defaultValue) {
const configVal = _.get(this, path)
return _.isString(configVal) ? configVal : defaultValue
},

getNumber(ruleName, defaultValue) {
return this.getNumberByPath(`rules["${ruleName}"][1]`, defaultValue)
},
Expand All @@ -33,6 +28,11 @@ module.exports = {
},

getString(ruleName, defaultValue) {
return this.getStringByPath(`rules["${ruleName}"][1]`, defaultValue)
if (this.rules && this.rules[ruleName]) {
const ruleValue = this.rules[ruleName]
return Array.isArray(ruleValue) ? ruleValue[1] : defaultValue
} else {
return defaultValue
}
},
}
13 changes: 5 additions & 8 deletions lib/rules/best-practises/explicit-types.js
Expand Up @@ -64,11 +64,14 @@ class ExplicitTypesChecker extends BaseChecker {
super(reporter, ruleId, meta)
this.configOption = (config && config.getString(ruleId, DEFAULT_OPTION)) || DEFAULT_OPTION
this.isExplicit = this.configOption === 'explicit'

this.validateConfigOption(this.configOption)
}

VariableDeclaration(node) {
if (!VALID_CONFIGURATION_OPTIONS.includes(this.configOption)) {
this.error(node, 'Error: Config error on [explicit-types]. See explicit-types.md.')
return
}

const typeToFind = 'ElementaryTypeName'
const onlyTypes = this.findNamesOfElementaryTypeName(node, typeToFind)

Expand All @@ -89,12 +92,6 @@ class ExplicitTypesChecker extends BaseChecker {
}
}

validateConfigOption(configOption) {
const configOk = VALID_CONFIGURATION_OPTIONS.includes(configOption)
if (!configOk)
throw new Error('Error: Config error on [explicit-types]. See explicit-types.md.')
}

validateVariables(configType, node, varsToBeChecked) {
const errorVars = varsToBeChecked.filter((type) => configType.includes(type))

Expand Down
35 changes: 14 additions & 21 deletions test/rules/best-practises/explicit-types.js
@@ -1,4 +1,3 @@
const assert = require('assert')
const linter = require('../../../lib/index')
const contractWith = require('../../common/contract-builder').contractWith
const { assertErrorCount, assertNoErrors, assertErrorMessage } = require('../../common/asserts')
Expand All @@ -21,30 +20,24 @@ const getZeroErrosObject = () => {
}

describe('Linter - explicit-types rule', () => {
it('should throw an error with a wrong configuration rule example 1', () => {
it('should throw an error with a wrong configuration rule', () => {
const code = contractWith('uint256 public constant SNAKE_CASE = 1;')
try {
linter.processStr(code, {
rules: { 'explicit-types': ['error', 'implicito'] },
})
} catch (error) {
assert.ok(
error.toString().includes('Error: Config error on [explicit-types]. See explicit-types.md.')
)
}

const report = linter.processStr(code, {
rules: { 'explicit-types': ['error', 'implicito'] },
})

assertErrorCount(report, 1)
assertErrorMessage(report, `Error: Config error on [explicit-types]. See explicit-types.md.`)
})

it('should throw an error with a wrong configuration rule example 2', () => {
it('should NOT throw error without the config array and default should take place', () => {
const code = contractWith('uint256 public constant SNAKE_CASE = 1;')
try {
linter.processStr(code, {
rules: { 'explicit-types': 'error' },
})
} catch (error) {
assert.ok(
error.toString().includes('Error: Config error on [explicit-types]. See explicit-types.md.')
)
}
const report = linter.processStr(code, {
rules: { 'explicit-types': 'error' },
})

assertNoErrors(report)
})

for (const key in VAR_DECLARATIONS) {
Expand Down

0 comments on commit 3822e4a

Please sign in to comment.