diff --git a/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/failures.js b/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/failures.js index 329a7dca91d..469e51c9042 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/failures.js +++ b/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/failures.js @@ -23,16 +23,12 @@ const COMMANDS_DEFINED_INLINE = ` 'use strict'; -const codegenNativeComponent = require('codegenNativeComponent'); const codegenNativeCommands = require('codegenNativeCommands'); +const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - BubblingEvent, - DirectEvent, -} from 'CodegenTypes'; - +import type {Int32} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; export type ModuleProps = $ReadOnly<{| ...ViewProps, @@ -40,12 +36,14 @@ export type ModuleProps = $ReadOnly<{| |}>; export const Commands = codegenNativeCommands<{ - +hotspotUpdate: (ref: React.Ref<'RCTView'>, x: Int32, y: Int32) => void; + +hotspotUpdate: (ref: React.Ref<'RCTView'>, x: Int32, y: Int32) => void, }>({ - supportedCommands: ['hotspotUpdate'] + supportedCommands: ['hotspotUpdate'], }); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const COMMANDS_DEFINED_MULTIPLE_TIMES = ` @@ -61,16 +59,12 @@ const COMMANDS_DEFINED_MULTIPLE_TIMES = ` 'use strict'; -const codegenNativeComponent = require('codegenNativeComponent'); const codegenNativeCommands = require('codegenNativeCommands'); +const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - BubblingEvent, - DirectEvent, -} from 'CodegenTypes'; - +import type {Int32} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; interface NativeCommands { +hotspotUpdate: (viewRef: React.Ref<'RCTView'>, x: Int32, y: Int32) => void; @@ -82,13 +76,15 @@ export type ModuleProps = $ReadOnly<{| |}>; export const Commands = codegenNativeCommands({ - supportedCommands: ['hotspotUpdate'] + supportedCommands: ['hotspotUpdate'], }); export const Commands2 = codegenNativeCommands({ - supportedCommands: ['hotspotUpdate'] + supportedCommands: ['hotspotUpdate'], }); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const COMMANDS_DEFINED_WITHOUT_REF = ` @@ -104,16 +100,12 @@ const COMMANDS_DEFINED_WITHOUT_REF = ` 'use strict'; -const codegenNativeComponent = require('codegenNativeComponent'); const codegenNativeCommands = require('codegenNativeCommands'); +const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - BubblingEvent, - DirectEvent, -} from 'CodegenTypes'; - +import type {Int32} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; interface NativeCommands { +hotspotUpdate: (x: Int32, y: Int32) => void; @@ -125,10 +117,12 @@ export type ModuleProps = $ReadOnly<{| |}>; export const Commands = codegenNativeCommands({ - supportedCommands: ['hotspotUpdate'] + supportedCommands: ['hotspotUpdate'], }); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const COMMANDS_DEFINED_WITH_NULLABLE_REF = ` @@ -144,16 +138,12 @@ const COMMANDS_DEFINED_WITH_NULLABLE_REF = ` 'use strict'; -const codegenNativeComponent = require('codegenNativeComponent'); const codegenNativeCommands = require('codegenNativeCommands'); +const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - BubblingEvent, - DirectEvent, -} from 'CodegenTypes'; - +import type {Int32} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; interface NativeCommands { +hotspotUpdate: (viewRef: ?React.Ref<'RCTView'>, x: Int32, y: Int32) => void; @@ -165,10 +155,12 @@ export type ModuleProps = $ReadOnly<{| |}>; export const Commands = codegenNativeCommands({ - supportedCommands: ['hotspotUpdate'] + supportedCommands: ['hotspotUpdate'], }); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const COMMANDS_DEFINED_WITH_MISMATCHED_METHOD_NAMES = ` @@ -184,20 +176,20 @@ const COMMANDS_DEFINED_WITH_MISMATCHED_METHOD_NAMES = ` 'use strict'; -const codegenNativeComponent = require('codegenNativeComponent'); const codegenNativeCommands = require('codegenNativeCommands'); +const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - BubblingEventHandler, - DirectEventHandler, -} from 'CodegenTypes'; - +import type {Int32} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; interface NativeCommands { +hotspotUpdate: (viewRef: React.Ref<'RCTView'>, x: Int32, y: Int32) => void; - +scrollTo: (viewRef: React.Ref<'RCTView'>, y: Int32, animated: boolean) => void; + +scrollTo: ( + viewRef: React.Ref<'RCTView'>, + y: Int32, + animated: boolean, + ) => void; } export type ModuleProps = $ReadOnly<{| @@ -206,10 +198,12 @@ export type ModuleProps = $ReadOnly<{| |}>; export const Commands = codegenNativeCommands({ - supportedCommands: ['scrollTo'] + supportedCommands: ['scrollTo'], }); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const COMMANDS_DEFINED_WITHOUT_METHOD_NAMES = ` @@ -225,20 +219,20 @@ const COMMANDS_DEFINED_WITHOUT_METHOD_NAMES = ` 'use strict'; -const codegenNativeComponent = require('codegenNativeComponent'); const codegenNativeCommands = require('codegenNativeCommands'); +const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - BubblingEventHandler, - DirectEventHandler, -} from 'CodegenTypes'; - +import type {Int32} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; interface NativeCommands { +hotspotUpdate: (viewRef: React.Ref<'RCTView'>, x: Int32, y: Int32) => void; - +scrollTo: (viewRef: React.Ref<'RCTView'>, y: Int32, animated: boolean) => void; + +scrollTo: ( + viewRef: React.Ref<'RCTView'>, + y: Int32, + animated: boolean, + ) => void; } export type ModuleProps = $ReadOnly<{| @@ -248,7 +242,9 @@ export type ModuleProps = $ReadOnly<{| export const Commands = codegenNativeCommands(); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const NULLABLE_WITH_DEFAULT = ` @@ -266,20 +262,18 @@ const NULLABLE_WITH_DEFAULT = ` const codegenNativeComponent = require('codegenNativeComponent'); -import type { - WithDefault, - Float, -} from 'CodegenTypes'; - +import type {WithDefault, Float} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; - +import type {NativeComponent} from 'codegenNativeComponent'; export type ModuleProps = $ReadOnly<{| ...ViewProps, nullable_with_default: ?WithDefault, |}>; -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const NON_OPTIONAL_KEY_WITH_DEFAULT_VALUE = ` @@ -297,21 +291,20 @@ const NON_OPTIONAL_KEY_WITH_DEFAULT_VALUE = ` const codegenNativeComponent = require('codegenNativeComponent'); -import type { - WithDefault, - Float, -} from 'CodegenTypes'; - +import type {WithDefault, Float} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; - +import type {NativeComponent} from 'codegenNativeComponent'; export type ModuleProps = $ReadOnly<{| ...ViewProps, required_key_with_default: WithDefault, |}>; -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; + module.exports = { COMMANDS_DEFINED_INLINE, COMMANDS_DEFINED_MULTIPLE_TIMES, diff --git a/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/fixtures.js index babaa9dc8f8..30f87877d04 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/fixtures.js @@ -82,8 +82,48 @@ import type { DirectEventHandler, WithDefault, } from 'CodegenTypes'; - import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; + +type ModuleProps = $ReadOnly<{| + ...ViewProps, + + // Props + boolean_default_true_optional_both?: WithDefault, + + // Events + onDirectEventDefinedInlineNull: DirectEventHandler, + onBubblingEventDefinedInlineNull: BubblingEventHandler, +|}>; + +export default (codegenNativeComponent('Module', { + interfaceOnly: true, + paperComponentName: 'RCTModule', +}): NativeComponent); +`; + +const ONE_OF_EACH_PROP_EVENT_DEFAULT_AND_OPTIONS_NO_CAST = ` +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow + */ + +'use strict'; + +const codegenNativeComponent = require('codegenNativeComponent'); + +import type { + BubblingEventHandler, + DirectEventHandler, + WithDefault, +} from 'CodegenTypes'; +import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; type ModuleProps = $ReadOnly<{| ...ViewProps, @@ -118,14 +158,15 @@ const NO_PROPS_EVENTS_ONLY_DEPRECATED_VIEW_CONFIG_NAME_OPTION = ` const codegenNativeComponent = require('codegenNativeComponent'); import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; type ModuleProps = $ReadOnly<{| ...ViewProps, |}>; -export default codegenNativeComponent('Module', { +export default (codegenNativeComponent('Module', { deprecatedViewConfigName: 'DeprecateModuleName', -}); +}): NativeComponent); `; const ALL_PROP_TYPES_NO_EVENTS = ` @@ -143,15 +184,11 @@ const ALL_PROP_TYPES_NO_EVENTS = ` const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - Float, - WithDefault, -} from 'CodegenTypes'; - -import type {ColorValue, ColorArrayValue, PointValue} from 'StyleSheetTypes'; +import type {Int32, Float, WithDefault} from 'CodegenTypes'; import type {ImageSource} from 'ImageSource'; +import type {ColorValue, ColorArrayValue, PointValue} from 'StyleSheetTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; type ModuleProps = $ReadOnly<{| ...ViewProps, @@ -191,8 +228,8 @@ type ModuleProps = $ReadOnly<{| int32_optional_both?: WithDefault, // String enum props - enum_optional_key?: WithDefault<('small' | 'large'), 'small'>, - enum_optional_both?: WithDefault<('small' | 'large'), 'small'>, + enum_optional_key?: WithDefault<'small' | 'large', 'small'>, + enum_optional_both?: WithDefault<'small' | 'large', 'small'>, // ImageSource props image_required: ImageSource, @@ -218,7 +255,9 @@ type ModuleProps = $ReadOnly<{| point_optional_both?: ?PointValue, |}>; -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const ARRAY_PROP_TYPES_NO_EVENTS = ` @@ -236,15 +275,11 @@ const ARRAY_PROP_TYPES_NO_EVENTS = ` const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - Float, - WithDefault, -} from 'CodegenTypes'; - -import type {ColorValue, PointValue} from 'StyleSheetTypes'; +import type {Int32, Float, WithDefault} from 'CodegenTypes'; import type {ImageSource} from 'ImageSource'; +import type {ColorValue, PointValue} from 'StyleSheetTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; type ModuleProps = $ReadOnly<{| ...ViewProps, @@ -275,8 +310,14 @@ type ModuleProps = $ReadOnly<{| array_int32_optional_both?: ?$ReadOnlyArray, // String enum props - array_enum_optional_key?: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>, - array_enum_optional_both?: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>, + array_enum_optional_key?: WithDefault< + $ReadOnlyArray<'small' | 'large'>, + 'small', + >, + array_enum_optional_both?: WithDefault< + $ReadOnlyArray<'small' | 'large'>, + 'small', + >, // ImageSource props array_image_required: $ReadOnlyArray, @@ -297,7 +338,9 @@ type ModuleProps = $ReadOnly<{| array_point_optional_both?: ?$ReadOnlyArray, |}>; -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const EVENTS_DEFINED_INLINE_WITH_ALL_TYPES = ` @@ -313,6 +356,7 @@ const EVENTS_DEFINED_INLINE_WITH_ALL_TYPES = ` 'use strict'; +import type {NativeComponent} from 'codegenNativeComponent'; const codegenNativeComponent = require('codegenNativeComponent'); import type { @@ -402,8 +446,9 @@ type ModuleProps = $ReadOnly<{| >, |}>; -export default codegenNativeComponent('Module'); - +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const EVENTS_DEFINED_AS_NULL_INLINE = ` @@ -421,14 +466,9 @@ const EVENTS_DEFINED_AS_NULL_INLINE = ` const codegenNativeComponent = require('codegenNativeComponent'); -import type { - BubblingEventHandler, - DirectEventHandler, -} from 'CodegenTypese'; - +import type {BubblingEventHandler, DirectEventHandler} from 'CodegenTypese'; import type {ViewProps} from 'ViewPropTypes'; - -const codegenNativeComponent = require('codegenNativeComponent'); +import type {NativeComponent} from 'codegenNativeComponent'; type ModuleProps = $ReadOnly<{| ...ViewProps, @@ -436,28 +476,28 @@ type ModuleProps = $ReadOnly<{| // No props // Events defined inline - onDirectEventDefinedInlineNull:DirectEventHandler, + onDirectEventDefinedInlineNull: DirectEventHandler, onDirectEventDefinedInlineNullOptionalKey?: DirectEventHandler, onDirectEventDefinedInlineNullOptionalValue: ?DirectEventHandler, onDirectEventDefinedInlineNullOptionalBoth?: DirectEventHandler, - onDirectEventDefinedInlineNullWithPaperName?: ? - DirectEventHandler< - null, - 'paperDirectEventDefinedInlineNullWithPaperName', - >, + onDirectEventDefinedInlineNullWithPaperName?: ?DirectEventHandler< + null, + 'paperDirectEventDefinedInlineNullWithPaperName', + >, onBubblingEventDefinedInlineNull: BubblingEventHandler, onBubblingEventDefinedInlineNullOptionalKey?: BubblingEventHandler, onBubblingEventDefinedInlineNullOptionalValue: ?BubblingEventHandler, onBubblingEventDefinedInlineNullOptionalBoth?: ?BubblingEventHandler, - onBubblingEventDefinedInlineNullWithPaperName?: ? - BubblingEventHandler< - null, - 'paperBubblingEventDefinedInlineNullWithPaperName', - >, + onBubblingEventDefinedInlineNullWithPaperName?: ?BubblingEventHandler< + null, + 'paperBubblingEventDefinedInlineNullWithPaperName', + >, |}>; -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const PROPS_AND_EVENTS_TYPES_EXPORTED = ` @@ -477,8 +517,8 @@ import type { BubblingEventHandler, DirectEventHandler, } from 'CodegenTypes'; - import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; const codegenNativeComponent = require('codegenNativeComponent'); @@ -498,7 +538,9 @@ export type ModuleProps = $ReadOnly<{| onDirectEventDefinedInlineWithPaperName: DirectEventHandler, |}>; -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const PROPS_AS_EXTERNAL_TYPES = ` @@ -514,6 +556,10 @@ const PROPS_AS_EXTERNAL_TYPES = ` 'use strict'; +const codegenNativeComponent = require('codegenNativeComponent'); + +import type {NativeComponent} from 'codegenNativeComponent'; + export type String = string; export type AnotherArray = $ReadOnlyArray; @@ -522,7 +568,9 @@ export type ModuleProps = $ReadOnly<{| array: AnotherArray, |}>; -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const COMMANDS_DEFINED_WITH_ALL_TYPES = ` @@ -538,20 +586,20 @@ const COMMANDS_DEFINED_WITH_ALL_TYPES = ` 'use strict'; -const codegenNativeComponent = require('codegenNativeComponent'); const codegenNativeCommands = require('codegenNativeCommands'); +const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - BubblingEventHandler, - DirectEventHandler, -} from 'CodegenTypes'; - +import type {Int32} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; interface NativeCommands { +hotspotUpdate: (viewRef: React.Ref<'RCTView'>, x: Int32, y: Int32) => void; - +scrollTo: (viewRef: React.Ref<'RCTView'>, y: Int32, animated: boolean) => void; + +scrollTo: ( + viewRef: React.Ref<'RCTView'>, + y: Int32, + animated: boolean, + ) => void; } export type ModuleProps = $ReadOnly<{| @@ -560,10 +608,12 @@ export type ModuleProps = $ReadOnly<{| |}>; export const Commands = codegenNativeCommands({ - supportedCommands: ['hotspotUpdate', 'scrollTo'] + supportedCommands: ['hotspotUpdate', 'scrollTo'], }); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const COMMANDS_WITH_EXTERNAL_TYPES = ` @@ -579,22 +629,22 @@ const COMMANDS_WITH_EXTERNAL_TYPES = ` 'use strict'; -const codegenNativeComponent = require('codegenNativeComponent'); const codegenNativeCommands = require('codegenNativeCommands'); +const codegenNativeComponent = require('codegenNativeComponent'); -import type { - Int32, - BubblingEventHandler, - DirectEventHandler, -} from 'CodegenTypes'; - +import type {Int32} from 'CodegenTypes'; import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; export type Boolean = boolean; export type Int = Int32; export type Void = void; -export type ScrollTo = (viewRef: React.Ref<'RCTView'>, y: Int, animated: Boolean) => Void +export type ScrollTo = ( + viewRef: React.Ref<'RCTView'>, + y: Int, + animated: Boolean, +) => Void; interface NativeCommands { +scrollTo: ScrollTo; @@ -606,10 +656,12 @@ export type ModuleProps = $ReadOnly<{| |}>; export const Commands = codegenNativeCommands({ - supportedCommands: ['scrollTo'] + supportedCommands: ['scrollTo'], }); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; const COMMANDS_AND_EVENTS_TYPES_EXPORTED = ` @@ -629,8 +681,8 @@ import type { BubblingEventHandler, DirectEventHandler, } from 'CodegenTypes'; - import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; const codegenNativeComponent = require('codegenNativeComponent'); @@ -664,13 +716,16 @@ export const Commands = codegenNativeCommands({ supportedCommands: ['scrollTo'] }); -export default codegenNativeComponent('Module'); +export default (codegenNativeComponent( + 'Module', +): NativeComponent); `; module.exports = { ALL_PROP_TYPES_NO_EVENTS, ARRAY_PROP_TYPES_NO_EVENTS, ONE_OF_EACH_PROP_EVENT_DEFAULT_AND_OPTIONS, + ONE_OF_EACH_PROP_EVENT_DEFAULT_AND_OPTIONS_NO_CAST, NO_PROPS_EVENTS_ONLY_DEPRECATED_VIEW_CONFIG_NAME_OPTION, EVENTS_DEFINED_INLINE_WITH_ALL_TYPES, EVENTS_DEFINED_AS_NULL_INLINE, diff --git a/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap index 50451790581..7f8b4e40397 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap @@ -4293,6 +4293,64 @@ Object { } `; +exports[`RN Codegen Flow Parser can generate fixture ONE_OF_EACH_PROP_EVENT_DEFAULT_AND_OPTIONS_NO_CAST 1`] = ` +Object { + "modules": Object { + "Module": Object { + "components": Object { + "Module": Object { + "commands": Array [], + "events": Array [ + Object { + "bubblingType": "direct", + "name": "onDirectEventDefinedInlineNull", + "optional": false, + "typeAnnotation": Object { + "argument": Object { + "properties": Array [], + "type": "ObjectTypeAnnotation", + }, + "type": "EventTypeAnnotation", + }, + }, + Object { + "bubblingType": "bubble", + "name": "onBubblingEventDefinedInlineNull", + "optional": false, + "typeAnnotation": Object { + "argument": Object { + "properties": Array [], + "type": "ObjectTypeAnnotation", + }, + "type": "EventTypeAnnotation", + }, + }, + ], + "extendsProps": Array [ + Object { + "knownTypeName": "ReactNativeCoreViewProps", + "type": "ReactNativeBuiltInType", + }, + ], + "interfaceOnly": true, + "paperComponentName": "RCTModule", + "props": Array [ + Object { + "name": "boolean_default_true_optional_both", + "optional": true, + "typeAnnotation": Object { + "default": true, + "type": "BooleanTypeAnnotation", + }, + }, + ], + }, + }, + }, + }, +} +`; + exports[`RN Codegen Flow Parser can generate fixture PROPS_AND_EVENTS_TYPES_EXPORTED 1`] = ` Object { "modules": Object { diff --git a/packages/react-native-codegen/src/parsers/flow/components/index.js b/packages/react-native-codegen/src/parsers/flow/components/index.js index 52570c3da92..7f4ca3e4c96 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -25,10 +25,18 @@ function findComponentConfig(ast) { ); defaultExports.forEach(statement => { + let declaration = statement.declaration; + + // codegenNativeComponent can be nested inside a cast + // expression so we need to go one level deeper + if (declaration.type === 'TypeCastExpression') { + declaration = declaration.expression; + } + try { - if (statement.declaration.callee.name === 'codegenNativeComponent') { - const typeArgumentParams = statement.declaration.typeArguments.params; - const funcArgumentParams = statement.declaration.arguments; + if (declaration.callee.name === 'codegenNativeComponent') { + const typeArgumentParams = declaration.typeArguments.params; + const funcArgumentParams = declaration.arguments; const nativeComponentType = {}; nativeComponentType.propsTypeName = typeArgumentParams[0].id.name; diff --git a/packages/react-native-codegen/src/parsers/flow/index.js b/packages/react-native-codegen/src/parsers/flow/index.js index d94c41c5f7f..11a1b2f33e4 100644 --- a/packages/react-native-codegen/src/parsers/flow/index.js +++ b/packages/react-native-codegen/src/parsers/flow/index.js @@ -40,11 +40,22 @@ function getConfigType(ast, types): 'module' | 'component' { const defaultExports = ast.body.filter( node => node.type === 'ExportDefaultDeclaration', ); - const isComponent = - defaultExports[0] && - defaultExports[0].declaration && - defaultExports[0].declaration.callee && - defaultExports[0].declaration.callee.name === 'codegenNativeComponent'; + + let isComponent = false; + + if (defaultExports.length > 0) { + let declaration = defaultExports[0].declaration; + // codegenNativeComponent can be nested inside a cast + // expression so we need to go one level deeper + if (declaration.type === 'TypeCastExpression') { + declaration = declaration.expression; + } + + isComponent = + declaration && + declaration.callee && + declaration.callee.name === 'codegenNativeComponent'; + } const typesExtendingTurboModule = Object.keys(types) .map(typeName => types[typeName])