mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Normalize Objective-C nullability annotations (#55898)
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
This commit is contained in:
1 parent
725b056dc7
commit
78c8842f9e
3 files changed
+58
-1
No files matched your search
@@ -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"(?<!_)\bnonnull\b", "_Nonnull", text)
|
||||
|
||||
# Normalize __nullable -> _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"(?<!_)\bnullable\b", "_Nullable", text)
|
||||
|
||||
return text
|
||||
|
||||
|
||||
def normalize_pointer_spacing(text: str) -> 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,
|
||||
)
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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
|
||||
Reference in new issue
Block a user