Add support for categories (#55773)

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

This diff adds support for parsing Objective-C categories in the C++ API snapshot parser.

Changelog:
[Internal]

Reviewed By: cipolleschi

Differential Revision: D94076350

fbshipit-source-id: 7dd144a09b8cd5e2b91d008cb2a5882226dfd293
This commit is contained in:
Dawid Małecki authored and meta-codesync[bot] committed 2026-03-04 02:47:32 -08:00
1 parent 851e5ae546
commit c90843d8f8
12 files changed
+189 -2

No files matched your search

+37 -1
View File
@@ -26,7 +26,12 @@ from .member import (
TypedefMember,
VariableMember,
)
from .scope import InterfaceScopeKind, ProtocolScopeKind, StructLikeScopeKind
from .scope import (
CategoryScopeKind,
InterfaceScopeKind,
ProtocolScopeKind,
StructLikeScopeKind,
)
from .snapshot import Snapshot
from .template import Template
from .utils import (
@@ -559,3 +564,34 @@ def create_class_scope(
print(
f"Unknown class visibility: {visibility} in {compound_object.location.file}"
)
def create_category_scope(
snapshot: Snapshot, scope_def: compound.CompounddefType
) -> None:
"""
Create a category scope in the snapshot (Objective-C category).
Categories extend existing classes with additional methods.
The compound name is in the format: ClassName(CategoryName)
"""
compound_name = scope_def.compoundname
# Parse ClassName(CategoryName) format
match = re.match(r"^(.+)\((.+)\)$", compound_name)
if not match:
print(f"Invalid category name format: {compound_name}")
return
class_name = match.group(1)
category_name = match.group(2)
category_scope = snapshot.create_category(class_name, category_name)
category_scope.location = scope_def.location.file
_process_objc_sections(
snapshot,
category_scope,
scope_def.sectiondef,
scope_def.location.file,
"category",
)
+2 -1
View File
@@ -14,6 +14,7 @@ import os
from doxmlparser import compound, index
from .builders import (
create_category_scope,
create_class_scope,
create_enum_scope,
create_interface_scope,
@@ -121,7 +122,7 @@ def build_snapshot(xml_dir: str) -> Snapshot:
elif compound_object.kind == "dir":
pass
elif compound_object.kind == "category":
print(f"Category not supported: {compound_object.compoundname}")
create_category_scope(snapshot, compound_object)
elif compound_object.kind == "page":
# Contains deprecation info
pass
+22
View File
@@ -253,6 +253,28 @@ class InterfaceScopeKind(ScopeKind):
return result
class CategoryScopeKind(ScopeKind):
def __init__(self, class_name: str, category_name: str) -> None:
super().__init__("category")
self.class_name: str = class_name
self.category_name: str = category_name
def to_string(self, scope: Scope) -> str:
result = f"{self.name} {self.class_name}({self.category_name}) {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
class TemporaryScopeKind(ScopeKind):
def __init__(self) -> None:
super().__init__("temporary")
+26
View File
@@ -6,6 +6,7 @@
from __future__ import annotations
from .scope import (
CategoryScopeKind,
EnumScopeKind,
InterfaceScopeKind,
NamespaceScopeKind,
@@ -134,6 +135,31 @@ class Snapshot:
current_scope.inner_scopes[scope_name] = new_scope
return new_scope
def create_category(
self, class_name: str, category_name: str
) -> Scope[CategoryScopeKind]:
"""
Create a category in the snapshot.
Categories are stored with a unique key: "ClassName(CategoryName)"
"""
scope_key = f"{class_name}({category_name})"
current_scope = self.root_scope
if scope_key in current_scope.inner_scopes:
scope = current_scope.inner_scopes[scope_key]
if scope.kind.name == "temporary":
scope.kind = CategoryScopeKind(class_name, category_name)
else:
raise RuntimeError(
f"Identifier {scope_key} already exists in scope {current_scope.name}"
)
return scope
else:
new_scope = Scope(CategoryScopeKind(class_name, category_name), scope_key)
new_scope.parent_scope = current_scope
current_scope.inner_scopes[scope_key] = new_scope
return new_scope
def create_enum(self, qualified_name: str) -> Scope[EnumScopeKind]:
"""
Create an enum in the snapshot.
@@ -0,0 +1,5 @@
category RCTConvert(EmptyCategory) {
}
interface RCTConvert : public NSObject {
}
@@ -0,0 +1,14 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@interface RCTConvert : NSObject
@end
@interface RCTConvert (EmptyCategory)
@end
@@ -0,0 +1,7 @@
category RCTConvert(UIActivityIndicatorView) {
public virtual static UIActivityIndicatorViewStyle UIActivityIndicatorViewStyle:(id json);
}
interface RCTConvert : public NSObject {
public virtual static UIActivityIndicatorViewStyle UIActivityIndicatorViewStyle:(id json);
}
@@ -0,0 +1,16 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@interface RCTConvert : NSObject
@end
@interface RCTConvert (UIActivityIndicatorView)
+ (UIActivityIndicatorViewStyle)UIActivityIndicatorViewStyle:(id)json;
@end
@@ -0,0 +1,9 @@
category RCTConvert(Transform) {
public virtual static CATransform3D CATransform3D:(id json);
public virtual static RCTTransformOrigin RCTTransformOrigin:(id json);
}
interface RCTConvert : public NSObject {
public virtual static CATransform3D CATransform3D:(id json);
public virtual static RCTTransformOrigin RCTTransformOrigin:(id json);
}
@@ -0,0 +1,17 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@interface RCTConvert : NSObject
@end
@interface RCTConvert (Transform)
+ (CATransform3D)CATransform3D:(id)json;
+ (RCTTransformOrigin)RCTTransformOrigin:(id)json;
@end
@@ -0,0 +1,12 @@
category RCTConvert(CategoryOne) {
public virtual static NSString * methodOne:(id json);
}
category RCTConvert(CategoryTwo) {
public virtual static NSString * methodTwo:(id json);
}
interface RCTConvert : public NSObject {
public virtual static NSString * methodOne:(id json);
public virtual static NSString * methodTwo:(id json);
}
@@ -0,0 +1,22 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@interface RCTConvert : NSObject
@end
@interface RCTConvert (CategoryOne)
+ (NSString *)methodOne:(id)json;
@end
@interface RCTConvert (CategoryTwo)
+ (NSString *)methodTwo:(id)json;
@end