Skip to content

Commit c388e6c

Browse files
MaeIgfacebook-github-bot
authored andcommitted
Wrap the content of the case Array and case $ReadOnlyArray in Flow into a separate function (#34948)
Summary: This PR aims to extract translateArrayTypeAnnotation logic into a separate function as it is done in typescript folder. This will enable us to extract translateArrayTypeAnnotation function into a shared folder in a later step. It is a task of #34872: > Wrap the content of the case Array: and case ReadOnlyArray in [Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L106-L107) into a separate function, as it is for the [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L218-L234) parser. This will enable us to unify the two parsers in a later step. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Wrap the content of the case Array and case $ReadOnlyArray in Flow into a separate function Pull Request resolved: #34948 Test Plan: yarn flow: <img width="645" alt="image" src="https://user-images.githubusercontent.com/40902940/195200715-7f60d927-e262-4a94-ad91-a884d37726b8.png"> yarn lint: <img width="502" alt="image" src="https://user-images.githubusercontent.com/40902940/195200799-7959e068-b5b7-4242-a7b1-7afd80866d7f.png"> yarn jest react-native-codegen: <img width="381" alt="image" src="https://user-images.githubusercontent.com/40902940/195200775-76957c19-d06d-431c-8555-889a4205374e.png"> Reviewed By: dmytrorykun Differential Revision: D40296730 Pulled By: cipolleschi fbshipit-source-id: ad2d965046e25ee000ae6e07075976e5a0c78f1a
1 parent bb519ec commit c388e6c

File tree

2 files changed

+89
-69
lines changed
  • packages/react-native-codegen/src/parsers

2 files changed

+89
-69
lines changed

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 88 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,85 @@ function nullGuard<T>(fn: () => T): ?T {
8080
return fn();
8181
}
8282

83+
function translateArrayTypeAnnotation(
84+
hasteModuleName: string,
85+
types: TypeDeclarationMap,
86+
aliasMap: {...NativeModuleAliasMap},
87+
cxxOnly: boolean,
88+
flowArrayType: 'Array' | '$ReadOnlyArray',
89+
flowElementType: $FlowFixMe,
90+
nullable: boolean,
91+
): Nullable<NativeModuleTypeAnnotation> {
92+
try {
93+
/**
94+
* TODO(T72031674): Migrate all our NativeModule specs to not use
95+
* invalid Array ElementTypes. Then, make the elementType a required
96+
* parameter.
97+
*/
98+
const [elementType, isElementTypeNullable] = unwrapNullable(
99+
translateTypeAnnotation(
100+
hasteModuleName,
101+
flowElementType,
102+
types,
103+
aliasMap,
104+
/**
105+
* TODO(T72031674): Ensure that all ParsingErrors that are thrown
106+
* while parsing the array element don't get captured and collected.
107+
* Why? If we detect any parsing error while parsing the element,
108+
* we should default it to null down the line, here. This is
109+
* the correct behaviour until we migrate all our NativeModule specs
110+
* to be parseable.
111+
*/
112+
nullGuard,
113+
cxxOnly,
114+
),
115+
);
116+
117+
if (elementType.type === 'VoidTypeAnnotation') {
118+
throw new UnsupportedArrayElementTypeAnnotationParserError(
119+
hasteModuleName,
120+
flowElementType,
121+
flowArrayType,
122+
'void',
123+
language,
124+
);
125+
}
126+
127+
if (elementType.type === 'PromiseTypeAnnotation') {
128+
throw new UnsupportedArrayElementTypeAnnotationParserError(
129+
hasteModuleName,
130+
flowElementType,
131+
flowArrayType,
132+
'Promise',
133+
language,
134+
);
135+
}
136+
137+
if (elementType.type === 'FunctionTypeAnnotation') {
138+
throw new UnsupportedArrayElementTypeAnnotationParserError(
139+
hasteModuleName,
140+
flowElementType,
141+
flowArrayType,
142+
'FunctionTypeAnnotation',
143+
language,
144+
);
145+
}
146+
147+
const finalTypeAnnotation: NativeModuleArrayTypeAnnotation<
148+
Nullable<NativeModuleBaseTypeAnnotation>,
149+
> = {
150+
type: 'ArrayTypeAnnotation',
151+
elementType: wrapNullable(isElementTypeNullable, elementType),
152+
};
153+
154+
return wrapNullable(nullable, finalTypeAnnotation);
155+
} catch (ex) {
156+
return wrapNullable(nullable, {
157+
type: 'ArrayTypeAnnotation',
158+
});
159+
}
160+
}
161+
83162
function translateTypeAnnotation(
84163
hasteModuleName: string,
85164
/**
@@ -116,74 +195,15 @@ function translateTypeAnnotation(
116195
language,
117196
);
118197

119-
try {
120-
/**
121-
* TODO(T72031674): Migrate all our NativeModule specs to not use
122-
* invalid Array ElementTypes. Then, make the elementType a required
123-
* parameter.
124-
*/
125-
const [elementType, isElementTypeNullable] = unwrapNullable(
126-
translateTypeAnnotation(
127-
hasteModuleName,
128-
typeAnnotation.typeParameters.params[0],
129-
types,
130-
aliasMap,
131-
/**
132-
* TODO(T72031674): Ensure that all ParsingErrors that are thrown
133-
* while parsing the array element don't get captured and collected.
134-
* Why? If we detect any parsing error while parsing the element,
135-
* we should default it to null down the line, here. This is
136-
* the correct behaviour until we migrate all our NativeModule specs
137-
* to be parseable.
138-
*/
139-
nullGuard,
140-
cxxOnly,
141-
),
142-
);
143-
144-
if (elementType.type === 'VoidTypeAnnotation') {
145-
throw new UnsupportedArrayElementTypeAnnotationParserError(
146-
hasteModuleName,
147-
typeAnnotation.typeParameters.params[0],
148-
typeAnnotation.type,
149-
'void',
150-
language,
151-
);
152-
}
153-
154-
if (elementType.type === 'PromiseTypeAnnotation') {
155-
throw new UnsupportedArrayElementTypeAnnotationParserError(
156-
hasteModuleName,
157-
typeAnnotation.typeParameters.params[0],
158-
typeAnnotation.type,
159-
'Promise',
160-
language,
161-
);
162-
}
163-
164-
if (elementType.type === 'FunctionTypeAnnotation') {
165-
throw new UnsupportedArrayElementTypeAnnotationParserError(
166-
hasteModuleName,
167-
typeAnnotation.typeParameters.params[0],
168-
typeAnnotation.type,
169-
'FunctionTypeAnnotation',
170-
language,
171-
);
172-
}
173-
174-
const finalTypeAnnotation: NativeModuleArrayTypeAnnotation<
175-
Nullable<NativeModuleBaseTypeAnnotation>,
176-
> = {
177-
type: 'ArrayTypeAnnotation',
178-
elementType: wrapNullable(isElementTypeNullable, elementType),
179-
};
180-
181-
return wrapNullable(nullable, finalTypeAnnotation);
182-
} catch (ex) {
183-
return wrapNullable(nullable, {
184-
type: 'ArrayTypeAnnotation',
185-
});
186-
}
198+
return translateArrayTypeAnnotation(
199+
hasteModuleName,
200+
types,
201+
aliasMap,
202+
cxxOnly,
203+
typeAnnotation.type,
204+
typeAnnotation.typeParameters.params[0],
205+
nullable,
206+
);
187207
}
188208
case '$ReadOnly': {
189209
assertGenericTypeAnnotationHasExactlyOneTypeParameter(

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function translateArrayTypeAnnotation(
8787
cxxOnly: boolean,
8888
tsArrayType: 'Array' | 'ReadonlyArray',
8989
tsElementType: $FlowFixMe,
90-
nullable: $FlowFixMe,
90+
nullable: boolean,
9191
): Nullable<NativeModuleTypeAnnotation> {
9292
try {
9393
/**

0 commit comments

Comments
 (0)