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

@@ -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);