From 1fe76500e80342b52f54c2c7ab17ea572bbe3786 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 28 Feb 2021 17:43:41 +0100 Subject: [PATCH] Enabled @typescript-eslint/no-unsafe-call eslint rule again --- .eslintrc | 3 +-- shlink-web-client.d.ts | 7 ++++++- src/container/index.ts | 3 ++- src/mercure/helpers/index.ts | 2 +- src/utils/helpers/hooks.ts | 2 +- test/mercure/helpers/index.test.tsx | 12 ++++++------ test/short-urls/helpers/ShortUrlsRowMenu.test.tsx | 2 +- 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.eslintrc b/.eslintrc index 34cf132e..ce1b40c7 100644 --- a/.eslintrc +++ b/.eslintrc @@ -16,9 +16,8 @@ }, "rules": { "@typescript-eslint/no-unsafe-assignment": "off", - "@typescript-eslint/no-unsafe-member-access": "off", - "@typescript-eslint/no-unsafe-call": "off", + "@typescript-eslint/no-unsafe-return": "off", "@typescript-eslint/naming-convention": "off", "@typescript-eslint/ban-types": "off", diff --git a/shlink-web-client.d.ts b/shlink-web-client.d.ts index 10678f09..4896a890 100644 --- a/shlink-web-client.d.ts +++ b/shlink-web-client.d.ts @@ -1,5 +1,10 @@ declare module 'event-source-polyfill' { - export const EventSourcePolyfill: any; + declare class EventSourcePolyfill { + public onmessage?: ({ data }: { data: string }) => void; + public onerror?: ({ status }: { status: number }) => void; + public close: Function; + public constructor(hubUrl: URL, options?: any); + } } declare module 'csvjson' { diff --git a/src/container/index.ts b/src/container/index.ts index b369c1f8..58871f19 100644 --- a/src/container/index.ts +++ b/src/container/index.ts @@ -20,7 +20,8 @@ type LazyActionMap = Record; const bottle = new Bottle(); const { container } = bottle; -const lazyService = (container: IContainer, serviceName: string) => (...args: any[]) => container[serviceName](...args); +const lazyService = (container: IContainer, serviceName: string) => + (...args: any[]) => (container[serviceName] as T)(...args); const mapActionService = (map: LazyActionMap, actionName: string): LazyActionMap => ({ ...map, // Wrap actual action service in a function so that it is lazily created the first time it is called diff --git a/src/mercure/helpers/index.ts b/src/mercure/helpers/index.ts index 33073818..bbd8a8d8 100644 --- a/src/mercure/helpers/index.ts +++ b/src/mercure/helpers/index.ts @@ -11,7 +11,7 @@ export const bindToMercureTopic = (mercureInfo: MercureInfo, topics: string[] const onEventSourceMessage = ({ data }: { data: string }) => onMessage(JSON.parse(data) as T); const onEventSourceError = ({ status }: { status: number }) => status === 401 && onTokenExpired(); - const subscriptions: EventSource[] = topics.map((topic) => { + const subscriptions = topics.map((topic) => { const hubUrl = new URL(mercureHubUrl); hubUrl.searchParams.append('topic', topic); diff --git a/src/utils/helpers/hooks.ts b/src/utils/helpers/hooks.ts index dfcdd926..89a50d89 100644 --- a/src/utils/helpers/hooks.ts +++ b/src/utils/helpers/hooks.ts @@ -34,7 +34,7 @@ export const useToggle = (initialValue = false): ToggleResult => { export const useSwipeable = (showSidebar: () => void, hideSidebar: () => void) => { const swipeMenuIfNoModalExists = (callback: () => void) => (e: any) => { - const swippedOnVisitsTable = (e.event.composedPath() as HTMLElement[]).some( // eslint-disable-lin @typescript-eslint/no-unsafe-call + const swippedOnVisitsTable = (e.event.composedPath() as HTMLElement[]).some( // eslint-disable-line @typescript-eslint/no-unsafe-call ({ classList }) => classList?.contains('visits-table'), ); diff --git a/test/mercure/helpers/index.test.tsx b/test/mercure/helpers/index.test.tsx index f4a3d187..adc3eafd 100644 --- a/test/mercure/helpers/index.test.tsx +++ b/test/mercure/helpers/index.test.tsx @@ -1,4 +1,4 @@ -import { EventSourcePolyfill as EventSource } from 'event-source-polyfill'; +import { EventSourcePolyfill } from 'event-source-polyfill'; import { Mock } from 'ts-mockery'; import { identity } from 'ramda'; import { bindToMercureTopic } from '../../../src/mercure/helpers'; @@ -22,7 +22,7 @@ describe('helpers', () => { ])('does not bind an EventSource when loading, error or no hub URL', (mercureInfo) => { bindToMercureTopic(mercureInfo, [ '' ], identity, identity); - expect(EventSource).not.toHaveBeenCalled(); + expect(EventSourcePolyfill).not.toHaveBeenCalled(); expect(onMessage).not.toHaveBeenCalled(); expect(onTokenExpired).not.toHaveBeenCalled(); }); @@ -42,16 +42,16 @@ describe('helpers', () => { token, }, [ topic ], onMessage, onTokenExpired); - expect(EventSource).toHaveBeenCalledWith(hubUrl, { + expect(EventSourcePolyfill).toHaveBeenCalledWith(hubUrl, { headers: { Authorization: `Bearer ${token}`, }, }); - const [ es ] = EventSource.mock.instances; + const [ es ] = (EventSourcePolyfill as any).mock.instances as EventSourcePolyfill[]; - es.onmessage({ data: '{"foo": "bar"}' }); - es.onerror({ status: 401 }); + es.onmessage?.({ data: '{"foo": "bar"}' }); + es.onerror?.({ status: 401 }); expect(onMessage).toHaveBeenCalledWith({ foo: 'bar' }); expect(onTokenExpired).toHaveBeenCalled(); diff --git a/test/short-urls/helpers/ShortUrlsRowMenu.test.tsx b/test/short-urls/helpers/ShortUrlsRowMenu.test.tsx index 3e0cfd5d..1c1aac93 100644 --- a/test/short-urls/helpers/ShortUrlsRowMenu.test.tsx +++ b/test/short-urls/helpers/ShortUrlsRowMenu.test.tsx @@ -60,7 +60,7 @@ describe('', () => { const wrapper = createWrapper(); expect(wrapper.find(modalComponent).prop('isOpen')).toEqual(false); - wrapper.find(modalComponent).prop('toggle')(); + (wrapper.find(modalComponent).prop('toggle') as Function)(); expect(wrapper.find(modalComponent).prop('isOpen')).toEqual(true); };