From 4cb37f36b3ff78cdac1f83f519373cf35fb0d022 Mon Sep 17 00:00:00 2001 From: Pascal Corpet Date: Wed, 28 Jun 2023 08:26:48 +0200 Subject: [PATCH] fix(bar): use readonly arrays for props as the library does not modify them --- packages/annotations/src/compute.ts | 4 ++-- packages/annotations/src/hooks.ts | 6 +++--- packages/bar/src/Bar.tsx | 2 +- packages/bar/src/compute/common.ts | 4 ++-- packages/bar/src/compute/grouped.ts | 4 ++-- packages/bar/src/compute/stacked.ts | 2 +- packages/bar/src/types.ts | 22 +++++++++++----------- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/annotations/src/compute.ts b/packages/annotations/src/compute.ts index 3feaa44ace..c359ac0b52 100644 --- a/packages/annotations/src/compute.ts +++ b/packages/annotations/src/compute.ts @@ -28,8 +28,8 @@ export const bindAnnotations = < getPosition, getDimensions, }: { - data: Datum[] - annotations: AnnotationMatcher[] + data: readonly Datum[] + annotations: readonly AnnotationMatcher[] getPosition: AnnotationPositionGetter getDimensions: AnnotationDimensionsGetter }): BoundAnnotation[] => diff --git a/packages/annotations/src/hooks.ts b/packages/annotations/src/hooks.ts index 7f6e447622..c870de5533 100644 --- a/packages/annotations/src/hooks.ts +++ b/packages/annotations/src/hooks.ts @@ -16,8 +16,8 @@ export const useAnnotations = ({ getPosition, getDimensions, }: { - data: Datum[] - annotations: AnnotationMatcher[] + data: readonly Datum[] + annotations: readonly AnnotationMatcher[] getPosition: AnnotationPositionGetter getDimensions: AnnotationDimensionsGetter }) => @@ -35,7 +35,7 @@ export const useAnnotations = ({ export const useComputedAnnotations = ({ annotations, }: { - annotations: BoundAnnotation[] + annotations: readonly BoundAnnotation[] }) => useMemo( () => diff --git a/packages/bar/src/Bar.tsx b/packages/bar/src/Bar.tsx index 80e8aad802..cca9ce7fb3 100644 --- a/packages/bar/src/Bar.tsx +++ b/packages/bar/src/Bar.tsx @@ -58,7 +58,7 @@ const InnerBar = ({ gridXValues, gridYValues, - layers = svgDefaultProps.layers as BarLayer[], + layers = svgDefaultProps.layers as readonly BarLayer[], barComponent = svgDefaultProps.barComponent, enableLabel = svgDefaultProps.enableLabel, diff --git a/packages/bar/src/compute/common.ts b/packages/bar/src/compute/common.ts index 406ee57ccb..058ccc3095 100644 --- a/packages/bar/src/compute/common.ts +++ b/packages/bar/src/compute/common.ts @@ -4,7 +4,7 @@ import { ScaleBandSpec, ScaleBand, computeScale } from '@nivo/scales' * Generates indexed scale. */ export const getIndexScale = ( - data: RawDatum[], + data: readonly RawDatum[], getIndex: (datum: RawDatum) => string, padding: number, indexScale: ScaleBandSpec, @@ -24,7 +24,7 @@ export const getIndexScale = ( /** * This method ensures all the provided keys exist in the entire series. */ -export const normalizeData = (data: RawDatum[], keys: string[]) => +export const normalizeData = (data: readonly RawDatum[], keys: readonly string[]) => data.map( item => ({ diff --git a/packages/bar/src/compute/grouped.ts b/packages/bar/src/compute/grouped.ts index 8a56ff4916..9d842d84e6 100644 --- a/packages/bar/src/compute/grouped.ts +++ b/packages/bar/src/compute/grouped.ts @@ -5,7 +5,7 @@ import { BarDatum, BarSvgProps, ComputedBarDatum, ComputedDatum } from '../types import { coerceValue, filterNullValues, getIndexScale, normalizeData } from './common' type Params = { - data: RawDatum[] + data: readonly RawDatum[] formatValue: (value: number) => string getColor: OrdinalColorScale> getIndex: (datum: RawDatum) => string @@ -186,7 +186,7 @@ export const generateGroupedBars = ({ getIndex: (datum: RawDatum) => string getTooltipLabel: (datum: ComputedDatum) => string margin: Margin - hiddenIds?: (string | number)[] + hiddenIds?: readonly (string | number)[] }) => { const keys = props.keys.filter(key => !hiddenIds.includes(key)) const data = normalizeData(props.data, keys) diff --git a/packages/bar/src/compute/stacked.ts b/packages/bar/src/compute/stacked.ts index 14a9778e83..50ba1f535f 100644 --- a/packages/bar/src/compute/stacked.ts +++ b/packages/bar/src/compute/stacked.ts @@ -179,7 +179,7 @@ export const generateStackedBars = ({ getIndex: (datum: RawDatum) => string getTooltipLabel: (datum: ComputedDatum) => string margin: Margin - hiddenIds?: (string | number)[] + hiddenIds?: readonly (string | number)[] }) => { const keys = props.keys.filter(key => !hiddenIds.includes(key)) const stackedData = stack().keys(keys).offset(stackOffsetDiverging)( diff --git a/packages/bar/src/types.ts b/packages/bar/src/types.ts index 8644bff6c8..24e8ea52ea 100644 --- a/packages/bar/src/types.ts +++ b/packages/bar/src/types.ts @@ -22,7 +22,7 @@ export interface BarDatum { } export interface DataProps { - data: RawDatum[] + data: readonly RawDatum[] } export type BarDatumWithColor = BarDatum & { @@ -109,8 +109,8 @@ interface BarCustomLayerBaseProps | 'tooltip' >, Dimensions { - bars: ComputedBarDatum[] - legendData: [BarLegendProps, LegendData[]][] + bars: readonly ComputedBarDatum[] + legendData: [BarLegendProps, readonly LegendData[]][] margin: Margin innerWidth: number @@ -208,7 +208,7 @@ export type BarHandlers = { export type BarCommonProps = { indexBy: PropertyAccessor - keys: string[] + keys: readonly string[] maxValue: 'auto' | number minValue: 'auto' | number @@ -253,12 +253,12 @@ export type BarCommonProps = { colors: OrdinalColorScaleConfig> theme: Theme - annotations: AnnotationMatcher>[] - legends: BarLegendProps[] + annotations: readonly AnnotationMatcher>[] + legends: readonly BarLegendProps[] renderWrapper?: boolean - initialHiddenIds: (string | number)[] + initialHiddenIds: readonly (string | number)[] } export type BarSvgProps = Partial> & @@ -275,9 +275,9 @@ export type BarSvgProps = Partial> - markers: CartesianMarkerProps[] + markers: readonly CartesianMarkerProps[] - layers: BarLayer[] + layers: readonly BarLayer[] role: string ariaLabel?: React.AriaAttributes['aria-label'] @@ -310,6 +310,6 @@ export type BarCanvasProps = Partial export type BarAnnotationsProps = { - annotations: AnnotationMatcher>[] - bars: ComputedBarDatum[] + annotations: readonly AnnotationMatcher>[] + bars: readonly ComputedBarDatum[] }