From b1ffe18599005ba9baaf334f696e288b8801176c Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Mon, 21 Nov 2022 13:05:55 +0000 Subject: [PATCH] Chore: Fix some as HTMLElement type assertions (#58667) --- .betterer.results | 34 ++++++------------- .../search/state/SearchStateManager.ts | 2 +- .../prometheus/components/PromQueryEditor.tsx | 2 +- .../datasource/testdata/QueryEditor.tsx | 8 ++--- .../components/SimulationQueryEditor.tsx | 2 +- .../components/SimulationSchemaForm.tsx | 2 +- public/app/plugins/panel/barchart/bars.ts | 4 ++- .../panel/canvas/CanvasContextMenu.tsx | 7 ++-- .../timeseries/plugins/ContextMenuPlugin.tsx | 5 ++- 9 files changed, 28 insertions(+), 38 deletions(-) diff --git a/.betterer.results b/.betterer.results index b09b00781de..b5d57820a1c 100644 --- a/.betterer.results +++ b/.betterer.results @@ -4651,8 +4651,7 @@ exports[`better eslint`] = { "public/app/features/search/state/SearchStateManager.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.", "2"] ], "public/app/features/search/types.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] @@ -6349,8 +6348,7 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/plugins/datasource/prometheus/components/PromQueryEditorByApp.test.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] @@ -6850,13 +6848,10 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Do not use any type assertions.", "3"], [0, 0, 0, "Unexpected any. Specify a different type.", "4"], - [0, 0, 0, "Do not use any type assertions.", "5"], + [0, 0, 0, "Unexpected any. Specify a different type.", "5"], [0, 0, 0, "Unexpected any. Specify a different type.", "6"], [0, 0, 0, "Do not use any type assertions.", "7"], - [0, 0, 0, "Do not use any type assertions.", "8"], - [0, 0, 0, "Unexpected any. Specify a different type.", "9"], - [0, 0, 0, "Do not use any type assertions.", "10"], - [0, 0, 0, "Unexpected any. Specify a different type.", "11"] + [0, 0, 0, "Unexpected any. Specify a different type.", "8"] ], "public/app/plugins/datasource/testdata/components/PredictablePulseEditor.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] @@ -6876,13 +6871,11 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Do not use any type assertions.", "3"], - [0, 0, 0, "Do not use any type assertions.", "4"], - [0, 0, 0, "Unexpected any. Specify a different type.", "5"] + [0, 0, 0, "Unexpected any. Specify a different type.", "4"] ], "public/app/plugins/datasource/testdata/components/SimulationSchemaForm.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Do not use any type assertions.", "2"] + [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], "public/app/plugins/datasource/testdata/components/StreamingClientEditor.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] @@ -7005,11 +6998,10 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Do not use any type assertions.", "2"], - [0, 0, 0, "Do not use any type assertions.", "3"], - [0, 0, 0, "Unexpected any. Specify a different type.", "4"], - [0, 0, 0, "Do not use any type assertions.", "5"], - [0, 0, 0, "Unexpected any. Specify a different type.", "6"], - [0, 0, 0, "Do not use any type assertions.", "7"] + [0, 0, 0, "Unexpected any. Specify a different type.", "3"], + [0, 0, 0, "Do not use any type assertions.", "4"], + [0, 0, 0, "Unexpected any. Specify a different type.", "5"], + [0, 0, 0, "Do not use any type assertions.", "6"] ], "public/app/plugins/panel/barchart/module.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], @@ -7067,9 +7059,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "10"], [0, 0, 0, "Do not use any type assertions.", "11"] ], - "public/app/plugins/panel/canvas/CanvasContextMenu.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "public/app/plugins/panel/canvas/CanvasPanel.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"] @@ -8185,9 +8174,6 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "15"], [0, 0, 0, "Unexpected any. Specify a different type.", "16"] ], - "public/app/plugins/panel/timeseries/plugins/ContextMenuPlugin.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "public/app/plugins/panel/timeseries/plugins/annotations/AnnotationEditor.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], diff --git a/public/app/features/search/state/SearchStateManager.ts b/public/app/features/search/state/SearchStateManager.ts index 5a218372349..06f7b589d72 100644 --- a/public/app/features/search/state/SearchStateManager.ts +++ b/public/app/features/search/state/SearchStateManager.ts @@ -105,7 +105,7 @@ export class SearchStateManager extends StateManagerBase { }; onStarredFilterChange = (e: FormEvent) => { - const starred = (e.target as HTMLInputElement).checked; + const starred = e.currentTarget.checked; this.setStateAndDoSearch({ starred }); }; diff --git a/public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx b/public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx index edb1af34bfc..0ba79277e90 100644 --- a/public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx @@ -75,7 +75,7 @@ export class PromQueryEditor extends PureComponent }; onInstantChange = (e: React.SyntheticEvent) => { - const instant = (e.target as HTMLInputElement).checked; + const instant = e.currentTarget.checked; this.query.instant = instant; this.setState({ instant }, this.onRunQuery); }; diff --git a/public/app/plugins/datasource/testdata/QueryEditor.tsx b/public/app/plugins/datasource/testdata/QueryEditor.tsx index dbd72aadc9c..084a6e0d125 100644 --- a/public/app/plugins/datasource/testdata/QueryEditor.tsx +++ b/public/app/plugins/datasource/testdata/QueryEditor.tsx @@ -124,22 +124,22 @@ export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: Props) }; const onInputChange = (e: FormEvent) => { - const { name, value, type } = e.target as HTMLInputElement | HTMLTextAreaElement; + const { name, value, type } = e.currentTarget; let newValue: any = value; if (type === 'number') { newValue = Number(value); } - if (name === 'levelColumn') { - newValue = (e.target as HTMLInputElement).checked; + if (name === 'levelColumn' && e.currentTarget instanceof HTMLInputElement) { + newValue = e.currentTarget.checked; } onUpdate({ ...query, [name]: newValue }); }; const onFieldChange = (field: string) => (e: ChangeEvent) => { - const { name, value, type } = e.target as HTMLInputElement; + const { name, value, type } = e.currentTarget; let newValue: any = value; if (type === 'number') { diff --git a/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx b/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx index fef2b7c00dc..d836a8307ea 100644 --- a/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx +++ b/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx @@ -64,7 +64,7 @@ export const SimulationQueryEditor = ({ onChange, query, ds }: EditorProps) => { }; const onUIDChanged = (e: FormEvent) => { - const { value } = e.target as HTMLInputElement; + const { value } = e.currentTarget; onUpdateKey({ ...simKey, uid: value ?? undefined }); }; diff --git a/public/app/plugins/datasource/testdata/components/SimulationSchemaForm.tsx b/public/app/plugins/datasource/testdata/components/SimulationSchemaForm.tsx index af84321a89a..7e33eb61eeb 100644 --- a/public/app/plugins/datasource/testdata/components/SimulationSchemaForm.tsx +++ b/public/app/plugins/datasource/testdata/components/SimulationSchemaForm.tsx @@ -60,7 +60,7 @@ export const SimulationSchemaForm = ({ config, schema, onChange }: SchemaFormPro const styles = useStyles2(getStyles); const onUpdateTextArea = (event: FormEvent) => { - const element = event.target as HTMLInputElement; + const element = event.currentTarget; onChange(JSON.parse(element.value)); }; diff --git a/public/app/plugins/panel/barchart/bars.ts b/public/app/plugins/panel/barchart/bars.ts index 119d8e22ae0..a0b636e1fa6 100644 --- a/public/app/plugins/panel/barchart/bars.ts +++ b/public/app/plugins/panel/barchart/bars.ts @@ -417,7 +417,9 @@ export function getConfig(opts: BarsOptions, theme: GrafanaTheme2) { let over = u.over; over.style.overflow = 'hidden'; u.root.querySelectorAll('.u-cursor-pt').forEach((el) => { - (el as HTMLElement).style.borderRadius = '0'; + if (el instanceof HTMLElement) { + el.style.borderRadius = '0'; + } }); }; diff --git a/public/app/plugins/panel/canvas/CanvasContextMenu.tsx b/public/app/plugins/panel/canvas/CanvasContextMenu.tsx index 8e299e8446d..29990ff9f60 100644 --- a/public/app/plugins/panel/canvas/CanvasContextMenu.tsx +++ b/public/app/plugins/panel/canvas/CanvasContextMenu.tsx @@ -36,8 +36,11 @@ export const CanvasContextMenu = ({ scene, panel }: Props) => { panel.setActivePanel(); const shouldSelectElement = event.currentTarget !== scene.div; - if (shouldSelectElement) { - scene.select({ targets: [event.currentTarget as HTMLElement | SVGElement] }); + if ( + shouldSelectElement && + (event.currentTarget instanceof HTMLElement || event.currentTarget instanceof SVGElement) + ) { + scene.select({ targets: [event.currentTarget] }); } setAnchorPoint({ x: event.pageX, y: event.pageY }); setIsMenuVisible(true); diff --git a/public/app/plugins/panel/timeseries/plugins/ContextMenuPlugin.tsx b/public/app/plugins/panel/timeseries/plugins/ContextMenuPlugin.tsx index 4e19f11ce24..31d44bd3ef5 100644 --- a/public/app/plugins/panel/timeseries/plugins/ContextMenuPlugin.tsx +++ b/public/app/plugins/panel/timeseries/plugins/ContextMenuPlugin.tsx @@ -119,9 +119,8 @@ export const ContextMenuPlugin: React.FC = ({ } isClick = true; - if (e.target) { - const target = e.target as HTMLElement; - if (!target.classList.contains('u-cursor-pt')) { + if (e.target instanceof HTMLElement) { + if (!e.target.classList.contains('u-cursor-pt')) { pluginLog('ContextMenuPlugin', false, 'canvas click'); setPoint({ seriesIdx: null, dataIdx: null }); }