From 9bbc2c4cbbe311636f926ef39e4b9facd76c942e Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Fri, 16 Dec 2022 09:59:00 +0000 Subject: [PATCH] Chore: fix some typings in @grafana/data (#60009) fix some typings in @grafana/data --- .betterer.results | 124 +++++------------- .../src/dataframe/ArrayDataFrame.ts | 4 +- .../src/dataframe/DataFrameJSON.ts | 2 +- .../src/dataframe/MutableDataFrame.ts | 10 +- .../src/dataframe/frameComparisons.ts | 11 +- .../src/dataframe/processDataFrame.ts | 10 +- .../grafana-data/src/datetime/rangeutil.ts | 10 +- .../src/field/displayProcessor.test.ts | 5 +- .../src/field/displayProcessor.ts | 6 +- .../grafana-data/src/field/fieldColor.test.ts | 2 +- .../grafana-data/src/field/fieldComparers.ts | 38 +++--- .../src/field/fieldDisplay.test.ts | 4 +- .../src/field/getFieldDisplayValuesProxy.ts | 71 +++++----- .../grafana-data/src/field/templateProxies.ts | 6 +- .../src/panel/registryFactories.ts | 4 +- packages/grafana-data/src/text/sanitize.ts | 4 +- packages/grafana-data/src/text/string.ts | 2 +- .../src/themes/colorManipulator.ts | 2 +- .../src/transformations/fieldReducer.test.ts | 2 +- .../src/transformations/fieldReducer.ts | 14 +- 20 files changed, 139 insertions(+), 192 deletions(-) diff --git a/.betterer.results b/.betterer.results index fafff607bb6..bc05669f6c5 100644 --- a/.betterer.results +++ b/.betterer.results @@ -51,21 +51,18 @@ exports[`better eslint`] = { [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.", "4"], - [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, "Unexpected any. Specify a different type.", "8"] + [0, 0, 0, "Do not use any type assertions.", "5"], + [0, 0, 0, "Unexpected any. Specify a different type.", "6"] ], "packages/grafana-data/src/dataframe/CircularDataFrame.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "packages/grafana-data/src/dataframe/DataFrameJSON.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], + [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, "Unexpected any. Specify a different type.", "5"] + [0, 0, 0, "Unexpected any. Specify a different type.", "4"] ], "packages/grafana-data/src/dataframe/DataFrameView.test.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -91,23 +88,18 @@ exports[`better eslint`] = { [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.", "4"], - [0, 0, 0, "Unexpected any. Specify a different type.", "5"], + [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.", "7"], [0, 0, 0, "Unexpected any. Specify a different type.", "8"], - [0, 0, 0, "Do not use any type assertions.", "9"], + [0, 0, 0, "Unexpected any. Specify a different type.", "9"], [0, 0, 0, "Unexpected any. Specify a different type.", "10"], - [0, 0, 0, "Unexpected any. Specify a different type.", "11"], + [0, 0, 0, "Do not use any type assertions.", "11"], [0, 0, 0, "Unexpected any. Specify a different type.", "12"], - [0, 0, 0, "Unexpected any. Specify a different type.", "13"], + [0, 0, 0, "Do not use any type assertions.", "13"], [0, 0, 0, "Unexpected any. Specify a different type.", "14"], [0, 0, 0, "Unexpected any. Specify a different type.", "15"], - [0, 0, 0, "Do not use any type assertions.", "16"], - [0, 0, 0, "Unexpected any. Specify a different type.", "17"], - [0, 0, 0, "Do not use any type assertions.", "18"], - [0, 0, 0, "Unexpected any. Specify a different type.", "19"], - [0, 0, 0, "Unexpected any. Specify a different type.", "20"], - [0, 0, 0, "Do not use any type assertions.", "21"] + [0, 0, 0, "Do not use any type assertions.", "16"] ], "packages/grafana-data/src/dataframe/dimensions.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] @@ -119,10 +111,7 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "3"] ], "packages/grafana-data/src/dataframe/frameComparisons.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, "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.", "0"] ], "packages/grafana-data/src/dataframe/processDataFrame.test.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] @@ -142,21 +131,16 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "11"], [0, 0, 0, "Unexpected any. Specify a different type.", "12"], [0, 0, 0, "Unexpected any. Specify a different type.", "13"], - [0, 0, 0, "Unexpected any. Specify a different type.", "14"], - [0, 0, 0, "Unexpected any. Specify a different type.", "15"], + [0, 0, 0, "Do not use any type assertions.", "14"], + [0, 0, 0, "Do not use any type assertions.", "15"], [0, 0, 0, "Unexpected any. Specify a different type.", "16"], - [0, 0, 0, "Unexpected any. Specify a different type.", "17"], + [0, 0, 0, "Do not use any type assertions.", "17"], [0, 0, 0, "Unexpected any. Specify a different type.", "18"], [0, 0, 0, "Do not use any type assertions.", "19"], [0, 0, 0, "Do not use any type assertions.", "20"], - [0, 0, 0, "Unexpected any. Specify a different type.", "21"], - [0, 0, 0, "Do not use any type assertions.", "22"], - [0, 0, 0, "Unexpected any. Specify a different type.", "23"], - [0, 0, 0, "Do not use any type assertions.", "24"], - [0, 0, 0, "Do not use any type assertions.", "25"], - [0, 0, 0, "Do not use any type assertions.", "26"], - [0, 0, 0, "Unexpected any. Specify a different type.", "27"], - [0, 0, 0, "Unexpected any. Specify a different type.", "28"] + [0, 0, 0, "Do not use any type assertions.", "21"], + [0, 0, 0, "Unexpected any. Specify a different type.", "22"], + [0, 0, 0, "Unexpected any. Specify a different type.", "23"] ], "packages/grafana-data/src/datetime/datemath.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -196,13 +180,9 @@ exports[`better eslint`] = { ], "packages/grafana-data/src/datetime/rangeutil.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, "Do not use any type assertions.", "4"], - [0, 0, 0, "Do not use any type assertions.", "5"], - [0, 0, 0, "Do not use any type assertions.", "6"], - [0, 0, 0, "Unexpected any. Specify a different type.", "7"] + [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"] ], "packages/grafana-data/src/datetime/timezones.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], @@ -233,37 +213,17 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "4"] ], "packages/grafana-data/src/field/displayProcessor.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.", "0"] ], "packages/grafana-data/src/field/displayProcessor.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.", "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-data/src/field/fieldColor.test.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], - "packages/grafana-data/src/field/fieldComparers.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.", "4"], - [0, 0, 0, "Unexpected any. Specify a different type.", "5"], - [0, 0, 0, "Unexpected any. Specify a different type.", "6"], - [0, 0, 0, "Unexpected any. Specify a different type.", "7"] + [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"] ], "packages/grafana-data/src/field/fieldDisplay.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.", "1"] ], "packages/grafana-data/src/field/fieldOverrides.test.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -309,10 +269,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "4"], [0, 0, 0, "Unexpected any. Specify a different type.", "5"] ], - "packages/grafana-data/src/field/getFieldDisplayValuesProxy.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"] - ], "packages/grafana-data/src/field/overrides/processors.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"], @@ -348,10 +304,7 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "7"] ], "packages/grafana-data/src/field/templateProxies.ts: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.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "packages/grafana-data/src/geo/layer.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -374,16 +327,8 @@ exports[`better eslint`] = { ], "packages/grafana-data/src/panel/registryFactories.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, "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"] - ], - "packages/grafana-data/src/text/sanitize.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], - "packages/grafana-data/src/text/string.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"] ], "packages/grafana-data/src/text/text.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] @@ -391,22 +336,13 @@ exports[`better eslint`] = { "packages/grafana-data/src/themes/colorManipulator.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-data/src/themes/createColors.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], - "packages/grafana-data/src/transformations/fieldReducer.test.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], "packages/grafana-data/src/transformations/fieldReducer.ts:5381": [ - [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, "Do not use any type assertions.", "3"], - [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.", "0"] ], "packages/grafana-data/src/transformations/matchers/predicates.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], diff --git a/packages/grafana-data/src/dataframe/ArrayDataFrame.ts b/packages/grafana-data/src/dataframe/ArrayDataFrame.ts index 83b23d23778..2b6132526ad 100644 --- a/packages/grafana-data/src/dataframe/ArrayDataFrame.ts +++ b/packages/grafana-data/src/dataframe/ArrayDataFrame.ts @@ -6,7 +6,7 @@ import { vectorToArray } from '../vector/vectorToArray'; import { guessFieldTypeFromNameAndValue, toDataFrameDTO } from './processDataFrame'; /** @public */ -export type ValueConverter = (val: any) => T; +export type ValueConverter = (val: unknown) => T; const NOOP: ValueConverter = (v) => v; @@ -67,7 +67,7 @@ export class ArrayDataFrame extends FunctionalVector implements Data /** * Add a field for each property in the object. This will guess the type */ - setFieldsFromObject(obj: any) { + setFieldsFromObject(obj: Record) { this.fields = Object.keys(obj).map((name) => { return { name, diff --git a/packages/grafana-data/src/dataframe/DataFrameJSON.ts b/packages/grafana-data/src/dataframe/DataFrameJSON.ts index 05f3502d673..79dd8e14acd 100644 --- a/packages/grafana-data/src/dataframe/DataFrameJSON.ts +++ b/packages/grafana-data/src/dataframe/DataFrameJSON.ts @@ -111,7 +111,7 @@ export interface FieldValueEntityLookup { NegInf?: number[]; } -const ENTITY_MAP: Record = { +const ENTITY_MAP: Record = { Inf: Infinity, NegInf: -Infinity, Undef: undefined, diff --git a/packages/grafana-data/src/dataframe/MutableDataFrame.ts b/packages/grafana-data/src/dataframe/MutableDataFrame.ts index 3b4a603268b..8d5c6964147 100644 --- a/packages/grafana-data/src/dataframe/MutableDataFrame.ts +++ b/packages/grafana-data/src/dataframe/MutableDataFrame.ts @@ -13,7 +13,7 @@ export type MutableField = Field>; type MutableVectorCreator = (buffer?: any[]) => MutableVector; -export const MISSING_VALUE: any = undefined; // Treated as connected in new graph panel +export const MISSING_VALUE = undefined; // Treated as connected in new graph panel export class MutableDataFrame extends FunctionalVector implements DataFrame, MutableVector { name?: string; @@ -67,7 +67,7 @@ export class MutableDataFrame extends FunctionalVector implements Da return this.first.length; } - addFieldFor(value: any, name?: string): MutableField { + addFieldFor(value: unknown, name?: string): MutableField { return this.addField({ name: name || '', // Will be filled in type: guessFieldTypeFromValue(value), @@ -79,7 +79,7 @@ export class MutableDataFrame extends FunctionalVector implements Da if (f.values) { if (Array.isArray(f.values)) { - buffer = f.values as any[]; + buffer = f.values; } else { buffer = (f.values as Vector).toArray(); } @@ -181,7 +181,7 @@ export class MutableDataFrame extends FunctionalVector implements Da /** * This will add each value to the corresponding column */ - appendRow(row: any[]) { + appendRow(row: unknown[]) { // Add any extra columns for (let i = this.fields.length; i < row.length; i++) { this.addField({ @@ -233,7 +233,7 @@ export class MutableDataFrame extends FunctionalVector implements Da set(index: number, value: T) { if (index > this.length) { - throw new Error('Unable ot set value beyond current length'); + throw new Error('Unable to set value beyond current length'); } const obj = (value as any) || {}; diff --git a/packages/grafana-data/src/dataframe/frameComparisons.ts b/packages/grafana-data/src/dataframe/frameComparisons.ts index 2f97d21ea09..01329cbe3ca 100644 --- a/packages/grafana-data/src/dataframe/frameComparisons.ts +++ b/packages/grafana-data/src/dataframe/frameComparisons.ts @@ -43,11 +43,14 @@ export function compareDataFrameStructures(a: DataFrame, b: DataFrame, skipConfi return false; } - const cfgA = fA.config as any; - const cfgB = fB.config as any; + const cfgA = fA.config; + const cfgB = fB.config; - let aKeys = Object.keys(cfgA); - let bKeys = Object.keys(cfgB); + // need to type assert on the object keys here + // see e.g. https://github.com/Microsoft/TypeScript/issues/12870 + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + let aKeys = Object.keys(cfgA) as Array; + let bKeys = Object.keys(cfgB) as Array; if (aKeys.length !== bKeys.length) { return false; diff --git a/packages/grafana-data/src/dataframe/processDataFrame.ts b/packages/grafana-data/src/dataframe/processDataFrame.ts index c1148f5ebff..781be3c7f7c 100644 --- a/packages/grafana-data/src/dataframe/processDataFrame.ts +++ b/packages/grafana-data/src/dataframe/processDataFrame.ts @@ -180,7 +180,7 @@ const NUMBER = /^\s*(-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?|NAN)\s*$/i; /** * Given a name and value, this will pick a reasonable field type */ -export function guessFieldTypeFromNameAndValue(name: string, v: any): FieldType { +export function guessFieldTypeFromNameAndValue(name: string, v: unknown): FieldType { if (name) { name = name.toLowerCase(); if (name === 'date' || name === 'time') { @@ -193,7 +193,7 @@ export function guessFieldTypeFromNameAndValue(name: string, v: any): FieldType /** * Check the field type to see what the contents are */ -export function getFieldTypeFromValue(v: any): FieldType { +export function getFieldTypeFromValue(v: unknown): FieldType { if (v instanceof Date || isDateTime(v)) { return FieldType.time; } @@ -218,7 +218,7 @@ export function getFieldTypeFromValue(v: any): FieldType { * * NOTE: this is will try to see if string values can be mapped to other types (like number) */ -export function guessFieldTypeFromValue(v: any): FieldType { +export function guessFieldTypeFromValue(v: unknown): FieldType { if (v instanceof Date || isDateTime(v)) { return FieldType.time; } @@ -299,9 +299,9 @@ export const guessFieldTypes = (series: DataFrame, guessDefined = false): DataFr return series; }; -export const isTableData = (data: any): data is DataFrame => data && data.hasOwnProperty('columns'); +export const isTableData = (data: unknown): data is DataFrame => Boolean(data && data.hasOwnProperty('columns')); -export const isDataFrame = (data: any): data is DataFrame => data && data.hasOwnProperty('fields'); +export const isDataFrame = (data: unknown): data is DataFrame => Boolean(data && data.hasOwnProperty('fields')); /** * Inspect any object and return the results as a DataFrame diff --git a/packages/grafana-data/src/datetime/rangeutil.ts b/packages/grafana-data/src/datetime/rangeutil.ts index 4c65cc1da8b..626780d7f25 100644 --- a/packages/grafana-data/src/datetime/rangeutil.ts +++ b/packages/grafana-data/src/datetime/rangeutil.ts @@ -86,11 +86,11 @@ const hiddenRangeOptions: TimeOption[] = [ { from: 'now', to: 'now+5y', display: 'Next 5 years' }, ]; -const rangeIndex: any = {}; -each(rangeOptions, (frame: any) => { +const rangeIndex: Record = {}; +each(rangeOptions, (frame) => { rangeIndex[frame.from + ' to ' + frame.to] = frame; }); -each(hiddenRangeOptions, (frame: any) => { +each(hiddenRangeOptions, (frame) => { rangeIndex[frame.from + ' to ' + frame.to] = frame; }); @@ -100,7 +100,7 @@ each(hiddenRangeOptions, (frame: any) => { // now/d to now // now/d // if no to then to now is assumed -export function describeTextRange(expr: any) { +export function describeTextRange(expr: string) { const isLast = expr.indexOf('+') !== 0; if (expr.indexOf('now') === -1) { expr = (isLast ? 'now-' : 'now') + expr; @@ -211,7 +211,7 @@ export const convertRawToRange = (raw: RawTimeRange, timeZone?: TimeZone, fiscal function isRelativeTime(v: DateTime | string) { if (typeof v === 'string') { - return (v as string).indexOf('now') >= 0; + return v.indexOf('now') >= 0; } return false; } diff --git a/packages/grafana-data/src/field/displayProcessor.test.ts b/packages/grafana-data/src/field/displayProcessor.test.ts index 281b755cc3d..c0962202099 100644 --- a/packages/grafana-data/src/field/displayProcessor.test.ts +++ b/packages/grafana-data/src/field/displayProcessor.test.ts @@ -21,7 +21,10 @@ function assertSame(input: any, processors: DisplayProcessor[], match: DisplayVa processors.forEach((processor) => { const value = processor(input); for (const key of Object.keys(match)) { - expect((value as any)[key]).toEqual((match as any)[key]); + // need to type assert on the object keys here + // see e.g. https://github.com/Microsoft/TypeScript/issues/12870 + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + expect(value[key as keyof typeof match]).toEqual(match[key as keyof typeof match]); } }); } diff --git a/packages/grafana-data/src/field/displayProcessor.ts b/packages/grafana-data/src/field/displayProcessor.ts index 0761d29897d..24aef56ab32 100644 --- a/packages/grafana-data/src/field/displayProcessor.ts +++ b/packages/grafana-data/src/field/displayProcessor.ts @@ -82,7 +82,7 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP const formatFunc = getValueFormat(unit || 'none'); const scaleFunc = getScaleCalculator(field, options.theme); - return (value: any, adjacentDecimals?: DecimalCount) => { + return (value: unknown, adjacentDecimals?: DecimalCount) => { const { mappings } = config; const isStringUnit = unit === 'string'; @@ -186,12 +186,12 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP }; } -function toStringProcessor(value: any): DisplayValue { +function toStringProcessor(value: unknown): DisplayValue { return { text: toString(value), numeric: anyToNumber(value) }; } export function getRawDisplayProcessor(): DisplayProcessor { - return (value: any) => ({ + return (value: unknown) => ({ text: getFieldTypeFromValue(value) === 'other' ? `${JSON.stringify(value, getCircularReplacer())}` : `${value}`, numeric: null as unknown as number, }); diff --git a/packages/grafana-data/src/field/fieldColor.test.ts b/packages/grafana-data/src/field/fieldColor.test.ts index 46a708d0fcd..bf20bce0cf8 100644 --- a/packages/grafana-data/src/field/fieldColor.test.ts +++ b/packages/grafana-data/src/field/fieldColor.test.ts @@ -12,7 +12,7 @@ function getTestField(mode: string): Field { config: { color: { mode: mode, - } as any, + }, }, state: {}, }; diff --git a/packages/grafana-data/src/field/fieldComparers.ts b/packages/grafana-data/src/field/fieldComparers.ts index 3deb8f6c12e..058642dc93e 100644 --- a/packages/grafana-data/src/field/fieldComparers.ts +++ b/packages/grafana-data/src/field/fieldComparers.ts @@ -1,6 +1,6 @@ import { isNumber } from 'lodash'; -import { dateTime } from '../datetime'; +import { dateTime, isDateTime } from '../datetime'; import { Field, FieldType } from '../types/dataFrame'; import { Vector } from '../types/vector'; @@ -25,7 +25,7 @@ export const fieldIndexComparer = (field: Field, reverse = false): IndexComparer }; /** @public */ -export const timeComparer = (a: any, b: any): number => { +export const timeComparer = (a: unknown, b: unknown): number => { if (!a || !b) { return falsyComparer(a, b); } @@ -34,12 +34,14 @@ export const timeComparer = (a: any, b: any): number => { return numericComparer(a, b); } - if (dateTime(a).isBefore(b)) { - return -1; - } + if (isDateTime(a) && isDateTime(b)) { + if (dateTime(a).isBefore(b)) { + return -1; + } - if (dateTime(b).isBefore(a)) { - return 1; + if (dateTime(b).isBefore(a)) { + return 1; + } } return 0; @@ -62,7 +64,7 @@ export const booleanComparer = (a: boolean, b: boolean): number => { return falsyComparer(a, b); }; -const falsyComparer = (a: any, b: any): number => { +const falsyComparer = (a: unknown, b: unknown): number => { if (!a && b) { return 1; } @@ -74,7 +76,7 @@ const falsyComparer = (a: any, b: any): number => { return 0; }; -const timeIndexComparer = (values: Vector, reverse: boolean): IndexComparer => { +const timeIndexComparer = (values: Vector, reverse: boolean): IndexComparer => { return (a: number, b: number): number => { const vA = values.get(a); const vB = values.get(b); @@ -82,26 +84,26 @@ const timeIndexComparer = (values: Vector, reverse: boolean): IndexComparer }; }; -const booleanIndexComparer = (values: Vector, reverse: boolean): IndexComparer => { +const booleanIndexComparer = (values: Vector, reverse: boolean): IndexComparer => { return (a: number, b: number): number => { - const vA: boolean = values.get(a); - const vB: boolean = values.get(b); + const vA = values.get(a); + const vB = values.get(b); return reverse ? booleanComparer(vB, vA) : booleanComparer(vA, vB); }; }; -const numericIndexComparer = (values: Vector, reverse: boolean): IndexComparer => { +const numericIndexComparer = (values: Vector, reverse: boolean): IndexComparer => { return (a: number, b: number): number => { - const vA: number = values.get(a); - const vB: number = values.get(b); + const vA = values.get(a); + const vB = values.get(b); return reverse ? numericComparer(vB, vA) : numericComparer(vA, vB); }; }; -const stringIndexComparer = (values: Vector, reverse: boolean): IndexComparer => { +const stringIndexComparer = (values: Vector, reverse: boolean): IndexComparer => { return (a: number, b: number): number => { - const vA: string = values.get(a); - const vB: string = values.get(b); + const vA = values.get(a); + const vB = values.get(b); return reverse ? stringComparer(vB, vA) : stringComparer(vA, vB); }; }; diff --git a/packages/grafana-data/src/field/fieldDisplay.test.ts b/packages/grafana-data/src/field/fieldDisplay.test.ts index 2a8d91aed78..b7c3d0c25ae 100644 --- a/packages/grafana-data/src/field/fieldDisplay.test.ts +++ b/packages/grafana-data/src/field/fieldDisplay.test.ts @@ -321,7 +321,7 @@ describe('FieldDisplay', () => { }); const cache = { numeric: 10, text: 'Value' }; - options.data![0].fields[1].display = (v: any) => { + options.data![0].fields[1].display = () => { return cache; }; @@ -528,5 +528,5 @@ function createDisplayOptions(extend: Partial = {} theme: createTheme(), }; - return merge(options, extend); + return merge(options, extend); } diff --git a/packages/grafana-data/src/field/getFieldDisplayValuesProxy.ts b/packages/grafana-data/src/field/getFieldDisplayValuesProxy.ts index aaeeb81012a..d39fc47292f 100644 --- a/packages/grafana-data/src/field/getFieldDisplayValuesProxy.ts +++ b/packages/grafana-data/src/field/getFieldDisplayValuesProxy.ts @@ -19,38 +19,41 @@ export function getFieldDisplayValuesProxy(options: { rowIndex: number; timeZone?: TimeZone; }): Record { - return new Proxy({} as Record, { - get: (obj: any, key: string): DisplayValue | undefined => { - // 1. Match the name - let field = options.frame.fields.find((f) => key === f.name); - if (!field) { - // 2. Match the array index - const k = toNumber(key); - field = options.frame.fields[k]; - } - if (!field) { - // 3. Match the config displayName - field = options.frame.fields.find((f) => key === f.config.displayName); - } - if (!field) { - // 4. Match the name label - field = options.frame.fields.find((f) => { - if (f.labels) { - return key === f.labels.name; - } - return false; - }); - } - if (!field) { - return undefined; - } - // TODO: we could supply the field here for the getDisplayProcessor fallback but we would also need theme which - // we do not have access to here - const displayProcessor = field.display ?? getDisplayProcessor(); - const raw = field.values.get(options.rowIndex); - const disp = displayProcessor(raw); - disp.toString = () => formattedValueToString(disp); - return disp; - }, - }); + return new Proxy( + {}, + { + get: (obj, key): DisplayValue | undefined => { + // 1. Match the name + let field = options.frame.fields.find((f) => key === f.name); + if (!field) { + // 2. Match the array index + const k = toNumber(key); + field = options.frame.fields[k]; + } + if (!field) { + // 3. Match the config displayName + field = options.frame.fields.find((f) => key === f.config.displayName); + } + if (!field) { + // 4. Match the name label + field = options.frame.fields.find((f) => { + if (f.labels) { + return key === f.labels.name; + } + return false; + }); + } + if (!field) { + return undefined; + } + // TODO: we could supply the field here for the getDisplayProcessor fallback but we would also need theme which + // we do not have access to here + const displayProcessor = field.display ?? getDisplayProcessor(); + const raw = field.values.get(options.rowIndex); + const disp = displayProcessor(raw); + disp.toString = () => formattedValueToString(disp); + return disp; + }, + } + ); } diff --git a/packages/grafana-data/src/field/templateProxies.ts b/packages/grafana-data/src/field/templateProxies.ts index 28d553ec4f7..e547f1b689e 100644 --- a/packages/grafana-data/src/field/templateProxies.ts +++ b/packages/grafana-data/src/field/templateProxies.ts @@ -9,9 +9,9 @@ import { getFieldDisplayName } from './fieldState'; */ export function getTemplateProxyForField(field: Field, frame?: DataFrame, frames?: DataFrame[]): any { return new Proxy( - {} as any, // This object shows up in test snapshots + {}, // This object shows up in test snapshots { - get: (obj: Field, key: string, reciever: any) => { + get: (obj, key) => { if (key === 'name') { return field.name; } @@ -33,7 +33,7 @@ export function getTemplateProxyForField(field: Field, frame?: DataFrame, frames }, }; } - return undefined; // (field as any)[key]; // any property? + return undefined; }, } ); diff --git a/packages/grafana-data/src/panel/registryFactories.ts b/packages/grafana-data/src/panel/registryFactories.ts index 0e007bc8ad0..54a292a984f 100644 --- a/packages/grafana-data/src/panel/registryFactories.ts +++ b/packages/grafana-data/src/panel/registryFactories.ts @@ -50,8 +50,8 @@ export function createFieldConfigRegistry( } } if (config.standardOptions) { - const customDefault: any = config.standardOptions[fieldConfigProp.id as FieldConfigProperty]?.defaultValue; - const customSettings: any = config.standardOptions[fieldConfigProp.id as FieldConfigProperty]?.settings; + const customDefault = config.standardOptions[fieldConfigProp.id as FieldConfigProperty]?.defaultValue; + const customSettings = config.standardOptions[fieldConfigProp.id as FieldConfigProperty]?.settings; if (customDefault) { fieldConfigProp = { ...fieldConfigProp, diff --git a/packages/grafana-data/src/text/sanitize.ts b/packages/grafana-data/src/text/sanitize.ts index 1539e83ed4d..6492db1ea06 100644 --- a/packages/grafana-data/src/text/sanitize.ts +++ b/packages/grafana-data/src/text/sanitize.ts @@ -1,10 +1,10 @@ import { sanitizeUrl as braintreeSanitizeUrl } from '@braintree/sanitize-url'; import * as xss from 'xss'; -const XSSWL = Object.keys(xss.whiteList).reduce((acc, element) => { +const XSSWL = Object.keys(xss.whiteList).reduce((acc, element) => { acc[element] = xss.whiteList[element]?.concat(['class', 'style']); return acc; -}, {} as xss.IWhiteList); +}, {}); const sanitizeXSS = new xss.FilterXSS({ whiteList: XSSWL, diff --git a/packages/grafana-data/src/text/string.ts b/packages/grafana-data/src/text/string.ts index 41fb2d0b4c1..3828f8bc95f 100644 --- a/packages/grafana-data/src/text/string.ts +++ b/packages/grafana-data/src/text/string.ts @@ -73,7 +73,7 @@ export function stringToMs(str: string): number { } export function toNumberString(value: number | undefined | null): string { - if (value !== null && value !== undefined && Number.isFinite(value as number)) { + if (value !== null && value !== undefined && Number.isFinite(value)) { return value.toString(); } return ''; diff --git a/packages/grafana-data/src/themes/colorManipulator.ts b/packages/grafana-data/src/themes/colorManipulator.ts index 4bc4f8c3f3d..0ab6eb2cca1 100644 --- a/packages/grafana-data/src/themes/colorManipulator.ts +++ b/packages/grafana-data/src/themes/colorManipulator.ts @@ -177,7 +177,7 @@ export function decomposeColor(color: string | DecomposeColor): DecomposeColor { */ export function recomposeColor(color: DecomposeColor) { const { type, colorSpace } = color; - let values: any = color.values; + let values = color.values; if (type.indexOf('rgb') !== -1) { // Only convert the first 3 values to int (i.e. not alpha) diff --git a/packages/grafana-data/src/transformations/fieldReducer.test.ts b/packages/grafana-data/src/transformations/fieldReducer.test.ts index 34c90c57a64..dd925166384 100644 --- a/packages/grafana-data/src/transformations/fieldReducer.test.ts +++ b/packages/grafana-data/src/transformations/fieldReducer.test.ts @@ -10,7 +10,7 @@ import { fieldReducers, ReducerID, reduceField } from './fieldReducer'; /** * Run a reducer and get back the value */ -function reduce(field: Field, id: string): any { +function reduce(field: Field, id: string) { return reduceField({ field, reducers: [id] })[id]; } diff --git a/packages/grafana-data/src/transformations/fieldReducer.ts b/packages/grafana-data/src/transformations/fieldReducer.ts index 4ce873ab34c..7a2a720bd46 100644 --- a/packages/grafana-data/src/transformations/fieldReducer.ts +++ b/packages/grafana-data/src/transformations/fieldReducer.ts @@ -1,7 +1,7 @@ // Libraries import { isNumber } from 'lodash'; -import { NullValueMode, Field, FieldState, FieldCalcs, FieldType } from '../types/index'; +import { NullValueMode, Field, FieldCalcs, FieldType } from '../types/index'; import { Registry, RegistryItem } from '../utils/Registry'; export enum ReducerID { @@ -72,7 +72,7 @@ export function reduceField(options: ReduceFieldOptions): FieldCalcs { } } if (!field.state) { - field.state = {} as FieldState; + field.state = {}; } const queue = fieldReducers.list(reducers); @@ -81,7 +81,7 @@ export function reduceField(options: ReduceFieldOptions): FieldCalcs { // This lets the concrete implementations assume at least one row const data = field.values; if (data.length < 1) { - const calcs = { ...field.state.calcs } as FieldCalcs; + const calcs: FieldCalcs = { ...field.state.calcs }; for (const reducer of queue) { calcs[reducer.id] = reducer.emptyInputResult !== null ? reducer.emptyInputResult : null; } @@ -266,7 +266,7 @@ export const fieldReducers = new Registry(() => [ ]); export function doStandardCalcs(field: Field, ignoreNulls: boolean, nullAsZero: boolean): FieldCalcs { - const calcs = { + const calcs: FieldCalcs = { sum: 0, max: -Number.MAX_VALUE, min: Number.MAX_VALUE, @@ -288,7 +288,7 @@ export function doStandardCalcs(field: Field, ignoreNulls: boolean, nullAsZero: // Just used for calculations -- not exposed as a stat previousDeltaUp: true, - } as FieldCalcs; + }; const data = field.values; calcs.count = ignoreNulls ? data.length : data.toArray().filter((val) => val != null).length; @@ -466,7 +466,7 @@ function calculateChangeCount(field: Field, ignoreNulls: boolean, nullAsZero: bo const data = field.values; let count = 0; let first = true; - let last: any = null; + let last = null; for (let i = 0; i < data.length; i++) { let currentValue = data.get(i); if (currentValue === null) { @@ -489,7 +489,7 @@ function calculateChangeCount(field: Field, ignoreNulls: boolean, nullAsZero: bo function calculateDistinctCount(field: Field, ignoreNulls: boolean, nullAsZero: boolean): FieldCalcs { const data = field.values; - const distinct = new Set(); + const distinct = new Set(); for (let i = 0; i < data.length; i++) { let currentValue = data.get(i); if (currentValue === null) {