From ed61a3b5f9587a487b4f78520e499c54bbd0e676 Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Thu, 16 May 2019 10:45:54 -0700 Subject: [PATCH] Handle component name Summary: In order to generate the view configs, we need to know the name of the component used in: ``` ReactNativeViewConfigRegistry.register( 'RCTNativeComponent', // <------- this name () => BooleanPropNativeComponentViewConfig, ); ``` For this, we'll use `component.name` in the schema (see fixture updates). Doing this would break the native code we generate though, since that code has the RCT stripped. So this diff adds support to mirror the native stripping of 'RCT' for generated native code Reviewed By: TheSavior Differential Revision: D15320422 fbshipit-source-id: be1ab9964078df2c7bc6e41462776f00b94b104f --- .../react-native-codegen/src/CodegenSchema.js | 1 + .../src/generators/GenerateViewConfigJs.js | 22 ++++++++++++++----- .../generators/__test_fixtures__/fixtures.js | 1 + .../GenerateViewConfigJs-test.js.snap | 2 +- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index dff76dae367..1ab45de9789 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -92,6 +92,7 @@ export type EventTypeShape = $ReadOnly<{| export type ComponentShape = $ReadOnly<{| interfaceOnly?: boolean, + isDeprecatedPaperComponentNameRCT?: boolean, extendsProps: $ReadOnlyArray<{| type: 'ReactNativeBuiltInType', knownTypeName: 'ReactNativeCoreViewProps', diff --git a/packages/react-native-codegen/src/generators/GenerateViewConfigJs.js b/packages/react-native-codegen/src/generators/GenerateViewConfigJs.js index 27763f726cf..5de60a02c88 100644 --- a/packages/react-native-codegen/src/generators/GenerateViewConfigJs.js +++ b/packages/react-native-codegen/src/generators/GenerateViewConfigJs.js @@ -71,7 +71,7 @@ const ::_COMPONENT_NAME_::ViewConfig = VIEW_CONFIG; verifyComponentAttributeEquivalence('::_COMPONENT_NAME_::', ::_COMPONENT_NAME_::ViewConfig); ReactNativeViewConfigRegistry.register( - '::_COMPONENT_NAME_::', + '::_COMPONENT_NAME_WITH_COMPAT_SUPPORT_::',::_COMPAT_COMMENT_:: () => ::_COMPONENT_NAME_::ViewConfig, ); @@ -265,10 +265,22 @@ module.exports = { .map(componentName => { const component = components[componentName]; - const replacedTemplate = componentTemplate.replace( - /::_COMPONENT_NAME_::/g, - componentName, - ); + const compatabilityComponentName = `${ + component.isDeprecatedPaperComponentNameRCT ? 'RCT' : '' + }${componentName}`; + + const replacedTemplate = componentTemplate + .replace(/::_COMPONENT_NAME_::/g, componentName) + .replace( + /::_COMPONENT_NAME_WITH_COMPAT_SUPPORT_::/g, + compatabilityComponentName, + ) + .replace( + /::_COMPAT_COMMENT_::/g, + component.isDeprecatedPaperComponentNameRCT + ? ' // RCT prefix present for paper support' + : '', + ); const replacedSource: string = j .withParser('flow')(replacedTemplate) 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 669c637ccd2..4bb696833cf 100644 --- a/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js @@ -18,6 +18,7 @@ const INTERFACE_ONLY: SchemaType = { components: { InterfaceOnlyComponent: { interfaceOnly: true, + isDeprecatedPaperComponentNameRCT: true, extendsProps: [ { type: 'ReactNativeBuiltInType', diff --git a/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap b/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap index 9508f8f515a..19f8cdee355 100644 --- a/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap +++ b/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap @@ -523,7 +523,7 @@ const InterfaceOnlyComponentViewConfig = { verifyComponentAttributeEquivalence('InterfaceOnlyComponent', InterfaceOnlyComponentViewConfig); ReactNativeViewConfigRegistry.register( - 'InterfaceOnlyComponent', + 'RCTInterfaceOnlyComponent', // RCT prefix present for paper support () => InterfaceOnlyComponentViewConfig, );