handle similar names location

pull/825/head
aniket-engg 4 years ago committed by Aniket
parent c0e9da3b31
commit a6a173d39f
  1. 18
      libs/remix-analyzer/src/solidity-analyzer/modules/similarVariableNames.ts

@ -4,6 +4,7 @@ import { default as algorithm } from './algorithmCategories'
import AbstractAst from './abstractAstView' import AbstractAst from './abstractAstView'
import { get } from 'fast-levenshtein' import { get } from 'fast-levenshtein'
import { util } from '@remix-project/remix-lib' import { util } from '@remix-project/remix-lib'
import { AstWalker } from '@remix-project/remix-astwalker'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, ContractHLAst, FunctionHLAst, VariableDeclarationAstNode, VisitFunction, ReportFunction, SupportedVersion} from './../../types' import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, ContractHLAst, FunctionHLAst, VariableDeclarationAstNode, VisitFunction, ReportFunction, SupportedVersion} from './../../types'
interface SimilarRecord { interface SimilarRecord {
@ -43,13 +44,20 @@ export default class similarVariableNames implements AnalyzerModule {
if (multipleContractsWithSameName) { if (multipleContractsWithSameName) {
multipleContractsWithSameNameComments = 'Note: Import aliases are currently not supported by this static analysis.' multipleContractsWithSameNameComments = 'Note: Import aliases are currently not supported by this static analysis.'
} }
const vars: string[] = this.getFunctionVariables(contract, func).map(getDeclaredVariableName) const vars: string[] = this.getFunctionVariables(contract, func).map(getDeclaredVariableName)
this.findSimilarVarNames(vars).map((sim) => { this.findSimilarVarNames(vars).map((sim) => {
warnings.push({ if(func.node.implemented) {
warning: `${funcName} : Variables have very similar names "${sim.var1}" and "${sim.var2}". ${hasModifiersComments} ${multipleContractsWithSameNameComments}`, const astWalker = new AstWalker()
location: func.node['src'] const functionBody: any = func.node.body
}) astWalker.walk(functionBody, (node) => {
if (node.nodeType === "Identifier" && (node.name === sim.var1 || node.name === sim.var2)) {
warnings.push({
warning: `${funcName} : Variables have very similar names "${sim.var1}" and "${sim.var2}". ${hasModifiersComments} ${multipleContractsWithSameNameComments}`,
location: func.node['src']
})
}
})
}
}) })
}) })
}) })

Loading…
Cancel
Save