Skip to content

Commit

Permalink
Support TypeScript type (T) for turbo module codegen (module only) (#…
Browse files Browse the repository at this point in the history
…34621)

Summary:
1. In some situation (I don't know exactly how it is triggered but I found that during porting it to `react-native-windows`), `ExportNamedDeclaration.exportKind` is missing. Just skip the checking to `exportKind` because the rest of the checking is sufficient without reading this field.
2. Add `TSParenthesizedType` to module codegen in TypeScript, so that type `(T)` is treated like `T`.

## Changelog

[General] [Changed] - codegen: support TypeScript type `(T)` for turbo module codegen (module only)

Pull Request resolved: #34621

Test Plan: `yarn jest` passed in `packages/react-native-codegen`

Reviewed By: RSNara

Differential Revision: D39322001

Pulled By: cipolleschi

fbshipit-source-id: 1855711da7062a065c05a10f275e26baa88cf75f
  • Loading branch information
ZihanChen-MSFT authored and facebook-github-bot committed Sep 9, 2022
1 parent 9788db0 commit 624bdc7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
readonly getArray: (arg: Array<string>) => Array<string>;
readonly getArray: (arg: ReadonlyArray<string>) => ReadonlyArray<string>;
readonly getArray: (arg: Array<string>) => (Array<(string)>);
readonly getArray: (arg: ReadonlyArray<string>) => (ReadonlyArray<(string)>);
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
Expand All @@ -336,8 +336,8 @@ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
readonly getArray: (arg: string[]) => string[];
readonly getArray: (arg: readonly string[]) => readonly string[];
readonly getArray: (arg: string[]) => ((string)[]);
readonly getArray: (arg: readonly string[]) => (readonly (string)[]);
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ function translateTypeAnnotation(
resolveTypeAnnotation(typeScriptTypeAnnotation, types);
switch (typeAnnotation.type) {
case 'TSParenthesizedType': {
return translateTypeAnnotation(
hasteModuleName,
typeAnnotation.typeAnnotation,
types,
aliasMap,
tryParse,
cxxOnly,
);
}
case 'TSArrayType': {
return translateArrayTypeAnnotation(
hasteModuleName,
Expand Down
38 changes: 19 additions & 19 deletions packages/react-native-codegen/src/parsers/typescript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ export type TypeDeclarationMap = {[declarationName: string]: $FlowFixMe};

function getTypes(ast: $FlowFixMe): TypeDeclarationMap {
return ast.body.reduce((types, node) => {
if (node.type === 'ExportNamedDeclaration' && node.exportKind === 'type') {
if (
node.declaration.type === 'TSTypeAliasDeclaration' ||
node.declaration.type === 'TSInterfaceDeclaration'
) {
types[node.declaration.id.name] = node.declaration;
switch (node.type) {
case 'ExportNamedDeclaration': {
if (node.declaration) {
switch (node.declaration.type) {
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
case 'TSEnumDeclaration': {
types[node.declaration.id.name] = node.declaration;
break;
}
}
}
break;
}
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
case 'TSEnumDeclaration': {
types[node.id.name] = node;
break;
}
} else if (
node.type === 'ExportNamedDeclaration' &&
node.exportKind === 'value' &&
node.declaration &&
node.declaration.type === 'TSEnumDeclaration'
) {
types[node.declaration.id.name] = node.declaration;
} else if (
node.type === 'TSTypeAliasDeclaration' ||
node.type === 'TSInterfaceDeclaration' ||
node.type === 'TSEnumDeclaration'
) {
types[node.id.name] = node;
}
return types;
}, {});
Expand Down

0 comments on commit 624bdc7

Please sign in to comment.