Fix hash generation for declare const types in API snapshot #54818 (#54822)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/54822

Updates hash generation in `versionExportedApis.js` to track changes to `declare const` type declarations like `AccessibilityInfo_default`.

Previously, changes to APIs (e.g., `AccessibilityInfo`) would not always update their hashes in `ReactNativeApi.d.ts`, causing the breaking change detection to miss actual API changes.

Three issues prevented proper hash tracking:

1. **`VariableDeclaration` types weren't tracked** - `declare const X: {...}` was not in the list of tracked declaration types
2. **Duplicate declarations caused overwrites** - `declare type X = typeof X` would overwrite `declare const X: typeof X_default` in the declarations map, losing the dependency on `X_default`
3. **`typeof X` queries weren't extracting dependencies** - references via `typeof` weren't being added to the dependency graph

## Fix

- Add `t.isVariableDeclaration(node)` to tracked types with proper name extraction
- Prevent overwriting existing declarations (first declaration wins)
- Handle `TSTypeQuery` nodes to extract `typeof X` dependencies

### Note on Hash Changes

This PR causes many hashes to change, even for types that haven't been modified. This is expected because the hash computation now includes `declare const` dependencies that were previously ignored.

**Before:** `AccessibilityInfo` hash only included `declare type AccessibilityInfo = typeof AccessibilityInfo` (self-reference)
**After:** Hash now includes `declare const AccessibilityInfo: typeof AccessibilityInfo_default` + the full `AccessibilityInfo_default` type

## Changelog:
[GENERAL] [FIXED] - hash generation includes `declare const` types in API snapshot

Reviewed By: huntie

Differential Revision: D88653322

fbshipit-source-id: abdf9c5e11bf8ff9e6e2f17a743a2d5aa213ae1c
This commit is contained in:
Emily Brown authored and meta-codesync[bot] committed 2025-12-10 03:10:32 -08:00
1 parent 753c1e1231
commit c36665f7c6
3 files changed
+120 -81

No files matched your search

