Skip to content

Commit

Permalink
process variable definitions as import usages
Browse files Browse the repository at this point in the history
  • Loading branch information
juanpcapurro committed Feb 28, 2023
1 parent 6f569ed commit 746cc7a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/rules/best-practises/no-unused-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@ class NoUnusedImportsChecker extends BaseChecker {
})
}

VariableDeclaration(node) {
if (node.typeName.type === 'UserDefinedTypeName') {
const importedName = node.typeName.namePath.split('.')[0]
if (this.importedNames[importedName]) {
this.importedNames[importedName].used = true
} else {
throw new Error('imported name isnt registered')
}
}
}

ContractDefinition(node) {
node.baseContracts.forEach((inheritanceSpecifier) => {
if (inheritanceSpecifier.baseName.type === 'UserDefinedTypeName') {
if (!this.importedNames[inheritanceSpecifier.baseName.namePath]) {
throw new Error('contract name isnt registered')
throw new Error('imported name isnt registered')
}
this.importedNames[inheritanceSpecifier.baseName.namePath].used = true
} else {
Expand All @@ -47,7 +58,7 @@ class NoUnusedImportsChecker extends BaseChecker {
if (this.importedNames[name]) {
this.importedNames[name].used = true
} else {
throw new Error('library name isnt registered')
throw new Error('imported name isnt registered')
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/rules/best-practises/no-unused-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ describe('Linter - no-unused-import', () => {
assertNoErrors(report)
})

it('should not raise when contract name is used in a state variable declaration', () => {
const code = `import {A} from './A.sol'; contract B { A public statevar; }`

const report = linter.processStr(code, {
rules: { 'no-unused-import': 'error' },
})
assertNoErrors(report)
})

it('should not raise when an imported subtype is used in a state variable declaration', () => {
const code = `import {A} from './A.sol'; contract B { A.thing public statevar; }`

const report = linter.processStr(code, {
rules: { 'no-unused-import': 'error' },
})
assertNoErrors(report)
})

it('should not raise when one of the imported contracts types is used in a function parameter declaration', () => {
const code = `import {A} from './A.sol'; contract B { function (A.thing statevar) public {} }`

const report = linter.processStr(code, {
rules: { 'no-unused-import': 'error' },
})
assertNoErrors(report)
})

it('should raise when some of the imported names are not used', () => {
const code = `import {A, B} from './A.sol'; contract C is A {}`

Expand Down

0 comments on commit 746cc7a

Please sign in to comment.