From 761d5c4208632a1c186cf4c9fbcf2541a816fea8 Mon Sep 17 00:00:00 2001 From: Yongjie Zhao Date: Fri, 8 Apr 2022 17:32:29 +0800 Subject: [PATCH] chore: cleanup as unknown conversion (#19587) --- .../cypress/utils/parsePostForm.ts | 2 +- .../superset-ui-core/src/color/utils.ts | 13 ++++- .../superset-ui-core/test/color/utils.test.ts | 14 +++++ .../test/models/ExtensibleFunction.test.ts | 2 +- .../test/query/buildQueryObject.test.ts | 3 +- .../plugins/plugin-chart-table/testData.ts | 2 +- .../src/Timeseries/transformers.ts | 2 +- .../SqlLab/components/QuerySearch/index.tsx | 6 +- .../util/getFormDataWithExtraFilters.test.ts | 18 +----- .../src/dashboard/util/injectCustomCss.ts | 2 +- .../ExploreChartHeader.test.tsx | 3 +- .../PropertiesModal/PropertiesModal.test.tsx | 58 +++++-------------- .../controlUtils/controlUtils.test.tsx | 16 ++++- .../data/database/DatabaseModal/index.tsx | 12 ++-- .../src/views/CRUD/data/database/types.ts | 2 +- 15 files changed, 70 insertions(+), 85 deletions(-) diff --git a/superset-frontend/cypress-base/cypress/utils/parsePostForm.ts b/superset-frontend/cypress-base/cypress/utils/parsePostForm.ts index 0a818d18d5..2d85a8681a 100644 --- a/superset-frontend/cypress-base/cypress/utils/parsePostForm.ts +++ b/superset-frontend/cypress-base/cypress/utils/parsePostForm.ts @@ -22,7 +22,7 @@ export default function parsePostForm(requestBody: ArrayBuffer) { type ParsedFields = Record; if (requestBody.constructor.name !== 'ArrayBuffer') { - return requestBody as unknown as ParsedFields; + return requestBody; } const lines = new TextDecoder('utf-8').decode(requestBody).split('\n'); const fields: ParsedFields = {}; diff --git a/superset-frontend/packages/superset-ui-core/src/color/utils.ts b/superset-frontend/packages/superset-ui-core/src/color/utils.ts index f81a554690..1b362efe3e 100644 --- a/superset-frontend/packages/superset-ui-core/src/color/utils.ts +++ b/superset-frontend/packages/superset-ui-core/src/color/utils.ts @@ -75,7 +75,14 @@ export function getAnalogousColors(colors: string[], results: number) { } export function addAlpha(color: string, opacity: number): string { - // coerce values so ti is between 0 and 1. - const rounded = Math.round(Math.min(Math.max(opacity || 1, 0), 1) * 255); - return color + rounded.toString(16).toUpperCase(); + // opacity value should be between 0 and 1. + if (opacity > 1 || opacity < 0) { + throw new Error(`The opacity should between 0 and 1, but got: ${opacity}`); + } + // the alpha value is between 00 - FF + const alpha = `0${Math.round(opacity * 255) + .toString(16) + .toUpperCase()}`.slice(-2); + + return `${color}${alpha}`; } diff --git a/superset-frontend/packages/superset-ui-core/test/color/utils.test.ts b/superset-frontend/packages/superset-ui-core/test/color/utils.test.ts index e203b94d8f..308eec726b 100644 --- a/superset-frontend/packages/superset-ui-core/test/color/utils.test.ts +++ b/superset-frontend/packages/superset-ui-core/test/color/utils.test.ts @@ -67,5 +67,19 @@ describe('color utils', () => { it('adds 50% opacity to white', () => { expect(addAlpha('#FFFFFF', 0.5)).toBe('#FFFFFF80'); }); + it('should apply transparent alpha', () => { + expect(addAlpha('#000000', 0)).toBe('#00000000'); + }); + it('should apply fully opaque', () => { + expect(addAlpha('#000000', 1)).toBe('#000000FF'); + }); + it('opacity should be between 0 and 1', () => { + expect(() => { + addAlpha('#000000', 2); + }).toThrow(); + expect(() => { + addAlpha('#000000', -1); + }).toThrow(); + }); }); }); diff --git a/superset-frontend/packages/superset-ui-core/test/models/ExtensibleFunction.test.ts b/superset-frontend/packages/superset-ui-core/test/models/ExtensibleFunction.test.ts index 3f8c38e1ae..931989853d 100644 --- a/superset-frontend/packages/superset-ui-core/test/models/ExtensibleFunction.test.ts +++ b/superset-frontend/packages/superset-ui-core/test/models/ExtensibleFunction.test.ts @@ -55,7 +55,7 @@ describe('ExtensibleFunction', () => { // @ts-ignore super(function customName() { // @ts-ignore - return customName.x as unknown; + return customName.x; }); // named function this.x = x; } diff --git a/superset-frontend/packages/superset-ui-core/test/query/buildQueryObject.test.ts b/superset-frontend/packages/superset-ui-core/test/query/buildQueryObject.test.ts index b8da644653..321e2a8401 100644 --- a/superset-frontend/packages/superset-ui-core/test/query/buildQueryObject.test.ts +++ b/superset-frontend/packages/superset-ui-core/test/query/buildQueryObject.test.ts @@ -285,7 +285,8 @@ describe('buildQueryObject', () => { datasource: '5__table', granularity_sqla: 'ds', viz_type: 'table', - url_params: null as unknown as undefined, + // @ts-expect-error + url_params: null, }).url_params, ).toBeUndefined(); }); diff --git a/superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/plugin-chart-table/testData.ts b/superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/plugin-chart-table/testData.ts index 2a54c2a4da..fd5245f691 100644 --- a/superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/plugin-chart-table/testData.ts +++ b/superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/plugin-chart-table/testData.ts @@ -25,7 +25,7 @@ import { // eslint-disable-next-line import/extensions import birthNamesJson from './birthNames.json'; -export const birthNames = birthNamesJson as unknown as TableChartProps; +export const birthNames = birthNamesJson as TableChartProps; export const basicFormData: TableChartFormData = { datasource: '1__table', diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts index 7ce72695be..7e8dbf855c 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts @@ -357,7 +357,7 @@ export function transformEventAnnotation( const eventData: MarkLine1DDataItemOption[] = [ { name: label, - xAxis: time as unknown as number, + xAxis: time, }, ]; diff --git a/superset-frontend/src/SqlLab/components/QuerySearch/index.tsx b/superset-frontend/src/SqlLab/components/QuerySearch/index.tsx index ae2562207e..762f35e898 100644 --- a/superset-frontend/src/SqlLab/components/QuerySearch/index.tsx +++ b/superset-frontend/src/SqlLab/components/QuerySearch/index.tsx @@ -226,7 +226,7 @@ function QuerySearch({ actions, displayLimit }: QuerySearchProps) { value: xt, label: xt, }))} - value={from as unknown as undefined} + value={{ value: from, label: from }} autosize={false} onChange={(selected: any) => setFrom(selected?.value)} /> @@ -235,7 +235,7 @@ function QuerySearch({ actions, displayLimit }: QuerySearchProps) { name="select-to" placeholder={t('[To]-')} options={TIME_OPTIONS.map(xt => ({ value: xt, label: xt }))} - value={to as unknown as undefined} + value={{ value: to, label: to }} autosize={false} onChange={(selected: any) => setTo(selected?.value)} /> @@ -247,7 +247,7 @@ function QuerySearch({ actions, displayLimit }: QuerySearchProps) { value: s, label: s, }))} - value={status as unknown as undefined} + value={{ value: status, label: status }} isLoading={false} autosize={false} onChange={(selected: any) => setStatus(selected?.value)} diff --git a/superset-frontend/src/dashboard/util/getFormDataWithExtraFilters.test.ts b/superset-frontend/src/dashboard/util/getFormDataWithExtraFilters.test.ts index 0756ef03b3..fda5edc1a9 100644 --- a/superset-frontend/src/dashboard/util/getFormDataWithExtraFilters.test.ts +++ b/superset-frontend/src/dashboard/util/getFormDataWithExtraFilters.test.ts @@ -16,13 +16,9 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter } from '@superset-ui/core'; import getFormDataWithExtraFilters, { GetFormDataWithExtraFiltersArguments, } from 'src/dashboard/util/charts/getFormDataWithExtraFilters'; -import { DASHBOARD_ROOT_ID } from 'src/dashboard/util/constants'; -import { LayoutItem } from 'src/dashboard/types'; -import { dashboardLayout } from 'spec/fixtures/mockDashboardLayout'; import { sliceId as chartId } from 'spec/fixtures/mockChartQueries'; describe('getFormDataWithExtraFilters', () => { @@ -63,16 +59,8 @@ describe('getFormDataWithExtraFilters', () => { }, sliceId: chartId, nativeFilters: { + filters: {}, filterSets: {}, - filters: { - [filterId]: { - id: filterId, - scope: { - rootPath: [DASHBOARD_ROOT_ID], - excluded: [], - }, - } as unknown as Filter, - }, }, dataMask: { [filterId]: { @@ -82,9 +70,7 @@ describe('getFormDataWithExtraFilters', () => { ownState: {}, }, }, - layout: dashboardLayout.present as unknown as { - [key: string]: LayoutItem; - }, + layout: {}, }; it('should include filters from the passed filters', () => { diff --git a/superset-frontend/src/dashboard/util/injectCustomCss.ts b/superset-frontend/src/dashboard/util/injectCustomCss.ts index 36b3f4d762..43cb66f7d9 100644 --- a/superset-frontend/src/dashboard/util/injectCustomCss.ts +++ b/superset-frontend/src/dashboard/util/injectCustomCss.ts @@ -40,7 +40,7 @@ export default function injectCustomCss(css: string) { document.querySelector(`.${className}`) || createStyleElement(className); if ('styleSheet' in style) { - (style as unknown as MysteryStyleElement).styleSheet.cssText = css; + (style as HTMLStyleElement & MysteryStyleElement).styleSheet.cssText = css; } else { style.innerHTML = css; } diff --git a/superset-frontend/src/explore/components/ExploreChartHeader/ExploreChartHeader.test.tsx b/superset-frontend/src/explore/components/ExploreChartHeader/ExploreChartHeader.test.tsx index 35dc9eb384..3c90d4650d 100644 --- a/superset-frontend/src/explore/components/ExploreChartHeader/ExploreChartHeader.test.tsx +++ b/superset-frontend/src/explore/components/ExploreChartHeader/ExploreChartHeader.test.tsx @@ -18,7 +18,6 @@ */ import React from 'react'; -import { Slice } from 'src/types/Chart'; import { render, screen } from 'spec/helpers/testing-library'; import userEvent from '@testing-library/user-event'; import ExploreHeader from '.'; @@ -80,7 +79,7 @@ const createProps = () => ({ slice_id: 318, slice_name: 'Age distribution of respondents', slice_url: '/superset/explore/?form_data=%7B%22slice_id%22%3A%20318%7D', - } as unknown as Slice, + }, slice_name: 'Age distribution of respondents', actions: { postChartFormData: () => null, diff --git a/superset-frontend/src/explore/components/PropertiesModal/PropertiesModal.test.tsx b/superset-frontend/src/explore/components/PropertiesModal/PropertiesModal.test.tsx index f9cdca0277..4ea1327603 100644 --- a/superset-frontend/src/explore/components/PropertiesModal/PropertiesModal.test.tsx +++ b/superset-frontend/src/explore/components/PropertiesModal/PropertiesModal.test.tsx @@ -18,57 +18,27 @@ */ import React from 'react'; -import { Slice } from 'src/types/Chart'; import { render, screen, waitFor } from 'spec/helpers/testing-library'; import fetchMock from 'fetch-mock'; import userEvent from '@testing-library/user-event'; import PropertiesModal, { PropertiesModalProps } from '.'; -const createProps = () => ({ - slice: { - cache_timeout: null, - certified_by: 'John Doe', - certification_details: 'Sample certification', - changed_on: '2021-03-19T16:30:56.750230', - changed_on_humanized: '7 days ago', - datasource: 'FCC 2018 Survey', - description: null, - description_markeddown: '', - edit_url: '/chart/edit/318', - form_data: { - adhoc_filters: [], - all_columns_x: ['age'], - color_scheme: 'supersetColors', - datasource: '49__table', - granularity_sqla: 'time_start', - groupby: null, - label_colors: {}, - link_length: '25', - queryFields: { groupby: 'groupby' }, - row_limit: 10000, +const createProps = () => + ({ + slice: { + cache_timeout: null, + certified_by: 'John Doe', + certification_details: 'Sample certification', + description: null, slice_id: 318, - time_range: 'No filter', - url_params: {}, - viz_type: 'histogram', - x_axis_label: 'age', - y_axis_label: 'count', + slice_name: 'Age distribution of respondents', + is_managed_externally: false, }, - modified: '7 days ago', - owners: [ - { - text: 'Superset Admin', - value: 1, - }, - ], - slice_id: 318, - slice_name: 'Age distribution of respondents', - slice_url: '/superset/explore/?form_data=%7B%22slice_id%22%3A%20318%7D', - } as unknown as Slice, - show: true, - onHide: jest.fn(), - onSave: jest.fn(), - addSuccessToast: jest.fn(), -}); + show: true, + onHide: jest.fn(), + onSave: jest.fn(), + addSuccessToast: jest.fn(), + } as PropertiesModalProps); fetchMock.get('glob:*/api/v1/chart/318', { body: { diff --git a/superset-frontend/src/explore/controlUtils/controlUtils.test.tsx b/superset-frontend/src/explore/controlUtils/controlUtils.test.tsx index ac8dcef7a5..1b9cf1ea55 100644 --- a/superset-frontend/src/explore/controlUtils/controlUtils.test.tsx +++ b/superset-frontend/src/explore/controlUtils/controlUtils.test.tsx @@ -16,12 +16,15 @@ * specific language governing permissions and limitations * under the License. */ -import { getChartControlPanelRegistry, t } from '@superset-ui/core'; +import { + DatasourceType, + getChartControlPanelRegistry, + t, +} from '@superset-ui/core'; import { ControlConfig, ControlPanelState, CustomControlItem, - DatasourceMeta, } from '@superset-ui/chart-controls'; import { getControlConfig, @@ -44,9 +47,16 @@ const getKnownControlState = (...args: Parameters) => describe('controlUtils', () => { const state: ControlPanelState = { datasource: { + id: 1, + type: DatasourceType.Table, columns: [{ column_name: 'a' }], metrics: [{ metric_name: 'first' }, { metric_name: 'second' }], - } as unknown as DatasourceMeta, + column_format: {}, + verbose_map: {}, + main_dttm_col: '', + datasource_name: '1__table', + description: null, + }, controls: {}, form_data: { datasource: '1__table', viz_type: 'table' }, }; diff --git a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx index 612a566923..c39feaee18 100644 --- a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx +++ b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx @@ -371,11 +371,11 @@ function dbReducer( action.payload.configuration_method === CONFIGURATION_METHOD.DYNAMIC_FORM ) { - const engineParamsCatalog = Object.keys( + const engineParamsCatalog = Object.entries( extra_json?.engine_params?.catalog || {}, - ).map(e => ({ - name: e, - value: extra_json?.engine_params?.catalog[e], + ).map(([key, value]) => ({ + name: key, + value, })); return { ...action.payload, @@ -418,9 +418,7 @@ const serializeExtra = (extraJson: DatabaseObject['extra_json']) => JSON.stringify({ ...extraJson, metadata_params: JSON.parse((extraJson?.metadata_params as string) || '{}'), - engine_params: JSON.parse( - (extraJson?.engine_params as unknown as string) || '{}', - ), + engine_params: JSON.parse((extraJson?.engine_params as string) || '{}'), schemas_allowed_for_file_upload: ( extraJson?.schemas_allowed_for_file_upload || [] ).filter(schema => schema !== ''), diff --git a/superset-frontend/src/views/CRUD/data/database/types.ts b/superset-frontend/src/views/CRUD/data/database/types.ts index f8e1a7806e..d48fa956e2 100644 --- a/superset-frontend/src/views/CRUD/data/database/types.ts +++ b/superset-frontend/src/views/CRUD/data/database/types.ts @@ -79,7 +79,7 @@ export type DatabaseObject = { // Extra extra_json?: { engine_params?: { - catalog: Record | string; + catalog?: Record | string; }; metadata_params?: {} | string; metadata_cache_timeout?: {