From e4e3588400cbdee2431fc292605079859d6d7102 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 30 Jun 2022 11:28:17 +0100 Subject: [PATCH] [v9.0.x] Chore: some low-hanging type assertion fruit (#51620) * Chore: some low-hanging type assertion fruit (#51618) * some low-hanging type assertion fruit * results (cherry picked from commit 2a6b32598dceb62725f3d4c1cae8145d33434115) * results Co-authored-by: Ashley Harrison --- .betterer.results | 72 ++++--------------- .../unified/PanelAlertTabContent.test.tsx | 43 ++++++----- .../StandardAnnotationQueryEditor.tsx | 2 +- .../AddPanelWidget/AddPanelWidget.test.tsx | 4 +- .../GeneralSettings.test.tsx | 22 +++--- .../dashboard/dashgrid/PanelChrome.test.tsx | 6 +- .../dashboard/state/DashboardModel.ts | 2 +- .../features/dashboard/state/utils.test.ts | 14 ++-- .../features/variables/inspect/utils.test.ts | 44 +++++------- 9 files changed, 83 insertions(+), 126 deletions(-) diff --git a/.betterer.results b/.betterer.results index 1c864159f0e..bf3c94c1f35 100644 --- a/.betterer.results +++ b/.betterer.results @@ -4323,29 +4323,17 @@ exports[`better eslint`] = { "public/app/features/alerting/unified/AmRoutes.tsx:1547549324": [ [55, 20, 124, "Do not use any type assertions.", "1859971542"] ], - "public/app/features/alerting/unified/PanelAlertTabContent.test.tsx:2885585711": [ + "public/app/features/alerting/unified/PanelAlertTabContent.test.tsx:2750168837": [ [148, 18, 168, "Do not use any type assertions.", "3635996173"], - [161, 14, 241, "Do not use any type assertions.", "583862789"], - [161, 14, 227, "Do not use any type assertions.", "578877230"], - [174, 5, 3, "Unexpected any. Specify a different type.", "193409811"], [186, 56, 87, "Do not use any type assertions.", "3591252716"], [188, 23, 3, "Unexpected any. Specify a different type.", "193409811"], [188, 28, 3, "Unexpected any. Specify a different type.", "193409811"], [189, 53, 90, "Do not use any type assertions.", "3360422103"], [190, 6, 3, "Unexpected any. Specify a different type.", "193409811"], [191, 6, 3, "Unexpected any. Specify a different type.", "193409811"], - [198, 43, 93, "Do not use any type assertions.", "491507647"], - [198, 43, 79, "Do not use any type assertions.", "2198135124"], - [202, 9, 3, "Unexpected any. Specify a different type.", "193409811"], - [224, 43, 122, "Do not use any type assertions.", "1400972852"], - [224, 43, 108, "Do not use any type assertions.", "2513195871"], - [229, 9, 3, "Unexpected any. Specify a different type.", "193409811"], - [251, 5, 46, "Do not use any type assertions.", "3849301614"], - [251, 5, 25, "Do not use any type assertions.", "2832141036"], - [251, 27, 3, "Unexpected any. Specify a different type.", "193409811"], - [253, 43, 70, "Do not use any type assertions.", "4292164046"], - [253, 43, 56, "Do not use any type assertions.", "2628506213"], - [256, 9, 3, "Unexpected any. Specify a different type.", "193409811"] + [257, 5, 46, "Do not use any type assertions.", "3849301614"], + [257, 5, 25, "Do not use any type assertions.", "2832141036"], + [257, 27, 3, "Unexpected any. Specify a different type.", "193409811"] ], "public/app/features/alerting/unified/Receivers.test.tsx:3742116867": [ [143, 34, 29, "Do not use any type assertions.", "2249960884"] @@ -4651,9 +4639,8 @@ exports[`better eslint`] = { "public/app/features/annotations/components/AnnotationResultMapper.tsx:1382385589": [ [131, 41, 15, "Do not use any type assertions.", "3610795007"] ], - "public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx:46885179": [ - [28, 10, 11, "Do not use any type assertions.", "750480870"], - [78, 17, 16, "Do not use any type assertions.", "388222280"] + "public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx:1738271764": [ + [28, 10, 11, "Do not use any type assertions.", "750480870"] ], "public/app/features/annotations/events_processing.ts:311785085": [ [2, 46, 3, "Unexpected any. Specify a different type.", "193409811"] @@ -4740,9 +4727,7 @@ exports[`better eslint`] = { "public/app/features/comments/CommentView.tsx:3338885322": [ [31, 20, 32, "Do not use any type assertions.", "3501587761"] ], - "public/app/features/dashboard/components/AddPanelWidget/AddPanelWidget.test.tsx:1976108855": [ - [9, 15, 20, "Do not use any type assertions.", "2923490522"], - [10, 11, 16, "Do not use any type assertions.", "388222280"], + "public/app/features/dashboard/components/AddPanelWidget/AddPanelWidget.test.tsx:3529318871": [ [11, 14, 16, "Do not use any type assertions.", "2607709806"], [11, 27, 3, "Unexpected any. Specify a different type.", "193409811"] ], @@ -4867,11 +4852,9 @@ exports[`better eslint`] = { "public/app/features/dashboard/components/DashboardSettings/DashboardSettings.tsx:1752755114": [ [40, 53, 3, "Unexpected any. Specify a different type.", "193409811"] ], - "public/app/features/dashboard/components/DashboardSettings/GeneralSettings.test.tsx:1246666905": [ + "public/app/features/dashboard/components/DashboardSettings/GeneralSettings.test.tsx:428611755": [ [13, 14, 50, "Do not use any type assertions.", "3302172944"], - [15, 5, 3, "Unexpected any. Specify a different type.", "193409811"], - [19, 15, 428, "Do not use any type assertions.", "4070674009"], - [19, 15, 410, "Do not use any type assertions.", "815903200"] + [15, 5, 3, "Unexpected any. Specify a different type.", "193409811"] ], "public/app/features/dashboard/components/DashboardSettings/GeneralSettings.tsx:2117359978": [ [38, 14, 51, "Do not use any type assertions.", "4093646718"] @@ -5208,15 +5191,13 @@ exports[`better eslint`] = { "public/app/features/dashboard/dashgrid/LazyLoader.tsx:2512448803": [ [52, 23, 60, "Do not use any type assertions.", "1332568031"] ], - "public/app/features/dashboard/dashgrid/PanelChrome.test.tsx:3284581358": [ + "public/app/features/dashboard/dashgrid/PanelChrome.test.tsx:1879414306": [ [21, 39, 3, "Unexpected any. Specify a different type.", "193409811"], [21, 44, 3, "Unexpected any. Specify a different type.", "193409811"], [24, 27, 184, "Do not use any type assertions.", "2137031202"], [24, 27, 164, "Do not use any type assertions.", "4126965436"], [30, 18, 53, "Do not use any type assertions.", "3326921050"], [30, 18, 42, "Do not use any type assertions.", "1020693066"], - [36, 11, 258, "Do not use any type assertions.", "3677324054"], - [36, 11, 244, "Do not use any type assertions.", "2585800637"], [45, 15, 140, "Do not use any type assertions.", "3815914317"], [45, 15, 122, "Do not use any type assertions.", "1489375732"], [50, 12, 104, "Do not use any type assertions.", "4098692416"], @@ -5377,7 +5358,7 @@ exports[`better eslint`] = { [1032, 24, 100, "Do not use any type assertions.", "1490128200"], [1032, 24, 89, "Do not use any type assertions.", "1592095128"] ], - "public/app/features/dashboard/state/DashboardModel.ts:2545728044": [ + "public/app/features/dashboard/state/DashboardModel.ts:2406615335": [ [61, 8, 3, "Unexpected any. Specify a different type.", "193409811"], [62, 15, 3, "Unexpected any. Specify a different type.", "193409811"], [69, 6, 3, "Unexpected any. Specify a different type.", "193409811"], @@ -5403,7 +5384,6 @@ exports[`better eslint`] = { [312, 10, 3, "Unexpected any. Specify a different type.", "193409811"], [327, 27, 3, "Unexpected any. Specify a different type.", "193409811"], [423, 32, 3, "Unexpected any. Specify a different type.", "193409811"], - [446, 14, 22, "Do not use any type assertions.", "1197383730"], [473, 22, 3, "Unexpected any. Specify a different type.", "193409811"], [689, 61, 3, "Unexpected any. Specify a different type.", "193409811"], [693, 62, 3, "Unexpected any. Specify a different type.", "193409811"], @@ -5510,20 +5490,6 @@ exports[`better eslint`] = { "public/app/features/dashboard/state/reducers.ts:2272523560": [ [64, 9, 3, "Unexpected any. Specify a different type.", "193409811"] ], - "public/app/features/dashboard/state/utils.test.ts:1279742222": [ - [8, 38, 64, "Do not use any type assertions.", "3380969"], - [8, 38, 50, "Do not use any type assertions.", "3221043074"], - [9, 37, 64, "Do not use any type assertions.", "524183405"], - [9, 37, 50, "Do not use any type assertions.", "420885766"], - [17, 38, 64, "Do not use any type assertions.", "3380969"], - [17, 38, 50, "Do not use any type assertions.", "3221043074"], - [18, 37, 64, "Do not use any type assertions.", "1764689129"], - [18, 37, 50, "Do not use any type assertions.", "560997762"], - [26, 38, 127, "Do not use any type assertions.", "281200413"], - [26, 38, 113, "Do not use any type assertions.", "3150415926"], - [30, 37, 64, "Do not use any type assertions.", "524183405"], - [30, 37, 50, "Do not use any type assertions.", "420885766"] - ], "public/app/features/dashboard/utils/getPanelMenu.test.ts:2434529719": [ [99, 18, 3, "Unexpected any. Specify a different type.", "193409811"], [100, 19, 3, "Unexpected any. Specify a different type.", "193409811"], @@ -7589,18 +7555,10 @@ exports[`better eslint`] = { [87, 60, 3, "Unexpected any. Specify a different type.", "193409811"], [88, 24, 3, "Unexpected any. Specify a different type.", "193409811"] ], - "public/app/features/variables/inspect/utils.test.ts:554142034": [ - [224, 11, 103, "Do not use any type assertions.", "2980842252"], - [224, 11, 89, "Do not use any type assertions.", "4144868007"], - [268, 11, 103, "Do not use any type assertions.", "2980842252"], - [268, 11, 89, "Do not use any type assertions.", "4144868007"], - [286, 11, 103, "Do not use any type assertions.", "2980842252"], - [286, 11, 89, "Do not use any type assertions.", "4144868007"], - [304, 11, 103, "Do not use any type assertions.", "2980842252"], - [304, 11, 89, "Do not use any type assertions.", "4144868007"], - [358, 30, 3, "Unexpected any. Specify a different type.", "193409811"], - [1220, 45, 3, "Unexpected any. Specify a different type.", "193409811"], - [1719, 28, 3, "Unexpected any. Specify a different type.", "193409811"] + "public/app/features/variables/inspect/utils.test.ts:1490985138": [ + [346, 30, 3, "Unexpected any. Specify a different type.", "193409811"], + [1208, 45, 3, "Unexpected any. Specify a different type.", "193409811"], + [1707, 28, 3, "Unexpected any. Specify a different type.", "193409811"] ], "public/app/features/variables/inspect/utils.ts:3863933569": [ [62, 77, 3, "Unexpected any. Specify a different type.", "193409811"], diff --git a/public/app/features/alerting/unified/PanelAlertTabContent.test.tsx b/public/app/features/alerting/unified/PanelAlertTabContent.test.tsx index cd0170429f1..af80c1289a6 100644 --- a/public/app/features/alerting/unified/PanelAlertTabContent.test.tsx +++ b/public/app/features/alerting/unified/PanelAlertTabContent.test.tsx @@ -159,7 +159,7 @@ const dashboard = { }, } as DashboardModel; -const panel = { +const panel = new PanelModel({ datasource: { type: 'prometheus', uid: dataSources.prometheus.uid, @@ -172,7 +172,7 @@ const panel = { refId: 'A', }, ], -} as any as PanelModel; +}); const ui = { row: byTestId('row'), @@ -196,11 +196,14 @@ describe('PanelAlertTabContent', () => { }); it('Will take into account panel maxDataPoints', async () => { - await renderAlertTabContent(dashboard, { - ...panel, - maxDataPoints: 100, - interval: '10s', - } as any as PanelModel); + await renderAlertTabContent( + dashboard, + new PanelModel({ + ...panel, + maxDataPoints: 100, + interval: '10s', + }) + ); const button = await ui.createButton.find(); const href = button.href; @@ -222,12 +225,15 @@ describe('PanelAlertTabContent', () => { }); it('Will work with default datasource', async () => { - await renderAlertTabContent(dashboard, { - ...panel, - datasource: undefined, - maxDataPoints: 100, - interval: '10s', - } as any as PanelModel); + await renderAlertTabContent( + dashboard, + new PanelModel({ + ...panel, + datasource: undefined, + maxDataPoints: 100, + interval: '10s', + }) + ); const button = await ui.createButton.find(); const href = button.href; @@ -251,10 +257,13 @@ describe('PanelAlertTabContent', () => { it('Will take into account datasource minInterval', async () => { (getDatasourceSrv() as any as MockDataSourceSrv).datasources[dataSources.prometheus.uid].interval = '7m'; - await renderAlertTabContent(dashboard, { - ...panel, - maxDataPoints: 100, - } as any as PanelModel); + await renderAlertTabContent( + dashboard, + new PanelModel({ + ...panel, + maxDataPoints: 100, + }) + ); const button = await ui.createButton.find(); const href = button.href; diff --git a/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx b/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx index 3d4b60e2fad..67a266418ec 100644 --- a/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx +++ b/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx @@ -76,7 +76,7 @@ export default class StandardAnnotationQueryEditor extends PureComponent { const props: Props = { - dashboard: {} as DashboardModel, - panel: {} as PanelModel, + dashboard: new DashboardModel({}), + panel: new PanelModel({}), addPanel: jest.fn() as any, }; Object.assign(props, propOverrides); diff --git a/public/app/features/dashboard/components/DashboardSettings/GeneralSettings.test.tsx b/public/app/features/dashboard/components/DashboardSettings/GeneralSettings.test.tsx index 5d77b3db8bd..0b91d43932c 100644 --- a/public/app/features/dashboard/components/DashboardSettings/GeneralSettings.test.tsx +++ b/public/app/features/dashboard/components/DashboardSettings/GeneralSettings.test.tsx @@ -17,19 +17,21 @@ setBackendSrv({ const setupTestContext = (options: Partial) => { const defaults: Props = { - dashboard: { - title: 'test dashboard title', - description: 'test dashboard description', - timepicker: { - refresh_intervals: ['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d', '2d'], - time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'], + dashboard: new DashboardModel( + { + title: 'test dashboard title', + description: 'test dashboard description', + timepicker: { + refresh_intervals: ['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d', '2d'], + time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'], + }, + timezone: 'utc', }, - meta: { + { folderId: 1, folderTitle: 'test', - }, - timezone: 'utc', - } as unknown as DashboardModel, + } + ), updateTimeZone: jest.fn(), updateWeekStart: jest.fn(), }; diff --git a/public/app/features/dashboard/dashgrid/PanelChrome.test.tsx b/public/app/features/dashboard/dashgrid/PanelChrome.test.tsx index a883596bac4..2cedf7c6e04 100644 --- a/public/app/features/dashboard/dashgrid/PanelChrome.test.tsx +++ b/public/app/features/dashboard/dashgrid/PanelChrome.test.tsx @@ -34,15 +34,15 @@ function setupTestContext(options: Partial) { setTimeSrv(timeSrv); const defaults: Props = { - panel: { + panel: new PanelModel({ id: 123, hasTitle: jest.fn(), replaceVariables: jest.fn(), - events: { subscribe: jest.fn() }, + events: new EventBusSrv(), getQueryRunner: () => panelQueryRunner, getOptions: jest.fn(), getDisplayTitle: jest.fn(), - } as unknown as PanelModel, + }), dashboard: { panelInitialized: jest.fn(), getTimezone: () => 'browser', diff --git a/public/app/features/dashboard/state/DashboardModel.ts b/public/app/features/dashboard/state/DashboardModel.ts index 29ca48162de..13b1a51d5f4 100644 --- a/public/app/features/dashboard/state/DashboardModel.ts +++ b/public/app/features/dashboard/state/DashboardModel.ts @@ -444,7 +444,7 @@ export class DashboardModel implements TimeModel { const rowPanels = panel.panels ?? []; for (const rowPanel of rowPanels) { - yield rowPanel as PanelModel; + yield rowPanel; } } } diff --git a/public/app/features/dashboard/state/utils.test.ts b/public/app/features/dashboard/state/utils.test.ts index 1e0f10bb20f..25ab6eaf4ee 100644 --- a/public/app/features/dashboard/state/utils.test.ts +++ b/public/app/features/dashboard/state/utils.test.ts @@ -6,8 +6,8 @@ import { deleteScopeVars, isOnTheSameGridRow } from './utils'; describe('isOnTheSameGridRow', () => { describe('when source panel is next to a panel', () => { it('then it should return true', () => { - const sourcePanel: PanelModel = { gridPos: { x: 0, y: 1, w: 4, h: 4 } } as unknown as PanelModel; - const otherPanel: PanelModel = { gridPos: { x: 4, y: 1, w: 4, h: 4 } } as unknown as PanelModel; + const sourcePanel = new PanelModel({ gridPos: { x: 0, y: 1, w: 4, h: 4 } }); + const otherPanel = new PanelModel({ gridPos: { x: 4, y: 1, w: 4, h: 4 } }); expect(isOnTheSameGridRow(sourcePanel, otherPanel)).toBe(true); }); @@ -15,8 +15,8 @@ describe('isOnTheSameGridRow', () => { describe('when source panel is not next to a panel', () => { it('then it should return false', () => { - const sourcePanel: PanelModel = { gridPos: { x: 0, y: 1, w: 4, h: 4 } } as unknown as PanelModel; - const otherPanel: PanelModel = { gridPos: { x: 4, y: 5, w: 4, h: 4 } } as unknown as PanelModel; + const sourcePanel = new PanelModel({ gridPos: { x: 0, y: 1, w: 4, h: 4 } }); + const otherPanel = new PanelModel({ gridPos: { x: 4, y: 5, w: 4, h: 4 } }); expect(isOnTheSameGridRow(sourcePanel, otherPanel)).toBe(false); }); @@ -24,11 +24,11 @@ describe('isOnTheSameGridRow', () => { describe('when source panel is repeated horizontally', () => { it('then it should return false', () => { - const sourcePanel: PanelModel = { + const sourcePanel = new PanelModel({ gridPos: { x: 0, y: 1, w: 4, h: 4 }, repeatDirection: REPEAT_DIR_HORIZONTAL, - } as unknown as PanelModel; - const otherPanel: PanelModel = { gridPos: { x: 4, y: 1, w: 4, h: 4 } } as unknown as PanelModel; + }); + const otherPanel = new PanelModel({ gridPos: { x: 4, y: 1, w: 4, h: 4 } }); expect(isOnTheSameGridRow(sourcePanel, otherPanel)).toBe(false); }); diff --git a/public/app/features/variables/inspect/utils.test.ts b/public/app/features/variables/inspect/utils.test.ts index c313f6ef6a0..63b1155a8f9 100644 --- a/public/app/features/variables/inspect/utils.test.ts +++ b/public/app/features/variables/inspect/utils.test.ts @@ -220,13 +220,10 @@ describe('getPropsWithVariable', () => { describe('getAffectedPanelIdsForVariable', () => { describe('when called with a real world example with rows and repeats', () => { it('then it should return correct panel ids', () => { - const panels = dashWithRepeatsAndRows.panels.map( - (panel: PanelModel) => - ({ - id: panel.id, - getSaveModel: () => panel, - } as unknown as PanelModel) - ); + const panels = dashWithRepeatsAndRows.panels.map((panel: PanelModel) => ({ + id: panel.id, + getSaveModel: () => panel, + })); const result = getAffectedPanelIdsForVariable('query0', panels); expect(result).toEqual([15, 16, 17, 11, 12, 13, 2, 5, 7, 6]); }); @@ -264,13 +261,10 @@ describe('getAllAffectedPanelIdsForVariableChange ', () => { panels: panelsAsJson, templating: { list: variables }, } = dashWithTemplateDependenciesAndPanels; - const panels = panelsAsJson.map( - (panel: PanelModel) => - ({ - id: panel.id, - getSaveModel: () => panel, - } as unknown as PanelModel) - ); + const panels = panelsAsJson.map((panel: PanelModel) => ({ + id: panel.id, + getSaveModel: () => panel, + })); const result = getAllAffectedPanelIdsForVariableChange('ds_instance', variables, panels); expect(result).toEqual([2, 3, 4, 5]); }); @@ -282,13 +276,10 @@ describe('getAllAffectedPanelIdsForVariableChange ', () => { panels: panelsAsJson, templating: { list: variables }, } = dashWithTemplateDependenciesAndPanels; - const panels = panelsAsJson.map( - (panel: PanelModel) => - ({ - id: panel.id, - getSaveModel: () => panel, - } as unknown as PanelModel) - ); + const panels = panelsAsJson.map((panel: PanelModel) => ({ + id: panel.id, + getSaveModel: () => panel, + })); const result = getAllAffectedPanelIdsForVariableChange('depends_on_all', variables, panels); expect(result).toEqual([2]); }); @@ -300,13 +291,10 @@ describe('getAllAffectedPanelIdsForVariableChange ', () => { panels: panelsAsJson, templating: { list: variables }, } = dashWithAllVariables; - const panels = panelsAsJson.map( - (panel: PanelModel) => - ({ - id: panel.id, - getSaveModel: () => panel, - } as unknown as PanelModel) - ); + const panels = panelsAsJson.map((panel: PanelModel) => ({ + id: panel.id, + getSaveModel: () => panel, + })); const result = getAllAffectedPanelIdsForVariableChange('unknown', variables, panels); expect(result).toEqual([2, 3]); });