Skip to content

Commit 00b7956

Browse files
ZihanChen-MSFTfacebook-github-bot
authored andcommitted
Refactor in turbo module TypeScript codegen: process (T), T|U, T|undefined and related stuff in a central place (#34814)
Summary: `TSParenthesizedType`, `TSUnionType`, `TSNullKeyword`, `TSUndefinedKeyword`, `TSVoidKeyword` etc are repeatly processed in so many places. In this change I put them in a new file `parseTopLevelType.js`, and everyone call that file, all repeat implementation are deleted. The `parseTopLevelType` function will look into a type consisted by the above types in any possible combination (but still very easy to do), and tell you if a type is nullable, or if there is a default value, and what is the real type with all noises removed. Array types and union types are processed in component twice, for property of array, and property of nested arrays (`componentsUtils.js`). They are extracted into single functions. ## Changelog [General] [Changed] - Refactor in turbo module TypeScript codegen: process `(T)`, `T|U`, `T|undefined` and related stuff in a central place Pull Request resolved: #34814 Test Plan: `yarn jest react-native-codegen` passed Reviewed By: yungsters, sammy-SC Differential Revision: D40094373 fbshipit-source-id: f28e145bc4e7734be9036815ea425d820eadb8f0
1 parent 967de03 commit 00b7956

File tree

8 files changed

+430
-528
lines changed

8 files changed

+430
-528
lines changed

packages/react-native-codegen/src/parsers/typescript/components/commands.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ import type {
1515
CommandTypeAnnotation,
1616
} from '../../../CodegenSchema.js';
1717
import type {TypeDeclarationMap} from '../utils.js';
18-
19-
const {getValueFromTypes} = require('../utils.js');
18+
const {parseTopLevelType} = require('../parseTopLevelType');
2019

2120
type EventTypeAST = Object;
2221

2322
function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) {
24-
const name = property.key.name;
25-
const optional = property.optional || false;
26-
const value = getValueFromTypes(
23+
const topLevelType = parseTopLevelType(
2724
property.typeAnnotation.typeAnnotation,
2825
types,
2926
);
30-
27+
const name = property.key.name;
28+
const optional = property.optional || topLevelType.optional;
29+
const value = topLevelType.type;
3130
const firstParam = value.parameters[0].typeAnnotation;
3231

3332
if (
@@ -45,10 +44,10 @@ function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) {
4544

4645
const params = value.parameters.slice(1).map(param => {
4746
const paramName = param.name;
48-
const paramValue = getValueFromTypes(
47+
const paramValue = parseTopLevelType(
4948
param.typeAnnotation.typeAnnotation,
5049
types,
51-
);
50+
).type;
5251

5352
const type =
5453
paramValue.type === 'TSTypeReference'

0 commit comments

Comments
 (0)