Skip to content

Commit 94f505b

Browse files
yux-mfacebook-github-bot
authored andcommitted
Extract inner switch case 'TSTypereference' to function translateTypeReferenceAnnotation() (#36463)
Summary: > [Codegen 91] Extract the inner switch in typescript/modules/index.js at line to parse the case TSTypereference ([lines](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L194-L275))into a function translateTypeReferenceAnnotation() (the goal is to try and get a simpler switch statement and to spot structural similiarities between the flow and typescript index.js files) Part of Issue #34872 ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [CHANGED] - Extract inner switch case 'TSTypereference' to function translateTypeReferenceAnnotation(). Pull Request resolved: #36463 Test Plan: - yarn lint - yarn run flow - yarn test react-native-codegen Reviewed By: cortinico Differential Revision: D44027755 Pulled By: cipolleschi fbshipit-source-id: b32d6adc56fcd0e1b42c255b16828b2306941d57
1 parent c0abff1 commit 94f505b

File tree

1 file changed

+103
-82
lines changed
  • packages/react-native-codegen/src/parsers/typescript/modules

1 file changed

+103
-82
lines changed

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

Lines changed: 103 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,97 @@ function translateObjectTypeAnnotation(
122122
);
123123
}
124124

125+
function translateTypeReferenceAnnotation(
126+
typeName: string,
127+
nullable: boolean,
128+
typeAnnotation: $FlowFixMe,
129+
hasteModuleName: string,
130+
types: TypeDeclarationMap,
131+
aliasMap: {...NativeModuleAliasMap},
132+
enumMap: {...NativeModuleEnumMap},
133+
tryParse: ParserErrorCapturer,
134+
cxxOnly: boolean,
135+
parser: Parser,
136+
): Nullable<NativeModuleTypeAnnotation> {
137+
switch (typeName) {
138+
case 'RootTag': {
139+
return emitRootTag(nullable);
140+
}
141+
case 'Promise': {
142+
return emitPromise(
143+
hasteModuleName,
144+
typeAnnotation,
145+
parser,
146+
nullable,
147+
types,
148+
aliasMap,
149+
enumMap,
150+
tryParse,
151+
cxxOnly,
152+
translateTypeAnnotation,
153+
);
154+
}
155+
case 'Array':
156+
case 'ReadonlyArray': {
157+
return emitArrayType(
158+
hasteModuleName,
159+
typeAnnotation,
160+
parser,
161+
types,
162+
aliasMap,
163+
enumMap,
164+
cxxOnly,
165+
nullable,
166+
translateTypeAnnotation,
167+
);
168+
}
169+
case 'Stringish': {
170+
return emitStringish(nullable);
171+
}
172+
case 'Int32': {
173+
return emitInt32(nullable);
174+
}
175+
case 'Double': {
176+
return emitDouble(nullable);
177+
}
178+
case 'Float': {
179+
return emitFloat(nullable);
180+
}
181+
case 'UnsafeObject':
182+
case 'Object': {
183+
return emitGenericObject(nullable);
184+
}
185+
case 'Partial': {
186+
throwIfPartialWithMoreParameter(typeAnnotation);
187+
188+
const annotatedElement = parser.extractAnnotatedElement(
189+
typeAnnotation,
190+
types,
191+
);
192+
193+
throwIfPartialNotAnnotatingTypeParameter(typeAnnotation, types, parser);
194+
195+
const properties = parser.computePartialProperties(
196+
annotatedElement.typeAnnotation.members,
197+
hasteModuleName,
198+
types,
199+
aliasMap,
200+
enumMap,
201+
tryParse,
202+
cxxOnly,
203+
);
204+
205+
return emitObject(nullable, properties);
206+
}
207+
default: {
208+
throw new UnsupportedGenericParserError(
209+
hasteModuleName,
210+
typeAnnotation,
211+
parser,
212+
);
213+
}
214+
}
215+
}
125216
function translateTypeAnnotation(
126217
hasteModuleName: string,
127218
/**
@@ -179,88 +270,18 @@ function translateTypeAnnotation(
179270
}
180271
}
181272
case 'TSTypeReference': {
182-
switch (typeAnnotation.typeName.name) {
183-
case 'RootTag': {
184-
return emitRootTag(nullable);
185-
}
186-
case 'Promise': {
187-
return emitPromise(
188-
hasteModuleName,
189-
typeAnnotation,
190-
parser,
191-
nullable,
192-
types,
193-
aliasMap,
194-
enumMap,
195-
tryParse,
196-
cxxOnly,
197-
translateTypeAnnotation,
198-
);
199-
}
200-
case 'Array':
201-
case 'ReadonlyArray': {
202-
return emitArrayType(
203-
hasteModuleName,
204-
typeAnnotation,
205-
parser,
206-
types,
207-
aliasMap,
208-
enumMap,
209-
cxxOnly,
210-
nullable,
211-
translateTypeAnnotation,
212-
);
213-
}
214-
case 'Stringish': {
215-
return emitStringish(nullable);
216-
}
217-
case 'Int32': {
218-
return emitInt32(nullable);
219-
}
220-
case 'Double': {
221-
return emitDouble(nullable);
222-
}
223-
case 'Float': {
224-
return emitFloat(nullable);
225-
}
226-
case 'UnsafeObject':
227-
case 'Object': {
228-
return emitGenericObject(nullable);
229-
}
230-
case 'Partial': {
231-
throwIfPartialWithMoreParameter(typeAnnotation);
232-
233-
const annotatedElement = parser.extractAnnotatedElement(
234-
typeAnnotation,
235-
types,
236-
);
237-
238-
throwIfPartialNotAnnotatingTypeParameter(
239-
typeAnnotation,
240-
types,
241-
parser,
242-
);
243-
244-
const properties = parser.computePartialProperties(
245-
annotatedElement.typeAnnotation.members,
246-
hasteModuleName,
247-
types,
248-
aliasMap,
249-
enumMap,
250-
tryParse,
251-
cxxOnly,
252-
);
253-
254-
return emitObject(nullable, properties);
255-
}
256-
default: {
257-
throw new UnsupportedGenericParserError(
258-
hasteModuleName,
259-
typeAnnotation,
260-
parser,
261-
);
262-
}
263-
}
273+
return translateTypeReferenceAnnotation(
274+
typeAnnotation.typeName.name,
275+
nullable,
276+
typeAnnotation,
277+
hasteModuleName,
278+
types,
279+
aliasMap,
280+
enumMap,
281+
tryParse,
282+
cxxOnly,
283+
parser,
284+
);
264285
}
265286
case 'TSInterfaceDeclaration': {
266287
const baseTypes = (typeAnnotation.extends ?? []).map(

0 commit comments

Comments
 (0)