Chore: improve some types (#62363)

* improve some types

* more tidy up

* better error message handling + update tests

* undo store changes
This commit is contained in:
Ashley Harrison 2023-02-01 10:50:34 +00:00 committed by GitHub
parent 414bcd0a78
commit b6aedaa57d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 44 additions and 180 deletions

View File

@ -2567,11 +2567,6 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/app/core/utils/errors.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"]
],
"public/app/core/utils/errors.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/app/core/utils/explore.test.ts:5381": [
@ -7737,9 +7732,6 @@ exports[`better eslint`] = {
[0, 0, 0, "Do not use any type assertions.", "14"],
[0, 0, 0, "Do not use any type assertions.", "15"]
],
"public/app/plugins/panel/xychart/types.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/app/polyfills/old-mediaquerylist.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
@ -7767,8 +7759,7 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "6"]
],
"public/app/types/dashboard.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.", "0"]
],
"public/app/types/events.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
@ -7785,9 +7776,6 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "11"],
[0, 0, 0, "Unexpected any. Specify a different type.", "12"]
],
"public/app/types/explore.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/app/types/jquery/jquery.d.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
@ -7799,11 +7787,6 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "7"],
[0, 0, 0, "Unexpected any. Specify a different type.", "8"]
],
"public/app/types/plugins.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"]
],
"public/app/types/store.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
@ -7820,16 +7803,11 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "2"]
],
"public/test/core/redux/mocks.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"]
],
"public/test/core/redux/reducerTester.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.", "1"]
],
"public/test/core/redux/reduxTester.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
@ -7881,17 +7859,6 @@ exports[`better eslint`] = {
"public/test/matchers/index.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/test/matchers/toEmitValues.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, "Unexpected any. Specify a different type.", "8"]
],
"public/test/matchers/toEmitValuesWith.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
@ -7902,23 +7869,6 @@ exports[`better eslint`] = {
"public/test/matchers/utils.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/test/mocks/angular.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"]
],
"public/test/mocks/backend_srv.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/test/mocks/common.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"]
],
"public/test/mocks/datasource_srv.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]
],
"public/test/mocks/workers.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]

View File

