File tree Expand file tree Collapse file tree 7 files changed +85
-56
lines changed
packages/react-native-codegen/src/parsers Expand file tree Collapse file tree 7 files changed +85
-56
lines changed Original file line number Diff line number Diff line change @@ -31,15 +31,10 @@ const {
3131} = require ( '../../parsers-commons' ) ;
3232const {
3333 emitArrayType,
34- emitBoolean,
3534 emitFunction,
36- emitNumber,
3735 emitGenericObject,
3836 emitPromise,
3937 emitRootTag,
40- emitVoid,
41- emitString,
42- emitMixed,
4338 emitUnion,
4439 emitCommonTypes,
4540 typeAliasResolution,
@@ -207,18 +202,6 @@ function translateTypeAnnotation(
207202 nullable ,
208203 ) ;
209204 }
210- case 'BooleanTypeAnnotation' : {
211- return emitBoolean ( nullable ) ;
212- }
213- case 'NumberTypeAnnotation' : {
214- return emitNumber ( nullable ) ;
215- }
216- case 'VoidTypeAnnotation' : {
217- return emitVoid ( nullable ) ;
218- }
219- case 'StringTypeAnnotation' : {
220- return emitString ( nullable ) ;
221- }
222205 case 'FunctionTypeAnnotation' : {
223206 return emitFunction (
224207 nullable ,
@@ -243,13 +226,6 @@ function translateTypeAnnotation(
243226 memberType : 'StringTypeAnnotation' ,
244227 } ) ;
245228 }
246- case 'MixedTypeAnnotation' : {
247- if ( cxxOnly ) {
248- return emitMixed ( nullable ) ;
249- } else {
250- return emitGenericObject ( nullable ) ;
251- }
252- }
253229 case 'EnumStringBody' :
254230 case 'EnumNumberBody' : {
255231 return typeEnumResolution (
@@ -262,11 +238,26 @@ function translateTypeAnnotation(
262238 ) ;
263239 }
264240 default : {
265- throw new UnsupportedTypeAnnotationParserError (
241+ const commonType = emitCommonTypes (
266242 hasteModuleName ,
243+ types ,
267244 typeAnnotation ,
268- parser . language ( ) ,
245+ aliasMap ,
246+ enumMap ,
247+ tryParse ,
248+ cxxOnly ,
249+ nullable ,
250+ parser ,
269251 ) ;
252+
253+ if ( ! commonType ) {
254+ throw new UnsupportedTypeAnnotationParserError (
255+ hasteModuleName ,
256+ typeAnnotation ,
257+ parser . language ( ) ,
258+ ) ;
259+ }
260+ return commonType ;
270261 }
271262 }
272263}
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ class FlowParser implements Parser {
6767 }
6868
6969 nameForGenericTypeAnnotation ( typeAnnotation : $FlowFixMe ) : string {
70- return typeAnnotation . id . name ;
70+ return typeAnnotation ? .id ? .name ;
7171 }
7272
7373 checkIfInvalidModule ( typeArguments : $FlowFixMe ) : boolean {
@@ -325,6 +325,10 @@ class FlowParser implements Parser {
325325 bodyProperties ( typeAlias : $FlowFixMe ) : $ReadOnlyArray < $FlowFixMe > {
326326 return typeAlias . body . properties ;
327327 }
328+
329+ convertKeywordToTypeAnnotation ( keyword : string ) : string {
330+ return keyword ;
331+ }
328332}
329333
330334module . exports = {
Original file line number Diff line number Diff line change @@ -248,4 +248,11 @@ export interface Parser {
248248 * @returns : an array of properties.
249249 */
250250 bodyProperties ( typeAlias : $FlowFixMe ) : $ReadOnlyArray < $FlowFixMe > ;
251+
252+ /**
253+ * Given a keyword convert it to TypeAnnotation.
254+ * @parameter keyword
255+ * @returns : converted TypeAnnotation to Keywords
256+ */
257+ convertKeywordToTypeAnnotation ( keyword : string ) : string ;
251258}
Original file line number Diff line number Diff line change @@ -239,4 +239,8 @@ export class MockedParser implements Parser {
239239 bodyProperties ( typeAlias : $FlowFixMe ) : $ReadOnlyArray < $FlowFixMe > {
240240 return typeAlias . body . properties ;
241241 }
242+
243+ convertKeywordToTypeAnnotation ( keyword : string ) : string {
244+ return keyword ;
245+ }
242246}
Original file line number Diff line number Diff line change @@ -521,9 +521,6 @@ function emitCommonTypes(
521521 nullable : boolean ,
522522 parser : Parser ,
523523) : $FlowFixMe {
524- const genericTypeAnnotationName =
525- parser . nameForGenericTypeAnnotation ( typeAnnotation ) ;
526-
527524 const typeMap = {
528525 Stringish : emitStringish ,
529526 Int32 : emitInt32 ,
@@ -533,8 +530,25 @@ function emitCommonTypes(
533530 Object : emitGenericObject ,
534531 $Partial : emitPartial ,
535532 Partial : emitPartial ,
533+ BooleanTypeAnnotation : emitBoolean ,
534+ NumberTypeAnnotation : emitNumber ,
535+ VoidTypeAnnotation : emitVoid ,
536+ StringTypeAnnotation : emitString ,
537+ MixedTypeAnnotation : cxxOnly ? emitMixed : emitGenericObject ,
536538 } ;
537539
540+ const typeAnnotationName = parser . convertKeywordToTypeAnnotation (
541+ typeAnnotation . type ,
542+ ) ;
543+
544+ const simpleEmitter = typeMap [ typeAnnotationName ] ;
545+ if ( simpleEmitter ) {
546+ return simpleEmitter ( nullable ) ;
547+ }
548+
549+ const genericTypeAnnotationName =
550+ parser . nameForGenericTypeAnnotation ( typeAnnotation ) ;
551+
538552 const emitter = typeMap [ genericTypeAnnotationName ] ;
539553 if ( ! emitter ) {
540554 return null ;
Original file line number Diff line number Diff line change @@ -34,15 +34,10 @@ const {parseObjectProperty} = require('../../parsers-commons');
3434
3535const {
3636 emitArrayType,
37- emitBoolean,
3837 emitFunction,
39- emitNumber,
4038 emitGenericObject,
4139 emitPromise,
4240 emitRootTag,
43- emitVoid,
44- emitString,
45- emitMixed,
4641 emitUnion,
4742 emitCommonTypes,
4843 typeAliasResolution,
@@ -353,18 +348,6 @@ function translateTypeAnnotation(
353348 parser ,
354349 ) ;
355350 }
356- case 'TSBooleanKeyword' : {
357- return emitBoolean ( nullable ) ;
358- }
359- case 'TSNumberKeyword' : {
360- return emitNumber ( nullable ) ;
361- }
362- case 'TSVoidKeyword' : {
363- return emitVoid ( nullable ) ;
364- }
365- case 'TSStringKeyword' : {
366- return emitString ( nullable ) ;
367- }
368351 case 'TSFunctionType' : {
369352 return emitFunction (
370353 nullable ,
@@ -382,18 +365,27 @@ function translateTypeAnnotation(
382365 case 'TSUnionType' : {
383366 return emitUnion ( nullable , hasteModuleName , typeAnnotation , parser ) ;
384367 }
385- case 'TSUnknownKeyword' : {
386- if ( cxxOnly ) {
387- return emitMixed ( nullable ) ;
388- }
389- // Fallthrough
390- }
391368 default : {
392- throw new UnsupportedTypeAnnotationParserError (
369+ const commonType = emitCommonTypes (
393370 hasteModuleName ,
371+ types ,
394372 typeAnnotation ,
395- parser . language ( ) ,
373+ aliasMap ,
374+ enumMap ,
375+ tryParse ,
376+ cxxOnly ,
377+ nullable ,
378+ parser ,
396379 ) ;
380+
381+ if ( ! commonType ) {
382+ throw new UnsupportedTypeAnnotationParserError (
383+ hasteModuleName ,
384+ typeAnnotation ,
385+ parser . language ( ) ,
386+ ) ;
387+ }
388+ return commonType ;
397389 }
398390 }
399391}
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ class TypeScriptParser implements Parser {
6767 }
6868
6969 nameForGenericTypeAnnotation ( typeAnnotation : $FlowFixMe ) : string {
70- return typeAnnotation . typeName . name ;
70+ return typeAnnotation ? .typeName ? .name ;
7171 }
7272
7373 checkIfInvalidModule ( typeArguments : $FlowFixMe ) : boolean {
@@ -311,6 +311,23 @@ class TypeScriptParser implements Parser {
311311 bodyProperties ( typeAlias : TypeDeclarationMap ) : $ReadOnlyArray < $FlowFixMe > {
312312 return typeAlias . body . body ;
313313 }
314+
315+ convertKeywordToTypeAnnotation ( keyword : string ) : string {
316+ switch ( keyword ) {
317+ case 'TSBooleanKeyword' :
318+ return 'BooleanTypeAnnotation' ;
319+ case 'TSNumberKeyword' :
320+ return 'NumberTypeAnnotation' ;
321+ case 'TSVoidKeyword' :
322+ return 'VoidTypeAnnotation' ;
323+ case 'TSStringKeyword' :
324+ return 'StringTypeAnnotation' ;
325+ case 'TSUnknownKeyword' :
326+ return 'MixedTypeAnnotation' ;
327+ }
328+
329+ return keyword ;
330+ }
314331}
315332
316333module . exports = {
You can’t perform that action at this time.
0 commit comments