Skip to content

Commit

Permalink
fix: Correct types for transformsArrayToProps (#2193)
Browse files Browse the repository at this point in the history
# Summary

Type TransformsStyle['transform'] has `string` added as option from
React Native 0.71.9 onward. This breaks the logic of
`transformsArrayToProps`.

This PR fixes this by scoping down the type, and removing a no-op usage
of `transformsArrayToProps`

This has been broken for a while, but not immediately visible to users
compiling using Metro and Babel.
  • Loading branch information
crazyfraggle committed Jan 3, 2024
1 parent 068820b commit 3939e20
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/lib/extract/extractTransform.ts
Expand Up @@ -8,6 +8,8 @@ import type {
TransformProps,
} from './types';

type TransformsStyleArray = Exclude<TransformsStyle['transform'], string>;

function appendTransformProps(props: TransformedProps) {
const { x, y, originX, originY, scaleX, scaleY, rotation, skewX, skewY } =
props;
Expand All @@ -20,15 +22,15 @@ function appendTransformProps(props: TransformedProps) {
skewX,
skewY,
originX,
originY,
originY
);
}

function universal2axis(
universal: NumberProp | NumberProp[] | undefined,
axisX: NumberProp | void,
axisY: NumberProp | void,
defaultValue?: number,
defaultValue?: number
): [number, number] {
let x;
let y;
Expand Down Expand Up @@ -65,14 +67,14 @@ function universal2axis(
}

export function transformsArrayToProps(
transformObjectsArray: TransformsStyle['transform'],
transformObjectsArray: TransformsStyleArray
) {
const props: TransformProps = {};
transformObjectsArray?.forEach((transformObject) => {
const keys = Object.keys(transformObject);
if (keys.length !== 1) {
console.error(
'You must specify exactly one property per transform object.',
'You must specify exactly one property per transform object.'
);
}
const key = keys[0] as keyof TransformProps;
Expand All @@ -83,14 +85,11 @@ export function transformsArrayToProps(
}

export function props2transform(
props: TransformProps | undefined,
props: TransformProps | undefined
): TransformedProps | null {
if (!props) {
return null;
}
const extractedProps = Array.isArray(props)
? transformsArrayToProps(props)
: props;
const {
rotation,
translate,
Expand All @@ -107,7 +106,7 @@ export function props2transform(
skewY,
x,
y,
} = extractedProps;
} = props;
if (
rotation == null &&
translate == null &&
Expand All @@ -130,13 +129,13 @@ export function props2transform(

if (Array.isArray(x) || Array.isArray(y)) {
console.warn(
'Passing SvgLengthList to x or y attribute where SvgLength expected',
'Passing SvgLengthList to x or y attribute where SvgLength expected'
);
}
const tr = universal2axis(
translate,
translateX || (Array.isArray(x) ? x[0] : x),
translateY || (Array.isArray(y) ? y[0] : y),
translateY || (Array.isArray(y) ? y[0] : y)
);
const or = universal2axis(origin, originX, originY);
const sc = universal2axis(scale, scaleX, scaleY, 1);
Expand All @@ -157,7 +156,7 @@ export function props2transform(

export function transformToMatrix(
props: TransformedProps | null,
transform: TransformProps['transform'],
transform: TransformProps['transform']
): ColumnMajorTransformMatrix | null {
if (!props && !transform) {
return null;
Expand All @@ -175,11 +174,11 @@ export function transformToMatrix(
columnMatrix[2],
columnMatrix[3],
columnMatrix[4],
columnMatrix[5],
columnMatrix[5]
);
} else {
const transformProps = props2transform(
transformsArrayToProps(transform as TransformsStyle['transform']),
transformsArrayToProps(transform as TransformsStyleArray)
);
transformProps && appendTransformProps(transformProps);
}
Expand All @@ -200,7 +199,7 @@ export function transformToMatrix(
}

export default function extractTransform(
props: TransformProps | TransformProps['transform'],
props: TransformProps | TransformProps['transform']
): ColumnMajorTransformMatrix | null {
if (Array.isArray(props) && typeof props[0] === 'number') {
return props as ColumnMajorTransformMatrix;
Expand All @@ -219,6 +218,6 @@ export default function extractTransform(
const transformProps = props as TransformProps;
return transformToMatrix(
props2transform(transformProps),
transformProps?.transform,
transformProps?.transform
);
}

0 comments on commit 3939e20

Please sign in to comment.