Skip to content

Commit 27ddbd8

Browse files
RSNarafacebook-github-bot
authored andcommitted
Refactor: Simplify deprecated component code-gen logic in View Config generator
Summary: **Motivation:** Readability. It was hard to tell how componentName, paperComponentName, and paperComponentNameDepreacted produced the NativeComponentRegistry.get call. Changelog: [Internal] Reviewed By: philIip Differential Revision: D32108276 fbshipit-source-id: ea7c9fe4dc50cdd6fec94b5cd25f7bbcfb451ef6
1 parent 25d4cb9 commit 27ddbd8

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js

+33-28
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,44 @@ function getReactDiffProcessValue(typeAnnotation) {
103103
}
104104

105105
const ComponentTemplate = ({
106-
componentNameWithCompatSupport,
107-
deprecationCheck,
106+
componentName,
107+
paperComponentName,
108+
paperComponentNameDeprecated,
108109
}: {
109-
componentNameWithCompatSupport: string,
110-
deprecationCheck: string,
111-
}) =>
112-
`
113-
let nativeComponentName = '${componentNameWithCompatSupport}';
114-
${deprecationCheck}
110+
componentName: string,
111+
paperComponentName: ?string,
112+
paperComponentNameDeprecated: ?string,
113+
}) => {
114+
const nativeComponentName = paperComponentName ?? componentName;
115+
116+
return `
117+
let nativeComponentName = '${nativeComponentName}';
118+
${
119+
paperComponentNameDeprecated != null
120+
? DeprecatedComponentNameCheckTemplate({
121+
componentName,
122+
paperComponentNameDeprecated,
123+
})
124+
: ''
125+
}
115126
export default NativeComponentRegistry.get(nativeComponentName, () => VIEW_CONFIG);
116127
`.trim();
128+
};
117129

118-
const DeprecatedComponentTemplate = ({
130+
const DeprecatedComponentNameCheckTemplate = ({
119131
componentName,
120-
componentNameDeprecated,
132+
paperComponentNameDeprecated,
121133
}: {
122134
componentName: string,
123-
componentNameDeprecated: string,
135+
paperComponentNameDeprecated: string,
124136
}) =>
125137
`
126138
if (UIManager.getViewManagerConfig('${componentName}')) {
127139
nativeComponentName = '${componentName}';
128-
} else if (UIManager.getViewManagerConfig('${componentNameDeprecated}')) {
129-
nativeComponentName = '${componentNameDeprecated}';
140+
} else if (UIManager.getViewManagerConfig('${paperComponentNameDeprecated}')) {
141+
nativeComponentName = '${paperComponentNameDeprecated}';
130142
} else {
131-
throw new Error('Failed to find native component for either "${componentName}" or "${componentNameDeprecated}"');
143+
throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}"');
132144
}
133145
`.trim();
134146

@@ -365,29 +377,22 @@ module.exports = {
365377
.map((componentName: string) => {
366378
const component = components[componentName];
367379

368-
const paperComponentName = component.paperComponentName
369-
? component.paperComponentName
370-
: componentName;
371-
372380
if (component.paperComponentNameDeprecated) {
373381
imports.add(UIMANAGER_IMPORT);
374382
}
375383

376-
const deprecatedCheckBlock = component.paperComponentNameDeprecated
377-
? DeprecatedComponentTemplate({
378-
componentName,
379-
componentNameDeprecated:
380-
component.paperComponentNameDeprecated || '',
381-
})
382-
: '';
383-
384384
const replacedTemplate = ComponentTemplate({
385-
componentNameWithCompatSupport: paperComponentName,
386-
deprecationCheck: deprecatedCheckBlock,
385+
componentName,
386+
paperComponentName: component.paperComponentName,
387+
paperComponentNameDeprecated:
388+
component.paperComponentNameDeprecated,
387389
});
388390

389391
const replacedSourceRoot = j.withParser('flow')(replacedTemplate);
390392

393+
const paperComponentName =
394+
component.paperComponentName ?? componentName;
395+
391396
replacedSourceRoot
392397
.find(j.Identifier, {
393398
name: 'VIEW_CONFIG',

0 commit comments

Comments
 (0)