From 443538114125fd44ecfc1bd0162099c53ff7cfb0 Mon Sep 17 00:00:00 2001 From: Joseph Izang Date: Sat, 27 May 2023 22:33:18 +0100 Subject: [PATCH] fix types in static analysis for easier comprehension --- .../src/lib/actions/staticAnalysisActions.ts | 12 +++++- .../src/lib/reducers/staticAnalysisReducer.ts | 15 +++++--- .../src/lib/remix-ui-static-analyser.tsx | 15 ++++++-- .../static-analyser/src/staticanalyser.d.ts | 37 +++++++++++++++++++ 4 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 libs/remix-ui/static-analyser/src/staticanalyser.d.ts diff --git a/libs/remix-ui/static-analyser/src/lib/actions/staticAnalysisActions.ts b/libs/remix-ui/static-analyser/src/lib/actions/staticAnalysisActions.ts index c8ce131cee..553f208d2e 100644 --- a/libs/remix-ui/static-analyser/src/lib/actions/staticAnalysisActions.ts +++ b/libs/remix-ui/static-analyser/src/lib/actions/staticAnalysisActions.ts @@ -1,11 +1,19 @@ +import { CompilationResult, SourceWithTarget } from '@remixproject/plugin-api' import React from 'react' //eslint-disable-line +import { AnalysisTab, RemixUiStaticAnalyserReducerActionType } from '../../staticanalyser' -export const compilation = (analysisModule, dispatch) => { +/** + * + * @param analysisModule { AnalysisTab } AnalysisTab ViewPlugin + * @param dispatch { React.Dispatch } analysisReducer function's dispatch method + */ +export const compilation = (analysisModule: AnalysisTab, + dispatch: React.Dispatch) => { if (analysisModule) { analysisModule.on( 'solidity', 'compilationFinished', - (file, source, languageVersion, data, input, version) => { + (file: string, source: SourceWithTarget, languageVersion: string, data: CompilationResult, input: string, version: string) => { if (languageVersion.indexOf('soljson') !== 0) return dispatch({ type: 'compilationFinished', payload: { file, source, languageVersion, data, input, version } }) } diff --git a/libs/remix-ui/static-analyser/src/lib/reducers/staticAnalysisReducer.ts b/libs/remix-ui/static-analyser/src/lib/reducers/staticAnalysisReducer.ts index 24c9a25868..1e12b09f03 100644 --- a/libs/remix-ui/static-analyser/src/lib/reducers/staticAnalysisReducer.ts +++ b/libs/remix-ui/static-analyser/src/lib/reducers/staticAnalysisReducer.ts @@ -1,11 +1,16 @@ -export const initialState = { - file: null, +import { RemixUiStaticAnalyserReducerActionType, RemixUiStaticAnalyserState } from "../../staticanalyser" + +export const initialState: RemixUiStaticAnalyserState = { + file: '', source: null, - languageVersion: null, - data: null + languageVersion: '', + data: null, + input: '', + version: '' } -export const analysisReducer = (state, action) => { +export const analysisReducer = (state: RemixUiStaticAnalyserState, + action: RemixUiStaticAnalyserReducerActionType) => { switch (action.type) { case 'compilationFinished': return { diff --git a/libs/remix-ui/static-analyser/src/lib/remix-ui-static-analyser.tsx b/libs/remix-ui/static-analyser/src/lib/remix-ui-static-analyser.tsx index d88f7f6eb1..68206a3312 100644 --- a/libs/remix-ui/static-analyser/src/lib/remix-ui-static-analyser.tsx +++ b/libs/remix-ui/static-analyser/src/lib/remix-ui-static-analyser.tsx @@ -13,6 +13,7 @@ import { CustomTooltip } from '@remix-ui/helper' import Tab from 'react-bootstrap/Tab' import Tabs from 'react-bootstrap/Tabs' import { Fade } from 'react-bootstrap' +import { AnalysisTab } from '../staticanalyser' declare global { interface Window { @@ -25,7 +26,7 @@ const _paq = window._paq = window._paq || [] //eslint-disable-line export interface RemixUiStaticAnalyserProps { registry: any, event: any, - analysisModule: any + analysisModule: AnalysisTab } export const RemixUiStaticAnalyser = (props: RemixUiStaticAnalyserProps) => { @@ -79,6 +80,11 @@ export const RemixUiStaticAnalyser = (props: RemixUiStaticAnalyserProps) => { const allWarnings = useRef({}) const [state, dispatch] = useReducer(analysisReducer, initialState) + /** + * Disable static analysis for contracts whose compiler version is + * less than 0.4.12 + * @param version {string} - Solidity compiler version + */ const setDisableForRun = (version: string) => { const truncateVersion = (version: string) => { const tmp: RegExpExecArray | null = /^(\d+.\d+.\d+)/.exec(version) @@ -123,7 +129,7 @@ export const RemixUiStaticAnalyser = (props: RemixUiStaticAnalyserProps) => { // Reset badge props.event.trigger('staticAnaysisWarning', []) // Reset state - dispatch({ type: '', payload: {} }) + dispatch({ type: '', payload: initialState }) // Show 'Enable Slither Analysis' checkbox if (currentWorkspace && currentWorkspace.isLocalhost === true) setShowSlither(true) else { @@ -139,7 +145,7 @@ export const RemixUiStaticAnalyser = (props: RemixUiStaticAnalyserProps) => { // Reset badge props.event.trigger('staticAnaysisWarning', []) // Reset state - dispatch({ type: '', payload: {} }) + dispatch({ type: '', payload: initialState }) setShowSlither(false) setSlitherEnabled(false) } @@ -219,6 +225,9 @@ export const RemixUiStaticAnalyser = (props: RemixUiStaticAnalyserProps) => { const warningMessage = [] const warningErrors = [] + const hints = await props.analysisModule.call('solhint', 'lint', currentFile) + console.log({ hints }) + // Remix Analysis _paq.push(['trackEvent', 'solidityStaticAnalyzer', 'analyze', 'remixAnalyzer']) const results = runner.run(lastCompilationResult, categoryIndex) diff --git a/libs/remix-ui/static-analyser/src/staticanalyser.d.ts b/libs/remix-ui/static-analyser/src/staticanalyser.d.ts new file mode 100644 index 0000000000..f6b4d66062 --- /dev/null +++ b/libs/remix-ui/static-analyser/src/staticanalyser.d.ts @@ -0,0 +1,37 @@ +import { CompilationResult, SourceWithTarget } from '@remixproject/plugin-api' + +import { ViewPlugin } from '@remixproject/engine-web'; +import { EventEmitter } from 'events'; +import Registry from '../state/registry'; +export declare class AnalysisTab extends ViewPlugin { + event: any; + events: EventEmitter; + registry: Registry; + element: HTMLDivElement; + _components: any; + _deps: { + offsetToLineColumnConverter: any; + }; + dispatch: any; + constructor(); + onActivation(): Promise; + setDispatch(dispatch: any): void; + render(): JSX.Element; + updateComponent(state: any): JSX.Element; + renderComponent(): void; +} + +type RemixUiStaticAnalyserState = { + file: string, + source: SourceWithTarget, + languageVersion: string, + data: CompilationResult + input?: string + version?: string +} + +type RemixUiStaticAnalyserReducerActionType = { + type: 'compilationFinished' | '' | any, + payload: RemixUiStaticAnalyserState +} +