diff --git a/plugins/cost-insights/src/components/BarChart/BarChart.tsx b/plugins/cost-insights/src/components/BarChart/BarChart.tsx index 65e7e39c64..aa1fd5c601 100644 --- a/plugins/cost-insights/src/components/BarChart/BarChart.tsx +++ b/plugins/cost-insights/src/components/BarChart/BarChart.tsx @@ -36,11 +36,11 @@ import { currencyFormatter } from '../../utils/formatters'; import { BarChartData, Maybe, - notEmpty, ResourceData, DataKey, CostInsightsTheme, } from '../../types'; +import { notEmpty } from '../../utils/assert'; import { useBarChartStyles } from '../../utils/styles'; import { resourceSort } from '../../utils/sort'; diff --git a/plugins/cost-insights/src/components/CostGrowth/CostGrowth.test.tsx b/plugins/cost-insights/src/components/CostGrowth/CostGrowth.test.tsx index 2cefcbba1e..9dff164fdf 100644 --- a/plugins/cost-insights/src/components/CostGrowth/CostGrowth.test.tsx +++ b/plugins/cost-insights/src/components/CostGrowth/CostGrowth.test.tsx @@ -17,11 +17,8 @@ import React, { PropsWithChildren } from 'react'; import { renderInTestApp } from '@backstage/test-utils'; import { CostGrowth } from './CostGrowth'; -import { - Currency, - Duration, - findAlways, -} from '../../types'; +import { Currency, Duration } from '../../types'; +import { findAlways } from '../../utils/assert'; import { MockConfigProvider, MockCurrencyProvider } from '../../utils/tests'; import { defaultCurrencies, CurrencyType } from '../../utils/currency'; diff --git a/plugins/cost-insights/src/components/CostInsightsNavigation/CostInsightsNavigation.tsx b/plugins/cost-insights/src/components/CostInsightsNavigation/CostInsightsNavigation.tsx index f190fa88bb..20c7f4ec46 100644 --- a/plugins/cost-insights/src/components/CostInsightsNavigation/CostInsightsNavigation.tsx +++ b/plugins/cost-insights/src/components/CostInsightsNavigation/CostInsightsNavigation.tsx @@ -25,7 +25,7 @@ import { } from '@material-ui/core'; import { useNavigationStyles } from '../../utils/styles'; import { useConfig, useScroll } from '../../hooks'; -import { findAlways } from '../../types'; +import { findAlways } from '../../utils/assert'; import { DefaultNavigation, NavigationItem, @@ -66,10 +66,10 @@ export const CostInsightsNavigation = ({ })} ) : ( - React.cloneElement(item.icon, { - className: classes.navigationIcon, - }) - ) + React.cloneElement(item.icon, { + className: classes.navigationIcon, + }) + ) } title={item.title} /> diff --git a/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx b/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx index c6269f7dc6..528cb1663d 100644 --- a/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx +++ b/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx @@ -26,11 +26,11 @@ import { useScroll, useFilters, useConfig } from '../../hooks'; import { mapFiltersToProps } from './selector'; import { DefaultNavigation } from '../../utils/navigation'; import { formatPercent } from '../../utils/formatters'; +import { findAlways } from '../../utils/assert'; import { Cost, CostInsightsTheme, MetricData, - findAlways, getComparedChange, } from '../../types'; diff --git a/plugins/cost-insights/src/components/CurrencySelect/CurrencySelect.tsx b/plugins/cost-insights/src/components/CurrencySelect/CurrencySelect.tsx index 413426e9af..2780c728f3 100644 --- a/plugins/cost-insights/src/components/CurrencySelect/CurrencySelect.tsx +++ b/plugins/cost-insights/src/components/CurrencySelect/CurrencySelect.tsx @@ -16,7 +16,9 @@ import React from 'react'; import { MenuItem, Select, SelectProps } from '@material-ui/core'; -import { Currency, CurrencyType, findAlways } from '../../types'; +import { Currency } from '../../types'; +import { CurrencyType } from '../../utils/currency'; +import { findAlways } from '../../utils/assert'; import { useSelectStyles as useStyles } from '../../utils/styles'; const NULL_VALUE = 'engineers'; diff --git a/plugins/cost-insights/src/components/PeriodSelect/PeriodSelect.tsx b/plugins/cost-insights/src/components/PeriodSelect/PeriodSelect.tsx index 07365c32eb..918200471f 100644 --- a/plugins/cost-insights/src/components/PeriodSelect/PeriodSelect.tsx +++ b/plugins/cost-insights/src/components/PeriodSelect/PeriodSelect.tsx @@ -20,7 +20,8 @@ import { formatLastTwoLookaheadQuarters, formatLastTwoMonths, } from '../../utils/formatters'; -import { Duration, findAlways } from '../../types'; +import { Duration } from '../../types'; +import { findAlways } from '../../utils/assert'; import { useSelectStyles as useStyles } from '../../utils/styles'; import { useLastCompleteBillingDate } from '../../hooks'; diff --git a/plugins/cost-insights/src/components/ProductInsightsCard/selector.ts b/plugins/cost-insights/src/components/ProductInsightsCard/selector.ts index 001ff717bd..efc8448034 100644 --- a/plugins/cost-insights/src/components/ProductInsightsCard/selector.ts +++ b/plugins/cost-insights/src/components/ProductInsightsCard/selector.ts @@ -16,7 +16,8 @@ import { MapFiltersToProps } from '../../hooks/useFilters'; import { MapLoadingToProps } from '../../hooks/useLoading'; -import { Duration, PageFilters, ProductPeriod, findAlways } from '../../types'; +import { Duration, PageFilters, ProductPeriod } from '../../types'; +import { findAlways } from '../../utils/assert'; type ProductInsightsCardFilterProps = PageFilters & { product: ProductPeriod; diff --git a/plugins/cost-insights/src/components/ProjectGrowthAlertCard/ProjectGrowthAlertCard.test.tsx b/plugins/cost-insights/src/components/ProjectGrowthAlertCard/ProjectGrowthAlertCard.test.tsx index 3e2fc8d32e..db3f2f9bf6 100644 --- a/plugins/cost-insights/src/components/ProjectGrowthAlertCard/ProjectGrowthAlertCard.test.tsx +++ b/plugins/cost-insights/src/components/ProjectGrowthAlertCard/ProjectGrowthAlertCard.test.tsx @@ -23,8 +23,9 @@ import { MockConfigProvider, MockBillingDateProvider, } from '../../utils/tests'; -import { AlertCost, findAlways } from '../../types'; +import { AlertCost } from '../../types'; import { defaultCurrencies } from '../../utils/currency'; +import { findAlways } from '../../utils/assert'; const engineers = findAlways(defaultCurrencies, c => c.kind === null); diff --git a/plugins/cost-insights/src/components/ResourceGrowthBarChartLegend/ResourceGrowthBarChartLegend.test.tsx b/plugins/cost-insights/src/components/ResourceGrowthBarChartLegend/ResourceGrowthBarChartLegend.test.tsx index 2bc998d9df..9599b3cca1 100644 --- a/plugins/cost-insights/src/components/ResourceGrowthBarChartLegend/ResourceGrowthBarChartLegend.test.tsx +++ b/plugins/cost-insights/src/components/ResourceGrowthBarChartLegend/ResourceGrowthBarChartLegend.test.tsx @@ -17,7 +17,9 @@ import React, { PropsWithChildren } from 'react'; import { renderInTestApp } from '@backstage/test-utils'; import { ResourceGrowthBarChartLegend } from './ResourceGrowthBarChartLegend'; -import { defaultCurrencies, Duration, findAlways } from '../../types'; +import { Duration } from '../../types'; +import { defaultCurrencies } from '../../utils/currency'; +import { findAlways } from '../../utils/assert'; import { MockConfigProvider, MockCurrencyProvider } from '../../utils/tests'; const engineers = findAlways(defaultCurrencies, c => c.kind === null); diff --git a/plugins/cost-insights/src/hooks/useCurrency.tsx b/plugins/cost-insights/src/hooks/useCurrency.tsx index b5079d37f1..ff41b033e4 100644 --- a/plugins/cost-insights/src/hooks/useCurrency.tsx +++ b/plugins/cost-insights/src/hooks/useCurrency.tsx @@ -20,7 +20,8 @@ import React, { useContext, PropsWithChildren, } from 'react'; -import { Currency, findAlways } from '../types'; +import { Currency } from '../types'; +import { findAlways } from '../utils/assert'; import { defaultCurrencies } from '../utils/currency'; export type CurrencyContextProps = { diff --git a/plugins/cost-insights/src/types/Duration.ts b/plugins/cost-insights/src/types/Duration.ts index 72501d46d5..09034ae9df 100644 --- a/plugins/cost-insights/src/types/Duration.ts +++ b/plugins/cost-insights/src/types/Duration.ts @@ -15,7 +15,7 @@ */ import moment from 'moment'; -import { assertNever } from './Maybe'; +import { assertNever } from '../utils/assert'; /** * Time periods for cost comparison; slight abuse of ISO 8601 periods. We take P1M and P3M to mean diff --git a/plugins/cost-insights/src/types/Maybe.ts b/plugins/cost-insights/src/types/Maybe.ts index d721f28d75..a01a658382 100644 --- a/plugins/cost-insights/src/types/Maybe.ts +++ b/plugins/cost-insights/src/types/Maybe.ts @@ -13,33 +13,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + export type Maybe = T | null; - -export function notEmpty( - value: TValue | null | undefined, -): value is TValue { - return value !== null && value !== undefined; -} - -// Utility for exhaustiveness checking in switch statements -export function assertNever(x: never): never { - throw new Error(`Exhaustiveness check failed: ${x}`); -} - -export function assertAlways(argument: T | undefined): T { - if (argument === undefined) { - throw new TypeError( - 'Expected to always find a value but received undefined', - ); - } - return argument; -} - -// Utility for working with static lists; asserts a value will always be found or -// throws an error -export function findAlways( - collection: T[], - callback: (el: T) => boolean, -): T { - return assertAlways(collection.find(callback)); -} diff --git a/plugins/cost-insights/src/utils/assert.ts b/plugins/cost-insights/src/utils/assert.ts new file mode 100644 index 0000000000..8f7caef0fa --- /dev/null +++ b/plugins/cost-insights/src/utils/assert.ts @@ -0,0 +1,44 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export function notEmpty( + value: TValue | null | undefined, +): value is TValue { + return value !== null && value !== undefined; +} + +// Utility for exhaustiveness checking in switch statements +export function assertNever(x: never): never { + throw new Error(`Exhaustiveness check failed: ${x}`); +} + +export function assertAlways(argument: T | undefined): T { + if (argument === undefined) { + throw new TypeError( + 'Expected to always find a value but received undefined', + ); + } + return argument; +} + +// Utility for working with static lists; asserts a value will always be found or +// throws an error +export function findAlways( + collection: T[], + callback: (el: T) => boolean, +): T { + return assertAlways(collection.find(callback)); +} diff --git a/plugins/cost-insights/src/utils/currency.ts b/plugins/cost-insights/src/utils/currency.ts index c15af4d80d..439dbb70c3 100644 --- a/plugins/cost-insights/src/utils/currency.ts +++ b/plugins/cost-insights/src/utils/currency.ts @@ -13,7 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { assertNever, Currency, Duration } from '../types'; +import { Currency, Duration } from '../types'; +import { assertNever } from '../utils/assert'; export enum CurrencyType { USD = 'USD', diff --git a/plugins/cost-insights/src/utils/mockData.ts b/plugins/cost-insights/src/utils/mockData.ts index c070f07e29..9609da6ce8 100644 --- a/plugins/cost-insights/src/utils/mockData.ts +++ b/plugins/cost-insights/src/utils/mockData.ts @@ -17,7 +17,6 @@ import { Duration, Entity, - findAlways, Product, ProductCost, ProductFilters, @@ -29,6 +28,7 @@ import { DefaultLoadingAction, getDefaultState as getDefaultLoadingState, } from '../utils/loading'; +import { findAlways } from '../utils/assert'; import { Config } from '@backstage/config'; import { ConfigApi } from '@backstage/core';