Skip to content

Commit

Permalink
Refactor in turbo module TypeScript codegen: process (T), T|U, `T…
Browse files Browse the repository at this point in the history
…|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
  • Loading branch information
ZihanChen-MSFT authored and facebook-github-bot committed Oct 7, 2022
1 parent 967de03 commit 00b7956
Show file tree
Hide file tree
Showing 8 changed files with 430 additions and 528 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ import type {
CommandTypeAnnotation,
} from '../../../CodegenSchema.js';
import type {TypeDeclarationMap} from '../utils.js';

const {getValueFromTypes} = require('../utils.js');
const {parseTopLevelType} = require('../parseTopLevelType');

type EventTypeAST = Object;

function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) {
const name = property.key.name;
const optional = property.optional || false;
const value = getValueFromTypes(
const topLevelType = parseTopLevelType(
property.typeAnnotation.typeAnnotation,
types,
);

const name = property.key.name;
const optional = property.optional || topLevelType.optional;
const value = topLevelType.type;
const firstParam = value.parameters[0].typeAnnotation;

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

const params = value.parameters.slice(1).map(param => {
const paramName = param.name;
const paramValue = getValueFromTypes(
const paramValue = parseTopLevelType(
param.typeAnnotation.typeAnnotation,
types,
);
).type;

const type =
paramValue.type === 'TSTypeReference'
Expand Down
Loading

0 comments on commit 00b7956

Please sign in to comment.