Skip to content

Commit

Permalink
Back out "[RNCodegen] codegenNativeCommands takes list of supported c…
Browse files Browse the repository at this point in the history
…ommands"

Summary: Original commit changeset: 189754a567a3

Reviewed By: rickhanlonii

Differential Revision: D16586885

fbshipit-source-id: 34a8f8395ca73e190ccf0242f02626094f6d87b6
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Jul 31, 2019
1 parent bc2b52d commit 678d5f7
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 202 deletions.
6 changes: 1 addition & 5 deletions Libraries/Utilities/codegenNativeCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@

'use strict';

type Options<T = string> = $ReadOnly<{|
supportedCommands: $ReadOnlyArray<T>,
|}>;

function codegenNativeCommands<T>(options: Options<$Keys<T>>): T {
function codegenNativeCommands<T>(): T {
return (({}: any): T);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ type ModuleProps = $ReadOnly<{|
onBubblingEventDefinedInlineNull: BubblingEventHandler<null>,
|}>;
export const Commands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['hotspotUpdate', 'scrollTo'],
});
export const Commands = codegenNativeCommands<NativeCommands>();
export default codegenNativeComponent<ModuleProps>('Module', {
interfaceOnly: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-inline-view-configs/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.4",
"version": "0.0.3",
"name": "babel-plugin-inline-view-configs",
"description": "Babel plugin to inline view configs for React Native",
"repository": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export type ModuleProps = $ReadOnly<{|
export const Commands = codegenNativeCommands<{
+hotspotUpdate: (ref: React.Ref<'RCTView'>, x: Int32, y: Int32) => void;
}>({
supportedCommands: ['hotspotUpdate']
});
}>();
export default codegenNativeComponent<ModuleProps>('Module');
`;
Expand Down Expand Up @@ -81,12 +79,8 @@ export type ModuleProps = $ReadOnly<{|
// No props or events
|}>;
export const Commands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['hotspotUpdate']
});
export const Commands2 = codegenNativeCommands<NativeCommands>({
supportedCommands: ['hotspotUpdate']
});
export const Commands = codegenNativeCommands<NativeCommands>();
export const Commands2 = codegenNativeCommands<NativeCommands>();
export default codegenNativeComponent<ModuleProps>('Module');
`;
Expand Down Expand Up @@ -124,93 +118,10 @@ export type ModuleProps = $ReadOnly<{|
// No props or events
|}>;
export const Commands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['hotspotUpdate']
});
export default codegenNativeComponent<ModuleProps>('Module');
`;

const COMMANDS_DEFINED_WITH_MISMATCHED_METHOD_NAMES = `
/**
* 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,
BubblingEventHandler,
DirectEventHandler,
} 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<NativeCommands>({
supportedCommands: ['scrollTo']
});
export default codegenNativeComponent<ModuleProps>('Module');
`;

const COMMANDS_DEFINED_WITHOUT_METHOD_NAMES = `
/**
* 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,
BubblingEventHandler,
DirectEventHandler,
} 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<NativeCommands>();
export default codegenNativeComponent<ModuleProps>('Module');
`;

const NULLABLE_WITH_DEFAULT = `
/**
* Copyright (c) Facebook, Inc. and its affiliates.
Expand Down Expand Up @@ -275,8 +186,6 @@ export default codegenNativeComponent<ModuleProps>('Module');
module.exports = {
COMMANDS_DEFINED_INLINE,
COMMANDS_DEFINED_MULTIPLE_TIMES,
COMMANDS_DEFINED_WITH_MISMATCHED_METHOD_NAMES,
COMMANDS_DEFINED_WITHOUT_METHOD_NAMES,
COMMANDS_DEFINED_WITHOUT_REF,
NULLABLE_WITH_DEFAULT,
NON_OPTIONAL_KEY_WITH_DEFAULT_VALUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,7 @@ export type ModuleProps = $ReadOnly<{|
// No props or events
|}>;
export const Commands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['hotspotUpdate', 'scrollTo']
});
export const Commands = codegenNativeCommands<NativeCommands>();
export default codegenNativeComponent<ModuleProps>('Module');
`;
Expand Down Expand Up @@ -605,9 +603,7 @@ export type ModuleProps = $ReadOnly<{|
// No props or events
|}>;
export const Commands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['scrollTo']
});
export const Commands = codegenNativeCommands<NativeCommands>();
export default codegenNativeComponent<ModuleProps>('Module');
`;
Expand Down Expand Up @@ -660,9 +656,7 @@ export type ModuleProps = $ReadOnly<{|
onDirectEventDefinedInlineWithPaperName: DirectEventHandler<EventInFile, 'paperDirectEventDefinedInlineWithPaperName'>,
|}>;
export const Commands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['scrollTo']
});
export const Commands = codegenNativeCommands<NativeCommands>();
export default codegenNativeComponent<ModuleProps>('Module');
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ exports[`RN Codegen Flow Parser Fails with error message COMMANDS_DEFINED_INLINE

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_WITH_MISMATCHED_METHOD_NAMES 1`] = `"codegenNativeCommands expected the same supportedCommands specified in the NativeCommands interface: hotspotUpdate, scrollTo"`;

exports[`RN Codegen Flow Parser Fails with error message COMMANDS_DEFINED_WITHOUT_METHOD_NAMES 1`] = `"codegenNativeCommands must be passed options including the supported commands"`;

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 Fails with error message NON_OPTIONAL_KEY_WITH_DEFAULT_VALUE 1`] = `"key required_key_with_default must be optional if used with WithDefault<> annotation"`;
Expand Down
64 changes: 10 additions & 54 deletions packages/react-native-codegen/src/parsers/flow/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {ComponentSchemaBuilderConfig} from './schema.js';
const {getCommands} = require('./commands');
const {getEvents} = require('./events');
const {getProps} = require('./props');
const {getCommandOptions, getOptions} = require('./options');
const {getOptions} = require('./options');
const {getExtendsProps} = require('./extends');

function findComponentConfig(ast) {
Expand Down Expand Up @@ -58,38 +58,28 @@ function findComponentConfig(ast) {

const commandsTypeNames = namedExports
.map(statement => {
let callExpression;
let calleeName;
try {
callExpression = statement.declaration.declarations[0].init;
calleeName = callExpression.callee.name;
calleeName = statement.declaration.declarations[0].init.callee.name;
} catch (e) {
// Not a function call
return;
}

if (calleeName !== 'codegenNativeCommands') {
return;
}

// const statement.declaration.declarations[0].init
if (callExpression.arguments.length !== 1) {
throw new Error(
'codegenNativeCommands must be passed options including the supported commands',
);
}

const typeArgumentParam = callExpression.typeArguments.params[0];
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 {
commandTypeName: typeArgumentParam.id.name,
commandOptionsExpression: callExpression.arguments[0],
};
return typeArgumentParam.id.name;
})
.filter(Boolean);

Expand All @@ -99,8 +89,7 @@ function findComponentConfig(ast) {

return {
...foundConfig,
commandTypeName: commandsTypeNames[0]?.commandTypeName,
commandOptionsExpression: commandsTypeNames[0]?.commandOptionsExpression,
commandTypeName: commandsTypeNames[0],
};
}

Expand All @@ -115,7 +104,7 @@ function getPropProperties(propsTypeName, types) {
}
}

function getCommandProperties(commandTypeName, types, commandOptions) {
function getCommandProperties(commandTypeName, types) {
if (commandTypeName == null) {
return [];
}
Expand All @@ -130,39 +119,13 @@ function getCommandProperties(commandTypeName, types, commandOptions) {
);
}

let properties;
try {
properties = typeAlias.body.properties;
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`,
);
}

const flowPropertyNames = properties
.map(property => property?.key?.name)
.filter(Boolean);

if (commandOptions == null || commandOptions.supportedCommands == null) {
throw new Error(
'codegenNativeCommands must be given an options object with supportedCommands array',
);
}

if (
commandOptions.supportedCommands.length !== flowPropertyNames.length ||
!commandOptions.supportedCommands.every(supportedCommand =>
flowPropertyNames.includes(supportedCommand),
)
) {
throw new Error(
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${flowPropertyNames.join(
', ',
)}`,
);
}

return properties;
}

// $FlowFixMe there's no flowtype for AST
Expand All @@ -171,18 +134,11 @@ function processComponent(ast, types): ComponentSchemaBuilderConfig {
componentName,
propsTypeName,
commandTypeName,
commandOptionsExpression,
optionsExpression,
} = findComponentConfig(ast);

const propProperties = getPropProperties(propsTypeName, types);
const commandOptions = getCommandOptions(commandOptionsExpression);

const commandProperties = getCommandProperties(
commandTypeName,
types,
commandOptions,
);
const commandProperties = getCommandProperties(commandTypeName, types);

const extendsProps = getExtendsProps(propProperties);
const options = getOptions(optionsExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,6 @@ import type {OptionsShape} from '../../../CodegenSchema.js';
// $FlowFixMe there's no flowtype for ASTs
type OptionsAST = Object;

export type CommandOptions = $ReadOnly<{|
supportedCommands: $ReadOnlyArray<string>,
|}>;

function getCommandOptions(
commandOptionsExpression: OptionsAST,
): ?CommandOptions {
if (commandOptionsExpression == null) {
return null;
}

let foundOptions;
try {
foundOptions = commandOptionsExpression.properties.reduce(
(options, prop) => {
options[prop.key.name] = (prop?.value?.elements || []).map(
element => element?.value,
);
return options;
},
{},
);
} catch (e) {
throw new Error(
'Failed to parse command options, please check that they are defined correctly',
);
}

return foundOptions;
}

function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
if (!optionsExpression) {
return null;
Expand Down Expand Up @@ -75,6 +44,5 @@ function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
}

module.exports = {
getCommandOptions,
getOptions,
};

0 comments on commit 678d5f7

Please sign in to comment.