@ -1,3 +1,4 @@
import { FetchError } from '@grafana/runtime';
import { getMessageFromError } from 'app/core/utils/errors';
describe('errors functions', () => {
@ -15,7 +16,7 @@ describe('errors functions', () => {
describe('when getMessageFromError gets an error object with message field', () => {
beforeEach(() => {
message = getMessageFromError({ message: 'error string' } as Error);
message = getMessageFromError(new Error('error string'));
});
it('should return the message text', () => {
@ -25,7 +26,7 @@ describe('errors functions', () => {
describe('when getMessageFromError gets an error object with data.message field', () => {
beforeEach(() => {
message = getMessageFromError({ data: { message: 'error string' } } as any);
message = getMessageFromError({ data: { message: 'error string' }, status: 500 } as FetchError);
});
it('should return the message text', () => {
@ -35,7 +36,7 @@ describe('errors functions', () => {
describe('when getMessageFromError gets an error object with statusText field', () => {
beforeEach(() => {
message = getMessageFromError({ statusText: 'error string' } as any);
message = getMessageFromError({ data: 'foo', statusText: 'error string', status: 500 } as FetchError);
});
it('should return the statusText text', () => {

View File

@ -1,17 +1,18 @@
import { isString } from 'lodash';
import { isFetchError } from '@grafana/runtime';
export function getMessageFromError(err: string | (Error & { data?: any; statusText?: string })): string {
if (err && !isString(err)) {
if (err.message) {
export function getMessageFromError(err: unknown): string {
if (err) {
if (typeof err === 'string') {
return err;
} else if (err instanceof Error) {
return err.message;
} else if (err.data && err.data.message) {
return err.data.message;
} else if (err.statusText) {
return err.statusText;
} else {
return JSON.stringify(err);
} else if (isFetchError(err)) {
if (err.data && err.data.message) {
return err.data.message;
} else if (err.statusText) {
return err.statusText;
}
}
}
return err;
return JSON.stringify(err);
}

View File

@ -13,8 +13,8 @@ const datasourceSrv = new DatasourceSrvMock(defaultDS, {
A: new MockObservableDataSourceApi('DSA', [{ data: ['AAAA'] }]),
B: new MockObservableDataSourceApi('DSB', [{ data: ['BBBB'] }]),
C: new MockObservableDataSourceApi('DSC', [{ data: ['CCCC'] }]),
D: new MockObservableDataSourceApi('DSD', [{ data: [] }], {}, 'syntax error near FROM'),
E: new MockObservableDataSourceApi('DSE', [{ data: [] }], {}, 'syntax error near WHERE'),
D: new MockObservableDataSourceApi('DSD', [{ data: [] }], undefined, 'syntax error near FROM'),
E: new MockObservableDataSourceApi('DSE', [{ data: [] }], undefined, 'syntax error near WHERE'),
Loki: new MockObservableDataSourceApi('Loki', [
{ data: ['A'], key: 'A' },
{ data: ['B'], key: 'B' },

View File

@ -24,13 +24,6 @@ export interface ScatterHoverEvent {
export type ScatterHoverCallback = (evt?: ScatterHoverEvent) => void;
export interface LegendInfo {
color: CanvasRenderingContext2D['strokeStyle'];
text: string;
symbol: string;
openEditor?: (evt: any) => void;
}
export interface ScatterFieldConfig extends GeneratedScatterFieldConfig {
pointSymbol?: DimensionSupplier<string>;
}

View File

@ -84,7 +84,7 @@ export enum DashboardInitPhase {
export interface DashboardInitError {
message: string;
error: any;
error: unknown;
}
export enum KioskMode {

View File

@ -9,7 +9,6 @@ import {
HistoryItem,
LogsModel,
PanelData,
QueryHint,
RawTimeRange,
TimeRange,
EventBusExtended,
@ -231,11 +230,8 @@ export interface QueryOptions {
export interface QueryTransaction {
id: string;
done: boolean;
error?: string | JSX.Element;
hints?: QueryHint[];
request: DataQueryRequest;
queries: DataQuery[];
result?: any; // Table model / Timeseries[] / Logs
scanning?: boolean;
}

View File

@ -1,5 +1,4 @@
import { PanelPlugin, PluginError, PluginMeta } from '@grafana/data';
import { TemplateSrv } from '@grafana/runtime';
export interface PluginDashboard {
dashboardId: number;
@ -31,13 +30,3 @@ export interface PluginsState {
isLoadingPluginDashboards: boolean;
panels: PanelPluginsIndex;
}
/**
* @deprecated use VariableQueryEditorProps instead
*/
export interface VariableQueryProps {
query: any;
onChange: (query: any, definition: string) => void;
datasource: any;
templateSrv: TemplateSrv;
}

View File

@ -5,8 +5,8 @@ export const mockToolkitActionCreator = <T extends string>(creator: PayloadActio
};
export type ToolkitActionCreatorWithoutPayloadMockType = typeof mockToolkitActionCreatorWithoutPayload &
ActionCreatorWithoutPayload<any>;
ActionCreatorWithoutPayload;
export const mockToolkitActionCreatorWithoutPayload = (creator: ActionCreatorWithoutPayload<any>) => {
export const mockToolkitActionCreatorWithoutPayload = (creator: ActionCreatorWithoutPayload) => {
return Object.assign(jest.fn(), creator);
};

View File

@ -30,7 +30,7 @@ interface ObjectType extends Object {
export const deepFreeze = <T>(obj: T): T => {
Object.freeze(obj);
const isNotException = (object: any, propertyName: any) =>
const isNotException = (object: unknown, propertyName: string) =>
typeof object === 'function'
? propertyName !== 'caller' && propertyName !== 'callee' && propertyName !== 'arguments'
: true;
@ -39,7 +39,7 @@ export const deepFreeze = <T>(obj: T): T => {
if (obj && obj instanceof Object) {
const object: ObjectType = obj;
Object.getOwnPropertyNames(object).forEach((propertyName) => {
const objectProperty: any = object[propertyName];
const objectProperty = object[propertyName];
if (
hasOwnProp.call(object, propertyName) &&
isNotException(object, propertyName) &&

View File

@ -4,7 +4,7 @@ import { Observable, Subscription } from 'rxjs';
import { expectObservable, forceObservableCompletion } from './utils';
function passMessage(received: any[], expected: any[]) {
function passMessage(received: unknown[], expected: unknown[]) {
return `${matcherHint('.not.toEmitValues')}
Expected observable to emit values:
@ -14,7 +14,7 @@ function passMessage(received: any[], expected: any[]) {
`;
}
function failMessage(received: any[], expected: any[]) {
function failMessage(received: unknown[], expected: unknown[]) {
return `${matcherHint('.toEmitValues')}
Expected observable to emit values:
@ -24,7 +24,7 @@ function failMessage(received: any[], expected: any[]) {
`;
}
function tryExpectations(received: any[], expected: any[]): jest.CustomMatcherResult {
function tryExpectations(received: unknown[], expected: unknown[]): jest.CustomMatcherResult {
try {
if (received.length !== expected.length) {
return {
@ -58,14 +58,14 @@ function tryExpectations(received: any[], expected: any[]): jest.CustomMatcherRe
}
}
export function toEmitValues(received: Observable<any>, expected: any[]): Promise<jest.CustomMatcherResult> {
export function toEmitValues(received: Observable<unknown>, expected: unknown[]): Promise<jest.CustomMatcherResult> {
const failsChecks = expectObservable(received);
if (failsChecks) {
return Promise.resolve(failsChecks);
}
return new Promise((resolve) => {
const receivedValues: any[] = [];
const receivedValues: unknown[] = [];
const subscription = new Subscription();
subscription.add(

View File

@ -1,17 +0,0 @@
export default class AngularJSMock {
service: any;
controller: any;
directive: any;
constructor() {
this.service = jest.fn();
this.controller = jest.fn();
this.directive = jest.fn();
}
module() {
return this;
}
}
module.exports = AngularJSMock;

View File

@ -1,5 +0,0 @@
export class BackendSrvMock {
search: any;
constructor() {}
}

View File

@ -1,54 +0,0 @@
import { NavModel, NavModelItem } from '@grafana/data';
export const backendSrv = {
get: jest.fn(),
getDashboard: jest.fn(),
getDashboardByUid: jest.fn(),
getFolderByUid: jest.fn(),
post: jest.fn(),
};
export function createNavTree(...args: any[]) {
const root: any[] = [];
let node = root;
for (const arg of args) {
const child: any = { id: arg, url: `/url/${arg}`, text: `${arg}-Text`, children: [] };
node.push(child);
node = child.children;
}
return root;
}
export function createNavModel(title: string, ...tabs: string[]): NavModel {
const node: NavModelItem = {
id: title,
text: title,
icon: 'exclamation-triangle',
subTitle: 'subTitle',
url: title,
children: [],
breadcrumbs: [],
};
const children: NavModelItem[] = [];
for (const tab of tabs) {
children.push({
id: tab,
subTitle: 'subTitle',
url: title,
text: title,
active: false,
});
}
children[0].active = true;
node.children = children;
return {
node: node,
main: node,
};
}

View File

@ -31,7 +31,12 @@ export class DatasourceSrvMock {
export class MockDataSourceApi extends DataSourceApi {
result: DataQueryResponse = { data: [] };
constructor(name?: string, result?: DataQueryResponse, meta?: any, public error: string | null = null) {
constructor(
name?: string,
result?: DataQueryResponse,
meta?: DataSourcePluginMeta,
public error: string | null = null
) {
super({ name: name ? name : 'MockDataSourceApi' } as DataSourceInstanceSettings);
if (result) {
this.result = result;
@ -60,7 +65,12 @@ export class MockDataSourceApi extends DataSourceApi {
export class MockObservableDataSourceApi extends DataSourceApi {
results: DataQueryResponse[] = [{ data: [] }];
constructor(name?: string, results?: DataQueryResponse[], meta?: any, private error: string | null = null) {
constructor(
name?: string,
results?: DataQueryResponse[],
meta?: DataSourcePluginMeta,
private error: string | null = null
) {
super({ name: name ? name : 'MockDataSourceApi' } as DataSourceInstanceSettings);
if (results) {