diff --git a/Libraries/Components/ActivityIndicator/ActivityIndicatorViewSchema.js b/Libraries/Components/ActivityIndicator/ActivityIndicatorViewSchema.js index dba8225c903..495d18c5100 100644 --- a/Libraries/Components/ActivityIndicator/ActivityIndicatorViewSchema.js +++ b/Libraries/Components/ActivityIndicator/ActivityIndicatorViewSchema.js @@ -67,6 +67,7 @@ const SwitchSchema: SchemaType = { }, }, ], + commands: [], }, }, }, diff --git a/Libraries/Components/RefreshControl/AndroidSwipeRefreshLayoutSchema.js b/Libraries/Components/RefreshControl/AndroidSwipeRefreshLayoutSchema.js index 5b7de36bbf2..7d8c788c534 100644 --- a/Libraries/Components/RefreshControl/AndroidSwipeRefreshLayoutSchema.js +++ b/Libraries/Components/RefreshControl/AndroidSwipeRefreshLayoutSchema.js @@ -89,6 +89,7 @@ const AndroidSwipeRefreshLayoutSchema: SchemaType = { }, }, ], + commands: [], }, }, }, diff --git a/Libraries/Components/RefreshControl/PullToRefreshViewSchema.js b/Libraries/Components/RefreshControl/PullToRefreshViewSchema.js index b6b08102ee8..cba10d9fdd1 100644 --- a/Libraries/Components/RefreshControl/PullToRefreshViewSchema.js +++ b/Libraries/Components/RefreshControl/PullToRefreshViewSchema.js @@ -71,6 +71,7 @@ const PullToRefreshViewSchema: SchemaType = { }, }, ], + commands: [], }, }, }, diff --git a/Libraries/Components/Slider/SliderSchema.js b/Libraries/Components/Slider/SliderSchema.js index 423868e432d..f5b6faddb57 100644 --- a/Libraries/Components/Slider/SliderSchema.js +++ b/Libraries/Components/Slider/SliderSchema.js @@ -210,6 +210,7 @@ const SliderSchema: SchemaType = { }, }, ], + commands: [], }, }, }, diff --git a/Libraries/Components/Switch/SwitchSchema.js b/Libraries/Components/Switch/SwitchSchema.js index 20adba0aefc..654834546e9 100644 --- a/Libraries/Components/Switch/SwitchSchema.js +++ b/Libraries/Components/Switch/SwitchSchema.js @@ -85,6 +85,7 @@ const SwitchSchema: SchemaType = { }, }, ], + commands: [], }, }, }, diff --git a/Libraries/Components/UnimplementedViews/UnimplementedNativeViewSchema.js b/Libraries/Components/UnimplementedViews/UnimplementedNativeViewSchema.js index 99773adcc18..e9fb0784944 100644 --- a/Libraries/Components/UnimplementedViews/UnimplementedNativeViewSchema.js +++ b/Libraries/Components/UnimplementedViews/UnimplementedNativeViewSchema.js @@ -34,6 +34,7 @@ const UnimplementedNativeViewSchema: SchemaType = { }, }, ], + commands: [], }, }, }, diff --git a/Libraries/Modal/ModalSchema.js b/Libraries/Modal/ModalSchema.js index 574eb57efa3..a3db92220c0 100644 --- a/Libraries/Modal/ModalSchema.js +++ b/Libraries/Modal/ModalSchema.js @@ -199,6 +199,7 @@ const ModalSchema: SchemaType = { }, }, ], + commands: [], }, }, }, diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 7be78d78725..c0a6522cccd 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -10,6 +10,26 @@ 'use strict'; +export type FunctionTypeAnnotation = $ReadOnly<{| + type: 'FunctionTypeAnnotation', + params: $ReadOnlyArray, +|}>; + +export type FunctionTypeParamAnnotation = $ReadOnly<{| + name: string, + typeAnnotation: TypeAnnotation, +|}>; + +export type TypeAnnotation = BooleanTypeAnnotation | Int32TypeAnnotation; + +export type BooleanTypeAnnotation = $ReadOnly<{| + type: 'BooleanTypeAnnotation', +|}>; + +export type Int32TypeAnnotation = $ReadOnly<{| + type: 'Int32TypeAnnotation', +|}>; + export type ObjectPropertyType = | $ReadOnly<{| type: 'BooleanTypeAnnotation', @@ -121,6 +141,12 @@ export type EventTypeShape = $ReadOnly<{| |}>, |}>; +export type CommandTypeShape = $ReadOnly<{| + name: string, + optional: boolean, + typeAnnotation: FunctionTypeAnnotation, +|}>; + export type OptionsShape = $ReadOnly<{| interfaceOnly?: boolean, @@ -143,6 +169,7 @@ export type ComponentShape = $ReadOnly<{| extendsProps: $ReadOnlyArray, events: $ReadOnlyArray, props: $ReadOnlyArray, + commands: $ReadOnlyArray, |}>; export type SchemaType = $ReadOnly<{| diff --git a/packages/react-native-codegen/src/__tests__/SchemaValidator-test.js b/packages/react-native-codegen/src/__tests__/SchemaValidator-test.js index 9d04dc751c1..0fe1984ef4d 100644 --- a/packages/react-native-codegen/src/__tests__/SchemaValidator-test.js +++ b/packages/react-native-codegen/src/__tests__/SchemaValidator-test.js @@ -40,6 +40,7 @@ describe('SchemaValidator', () => { ], events: [], props: [simpleProp], + commands: [], }, }, }, @@ -54,6 +55,7 @@ describe('SchemaValidator', () => { ], events: [], props: [simpleProp], + commands: [], }, }, }, diff --git a/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js index 7ccec3f04c2..3c86118d7f8 100644 --- a/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js @@ -25,6 +25,7 @@ const NO_PROPS_NO_EVENTS: SchemaType = { ], events: [], props: [], + commands: [], }, }, }, @@ -74,6 +75,7 @@ const INTERFACE_ONLY: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -102,6 +104,7 @@ const BOOLEAN_PROP: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -138,6 +141,7 @@ const STRING_PROP: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -182,6 +186,7 @@ const INTEGER_PROPS: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -250,6 +255,7 @@ const FLOAT_PROPS: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -278,6 +284,7 @@ const COLOR_PROP: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -306,6 +313,7 @@ const IMAGE_PROP: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -334,6 +342,7 @@ const POINT_PROP: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -446,6 +455,7 @@ const ARRAY_PROPS: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -498,6 +508,7 @@ const MULTI_NATIVE_PROP: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -537,6 +548,7 @@ const ENUM_PROP: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -651,6 +663,7 @@ const EVENT_PROPS: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -722,6 +735,7 @@ const EVENT_NESTED_OBJECT_PROPS: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -750,6 +764,7 @@ const TWO_COMPONENTS_SAME_FILE: SchemaType = { }, }, ], + commands: [], }, MultiComponent2NativeComponent: { @@ -770,6 +785,7 @@ const TWO_COMPONENTS_SAME_FILE: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -798,6 +814,7 @@ const TWO_COMPONENTS_DIFFERENT_FILES: SchemaType = { }, }, ], + commands: [], }, }, }, @@ -822,6 +839,7 @@ const TWO_COMPONENTS_DIFFERENT_FILES: SchemaType = { }, }, ], + commands: [], }, }, }, diff --git a/packages/react-native-codegen/src/generators/__tests__/GenerateViewConfigJs-test.js b/packages/react-native-codegen/src/generators/__tests__/GenerateViewConfigJs-test.js index e9bc944d453..039398cb51d 100644 --- a/packages/react-native-codegen/src/generators/__tests__/GenerateViewConfigJs-test.js +++ b/packages/react-native-codegen/src/generators/__tests__/GenerateViewConfigJs-test.js @@ -41,6 +41,7 @@ describe('GenerateViewConfigJs', () => { ], events: [], props: [], + commands: [], }, }, }, diff --git a/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/failures.js b/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/failures.js new file mode 100644 index 00000000000..19907602b12 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/failures.js @@ -0,0 +1,130 @@ +/** + * 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. + * + * @flow + * @format + */ + +'use strict'; + +const COMMANDS_DEFINED_INLINE = ` +/** + * 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'); +const codegenNativeCommands = require('codegenNativeCommands'); + +import type { + Int32, + BubblingEvent, + DirectEvent, +} from 'CodegenTypes'; + +import type {ViewProps} from 'ViewPropTypes'; + +export type ModuleProps = $ReadOnly<{| + ...ViewProps, + // No props +|}>; + +export const Commands = codegenNativeCommands<{ + +hotspotUpdate: (ref: React.Ref<'RCTView'>, x: Int32, y: Int32) => void; +}>(); + +export default codegenNativeComponent('Module'); +`; + +const COMMANDS_DEFINED_MULTIPLE_TIMES = ` +/** + * 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'); +const codegenNativeCommands = require('codegenNativeCommands'); + +import type { + Int32, + BubblingEvent, + DirectEvent, +} from 'CodegenTypes'; + +import type {ViewProps} from 'ViewPropTypes'; + +interface NativeCommands { + +hotspotUpdate: (x: Int32, y: Int32) => void; +} + +export type ModuleProps = $ReadOnly<{| + ...ViewProps, + // No props or events +|}>; + +export const Commands = codegenNativeCommands(); +export const Commands2 = codegenNativeCommands(); + +export default codegenNativeComponent('Module'); +`; + +const COMMANDS_DEFINED_WITHOUT_REF = ` +/** + * 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'); +const codegenNativeCommands = require('codegenNativeCommands'); + +import type { + Int32, + BubblingEvent, + DirectEvent, +} from 'CodegenTypes'; + +import type {ViewProps} from 'ViewPropTypes'; + +interface NativeCommands { + +hotspotUpdate: (x: Int32, y: Int32) => void; +} + +export type ModuleProps = $ReadOnly<{| + ...ViewProps, + // No props or events +|}>; + +export const Commands = codegenNativeCommands(); + +export default codegenNativeComponent('Module'); +`; + +module.exports = { + COMMANDS_DEFINED_INLINE, + COMMANDS_DEFINED_MULTIPLE_TIMES, + COMMANDS_DEFINED_WITHOUT_REF, +}; diff --git a/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js index eced8746b7f..9841dbda910 100644 --- a/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js @@ -621,6 +621,45 @@ export type ModuleProps = $ReadOnly<{| export default codegenNativeComponent('Module'); `; +const COMMANDS_DEFINED_WITH_ALL_TYPES = ` +/** + * 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'); +const codegenNativeCommands = require('codegenNativeCommands'); + +import type { + Int32, + BubblingEvent, + DirectEvent, +} from 'CodegenTypes'; + +import type {ViewProps} from 'ViewPropTypes'; + +interface NativeCommands { + +hotspotUpdate: (viewRef: React.Ref<'RCTView'>, x: Int32, y: Int32) => void; + +scrollTo: (viewRef: React.Ref<'RCTView'>, y: Int32, animated: boolean) => void; +} + +export type ModuleProps = $ReadOnly<{| + ...ViewProps, + // No props or events +|}>; + +export const Commands = codegenNativeCommands(); + +export default codegenNativeComponent('Module'); +`; + module.exports = { ALL_PROP_TYPES_NO_EVENTS, ARRAY_PROP_TYPES_NO_EVENTS, @@ -631,4 +670,5 @@ module.exports = { EVENTS_DEFINED_AS_NULL_IN_FILE, EVENTS_DEFINED_AS_NULL_INLINE, PROPS_AND_EVENTS_TYPES_EXPORTED, + COMMANDS_DEFINED_WITH_ALL_TYPES, }; diff --git a/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap index 7919d2ffb82..60e6d12879b 100644 --- a/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap @@ -1,11 +1,18 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`RN Codegen Flow Parser Fails with error message COMMANDS_DEFINED_INLINE 1`] = `"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias"`; + +exports[`RN Codegen Flow Parser Fails with error message COMMANDS_DEFINED_MULTIPLE_TIMES 1`] = `"codegenNativeCommands may only be called once in a file"`; + +exports[`RN Codegen Flow Parser Fails with error message COMMANDS_DEFINED_WITHOUT_REF 1`] = `"The first argument of method hotspotUpdate must be of type React.Ref<>"`; + exports[`RN Codegen Flow Parser can generate fixture ALL_PROP_TYPES_NO_EVENTS 1`] = ` Object { "modules": Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "events": Array [], "extendsProps": Array [ Object { @@ -448,6 +455,7 @@ Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "events": Array [], "extendsProps": Array [ Object { @@ -832,12 +840,78 @@ Object { } `; +exports[`RN Codegen Flow Parser can generate fixture COMMANDS_DEFINED_WITH_ALL_TYPES 1`] = ` +Object { + "modules": Object { + "Module": Object { + "components": Object { + "Module": Object { + "commands": Array [ + Object { + "name": "hotspotUpdate", + "optional": false, + "typeAnnotation": Object { + "params": Array [ + Object { + "name": "x", + "typeAnnotation": Object { + "type": "Int32TypeAnnotation", + }, + }, + Object { + "name": "y", + "typeAnnotation": Object { + "type": "Int32TypeAnnotation", + }, + }, + ], + "type": "FunctionTypeAnnotation", + }, + }, + Object { + "name": "scrollTo", + "optional": false, + "typeAnnotation": Object { + "params": Array [ + Object { + "name": "y", + "typeAnnotation": Object { + "type": "Int32TypeAnnotation", + }, + }, + Object { + "name": "animated", + "typeAnnotation": Object { + "type": "BooleanTypeAnnotation", + }, + }, + ], + "type": "FunctionTypeAnnotation", + }, + }, + ], + "events": Array [], + "extendsProps": Array [ + Object { + "knownTypeName": "ReactNativeCoreViewProps", + "type": "ReactNativeBuiltInType", + }, + ], + "props": Array [], + }, + }, + }, + }, +} +`; + exports[`RN Codegen Flow Parser can generate fixture EVENTS_DEFINED_AS_NULL_IN_FILE 1`] = ` Object { "modules": Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "events": Array [ Object { "bubblingType": "direct", @@ -956,6 +1030,7 @@ Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "events": Array [ Object { "bubblingType": "direct", @@ -1074,6 +1149,7 @@ Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "events": Array [ Object { "bubblingType": "direct", @@ -2912,6 +2988,7 @@ Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "events": Array [ Object { "bubblingType": "direct", @@ -4750,6 +4827,7 @@ Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "deprecatedViewConfigName": "DeprecateModuleName", "events": Array [], "extendsProps": Array [ @@ -4772,6 +4850,7 @@ Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "events": Array [ Object { "bubblingType": "direct", @@ -4829,6 +4908,7 @@ Object { "Module": Object { "components": Object { "Module": Object { + "commands": Array [], "events": Array [ Object { "bubblingType": "bubble", diff --git a/packages/react-native-codegen/src/parsers/flow/__tests__/parser-test.js b/packages/react-native-codegen/src/parsers/flow/__tests__/parser-test.js index 3807a1670ba..7acbc337d3b 100644 --- a/packages/react-native-codegen/src/parsers/flow/__tests__/parser-test.js +++ b/packages/react-native-codegen/src/parsers/flow/__tests__/parser-test.js @@ -13,8 +13,9 @@ const FlowParser = require('../index.js'); const fixtures = require('../__test_fixtures__/fixtures.js'); +const failureFixtures = require('../__test_fixtures__/failures.js'); jest.mock('fs', () => ({ - readFileSync: filename => fixtures[filename], + readFileSync: filename => fixtures[filename] || failureFixtures[filename], })); describe('RN Codegen Flow Parser', () => { @@ -25,4 +26,14 @@ describe('RN Codegen Flow Parser', () => { expect(FlowParser.parseFile(fixtureName)).toMatchSnapshot(); }); }); + + Object.keys(failureFixtures) + .sort() + .forEach(fixtureName => { + it(`Fails with error message ${fixtureName}`, () => { + expect(() => { + FlowParser.parseFile(fixtureName); + }).toThrowErrorMatchingSnapshot(); + }); + }); }); diff --git a/packages/react-native-codegen/src/parsers/flow/commands.js b/packages/react-native-codegen/src/parsers/flow/commands.js new file mode 100644 index 00000000000..c6688e99d47 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/flow/commands.js @@ -0,0 +1,89 @@ +/** + * 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. + * + * @flow + * @format + */ + +'use strict'; + +import type {CommandTypeShape} from '../../CodegenSchema.js'; + +type EventTypeAST = Object; + +function buildCommandSchema(property) { + const name = property.key.name; + const optional = property.optional; + const value = property.value; + + const firstParam = value.params[0].typeAnnotation; + + if ( + !( + firstParam.id.type === 'QualifiedTypeIdentifier' && + firstParam.id.qualification.name === 'React' && + firstParam.id.id.name === 'Ref' + ) + ) { + throw new Error( + `The first argument of method ${name} must be of type React.Ref<>`, + ); + } + + const params = value.params.slice(1).map(param => { + const paramName = param.name.name; + const type = + param.typeAnnotation.type === 'GenericTypeAnnotation' + ? param.typeAnnotation.id.name + : param.typeAnnotation.type; + let returnType; + + switch (type) { + case 'BooleanTypeAnnotation': + returnType = { + type: 'BooleanTypeAnnotation', + }; + break; + case 'Int32': + returnType = { + type: 'Int32TypeAnnotation', + }; + break; + default: + (type: empty); + throw new Error( + `Unsupported param type for method "${name}", param "${paramName}". Found ${type}`, + ); + } + + return { + name: paramName, + typeAnnotation: returnType, + }; + }); + + return { + name, + optional, + typeAnnotation: { + type: 'FunctionTypeAnnotation', + params, + }, + }; +} + +function getCommands( + commandTypeAST: $ReadOnlyArray, +): $ReadOnlyArray { + return commandTypeAST + .filter(property => property.type === 'ObjectTypeProperty') + .map(buildCommandSchema) + .filter(Boolean); +} + +module.exports = { + getCommands, +}; diff --git a/packages/react-native-codegen/src/parsers/flow/index.js b/packages/react-native-codegen/src/parsers/flow/index.js index bab3dd10a53..00e567a2791 100644 --- a/packages/react-native-codegen/src/parsers/flow/index.js +++ b/packages/react-native-codegen/src/parsers/flow/index.js @@ -15,6 +15,7 @@ import type {SchemaType} from '../../CodegenSchema.js'; const flowParser = require('flow-parser'); const fs = require('fs'); const {buildSchema} = require('./schema'); +const {getCommands} = require('./commands'); const {getEvents} = require('./events'); const {getProps} = require('./props'); const {getOptions} = require('./options'); @@ -23,11 +24,11 @@ const {getExtendsProps} = require('./extends'); function findConfig(ast) { const foundConfigs = []; - const allExports = ast.body.filter( + const defaultExports = ast.body.filter( node => node.type === 'ExportDefaultDeclaration', ); - allExports.forEach(statement => { + defaultExports.forEach(statement => { try { if (statement.declaration.callee.name === 'codegenNativeComponent') { const typeArgumentParams = statement.declaration.typeArguments.params; @@ -53,14 +54,98 @@ function findConfig(ast) { throw new Error('Only one component is supported per file'); } - return foundConfigs[0]; + const foundConfig = foundConfigs[0]; + + const namedExports = ast.body.filter( + node => node.type === 'ExportNamedDeclaration', + ); + + const commandsTypeNames = namedExports + .map(statement => { + let calleeName; + try { + calleeName = statement.declaration.declarations[0].init.callee.name; + } catch (e) { + // Not a function call + return; + } + + if (calleeName !== 'codegenNativeCommands') { + return; + } + + const typeArgumentParam = + statement.declaration.declarations[0].init.typeArguments.params[0]; + + if (typeArgumentParam.type !== 'GenericTypeAnnotation') { + throw new Error( + "codegenNativeCommands doesn't support inline definitions. Specify a file local type alias", + ); + } + + return typeArgumentParam.id.name; + }) + .filter(Boolean); + + if (commandsTypeNames.length > 1) { + throw new Error('codegenNativeCommands may only be called once in a file'); + } + + return { + ...foundConfig, + commandTypeName: commandsTypeNames[0], + }; } +// function findCommands(ast) { +// const foundCommands = []; +// +// const allExports = ast.body.filter( +// node => node.type === 'ExportNamedDeclaration', +// ); +// +// allExports.forEach(statement => { +// let calleeName; +// try { +// calleeName = statement.declaration.declarations[0].init.callee.name; +// } catch (e) { +// // Not a function call +// return; +// } +// +// if (calleeName !== 'codegenNativeCommands') { +// return; +// } +// +// const typeArgumentParam = +// statement.declaration.declarations[0].init.typeArguments.params[0]; +// +// if (typeArgumentParam.type !== 'GenericTypeAnnotation') { +// throw new Error( +// "codegenNativeCommands doesn't support inline definitions. Specify a file local type alias", +// ); +// } +// +// foundCommands.push(typeArgumentParam.id.name); +// }); +// +// if (foundCommands.length > 1) { +// throw new Error('codegenNativeCommands may only be called once in a file'); +// } +// +// return foundCommands[0]; +// } + function getTypes(ast) { return ast.body.reduce((types, node) => { if (node.type === 'ExportNamedDeclaration') { - types[node.declaration.id.name] = node.declaration; - } else if (node.type === 'TypeAlias') { + if (node.declaration.type !== 'VariableDeclaration') { + types[node.declaration.id.name] = node.declaration; + } + } else if ( + node.type === 'TypeAlias' || + node.type === 'InterfaceDeclaration' + ) { types[node.id.name] = node; } return types; @@ -73,7 +158,31 @@ function getPropProperties(propsTypeName, types) { return typeAlias.right.typeParameters.params[0].properties; } catch (e) { throw new Error( - `Failed find type definition for "${propsTypeName}", please check that you have a valid codegen flow file`, + `Failed to find type definition for "${propsTypeName}", please check that you have a valid codegen flow file`, + ); + } +} + +function getCommandProperties(commandTypeName, types) { + if (commandTypeName == null) { + return []; + } + + const typeAlias = types[commandTypeName]; + + if (typeAlias.type !== 'InterfaceDeclaration') { + throw new Error( + `The type argument for codegenNativeCommands must be an interface, receieved ${ + typeAlias.type + }`, + ); + } + + try { + return typeAlias.body.properties; + } catch (e) { + throw new Error( + `Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen flow file`, ); } } @@ -82,15 +191,23 @@ function processString(contents: string) { const ast = flowParser.parse(contents); const types = getTypes(ast); - const {componentName, propsTypeName, optionsExpression} = findConfig(ast); + const { + componentName, + propsTypeName, + commandTypeName, + optionsExpression, + } = findConfig(ast); + // const commandsTypeName = findCommands(ast); const propProperties = getPropProperties(propsTypeName, types); + const commandProperties = getCommandProperties(commandTypeName, types); const extendsProps = getExtendsProps(propProperties); const options = getOptions(optionsExpression); const props = getProps(propProperties); const events = getEvents(propProperties, types); + const commands = getCommands(commandProperties); return { filename: componentName, @@ -99,6 +216,7 @@ function processString(contents: string) { extendsProps, events, props, + commands, }; } diff --git a/packages/react-native-codegen/src/parsers/flow/schema.js b/packages/react-native-codegen/src/parsers/flow/schema.js index 5f2c8f0da9f..9f784a915d7 100644 --- a/packages/react-native-codegen/src/parsers/flow/schema.js +++ b/packages/react-native-codegen/src/parsers/flow/schema.js @@ -13,6 +13,7 @@ import type { EventTypeShape, PropTypeShape, + CommandTypeShape, ExtendsPropsShape, SchemaType, OptionsShape, @@ -24,6 +25,7 @@ type SchemaBuilderConfig = $ReadOnly<{| extendsProps: $ReadOnlyArray, events: $ReadOnlyArray, props: $ReadOnlyArray, + commands: $ReadOnlyArray, options?: ?OptionsShape, |}>; @@ -34,6 +36,7 @@ function buildSchema({ events, props, options, + commands, }: SchemaBuilderConfig): SchemaType { return { modules: { @@ -44,6 +47,7 @@ function buildSchema({ extendsProps, events, props, + commands, }, }, },