+74 -74
View File
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<23a3208168c8d68b6cfefa829d849408>>
* @generated SignedSource<<bffba0612c645ddb6166e1f5bb1187f2>>
*
* This file was generated by scripts/js-api/build-types/index.js.
*/
@@ -5954,14 +5954,14 @@ declare type WrapperComponentProvider = (
) => React.ComponentType<any>
export {
AccessibilityActionEvent, // f6181a2c
AccessibilityInfo, // 70604904
AccessibilityInfo, // ce4850e1
AccessibilityProps, // 5a2836fc
AccessibilityRole, // f2f2e066
AccessibilityState, // b0c2b3f7
AccessibilityValue, // cf8bcb74
ActionSheetIOS, // 88e6bfb0
ActionSheetIOS, // b558559e
ActionSheetIOSOptions, // 1756eb5a
ActivityIndicator, // 8d041a45
ActivityIndicator, // 0fbc1b53
ActivityIndicatorProps, // 64ab03e1
Alert, // 5bf12165
AlertButton, // bf1a3b60
@@ -5969,44 +5969,44 @@ export {
AlertOptions, // a0cdac0f
AlertType, // 5ab91217
AndroidKeyboardEvent, // e03becc8
Animated, // 9c6edf39
Animated, // 47139b9b
AppConfig, // ebddad4b
AppRegistry, // 6cdee1d6
AppState, // f7097b1b
AppState, // 12012be5
AppStateEvent, // 80f034c3
AppStateStatus, // 447e5ef2
Appearance, // 00cbaa0a
AutoCapitalize, // c0e857a0
BackHandler, // a9f9bad9
BackHandler, // 0b13c289
BackPressEventName, // 4620fb76
BlurEvent, // 870b9bb5
BoxShadowValue, // b679703f
Button, // dd130b61
Button, // ef82aed4
ButtonProps, // 3c081e75
Clipboard, // 9b8c878e
Clipboard, // 41addb89
CodegenTypes, // 030a94b8
ColorSchemeName, // 31a4350e
ColorValue, // 98989a8f
ComponentProvider, // b5c60ddd
ComponentProviderInstrumentationHook, // 9f640048
CursorValue, // 26522595
DevMenu, // 99e9fcd6
DevSettings, // 1a2f3a5f
DeviceEventEmitter, // 31dc96e7
DeviceInfo, // cc37eaec
DevMenu, // 21b8b7a9
DevSettings, // e55b91dc
DeviceEventEmitter, // 650e6394
DeviceInfo, // 521bfb71
DeviceInfoConstants, // 279e7858
DimensionValue, // b163a381
Dimensions, // 980ef68c
DimensionsPayload, // 653bc26c
DisplayMetrics, // 1dc35cef
DisplayMetricsAndroid, // 872e62eb
DrawerLayoutAndroid, // 14121b61
DrawerLayoutAndroid, // 5659aca9
DrawerLayoutAndroidProps, // 66371296
DrawerSlideEvent, // cc43db83
DropShadowValue, // e9df2606
DynamicColorIOS, // 1f9b3410
DynamicColorIOS, // d96c228c
DynamicColorIOSTuple, // 023ce58e
Easing, // b624f91d
Easing, // 673080ba
EasingFunction, // 14aee4c0
EdgeInsetsValue, // bd44afe6
EmitterSubscription, // de50c359
@@ -6015,8 +6015,8 @@ export {
EventSubscription, // b8d084aa
ExtendedExceptionData, // 5a6ccf5a
FilterFunction, // bf24c0e3
FlatList, // 940a1186
FlatListProps, // 6fc8f490
FlatList, // a6600e49
FlatListProps, // de2108a1
FocusEvent, // 529b43eb
FontVariant, // 7c7558bb
GestureResponderEvent, // b466f6d6
@@ -6024,10 +6024,10 @@ export {
Handle, // 2d65285d
HostComponent, // 5e13ff5a
HostInstance, // 489cbe7f
I18nManager, // f2fa58ce
I18nManager, // f9870e00
IOSKeyboardEvent, // e67bfe3a
IgnorePattern, // ec6f6ece
Image, // 04474205
Image, // 957b3726
ImageBackground, // 06d5af5c
ImageBackgroundProps, // 473a9eb4
ImageErrorEvent, // b7b2ae63
@@ -6044,23 +6044,23 @@ export {
ImageSourcePropType, // bfb5e5c6
ImageStyle, // 8b22ac76
ImageURISource, // 016eb083
InputAccessoryView, // 591855d8
InputAccessoryView, // ddb7f881
InputAccessoryViewProps, // 4b6f5450
InputModeOptions, // 4e8581b9
Insets, // e7fe432a
InteractionManager, // 301bfa63
InteractionManager, // c324d6e3
KeyDownEvent, // 5309360e
KeyEvent, // 20fa4267
KeyUpEvent, // 7c3054e1
Keyboard, // 87311c77
KeyboardAvoidingView, // 4a933094
Keyboard, // 49414c97
KeyboardAvoidingView, // 23d0b271
KeyboardAvoidingViewProps, // 81cba23f
KeyboardEvent, // c3f895d4
KeyboardEventEasing, // af4091c8
KeyboardEventName, // 59299ad6
KeyboardMetrics, // 7997b799
KeyboardTypeOptions, // ae44efe7
LayoutAnimation, // 795a5df3
LayoutAnimation, // 54110cf7
LayoutAnimationAnim, // 074da826
LayoutAnimationConfig, // 5e200571
LayoutAnimationProperties, // 84e6e197
@@ -6070,48 +6070,48 @@ export {
LayoutChangeEvent, // c674f902
LayoutConformanceProps, // 055f03b8
LayoutRectangle, // 6601b294
Linking, // 292de0a0
Linking, // b5645d2b
ListRenderItem, // b5353fd8
ListRenderItemInfo, // e8595b03
ListViewToken, // 833d3481
LogBox, // b58880c6
LogBox, // 04665d29
LogData, // 89af6d4c
MeasureInWindowOnSuccessCallback, // a285f598
MeasureLayoutOnSuccessCallback, // 3592502a
MeasureOnSuccessCallback, // 82824e59
Modal, // 78e8a79d
Modal, // 9f28d6fe
ModalBaseProps, // 0c81c9b1
ModalProps, // a69426b0
ModalPropsAndroid, // 515fb173
ModalPropsIOS, // 4fbcedf6
ModeChangeEvent, // af2c4926
ModeChangeEvent, // 16790307
MouseEvent, // 53ede3db
NativeAppEventEmitter, // b4d20c1d
NativeAppEventEmitter, // e1340a0a
NativeColorValue, // d2094c29
NativeComponentRegistry, // 7fd99ba6
NativeDialogManagerAndroid, // 6254873e
NativeDialogManagerAndroid, // 5be8497e
NativeEventEmitter, // d72906cc
NativeEventSubscription, // de3942e7
NativeMethods, // 03dc51c5
NativeMethodsMixin, // 4b061b7e
NativeModules, // 1cf72876
NativeModules, // 4597cd36
NativeMouseEvent, // ff25cf35
NativePointerEvent, // 89c1f3ad
NativeScrollEvent, // caad7f53
NativeSyntheticEvent, // d2a1fe6a
NativeTouchEvent, // 59b676df
NativeUIEvent, // 44ac26ac
Networking, // b674447b
Networking, // bbc5be42
OpaqueColorValue, // 25f3fa5b
PanResponder, // 98a9b6fc
PanResponder, // d803bfcf
PanResponderCallbacks, // d325aa56
PanResponderGestureState, // 54baf558
PanResponderInstance, // c8b0d00c
PanResponderInstance, // 84d7fd52
Permission, // 06473f4f
PermissionStatus, // 4b7de97b
PermissionsAndroid, // 0c1619e7
PermissionsAndroid, // db2a401e
PixelRatio, // 10d9e32d
Platform, // dc0f5740
Platform, // b73caa89
PlatformColor, // 8297ec62
PlatformOSType, // 0a17561e
PlatformSelectSpec, // 09ed7758
@@ -6119,16 +6119,16 @@ export {
PointerEvent, // ff3129ff
PressabilityConfig, // 9bb563c2
PressabilityEventHandlers, // ade29c37
Pressable, // 3c6e4eb9
Pressable, // a4242e8c
PressableAndroidRippleConfig, // 42bc9727
PressableProps, // 42ae2cc7
PressableStateCallbackType, // 9af36561
ProcessedColorValue, // 33f74304
ProgressBarAndroid, // 03e66cf5
ProgressBarAndroid, // f6740378
ProgressBarAndroidProps, // 021ef6d2
PromiseTask, // 5102c862
PublicRootInstance, // 8040afd7
PublicTextInstance, // 7d73f802
PublicTextInstance, // e775d6b1
PushNotificationEventName, // 84e7e150
PushNotificationIOS, // b4d1fe78
PushNotificationPermissions, // c2e7ae4f
@@ -6143,29 +6143,29 @@ export {
ReturnKeyTypeOptions, // afd47ba3
Role, // af7b889d
RootTag, // 3cd10504
RootTagContext, // 15b60335
RootTagContext, // 38bfc8f6
RootViewStyleProvider, // cc8d50e9
Runnable, // 2cb32c54
Runnables, // d3749ae1
SafeAreaView, // 4364c7bb
SafeAreaView, // 1b0e10d1
ScaledSize, // 07e417c7
ScrollEvent, // 84e5b805
ScrollResponderType, // d39056e7
ScrollResponderType, // 8eff013e
ScrollToLocationParamsType, // d7ecdad1
ScrollView, // 7fb7c469
ScrollViewImperativeMethods, // eb20aa46
ScrollViewProps, // d25b90bd
ScrollView, // dcb23e16
ScrollViewImperativeMethods, // 92677f19
ScrollViewProps, // 68051136
ScrollViewPropsAndroid, // 84e2134b
ScrollViewPropsIOS, // d83c9733
ScrollViewScrollToOptions, // 3313411e
SectionBase, // b376bddc
SectionList, // 19c9aaa2
SectionList, // ab067389
SectionListData, // 119baf83
SectionListProps, // 52787c42
SectionListProps, // e0865379
SectionListRenderItem, // 1fad0435
SectionListRenderItemInfo, // 745e1992
Separators, // 6a45f7e3
Settings, // 4282b0da
Settings, // 2be0c61e
Share, // e4591b32
ShareAction, // ead1004a
ShareActionSheetError, // 55e4f451
@@ -6178,18 +6178,18 @@ export {
StatusBarProps, // 06c98add
StatusBarStyle, // 986b2051
StyleProp, // fa0e9b4a
StyleSheet, // 366689d4
StyleSheet, // 17b08ddb
SubmitBehavior, // c4ddf490
Switch, // aebc9941
Switch, // cd54192c
SwitchChangeEvent, // 2e5bd2de
SwitchProps, // 5b2f78be
Systrace, // b5aa21fc
TVViewPropsIOS, // 330ce7b5
TargetedEvent, // 16e98910
TaskProvider, // 266dedf2
Text, // bd525aa9
Text, // e55ac2e2
TextContentType, // 239b3ecc
TextInput, // 282b394e
TextInput, // cf7a3331
TextInputAndroidProps, // 3f09ce49
TextInputChangeEvent, // 380cbe93
TextInputContentSizeChangeEvent, // 5fba3f54
@@ -6203,39 +6203,39 @@ export {
TextLayoutEvent, // 45b0a8d7
TextProps, // 95d8874d
TextStyle, // f3404e2b
ToastAndroid, // b4875e35
Touchable, // 93eb6c63
TouchableHighlight, // b4304a98
TouchableHighlightProps, // c871f353
ToastAndroid, // 88a8969a
Touchable, // b89b4800
TouchableHighlight, // 254e7a9d
TouchableHighlightProps, // 4af01d74
TouchableNativeFeedback, // 60a3e196
TouchableNativeFeedbackProps, // 372d3213
TouchableOpacity, // 7e33acfd
TouchableOpacityProps, // 73c84562
TouchableOpacity, // bd92a35c
TouchableOpacityProps, // 822164b4
TouchableWithoutFeedback, // 7363a906
TouchableWithoutFeedbackProps, // 68e3d87f
TransformsStyle, // 65e70f18
TurboModule, // dfe29706
TurboModuleRegistry, // 4ace6db2
UIManager, // 8d2c8281
UTFSequence, // baacd11b
Vibration, // 315e131d
View, // 39dd4de4
UIManager, // a1a7cc01
UTFSequence, // ad625158
Vibration, // 31e4bbf8
View, // 76fc9522
ViewProps, // 1945fbb5
ViewPropsAndroid, // 21385d96
ViewPropsIOS, // 58ee19bf
ViewStyle, // c2db0e6e
VirtualViewMode, // 85a69ef6
VirtualizedList, // 4d513939
VirtualizedListProps, // d3376e70
VirtualizedSectionList, // 446ba0df
VirtualizedSectionListProps, // 4f35f007
VirtualViewMode, // 6be59722
VirtualizedList, // 68c7345e
VirtualizedListProps, // de80b6d7
VirtualizedSectionList, // 9fd9cd61
VirtualizedSectionListProps, // bdbfdf88
WrapperComponentProvider, // 9cf3844c
codegenNativeCommands, // e16d62f7
codegenNativeComponent, // ed4c8103
findNodeHandle, // c6b08494
processColor, // 00453092
registerCallableModule, // 86b65226
requireNativeComponent, // e659aa32
codegenNativeCommands, // 628a7c0a
codegenNativeComponent, // a733b8b6
findNodeHandle, // 65981202
processColor, // 6e877698
registerCallableModule, // 839c8cfe
requireNativeComponent, // 72c09c3d
useAnimatedValue, // b18adb63
useColorScheme, // c216d6f7
usePressability, // fe1f27d8
@@ -726,4 +726,27 @@ describe('versionExportedApis', () => {
"
`);
});
test('should handle typeof with declare const and track dependencies', async () => {
const code = `
type AccessibilityInfo_default = {
isScreenReaderEnabled: () => Promise<boolean>;
};
declare const AccessibilityInfo: typeof AccessibilityInfo_default;
declare type AccessibilityInfo = typeof AccessibilityInfo;
export { AccessibilityInfo };
`;
const result = await translate(code);
expect(result).toMatchInlineSnapshot(`
"type AccessibilityInfo_default = {
isScreenReaderEnabled: () => Promise<boolean>;
};
declare const AccessibilityInfo: typeof AccessibilityInfo_default;
declare type AccessibilityInfo = typeof AccessibilityInfo;
export {
AccessibilityInfo, // 2b0c85e6, Deps: [AccessibilityInfo_default], Total: 1, Tree: AccessibilityInfo→[AccessibilityInfo_default]
};
"
`);
});
});
@@ -44,21 +44,29 @@ function createVersionExportedApis(
// Collect all type declarations and build dependency graph
for (const nodePath of path.get('body')) {
const node = nodePath.node;
const typeName = node.id?.name;
// VariableDeclaration has name at declarations[0].id.name, others at node.id.name
const typeName = t.isVariableDeclaration(node)
? node.declarations?.[0]?.id?.name
: node.id?.name;
if (
(t.isTSDeclareFunction(node) ||
t.isTSTypeAliasDeclaration(node) ||
t.isTSInterfaceDeclaration(node) ||
t.isTSEnumDeclaration(node) ||
t.isClassDeclaration(node) ||
t.isTSModuleDeclaration(node)) &&
t.isTSModuleDeclaration(node) ||
t.isVariableDeclaration(node)) &&
typeName != null
) {
declarations.set(typeName, node);
dependencyGraph.set(
typeName,
Array.from(getTypeReferencesForNode(node)),
);
// Don't overwrite if declaration already exists
// (e.g., prefer `declare const X: typeof X_default` over `declare type X = typeof X`)
if (!declarations.has(typeName)) {
declarations.set(typeName, node);
dependencyGraph.set(
typeName,
Array.from(getTypeReferencesForNode(node)),
);
}
}
}
@@ -339,6 +347,14 @@ function createVersionExportedApis(
getTypeReferencesForNode(node.typeAnnotation, refs);
}
// Handle typeof queries (`typeof X`)
if (t.isTSTypeQuery(node) && node.exprName) {
const exprName = node.exprName;
if (t.isIdentifier(exprName) || t.isTSQualifiedName(exprName)) {
refs.add(extractQualifiedName(exprName));
}
}
// Handle conditional types (`T extends U ? X : Y`)
if (t.isTSConditionalType(node)) {
getTypeReferencesForNode(node.checkType, refs);