Parse string unions as StringLiteralUnionTypeAnnotation (#46845)

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

Previously, the parser was throwing away the values of a string union when storing it in the schema. It would only store the union as

```
{
  type: 'UnionTypeAnnotation',
  memberType: 'StringTypeAnnotation'
}
```

We now track the string literals through the parser and store them in the schema:

```
{
  type: 'StringLiteralUnionTypeAnnotation',
  types: [
    {
      type: 'StringLiteralTypeAnnotation'
      value: 'light'
    },
    {
      type: 'StringLiteralTypeAnnotation'
      value: 'dark'
    },
  ],
}
```

We aren't changing the generators, those still just output "string". They could eventually be made smarter.

The value of this is that the compat checker can now error when the union changes.

Changelog: [Internal]

Reviewed By: yungsters

Differential Revision: D63917685

fbshipit-source-id: 34a10e1f1910d2935837a3659f66049fd4473134
This commit is contained in:
Eli White
2024-10-07 16:56:04 -07:00
committed by Facebook GitHub Bot
parent 1ffef5669c
commit a669de1dd2
26 changed files with 351 additions and 57 deletions
@@ -23,6 +23,7 @@ import type {
NativeModuleObjectTypeAnnotation,
StringTypeAnnotation,
StringLiteralTypeAnnotation,
StringLiteralUnionTypeAnnotation,
NativeModuleTypeAliasTypeAnnotation,
Nullable,
ReservedTypeAnnotation,
@@ -60,6 +61,7 @@ export type StructProperty = $ReadOnly<{
export type StructTypeAnnotation =
| StringTypeAnnotation
| StringLiteralTypeAnnotation
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| Int32TypeAnnotation
| DoubleTypeAnnotation
@@ -96,6 +96,8 @@ function toObjCType(
return 'NSString *';
case 'StringLiteralTypeAnnotation':
return 'NSString *';
case 'StringLiteralUnionTypeAnnotation':
return 'NSString *';
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'FloatTypeAnnotation':
@@ -177,6 +179,8 @@ function toObjCValue(
return value;
case 'StringLiteralTypeAnnotation':
return value;
case 'StringLiteralUnionTypeAnnotation':
return value;
case 'NumberTypeAnnotation':
return wrapPrimitive('double');
case 'FloatTypeAnnotation':
@@ -87,6 +87,8 @@ function toObjCType(
return 'NSString *';
case 'StringLiteralTypeAnnotation':
return 'NSString *';
case 'StringLiteralUnionTypeAnnotation':
return 'NSString *';
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'FloatTypeAnnotation':
@@ -167,6 +169,8 @@ function toObjCValue(
return RCTBridgingTo('String');
case 'StringLiteralTypeAnnotation':
return RCTBridgingTo('String');
case 'StringLiteralUnionTypeAnnotation':
return RCTBridgingTo('String');
case 'NumberTypeAnnotation':
return RCTBridgingTo('Double');
case 'FloatTypeAnnotation':
@@ -22,6 +22,8 @@ function getEventEmitterTypeObjCType(
return 'NSString *_Nonnull';
case 'StringLiteralTypeAnnotation':
return 'NSString *_Nonnull';
case 'StringLiteralUnionTypeAnnotation':
return 'NSString *_Nonnull';
case 'NumberTypeAnnotation':
return 'NSNumber *_Nonnull';
case 'BooleanTypeAnnotation':
@@ -259,6 +259,8 @@ function getParamObjCType(
return notStruct(wrapOptional('NSString *', !nullable));
case 'StringLiteralTypeAnnotation':
return notStruct(wrapOptional('NSString *', !nullable));
case 'StringLiteralUnionTypeAnnotation':
return notStruct(wrapOptional('NSString *', !nullable));
case 'NumberTypeAnnotation':
return notStruct(isRequired ? 'double' : 'NSNumber *');
case 'FloatTypeAnnotation':
@@ -336,6 +338,10 @@ function getReturnObjCType(
// TODO: Can NSString * returns not be _Nullable?
// In the legacy codegen, we don't surround NSSTring * with _Nullable
return wrapOptional('NSString *', isRequired);
case 'StringLiteralUnionTypeAnnotation':
// TODO: Can NSString * returns not be _Nullable?
// In the legacy codegen, we don't surround NSSTring * with _Nullable
return wrapOptional('NSString *', isRequired);
case 'NumberTypeAnnotation':
return wrapOptional('NSNumber *', isRequired);
case 'FloatTypeAnnotation':
@@ -404,6 +410,8 @@ function getReturnJSType(
return 'StringKind';
case 'StringLiteralTypeAnnotation':
return 'StringKind';
case 'StringLiteralUnionTypeAnnotation':
return 'StringKind';
case 'NumberTypeAnnotation':
return 'NumberKind';
case 'FloatTypeAnnotation':