Skip to content

Commit 771d52d

Browse files
rubennortefacebook-github-bot
authored andcommitted
Check Animated allowlist literals against supported key types
Summary: ## Context Annotating allowlist literals as arbitrary string dictionaries loses their exact keys and accepts spelling mistakes. These lists should be checked against the style, transform and interpolation type domains while preserving their inferred literal shapes. ## Changes - Check color, style, transform and interpolation allowlists with satisfies and finite key domains derived from their source types, using as const to retain true literal values in both OSS and internal Flow. - Collect transform keys distributively across the transform union, preserving support for matrix and the four legacy transform-style keys. - Keep existing feature-flag gating and the public allowlist export type unchanged. - Preserve runtime output exactly; the constraints validate list entries, not completeness or native-backend support. Flow configurations that check React Native implementation source need `experimental.tslib_syntax=true` for satisfies; React Native's validation configuration enables it. Changelog: [Internal] Differential Revision: D132934091
1 parent fb7549c commit 771d52d

1 file changed

Lines changed: 48 additions & 11 deletions

File tree

‎packages/react-native/Libraries/Animated/NativeAnimatedAllowlist.js‎

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,54 @@
88
* @format
99
*/
1010

11+
import type {
12+
____TransformStyle_Internal,
13+
ImageStyle,
14+
NativeColorValue,
15+
TextStyle,
16+
} from '../StyleSheet/StyleSheetTypes';
17+
import type {
18+
InterpolationConfigSupportedOutputType,
19+
InterpolationConfigType,
20+
} from './nodes/AnimatedInterpolation';
21+
import type {AnimatedNodeConfig} from './nodes/AnimatedNode';
1122
import type {AnimatedPropsAllowlist} from './nodes/AnimatedProps';
1223

1324
import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNativeFeatureFlags';
1425

26+
type KeyAllowlist<K extends string> = Readonly<Partial<Record<K, true>>>;
27+
type KeyOfUnion<T> = T extends unknown ? keyof T : empty;
28+
29+
type AnimatableStyle = Readonly<{...TextStyle, ...ImageStyle}>;
30+
type StyleKey = keyof AnimatableStyle;
31+
type ColorStyleKey = Values<{
32+
[K in StyleKey]: NativeColorValue extends NonNullable<AnimatableStyle[K]>
33+
? K
34+
: empty,
35+
}>;
36+
37+
type TransformEntry = Exclude<
38+
NonNullable<____TransformStyle_Internal['transform']>,
39+
string,
40+
>[number];
41+
type TransformKey = KeyOfUnion<NonNullable<TransformEntry>>;
42+
type LegacyTransformStyleKey = Extract<
43+
TransformKey,
44+
'scaleX' | 'scaleY' | 'translateX' | 'translateY',
45+
>;
46+
47+
type InterpolationParamKey = Exclude<
48+
keyof InterpolationConfigType<InterpolationConfigSupportedOutputType>,
49+
keyof AnimatedNodeConfig,
50+
>;
51+
1552
/**
1653
* Styles allowed by the native animated implementation.
1754
*
1855
* In general native animated implementation should support any numeric or color property that
1956
* doesn't need to be updated through the shadow view hierarchy (all non-layout properties).
2057
*/
21-
const SUPPORTED_COLOR_STYLES: {[key: string]: true} = {
58+
const SUPPORTED_COLOR_STYLES = {
2259
backgroundColor: true,
2360
borderBottomColor: true,
2461
borderColor: true,
@@ -29,9 +66,9 @@ const SUPPORTED_COLOR_STYLES: {[key: string]: true} = {
2966
borderTopColor: true,
3067
color: true,
3168
tintColor: true,
32-
};
69+
} as const satisfies KeyAllowlist<ColorStyleKey>;
3370

34-
const SUPPORTED_STYLES: {[key: string]: true} = {
71+
const SUPPORTED_STYLES = {
3572
...SUPPORTED_COLOR_STYLES,
3673
borderBottomEndRadius: true,
3774
borderBottomLeftRadius: true,
@@ -61,7 +98,7 @@ const SUPPORTED_STYLES: {[key: string]: true} = {
6198
translateY: true,
6299
// eslint-disable-next-line react-hooks/rules-of-hooks
63100
...(ReactNativeFeatureFlags.useSharedAnimatedBackend()
64-
? {
101+
? ({
65102
/* dimension */
66103
height: true,
67104
maxHeight: true,
@@ -114,11 +151,11 @@ const SUPPORTED_STYLES: {[key: string]: true} = {
114151
gap: true,
115152
rowGap: true,
116153
columnGap: true,
117-
}
154+
} as const)
118155
: {}),
119-
};
156+
} as const satisfies KeyAllowlist<StyleKey | LegacyTransformStyleKey>;
120157

121-
const SUPPORTED_TRANSFORMS: {[key: string]: true} = {
158+
const SUPPORTED_TRANSFORMS = {
122159
translateX: true,
123160
translateY: true,
124161
scale: true,
@@ -132,18 +169,18 @@ const SUPPORTED_TRANSFORMS: {[key: string]: true} = {
132169
skewX: true,
133170
skewY: true,
134171
...(ReactNativeFeatureFlags.shouldUseAnimatedObjectForTransform()
135-
? {matrix: true}
172+
? ({matrix: true} as const)
136173
: {}),
137-
};
174+
} as const satisfies KeyAllowlist<TransformKey>;
138175

139-
const SUPPORTED_INTERPOLATION_PARAMS: {[key: string]: true} = {
176+
const SUPPORTED_INTERPOLATION_PARAMS = {
140177
inputRange: true,
141178
outputRange: true,
142179
extrapolate: true,
143180
extrapolateRight: true,
144181
extrapolateLeft: true,
145182
easing: true,
146-
};
183+
} as const satisfies KeyAllowlist<InterpolationParamKey>;
147184

148185
/**
149186
* Default allowlist for component props that support native animated values.

0 commit comments

Comments
 (0)