mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Back out "[RNCodegen] codegenNativeCommands takes list of supported commands"
Summary: Original commit changeset: 189754a567a3 Reviewed By: rickhanlonii Differential Revision: D16586885 fbshipit-source-id: 34a8f8395ca73e190ccf0242f02626094f6d87b6
This commit is contained in:
committed by
Facebook Github Bot
parent
bc2b52d22c
commit
678d5f7cd1
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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": {
|
||||
|
||||
+3
-94
@@ -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');
|
||||
`;
|
||||
@@ -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');
|
||||
`;
|
||||
@@ -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.
|
||||
@@ -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,
|
||||
|
||||
+3
-9
@@ -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');
|
||||
`;
|
||||
@@ -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');
|
||||
`;
|
||||
@@ -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');
|
||||
`;
|
||||
|
||||
-4
@@ -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"`;
|
||||
|
||||
@@ -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) {
|
||||
@@ -58,12 +58,11 @@ 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;
|
||||
}
|
||||
|
||||
@@ -71,14 +70,8 @@ function findComponentConfig(ast) {
|
||||
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(
|
||||
@@ -86,10 +79,7 @@ function findComponentConfig(ast) {
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
commandTypeName: typeArgumentParam.id.name,
|
||||
commandOptionsExpression: callExpression.arguments[0],
|
||||
};
|
||||
return typeArgumentParam.id.name;
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
@@ -99,8 +89,7 @@ function findComponentConfig(ast) {
|
||||
|
||||
return {
|
||||
...foundConfig,
|
||||
commandTypeName: commandsTypeNames[0]?.commandTypeName,
|
||||
commandOptionsExpression: commandsTypeNames[0]?.commandOptionsExpression,
|
||||
commandTypeName: commandsTypeNames[0],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -115,7 +104,7 @@ function getPropProperties(propsTypeName, types) {
|
||||
}
|
||||
}
|
||||
|
||||
function getCommandProperties(commandTypeName, types, commandOptions) {
|
||||
function getCommandProperties(commandTypeName, types) {
|
||||
if (commandTypeName == null) {
|
||||
return [];
|
||||
}
|
||||
@@ -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
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
@@ -75,6 +44,5 @@ function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getCommandOptions,
|
||||
getOptions,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user