From c347ce8483b3672cb1381ba443efe7528753e97b Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Mon, 29 Mar 2021 01:24:27 +0530 Subject: [PATCH] Fix @typescript-eslint/no-unsafe-call errors --- .eslintrc.json | 1 - app/config/import.ts | 2 +- app/plugins.ts | 2 ++ app/ui/window.ts | 6 +++--- cli/index.ts | 2 ++ lib/actions/ui.ts | 4 +--- lib/components/split-pane.tsx | 2 ++ lib/components/term.tsx | 3 ++- lib/containers/hyper.tsx | 5 +++-- lib/reducers/ui.ts | 4 ++-- lib/utils/effects.ts | 1 + lib/utils/plugins.ts | 22 +++++++++++++++------- test/unit/cli-api.test.ts | 2 ++ test/unit/window-utils.test.ts | 2 ++ webpack.config.ts | 2 ++ 15 files changed, 40 insertions(+), 20 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 5ca0c309..8df0b80a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -105,7 +105,6 @@ "no-shadow": "off", "@typescript-eslint/no-shadow": ["error"], "@typescript-eslint/no-unsafe-assignment": "off", - "@typescript-eslint/no-unsafe-call": "off", "@typescript-eslint/no-unsafe-member-access": "off", "@typescript-eslint/no-unsafe-return": "off", "@typescript-eslint/restrict-template-expressions": "off" diff --git a/app/config/import.ts b/app/config/import.ts index a38d18ec..88a056f8 100644 --- a/app/config/import.ts +++ b/app/config/import.ts @@ -7,7 +7,7 @@ import {rawConfig} from '../../lib/config'; let defaultConfig: rawConfig; -const _write = (path: string, data: any) => { +const _write = (path: string, data: string) => { // This method will take text formatted as Unix line endings and transform it // to text formatted with DOS line endings. We do this because the default // text editor on Windows (notepad) doesn't Deal with LF files. Still. In 2017. diff --git a/app/plugins.ts b/app/plugins.ts index 514f0685..9bd99872 100644 --- a/app/plugins.ts +++ b/app/plugins.ts @@ -1,3 +1,5 @@ +// eslint-disable-next-line eslint-comments/disable-enable-pair +/* eslint-disable @typescript-eslint/no-unsafe-call */ import {app, dialog, BrowserWindow, App} from 'electron'; import {resolve, basename} from 'path'; import {writeFileSync} from 'fs'; diff --git a/app/ui/window.ts b/app/ui/window.ts index 68c9c358..9ad25ed0 100644 --- a/app/ui/window.ts +++ b/app/ui/window.ts @@ -51,7 +51,7 @@ export function newWindow( window.uid = classOpts.uid; const rpc = createRPC(window); - const sessions = new Map(); + const sessions = new Map(); const updateBackgroundColor = () => { const cfg_ = app.plugins.getDecoratedConfig(); @@ -187,11 +187,11 @@ export function newWindow( session.resize({cols, rows}); } }); - rpc.on('data', ({uid, data, escaped}) => { + rpc.on('data', ({uid, data, escaped}: {uid: string; data: string; escaped: boolean}) => { const session = sessions.get(uid); if (session) { if (escaped) { - const escapedData = session.shell.endsWith('cmd.exe') + const escapedData = session.shell?.endsWith('cmd.exe') ? `"${data}"` // This is how cmd.exe does it : `'${data.replace(/'/g, `'\\''`)}'`; // Inside a single-quoted string nothing is interpreted diff --git a/cli/index.ts b/cli/index.ts index 9d0d2962..c3c62661 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -231,7 +231,9 @@ const main = (argv: string[]) => { const child = spawn(process.execPath, args_, options); if (flags.verbose) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call child.stdout?.on('data', (data) => console.log(data.toString('utf8'))); + // eslint-disable-next-line @typescript-eslint/no-unsafe-call child.stderr?.on('data', (data) => console.error(data.toString('utf8'))); } if (flags.verbose) { diff --git a/lib/actions/ui.ts b/lib/actions/ui.ts index b6853746..53907ccf 100644 --- a/lib/actions/ui.ts +++ b/lib/actions/ui.ts @@ -29,9 +29,7 @@ import { import {setActiveGroup} from './term-groups'; import parseUrl from 'parse-url'; import {HyperState, HyperDispatch, HyperActions} from '../hyper'; -import {Stats} from 'fs'; - -const {stat} = window.require('fs'); +import {stat, Stats} from 'fs'; export function openContextMenu(uid: string, selection: any) { return (dispatch: HyperDispatch, getState: () => HyperState) => { diff --git a/lib/components/split-pane.tsx b/lib/components/split-pane.tsx index 4824c45b..8ca6cd8c 100644 --- a/lib/components/split-pane.tsx +++ b/lib/components/split-pane.tsx @@ -47,6 +47,7 @@ export default class SplitPane extends React.PureComponent { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call ev.preventDefault(); this.setState({dragging: true}); window.addEventListener('mousemove', this.onDrag); @@ -65,6 +66,7 @@ export default class SplitPane extends React.PureComponent { // intercepting paste event for any necessary processing of // clipboard data, if result is falsy, paste event continues - onWindowPaste = (e: any) => { + onWindowPaste = (e: Event) => { if (!this.props.isTermActive) return; const processed = processClipboard(); if (processed) { e.preventDefault(); e.stopPropagation(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-call (this.term as any)._core.handler(processed); } }; diff --git a/lib/containers/hyper.tsx b/lib/containers/hyper.tsx index 8c5e70a3..41248fa0 100644 --- a/lib/containers/hyper.tsx +++ b/lib/containers/hyper.tsx @@ -52,6 +52,7 @@ class Hyper extends React.PureComponent { attachKeyListeners() { if (!this.mousetrap) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call this.mousetrap = new (Mousetrap as any)(window, true); this.mousetrap.stopCallback = () => { // All events should be intercepted even if focus is in an input/textarea @@ -65,10 +66,10 @@ class Hyper extends React.PureComponent { Object.keys(keys).forEach((commandKeys) => { this.mousetrap.bind( commandKeys, - (e: any) => { + (e) => { const command = keys[commandKeys]; // We should tell to xterm that it should ignore this event. - e.catched = true; + (e as any).catched = true; this.props.execCommand(command, getCommandHandler(command), e); shouldPreventDefault(command) && e.preventDefault(); }, diff --git a/lib/reducers/ui.ts b/lib/reducers/ui.ts index 46ca3f1e..26e7f1b2 100644 --- a/lib/reducers/ui.ts +++ b/lib/reducers/ui.ts @@ -304,12 +304,12 @@ const reducer = (state = initial, action: HyperActions) => { case SESSION_PTY_EXIT: state_ = state - .updateIn(['openAt'], (times: ImmutableType) => { + .updateIn(['openAt'], (times: ImmutableType>) => { const times_ = times.asMutable(); delete times_[action.uid]; return times_; }) - .updateIn(['activityMarkers'], (markers: ImmutableType) => { + .updateIn(['activityMarkers'], (markers: ImmutableType>) => { const markers_ = markers.asMutable(); delete markers_[action.uid]; return markers_; diff --git a/lib/utils/effects.ts b/lib/utils/effects.ts index 0d94a998..acfefbb1 100644 --- a/lib/utils/effects.ts +++ b/lib/utils/effects.ts @@ -10,6 +10,7 @@ import {Middleware} from 'redux'; const effectsMiddleware: Middleware = () => (next) => (action) => { const ret = next(action); if (action.effect) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call action.effect(); delete action.effect; } diff --git a/lib/utils/plugins.ts b/lib/utils/plugins.ts index 0c652539..d44461c2 100644 --- a/lib/utils/plugins.ts +++ b/lib/utils/plugins.ts @@ -31,7 +31,7 @@ import {ObjectTypedKeys} from './object'; const plugins = remote.require('./plugins') as typeof import('../../app/plugins'); // `require`d modules -let modules: any; +let modules: hyperPlugin[]; // cache of decorated components let decorated: Record> = {}; @@ -74,6 +74,7 @@ function exposeDecorated

>( onRef = (decorated_: any) => { if (this.props.onDecorated) { try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call this.props.onDecorated(decorated_); } catch (e) { notify('Plugin error', `Error occurred. Check Developer Tools for details`, {error: e}); @@ -93,7 +94,7 @@ function getDecorated

(parent: React.ComponentType

, name: string): React.Co modules.forEach((mod: any) => { const method = 'decorate' + name; - const fn = mod[method]; + const fn: Function & {_pluginName: string} = mod[method]; if (fn) { let class__; @@ -190,8 +191,9 @@ const clearModulesCache = () => { const {path, localPath} = plugins.getBasePaths(); // trigger unload hooks - modules.forEach((mod: any) => { + modules.forEach((mod) => { if (mod.onRendererUnload) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call mod.onRendererUnload(window); } }); @@ -255,8 +257,8 @@ const loadModules = () => { const loadedPlugins = plugins.getLoadedPluginVersions().map((plugin: any) => plugin.name); modules = paths.plugins .concat(paths.localPlugins) - .filter((plugin: any) => loadedPlugins.indexOf(basename(plugin)) !== -1) - .map((path: any) => { + .filter((plugin) => loadedPlugins.indexOf(basename(plugin)) !== -1) + .map((path) => { let mod: hyperPlugin; const pluginName = getPluginName(path); const pluginVersion = getPluginVersion(path); @@ -264,7 +266,7 @@ const loadModules = () => { // window.require allows us to ensure this doesn't get // in the way of our build try { - mod = window.require(path) as any; + mod = window.require(path); } catch (err) { notify( 'Plugin load error', @@ -358,13 +360,14 @@ const loadModules = () => { } if (mod.onRendererWindow) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call mod.onRendererWindow(window); } console.log(`Plugin ${pluginName} (${pluginVersion}) loaded.`); return mod; }) - .filter((mod: any) => Boolean(mod)); + .filter((mod): mod is hyperPlugin => Boolean(mod)); const deprecatedPlugins = plugins.getDeprecatedConfig(); Object.keys(deprecatedPlugins).forEach((name) => { @@ -398,6 +401,7 @@ function getProps(name: keyof typeof propsDecorators, props: any, ...fnArgs: any } try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call ret_ = fn(...fnArgs, props_); } catch (err) { notify('Plugin error', `${fn._pluginName}: Error occurred in \`${name}\`. Check Developer Tools for details.`, { @@ -454,6 +458,7 @@ export function connect( let ret_; try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call ret_ = fn(state, ret); } catch (err) { notify( @@ -479,6 +484,7 @@ export function connect( let ret_; try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call ret_ = fn(dispatch, ret); } catch (err) { notify( @@ -514,12 +520,14 @@ const decorateReducer: { } = (name: T, fn: any) => { const reducers = reducersDecorators[name]; return (state: any, action: any) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call let state_ = fn(state, action); reducers.forEach((pluginReducer: any) => { let state__; try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call state__ = pluginReducer(state_, action); } catch (err) { notify('Plugin error', `${fn._pluginName}: Error occurred in \`${name}\`. Check Developer Tools for details.`, { diff --git a/test/unit/cli-api.test.ts b/test/unit/cli-api.test.ts index ef88faea..a9c9b252 100644 --- a/test/unit/cli-api.test.ts +++ b/test/unit/cli-api.test.ts @@ -1,3 +1,5 @@ +// eslint-disable-next-line eslint-comments/disable-enable-pair +/* eslint-disable @typescript-eslint/no-unsafe-call */ import test from 'ava'; // eslint-disable-next-line @typescript-eslint/no-var-requires const proxyquire = require('proxyquire').noCallThru(); diff --git a/test/unit/window-utils.test.ts b/test/unit/window-utils.test.ts index f44aa662..89ce665f 100644 --- a/test/unit/window-utils.test.ts +++ b/test/unit/window-utils.test.ts @@ -1,3 +1,5 @@ +// eslint-disable-next-line eslint-comments/disable-enable-pair +/* eslint-disable @typescript-eslint/no-unsafe-call */ import test from 'ava'; // eslint-disable-next-line @typescript-eslint/no-var-requires const proxyquire = require('proxyquire').noCallThru(); diff --git a/webpack.config.ts b/webpack.config.ts index cbf82a11..611fd851 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -29,6 +29,7 @@ const config: webpack.Configuration[] = [ ] }, plugins: [ + // eslint-disable-next-line @typescript-eslint/no-unsafe-call new Copy({ patterns: [ { @@ -98,6 +99,7 @@ const config: webpack.Configuration[] = [ NODE_ENV: JSON.stringify(nodeEnv) } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-call new Copy({ patterns: [ {