From 19ef41589534e9046ec60366f4fdc09be0816e6f Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Tue, 26 Jan 2021 17:03:49 -0800 Subject: [PATCH] Distinguish root Pojos from non-root Pojos Summary: The Pojo data structure now keeps a flag `isRoot`, so that we can customize code-generation for the Props pojo vs its helper pojos. In the following diff (i.e: D26041103), we'll add a DoNotStrip annotation to the props pojo. That doesn't need to exist on the helper pojos, because they'll all be referenced in the root props pojo. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D26041102 fbshipit-source-id: ae2e1ba346d038853b5ff30cd5524a809ef97053 --- .../GeneratePropsJavaPojo/PojoCollector.js | 26 ++++++++++++++++--- .../components/GeneratePropsJavaPojo/index.js | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/PojoCollector.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/PojoCollector.js index ab75b7f3ff5..767b96157f3 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/PojoCollector.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/PojoCollector.js @@ -28,6 +28,7 @@ export type Pojo = { name: string, namespace: string, properties: $ReadOnlyArray, + isRoot: boolean, }; export type PojoProperty = NamedShape; @@ -93,14 +94,14 @@ export type PojoTypeAnnotation = class PojoCollector { _pojos: Map = new Map(); - process( + _process( namespace: string, pojoName: string, typeAnnotation: PropTypeAnnotation, ): PojoTypeAnnotation { switch (typeAnnotation.type) { case 'ObjectTypeAnnotation': { - this._insertPojo(namespace, pojoName, typeAnnotation); + this._insertPojo(namespace, pojoName, typeAnnotation, false); return { type: 'PojoTypeAliasTypeAnnotation', name: pojoName, @@ -117,7 +118,12 @@ class PojoCollector { const pojoElementType = (() => { switch (elementType.type) { case 'ObjectTypeAnnotation': { - this._insertPojo(namespace, `${pojoName}Element`, elementType); + this._insertPojo( + namespace, + `${pojoName}Element`, + elementType, + false, + ); return { type: 'PojoTypeAliasTypeAnnotation', name: `${pojoName}Element`, @@ -129,6 +135,7 @@ class PojoCollector { namespace, `${pojoName}ElementElement`, objectTypeAnnotation, + false, ); return { type: 'ArrayTypeAnnotation', @@ -153,17 +160,19 @@ class PojoCollector { return typeAnnotation; } } + _insertPojo( namespace: string, pojoName: string, objectTypeAnnotation: ObjectTypeAnnotation, + isRoot: boolean, ) { const properties = objectTypeAnnotation.properties.map(property => { const propertyPojoName = pojoName + capitalize(property.name); return { ...property, - typeAnnotation: this.process( + typeAnnotation: this._process( namespace, propertyPojoName, property.typeAnnotation, @@ -173,11 +182,20 @@ class PojoCollector { this._pojos.set(pojoName, { name: pojoName, + isRoot, namespace, properties, }); } + processPojo( + namespace: string, + pojoName: string, + objectTypeAnnotation: ObjectTypeAnnotation, + ) { + this._insertPojo(namespace, pojoName, objectTypeAnnotation, true); + } + getAllPojos(): $ReadOnlyArray { return [...this._pojos.values()]; } diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/index.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/index.js index f5fa8c77f01..f24a6bb827e 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/index.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/index.js @@ -55,7 +55,7 @@ module.exports = { const {props} = component; - pojoCollector.process( + pojoCollector.processPojo( capitalize(hasteModuleName), `${capitalize(componentName)}Props`, {