Repository navigation
Commit 845f593
authored
[Codegen] Parse Record<string, T> in TypeScript TurboModule specs as a dictionary
## Summary:
Fixes #59981.
`@react-native/codegen`'s TypeScript parser rejects `Record<string, T>` and `Readonly<Record<string, T>>` in a TurboModule spec with `Unrecognized generic type 'Record' in NativeModule spec.` It accepts `{[key: string]: T}`, which is the same type, and parses it as a dictionary of `T`. So specs that use the idiomatic TypeScript spelling fail at `pod install` / Gradle codegen time.
`translateTypeReferenceAnnotation` now handles `Record`. When the key type is `string`, it emits the same `GenericObjectTypeAnnotation` with a `dictionaryValueType` that an index signature gets. `Readonly<…>` is already unwrapped before this point. Any other `Record`, such as `Record<number, T>` or a wrong number of type arguments, fails with a new, specific error, `'Record' is only supported as Record<string, T>.`, instead of the misleading "Unrecognized generic type". The schema for `{[key: string]: T}` and `CodegenTypes.UnsafeObject` is unchanged.
## Changelog:
[GENERAL] [ADDED] - Codegen: parse `Record<string, T>` in TypeScript TurboModule specs as a dictionary
## Test Plan:
New tests:
- e2e tests in `typescript-module-parser-e2e-test.js`, run for each parameter variant (required, optional, nullable, nullable+optional) and for nullable and non-nullable returns:
- `Record<string, number>` parses to a dictionary of numbers.
- `Record<string, unknown>` gives the same schema as `{[key: string]: unknown}`.
- `Readonly<Record<string, Animal>>` gives a dictionary of the `Animal` alias.
- `Record<number, string>` and `Record<string>` throw `UnsupportedRecordTypeParserError`.
- Snapshot fixture `NATIVE_MODULE_WITH_RECORD` and failure fixture `NATIVE_MODULES_WITH_RECORD_WITH_NUMBER_KEY`. The new fixture is listed as a TS-only case in `checkModuleSnaps-test.js`.
Before the fix, the 14 positive e2e cases failed with `Unrecognized generic type 'Record'`. After it:
```
yarn test packages/react-native-codegen
Test Suites: 64 passed, 64 total
Tests: 3174 passed, 3174 total
Snapshots: 1281 passed, 1281 total
yarn flow-check # Found 0 errors
yarn eslint --max-warnings 0 <changed files> # clean
yarn prettier --check <changed files> # clean
```
With the issue's reproducer ([YevheniiKotyrlo/react-native-codegen-record-in-module-spec](https://github.com/YevheniiKotyrlo/react-native-codegen-record-in-module-spec) at `03048b2`), run with `bash` and the local Node instead of Docker. "Before" runs the scripts unchanged against `react-native@0.90.0-nightly-20261117-c1acb2bd6`. "After" runs the same scripts with `node_modules/@react-native/codegen` replaced by a symlink to this branch's built package:
| Script | Before | After |
| --- | --- | --- |
| `repro.sh` | exit 1: `Record<string, unknown>` and `Readonly<Record<string, unknown>>` → `UnsupportedGenericParserError: Unrecognized generic type 'Record'` | exit 0: both give `{"type":"GenericObjectTypeAnnotation","dictionaryValueType":{"type":"GenericObjectTypeAnnotation"}}`, the same as `{[key: string]: unknown}` |
| `repro-array-element.sh` (`ReadonlyArray<Rule>`, where `Rule` has a `Record<string, unknown>` member) | exit 1: the array element is silently `{"type":"AnyTypeAnnotation"}` | exit 0: element is `TypeAliasTypeAnnotation` `Rule`, and `Rule.matched` is a dictionary |
The `NATIVE_MODULE_WITH_RECORD` snapshot fixture includes the `ReadonlyArray<Rule>` case, so this silent fallback to `any` is covered too.
End to end, with the built codegen package, on a spec like the issue's reproducer (`setOptions(options: Record<string, unknown>): void`, `getOptions(): Readonly<Record<string, number>>`, `getNames(): Readonly<Record<string, string | null>>`):
- `node lib/cli/combine/combine-js-to-schema-cli.js schema.json specs/` (the script Android's `generateCodegenSchemaFromJavaScript` runs) now writes a schema with `GenericObjectTypeAnnotation` + `dictionaryValueType`, instead of failing.
- `RNCodegen.generate` with `modulesAndroid`, `modulesIOS` and `modulesCxx` on that schema produces `ReadableMap` / `WritableMap` in Java and `jsi::Object` in the C++ spec. These are the same signatures that `{[key: string]: T}` produces.
Not run: native builds of an app that uses such a spec. The generated code is identical to what an index-signature dictionary already produces.
Differential Revision: D133012784
Pull Request resolved: #600791 parent 7564317 commit 845f593
7 files changed
Lines changed: 276 additions & 0 deletions
File tree
- packages/react-native-codegen/src/parsers
- consistency/__tests__
- typescript/modules
- __test_fixtures__
- __tests__
- __snapshots__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
264 | 274 | | |
265 | 275 | | |
266 | 276 | | |
| |||
477 | 487 | | |
478 | 488 | | |
479 | 489 | | |
| 490 | + | |
480 | 491 | | |
481 | 492 | | |
482 | 493 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
310 | 331 | | |
311 | 332 | | |
312 | 333 | | |
| |||
321 | 342 | | |
322 | 343 | | |
323 | 344 | | |
| 345 | + | |
324 | 346 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1045 | 1045 | | |
1046 | 1046 | | |
1047 | 1047 | | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
1048 | 1079 | | |
1049 | 1080 | | |
| 1081 | + | |
1050 | 1082 | | |
1051 | 1083 | | |
1052 | 1084 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
3221 | 3223 | | |
3222 | 3224 | | |
3223 | 3225 | | |
| 3226 | + | |
| 3227 | + | |
| 3228 | + | |
| 3229 | + | |
| 3230 | + | |
| 3231 | + | |
| 3232 | + | |
| 3233 | + | |
| 3234 | + | |
| 3235 | + | |
| 3236 | + | |
| 3237 | + | |
| 3238 | + | |
| 3239 | + | |
| 3240 | + | |
| 3241 | + | |
| 3242 | + | |
| 3243 | + | |
| 3244 | + | |
| 3245 | + | |
| 3246 | + | |
| 3247 | + | |
| 3248 | + | |
| 3249 | + | |
| 3250 | + | |
| 3251 | + | |
| 3252 | + | |
| 3253 | + | |
| 3254 | + | |
| 3255 | + | |
| 3256 | + | |
| 3257 | + | |
| 3258 | + | |
| 3259 | + | |
| 3260 | + | |
| 3261 | + | |
| 3262 | + | |
| 3263 | + | |
| 3264 | + | |
| 3265 | + | |
| 3266 | + | |
| 3267 | + | |
| 3268 | + | |
| 3269 | + | |
| 3270 | + | |
| 3271 | + | |
| 3272 | + | |
| 3273 | + | |
| 3274 | + | |
| 3275 | + | |
| 3276 | + | |
| 3277 | + | |
| 3278 | + | |
| 3279 | + | |
| 3280 | + | |
| 3281 | + | |
| 3282 | + | |
| 3283 | + | |
| 3284 | + | |
| 3285 | + | |
| 3286 | + | |
| 3287 | + | |
| 3288 | + | |
| 3289 | + | |
| 3290 | + | |
| 3291 | + | |
| 3292 | + | |
| 3293 | + | |
| 3294 | + | |
| 3295 | + | |
| 3296 | + | |
| 3297 | + | |
| 3298 | + | |
| 3299 | + | |
| 3300 | + | |
| 3301 | + | |
| 3302 | + | |
| 3303 | + | |
| 3304 | + | |
| 3305 | + | |
| 3306 | + | |
| 3307 | + | |
| 3308 | + | |
| 3309 | + | |
| 3310 | + | |
| 3311 | + | |
| 3312 | + | |
| 3313 | + | |
| 3314 | + | |
| 3315 | + | |
| 3316 | + | |
| 3317 | + | |
| 3318 | + | |
| 3319 | + | |
| 3320 | + | |
| 3321 | + | |
| 3322 | + | |
| 3323 | + | |
| 3324 | + | |
| 3325 | + | |
| 3326 | + | |
| 3327 | + | |
| 3328 | + | |
| 3329 | + | |
| 3330 | + | |
| 3331 | + | |
| 3332 | + | |
| 3333 | + | |
| 3334 | + | |
| 3335 | + | |
| 3336 | + | |
| 3337 | + | |
| 3338 | + | |
| 3339 | + | |
| 3340 | + | |
| 3341 | + | |
| 3342 | + | |
3224 | 3343 | | |
3225 | 3344 | | |
3226 | 3345 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
268 | 269 | | |
269 | 270 | | |
270 | 271 | | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
271 | 326 | | |
272 | 327 | | |
273 | 328 | | |
| |||
979 | 1034 | | |
980 | 1035 | | |
981 | 1036 | | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
982 | 1047 | | |
983 | 1048 | | |
984 | 1049 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| |||
156 | 158 | | |
157 | 159 | | |
158 | 160 | | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
159 | 185 | | |
160 | 186 | | |
161 | 187 | | |
| |||
0 commit comments