From 78c8842f9e85387bd3d87f8d96faab35913820d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Thu, 5 Mar 2026 07:21:42 -0800 Subject: [PATCH] Normalize Objective-C nullability annotations (#55898) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55898 Normalize Objective-C nullability annotations in the cxx-api parser to ensure consistent output in API snapshots. Objective-C has three different forms of nullability annotations that are semantically equivalent: - `nonnull`/`nullable` (context-sensitive keywords) - `_Nonnull`/`_Nullable` (type qualifiers) - `__nonnull`/`__nullable` (legacy Apple macros) This diff normalizes all forms to `_Nonnull`/`_Nullable` for consistency. **Examples:** - `nonnull NSString*` → `_Nonnull NSString*` - `NSString* __nonnull` → `NSString* _Nonnull` - `nullable id` → `_Nullable id` - `id __nullable` → `id _Nullable` - `NSString* _Nonnull` → `NSString* _Nonnull` (unchanged) Added a test case to verify the normalization works correctly. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D95195669 fbshipit-source-id: ca2afd14b3c8bf7e62864daf9c3848f5d713898e --- .../cxx-api/parser/utils/text_resolution.py | 28 ++++++++++++++++++- .../should_normalize_nullability/snapshot.api | 10 +++++++ .../should_normalize_nullability/test.h | 21 ++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 scripts/cxx-api/tests/snapshots/should_normalize_nullability/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_normalize_nullability/test.h diff --git a/scripts/cxx-api/parser/utils/text_resolution.py b/scripts/cxx-api/parser/utils/text_resolution.py index 8d5e7c46ad2..e9b67f64203 100644 --- a/scripts/cxx-api/parser/utils/text_resolution.py +++ b/scripts/cxx-api/parser/utils/text_resolution.py @@ -90,6 +90,30 @@ def normalize_angle_brackets(text: str) -> str: return text +def normalize_nullability(text: str) -> str: + """Normalize Objective-C nullability annotations to a consistent form. + + There are three forms of nullability annotations in Objective-C: + - nonnull/nullable (context-sensitive keywords, typically used as prefix) + - _Nonnull/_Nullable (type qualifiers, can appear after the type) + - __nonnull/__nullable (legacy Apple macros, deprecated but still used) + + This function normalizes all forms to _Nonnull/_Nullable, which is the + most flexible form that can be used in any position. + """ + # Normalize __nonnull -> _Nonnull (must come before nonnull) + text = re.sub(r"\b__nonnull\b", "_Nonnull", text) + # Normalize nonnull -> _Nonnull (negative lookbehind to avoid _Nonnull) + text = re.sub(r"(? _Nullable (must come before nullable) + text = re.sub(r"\b__nullable\b", "_Nullable", text) + # Normalize nullable -> _Nullable (negative lookbehind to avoid _Nullable) + text = re.sub(r"(? str: """Normalize spacing around pointer (*) and reference (&, &&) symbols. @@ -179,7 +203,9 @@ def resolve_linked_text_name( name = name[1:-1].strip() return ( - normalize_pointer_spacing(normalize_angle_brackets(name.strip())), + normalize_nullability( + normalize_pointer_spacing(normalize_angle_brackets(name.strip())) + ), initialier_type, ) diff --git a/scripts/cxx-api/tests/snapshots/should_normalize_nullability/snapshot.api b/scripts/cxx-api/tests/snapshots/should_normalize_nullability/snapshot.api new file mode 100644 index 00000000000..dc16c53ac34 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_normalize_nullability/snapshot.api @@ -0,0 +1,10 @@ +interface NullabilityTest : public NSObject { + public @property (assign) NSString* nonnullProperty; + public @property (assign) NSString* nullableProperty; + public virtual NSString* _Nonnull legacyNonnullMethod(); + public virtual NSString* _Nonnull modernNonnullMethod(); + public virtual NSString* _Nullable legacyNullableMethod(); + public virtual NSString* _Nullable modernNullableMethod(); + public virtual _Nonnull NSString* nonnullMethod(); + public virtual _Nullable NSString* nullableMethod(); +} diff --git a/scripts/cxx-api/tests/snapshots/should_normalize_nullability/test.h b/scripts/cxx-api/tests/snapshots/should_normalize_nullability/test.h new file mode 100644 index 00000000000..b702b80276b --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_normalize_nullability/test.h @@ -0,0 +1,21 @@ +/* + * 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 NullabilityTest : NSObject + +// Different nullability annotation styles that should be normalized +- (nonnull NSString *)nonnullMethod; +- (nullable NSString *)nullableMethod; +- (NSString *__nonnull)legacyNonnullMethod; +- (NSString *__nullable)legacyNullableMethod; +- (NSString *_Nonnull)modernNonnullMethod; +- (NSString *_Nullable)modernNullableMethod; + +@property (nonatomic, nonnull) NSString *nonnullProperty; +@property (nonatomic, nullable) NSString *nullableProperty; + +@end