simplify static analysis actions

pull/3838/head
Joseph Izang 1 year ago
parent 1f41cbce7f
commit d5d826ac46
  1. 190
      libs/remix-ui/static-analyser/src/lib/actions/staticAnalysisActions.ts

@ -23,24 +23,6 @@ export const compilation = (analysisModule: AnalysisTab,
} }
} }
export const runLinting = async (solhintEnabled, setHints, hints: SolHintReport[], isSupportedVersion, state: RemixUiStaticAnalyserState,
props: RemixUiStaticAnalyserProps, setStartAnalysis: React.Dispatch<React.SetStateAction<boolean>>) => {
// Run solhint
setStartAnalysis(true)
props.analysisModule.hints = []
if (!isSupportedVersion) return
if (solhintEnabled === false) return
if (state.data !== null) {
props.analysisModule.hints = []
const hintsResult = await props.analysisModule.call('solhint', 'lint', state.file)
props.analysisModule.hints = solhintEnabled === false ? 0 : hintsResult
setHints(hintsResult)
// props.analysisModule.emit('statusChanged', { key: hints.length,
// title: `${hints.length} warning${hints.length === 1 ? '' : 's'}`, type: 'warning'})
return hintsResult
}
}
/** /**
* Run the analysis on the currently compiled contract * Run the analysis on the currently compiled contract
* @param lastCompilationResult * @param lastCompilationResult
@ -62,122 +44,88 @@ export const runLinting = async (solhintEnabled, setHints, hints: SolHintReport[
*/ */
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function run (lastCompilationResult, lastCompilationSource, currentFile: string, state: RemixUiStaticAnalyserState, props: RemixUiStaticAnalyserProps, isSupportedVersion, showSlither, categoryIndex: number[], groupedModules, runner, _paq, message, showWarnings, allWarnings: React.RefObject<any>, warningContainer: React.RefObject<any>, calculateWarningStateEntries: (e:[string, any][]) => {length: number, errors: any[] }, warningState, setHints: React.Dispatch<React.SetStateAction<SolHintReport[]>>, hints: SolHintReport[], setSlitherWarnings: React.Dispatch<React.SetStateAction<any[]>>, setSsaWarnings: React.Dispatch<React.SetStateAction<any[]>>, export async function run (lastCompilationResult, lastCompilationSource, currentFile: string, state: RemixUiStaticAnalyserState, props: RemixUiStaticAnalyserProps, isSupportedVersion, showSlither, categoryIndex: number[], groupedModules, runner, _paq, message, showWarnings, allWarnings: React.RefObject<any>, warningContainer: React.RefObject<any>, calculateWarningStateEntries: (e:[string, any][]) => {length: number, errors: any[] }, warningState, setHints: React.Dispatch<React.SetStateAction<SolHintReport[]>>, hints: SolHintReport[], setSlitherWarnings: React.Dispatch<React.SetStateAction<any[]>>, setSsaWarnings: React.Dispatch<React.SetStateAction<any[]>>,
slitherEnabled: boolean, setStartAnalysis: React.Dispatch<React.SetStateAction<boolean>>, solhintEnabled: boolean, basicEnabled: boolean) { slitherEnabled: boolean, setStartAnalysis: React.Dispatch<React.SetStateAction<boolean>>, solhintEnabled: boolean) {
setStartAnalysis(true) setStartAnalysis(true)
setHints([]) setHints([])
setSsaWarnings([]) setSsaWarnings([])
setSlitherWarnings([]) setSlitherWarnings([])
props.analysisModule.hints = []
if (!isSupportedVersion) return if (!isSupportedVersion) return
if (state.data !== null) { if (state.data !== null) {
if (lastCompilationResult && (categoryIndex.length > 0)) { if (lastCompilationResult && (categoryIndex.length > 0 || showSlither)) {
_paq.push(['trackEvent', 'solidityStaticAnalyzer', 'analyze', 'remixAnalyzer'])
const warningMessage = [] const warningMessage = []
const warningErrors = [] const warningErrors = []
props.analysisModule.hints = []
// Run solhint // Run solhint
// const hintsResult = await props.analysisModule.call('solhint', 'lint', state.file) const hintsResult = await props.analysisModule.call('solhint', 'lint', state.file)
// props.analysisModule.hints = solhintEnabled === false ? 0 : hintsResult props.analysisModule.hints = solhintEnabled === false ? 0 : hintsResult
// setHints(hintsResult) setHints(hintsResult)
// const warningResult = calculateWarningStateEntries(Object.entries(warningState))
// props.analysisModule.emit('statusChanged', { key: hints.length+warningResult.length, // props.analysisModule.emit('statusChanged', { key: hints.length+warningResult.length,
// title: `${hints.length+warningResult.length} warning${hints.length+warningResult.length === 1 ? '' : 's'}`, type: 'warning'}) // title: `${hints.length+warningResult.length} warning${hints.length+warningResult.length === 1 ? '' : 's'}`, type: 'warning'})
// Remix Analysis // Remix Analysis
const results = runner.run(lastCompilationResult, categoryIndex) _paq.push(['trackEvent', 'solidityStaticAnalyzer', 'analyze', 'remixAnalyzer'])
for (const result of results) { const results = runner.run(lastCompilationResult, categoryIndex)
let moduleName for (const result of results) {
Object.keys(groupedModules).map(key => { let moduleName
groupedModules[key].forEach(el => { Object.keys(groupedModules).map(key => {
if (el.name === result.name) { groupedModules[key].forEach(el => {
moduleName = groupedModules[key][0].categoryDisplayName if (el.name === result.name) {
} moduleName = groupedModules[key][0].categoryDisplayName
}) }
}) })
// iterate over the warnings and create an object })
for (const item of result.report) { // iterate over the warnings and create an object
let location: any = {} for (const item of result.report) {
let locationString = 'not available' let location: any = {}
let column = 0 let locationString = 'not available'
let row = 0 let column = 0
let fileName = currentFile let row = 0
let isLibrary = false let fileName = currentFile
let isLibrary = false
if (item.location) { if (item.location) {
const split = item.location.split(':') const split = item.location.split(':')
const file = split[2] const file = split[2]
location = { location = {
start: parseInt(split[0]), start: parseInt(split[0]),
length: parseInt(split[1]) length: parseInt(split[1])
}
location = props.analysisModule._deps.offsetToLineColumnConverter.offsetToLineColumn(
location,
parseInt(file),
lastCompilationSource.sources,
lastCompilationResult.sources
)
row = location.start.line
column = location.start.column
locationString = row + 1 + ':' + column + ':'
fileName = Object.keys(lastCompilationResult.sources)[file]
}
if (fileName !== currentFile) {
const { file, provider } = await props.analysisModule.call('fileManager', 'getPathFromUrl', fileName)
if (file.startsWith('.deps') || (provider.type === 'localhost' && file.startsWith('localhost/node_modules'))) isLibrary = true
} }
const msg = message(result.name, item.warning, item.more, fileName, locationString) location = props.analysisModule._deps.offsetToLineColumnConverter.offsetToLineColumn(
const options = { location,
type: 'warning', parseInt(file),
useSpan: true, lastCompilationSource.sources,
errFile: fileName, lastCompilationResult.sources
fileName, )
isLibrary, row = location.start.line
errLine: row, column = location.start.column
errCol: column, locationString = row + 1 + ':' + column + ':'
item: item, fileName = Object.keys(lastCompilationResult.sources)[file]
name: result.name, }
locationString, if(fileName !== currentFile) {
more: item.more, const {file, provider} = await props.analysisModule.call('fileManager', 'getPathFromUrl', fileName)
location: location if (file.startsWith('.deps') || (provider.type === 'localhost' && file.startsWith('localhost/node_modules'))) isLibrary = true
} }
warningErrors.push(options) const msg = message(result.name, item.warning, item.more, fileName, locationString)
warningMessage.push({ msg, options, hasWarning: true, warningModuleName: moduleName }) const options = {
setSsaWarnings(warningMessage) type: 'warning',
props.event.trigger('staticAnaysisWarning', [warningMessage.length]) useSpan: true,
errFile: fileName,
fileName,
isLibrary,
errLine: row,
errCol: column,
item: item,
name: result.name,
locationString,
more: item.more,
location: location
}
warningErrors.push(options)
warningMessage.push({ msg, options, hasWarning: true, warningModuleName: moduleName })
setSsaWarnings(warningMessage)
} }
} }
} else {
if (categoryIndex.length) {
warningContainer.current.innerText = 'No compiled AST available'
}
props.event.trigger('staticAnaysisWarning', [-1])
}
}
}
/**
* Run the analysis on the currently compiled contract
* @param lastCompilationResult
* @param lastCompilationSource
* @param currentFile {string} current file path
* @param state { RemixUiStaticAnalyserState}
* @param props {RemixUiStaticAnalyserProps}
* @param isSupportedVersion {boolean}
* @param slitherEnabled {boolean}
* @param categoryIndex {number[]}
* @param groupedModules {any}
* @param runner {any}
* @param _paq {any}
* @param message {any}
* @param showWarnings {boolean}
* @param allWarnings {React.RefObject<object>}
* @param warningContainer {React.RefObject<object>}
* @returns {Promise<void>}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function runSlitherAnalysis (lastCompilationResult, lastCompilationSource, currentFile: string, state: RemixUiStaticAnalyserState, props: RemixUiStaticAnalyserProps, isSupportedVersion, showSlither, categoryIndex: number[], groupedModules, runner, _paq, message, showWarnings, allWarnings: React.RefObject<any>, warningContainer: React.RefObject<any>, calculateWarningStateEntries: (e:[string, any][]) => {length: number, errors: any[] }, warningState, setHints: React.Dispatch<React.SetStateAction<SolHintReport[]>>, hints: SolHintReport[], setSlitherWarnings: React.Dispatch<React.SetStateAction<any[]>>, setSsaWarnings: React.Dispatch<React.SetStateAction<any[]>>,
slitherEnabled: boolean, setStartAnalysis: React.Dispatch<React.SetStateAction<boolean>>) {
// Slither Analysis // Slither Analysis
if (showSlither && slitherEnabled) { if (showSlither && slitherEnabled) {
setSlitherWarnings([]) setSlitherWarnings([])
@ -247,12 +195,20 @@ slitherEnabled: boolean, setStartAnalysis: React.Dispatch<React.SetStateAction<b
return prev return prev
}) })
} }
showWarnings(warningMessage, 'warningModuleName')
} }
} catch(error) { } catch(error) {
props.analysisModule.call('terminal', 'log', { type: 'error', value: '[Slither Analysis]: Error occured! See remixd console for details.' }) props.analysisModule.call('terminal', 'log', { type: 'error', value: '[Slither Analysis]: Error occured! See remixd console for details.' })
showWarnings(warningMessage, 'warningModuleName')
} }
} } else showWarnings(warningMessage, 'warningModuleName')
setStartAnalysis(false) setStartAnalysis(false)
} else {
if (categoryIndex.length) {
warningContainer.current.innerText = 'No compiled AST available'
}
props.event.trigger('staticAnaysisWarning', [-1]) props.event.trigger('staticAnaysisWarning', [-1])
} }
}
}

Loading…
Cancel
Save