From ba6d2f2a82ec5e515cdad7da9d666a51f03c5f17 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Tue, 20 Dec 2022 13:07:57 +0000 Subject: [PATCH] Chore: improve some more types (#60476) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * improve some more types * fixes Co-authored-by: Zoltán Bedi --- .betterer.results | 42 ++++--------------- .../textbox-variables.spec.ts | 16 +++---- packages/grafana-runtime/src/index.ts | 1 + .../src/utils/DataSourceWithBackend.ts | 10 ++--- .../src/utils/queryResponse.test.ts | 4 +- .../src/utils/queryResponse.ts | 11 ++++- .../src/utils/toDataQueryError.ts | 6 +-- .../ButtonCascader/ButtonCascader.tsx | 2 +- .../CustomScrollbar/CustomScrollbar.tsx | 4 +- .../components/DataSourceTestingStatus.tsx | 2 +- .../features/datasources/state/reducers.ts | 3 +- public/app/types/datasources.ts | 8 +--- 12 files changed, 43 insertions(+), 66 deletions(-) diff --git a/.betterer.results b/.betterer.results index 8be8218b1b7..96a17907fd6 100644 --- a/.betterer.results +++ b/.betterer.results @@ -35,12 +35,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], - "e2e/dashboards-suite/textbox-variables.spec.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"] - ], "e2e/panels-suite/panelEdit_queries.spec.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] @@ -974,19 +968,13 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "2"] ], "packages/grafana-runtime/src/utils/DataSourceWithBackend.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"], + [0, 0, 0, "Unexpected any. Specify a different type.", "0"], + [0, 0, 0, "Do not use any type assertions.", "1"], + [0, 0, 0, "Do not use any type assertions.", "2"], [0, 0, 0, "Unexpected any. Specify a different type.", "3"], - [0, 0, 0, "Do not use any type assertions.", "4"], - [0, 0, 0, "Do not use any type assertions.", "5"], - [0, 0, 0, "Unexpected any. Specify a different type.", "6"], - [0, 0, 0, "Unexpected any. Specify a different type.", "7"], - [0, 0, 0, "Unexpected any. Specify a different type.", "8"], - [0, 0, 0, "Do not use any type assertions.", "9"], - [0, 0, 0, "Do not use any type assertions.", "10"], - [0, 0, 0, "Unexpected any. Specify a different type.", "11"], - [0, 0, 0, "Do not use any type assertions.", "12"] + [0, 0, 0, "Unexpected any. Specify a different type.", "4"], + [0, 0, 0, "Unexpected any. Specify a different type.", "5"], + [0, 0, 0, "Unexpected any. Specify a different type.", "6"] ], "packages/grafana-runtime/src/utils/plugin.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] @@ -994,21 +982,14 @@ exports[`better eslint`] = { "packages/grafana-runtime/src/utils/queryResponse.test.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"] + [0, 0, 0, "Unexpected any. Specify a different type.", "2"] ], "packages/grafana-runtime/src/utils/queryResponse.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], [0, 0, 0, "Do not use any type assertions.", "2"], [0, 0, 0, "Do not use any type assertions.", "3"], - [0, 0, 0, "Do not use any type assertions.", "4"], - [0, 0, 0, "Do not use any type assertions.", "5"], - [0, 0, 0, "Unexpected any. Specify a different type.", "6"] - ], - "packages/grafana-runtime/src/utils/toDataQueryError.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"] + [0, 0, 0, "Do not use any type assertions.", "4"] ], "packages/grafana-toolkit/src/cli/tasks/component.create.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -1108,9 +1089,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"] ], - "packages/grafana-ui/src/components/ButtonCascader/ButtonCascader.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], "packages/grafana-ui/src/components/Card/Card.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] @@ -1127,10 +1105,6 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Do not use any type assertions.", "3"] ], - "packages/grafana-ui/src/components/CustomScrollbar/CustomScrollbar.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"] - ], "packages/grafana-ui/src/components/DataLinks/DataLinkInput.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], diff --git a/e2e/dashboards-suite/textbox-variables.spec.ts b/e2e/dashboards-suite/textbox-variables.spec.ts index 0f59bf6a9a9..9c18b5327f9 100644 --- a/e2e/dashboards-suite/textbox-variables.spec.ts +++ b/e2e/dashboards-suite/textbox-variables.spec.ts @@ -33,8 +33,8 @@ describe.skip('TextBox - change query scenarios', function () { saveDashboard(false); e2e() - .get('@dashuid') - .then((dashuid: any) => { + .get('@dashuid') + .then((dashuid) => { expect(dashuid).not.to.eq(PAGE_UNDER_TEST); e2e.flows.openDashboard({ uid: dashuid }); @@ -59,8 +59,8 @@ describe.skip('TextBox - change query scenarios', function () { saveDashboard(true); e2e() - .get('@dashuid') - .then((dashuid: any) => { + .get('@dashuid') + .then((dashuid) => { expect(dashuid).not.to.eq(PAGE_UNDER_TEST); e2e.flows.openDashboard({ uid: dashuid }); @@ -85,8 +85,8 @@ describe.skip('TextBox - change picker value scenarios', function () { saveDashboard(false); e2e() - .get('@dashuid') - .then((dashuid: any) => { + .get('@dashuid') + .then((dashuid) => { expect(dashuid).not.to.eq(PAGE_UNDER_TEST); e2e.flows.openDashboard({ uid: dashuid }); @@ -108,8 +108,8 @@ describe.skip('TextBox - change picker value scenarios', function () { saveDashboard(true); e2e() - .get('@dashuid') - .then((dashuid: any) => { + .get('@dashuid') + .then((dashuid) => { expect(dashuid).not.to.eq(PAGE_UNDER_TEST); e2e.flows.openDashboard({ uid: dashuid }); diff --git a/packages/grafana-runtime/src/index.ts b/packages/grafana-runtime/src/index.ts index 2652ebfe41b..bc09e8a47f9 100644 --- a/packages/grafana-runtime/src/index.ts +++ b/packages/grafana-runtime/src/index.ts @@ -23,6 +23,7 @@ export { frameToMetricFindValue, type BackendDataSourceResponse, type DataResponse, + type TestingStatus, } from './utils/queryResponse'; export { PanelRenderer, type PanelRendererProps } from './components/PanelRenderer'; export { PanelDataErrorView, type PanelDataErrorViewProps } from './components/PanelDataErrorView'; diff --git a/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts b/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts index b35a7485368..c977a4acc31 100644 --- a/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts +++ b/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts @@ -46,7 +46,7 @@ export function isExpressionReference(ref?: DataSourceRef | string | null): bool if (!ref) { return false; } - const v = (ref as any).type ?? ref; + const v = typeof ref === 'string' ? ref : ref.type; return v === ExpressionDatasourceRef.type || v === '-100'; // -100 was a legacy accident that should be removed } @@ -88,7 +88,7 @@ enum PluginRequestHeaders { * * @public */ -export type HealthCheckResultDetails = Record | undefined; +export type HealthCheckResultDetails = Record | undefined; /** * Describes the payload returned when checking the health of a data source @@ -320,8 +320,8 @@ class DataSourceWithBackend< headers: this.getRequestHeaders(), }) ) - .then((v: FetchResponse) => v.data as HealthCheckResult) - .catch((err) => err.data as HealthCheckResult); + .then((v: FetchResponse) => v.data) + .catch((err) => err.data); } /** @@ -360,7 +360,7 @@ export function toStreamingDataResponse( for (const f of rsp.data) { const addr = parseLiveChannelAddress(f.meta?.channel); if (addr) { - const frame = f as DataFrame; + const frame: DataFrame = f; streams.push( live.getDataStream({ addr, diff --git a/packages/grafana-runtime/src/utils/queryResponse.test.ts b/packages/grafana-runtime/src/utils/queryResponse.test.ts index e14ea0eed7f..a36a58521af 100644 --- a/packages/grafana-runtime/src/utils/queryResponse.test.ts +++ b/packages/grafana-runtime/src/utils/queryResponse.test.ts @@ -280,7 +280,7 @@ describe('Query Response parser', () => { }); describe('Cache notice', () => { - let resp: any; + let resp: FetchResponse; beforeEach(() => { resp = { @@ -311,7 +311,7 @@ describe('Query Response parser', () => { test('does not remove existing notices', () => { const queries: DataQuery[] = [{ refId: 'A' }]; resp.headers.set('X-Cache', 'HIT'); - resp.data.results.A.frames[0].schema.meta = { notices: [{ severity: 'info', text: 'Example' }] }; + resp.data.results.A.frames![0].schema!.meta = { notices: [{ severity: 'info', text: 'Example' }] }; expect(toDataQueryResponse(resp, queries).data[0].meta.notices).toStrictEqual([ { severity: 'info', text: 'Example' }, cachedResponseNotice, diff --git a/packages/grafana-runtime/src/utils/queryResponse.ts b/packages/grafana-runtime/src/utils/queryResponse.ts index a17aaf1c1d9..908e17e7e01 100644 --- a/packages/grafana-runtime/src/utils/queryResponse.ts +++ b/packages/grafana-runtime/src/utils/queryResponse.ts @@ -17,6 +17,7 @@ import { import { FetchError, FetchResponse } from '../services'; +import { HealthCheckResultDetails } from './DataSourceWithBackend'; import { toDataQueryError } from './toDataQueryError'; export const cachedResponseNotice: QueryResultMetaNotice = { severity: 'info', text: 'Cached response' }; @@ -130,7 +131,7 @@ export function toDataQueryResponse( rsp.state = LoadingState.Error; } if (!rsp.error) { - rsp.error = toDataQueryError(res as DataQueryError); + rsp.error = toDataQueryError(res); } } @@ -160,6 +161,12 @@ function addCacheNotice(frame: DataFrameJSON): DataFrameJSON { }; } +export interface TestingStatus { + message?: string | null; + status?: string | null; + details?: HealthCheckResultDetails; +} + /** * Data sources using api/ds/query to test data sources can use this function to * handle errors and convert them to TestingStatus object. @@ -171,7 +178,7 @@ function addCacheNotice(frame: DataFrameJSON): DataFrameJSON { * * @returns {TestingStatus} */ -export function toTestingStatus(err: FetchError): any { +export function toTestingStatus(err: FetchError): TestingStatus { const queryResponse = toDataQueryResponse(err); // POST api/ds/query errors returned as { message: string, error: string } objects if (queryResponse.error?.data?.message) { diff --git a/packages/grafana-runtime/src/utils/toDataQueryError.ts b/packages/grafana-runtime/src/utils/toDataQueryError.ts index 28946be8109..f21a2413eaf 100644 --- a/packages/grafana-runtime/src/utils/toDataQueryError.ts +++ b/packages/grafana-runtime/src/utils/toDataQueryError.ts @@ -7,11 +7,11 @@ import { DataQueryError } from '@grafana/data'; * @public */ export function toDataQueryError(err: DataQueryError | string | unknown): DataQueryError { - const error = (err || {}) as DataQueryError; + const error: DataQueryError = err || {}; if (!error.message) { - if (typeof err === 'string' || err instanceof String) { - return { message: err } as DataQueryError; + if (typeof err === 'string') { + return { message: err }; } let message = 'Query error'; diff --git a/packages/grafana-ui/src/components/ButtonCascader/ButtonCascader.tsx b/packages/grafana-ui/src/components/ButtonCascader/ButtonCascader.tsx index c0ad9212b9c..365133b2584 100644 --- a/packages/grafana-ui/src/components/ButtonCascader/ButtonCascader.tsx +++ b/packages/grafana-ui/src/components/ButtonCascader/ButtonCascader.tsx @@ -51,7 +51,7 @@ export const ButtonCascader: React.FC = (props) => { // Weird way to do this bit it goes around a styling issue in Button where even null/undefined child triggers // styling change which messes up the look if there is only single icon content. - let content: any = props.children; + let content: React.ReactNode = props.children; if (!hideDownIcon) { content = [props.children, ]; } diff --git a/packages/grafana-ui/src/components/CustomScrollbar/CustomScrollbar.tsx b/packages/grafana-ui/src/components/CustomScrollbar/CustomScrollbar.tsx index 3e4c7ff6c09..dc3f0ec21fc 100644 --- a/packages/grafana-ui/src/components/CustomScrollbar/CustomScrollbar.tsx +++ b/packages/grafana-ui/src/components/CustomScrollbar/CustomScrollbar.tsx @@ -49,7 +49,7 @@ export const CustomScrollbar = ({ onScroll, children, }: React.PropsWithChildren) => { - const ref = useRef(null); + const ref = useRef void }>(null); const styles = useStyles2(getStyles); useEffect(() => { @@ -73,7 +73,7 @@ export const CustomScrollbar = ({ return; } setTimeout(() => { - const scrollbar = ref.current as any; + const scrollbar = ref.current; if (scrollbar?.update) { scrollbar.update(); } diff --git a/public/app/features/datasources/components/DataSourceTestingStatus.tsx b/public/app/features/datasources/components/DataSourceTestingStatus.tsx index 99987f9ec74..43d67e578fd 100644 --- a/public/app/features/datasources/components/DataSourceTestingStatus.tsx +++ b/public/app/features/datasources/components/DataSourceTestingStatus.tsx @@ -1,8 +1,8 @@ import React from 'react'; import { selectors as e2eSelectors } from '@grafana/e2e-selectors'; +import { TestingStatus } from '@grafana/runtime'; import { Alert } from '@grafana/ui'; -import { TestingStatus } from 'app/types'; export type Props = { testingStatus?: TestingStatus; diff --git a/public/app/features/datasources/state/reducers.ts b/public/app/features/datasources/state/reducers.ts index c0ee343d378..3a9921d1af3 100644 --- a/public/app/features/datasources/state/reducers.ts +++ b/public/app/features/datasources/state/reducers.ts @@ -1,7 +1,8 @@ import { AnyAction, createAction } from '@reduxjs/toolkit'; import { DataSourcePluginMeta, DataSourceSettings, LayoutMode, LayoutModes } from '@grafana/data'; -import { DataSourcesState, DataSourceSettingsState, TestingStatus } from 'app/types'; +import { TestingStatus } from '@grafana/runtime'; +import { DataSourcesState, DataSourceSettingsState } from 'app/types'; import { GenericDataSourcePlugin } from '../types'; diff --git a/public/app/types/datasources.ts b/public/app/types/datasources.ts index 207bd18995c..585185c221e 100644 --- a/public/app/types/datasources.ts +++ b/public/app/types/datasources.ts @@ -1,5 +1,5 @@ import { DataSourcePluginMeta, DataSourceSettings, LayoutMode } from '@grafana/data'; -import { HealthCheckResultDetails } from '@grafana/runtime/src/utils/DataSourceWithBackend'; +import { TestingStatus } from '@grafana/runtime'; import { GenericDataSourcePlugin } from 'app/features/datasources/types'; export interface DataSourcesState { @@ -17,12 +17,6 @@ export interface DataSourcesState { isSortAscending: boolean; } -export interface TestingStatus { - message?: string | null; - status?: string | null; - details?: HealthCheckResultDetails; -} - export interface DataSourceSettingsState { plugin?: GenericDataSourcePlugin | null; testingStatus?: TestingStatus;