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
This commit is contained in:
Ramanpreet Nara
2021-01-26 17:07:00 -08:00
committed by Facebook GitHub Bot
parent 6506f7a680
commit 19ef415895
2 changed files with 23 additions and 5 deletions
@@ -28,6 +28,7 @@ export type Pojo = {
name: string,
namespace: string,
properties: $ReadOnlyArray<PojoProperty>,
isRoot: boolean,
};
export type PojoProperty = NamedShape<PojoTypeAnnotation>;
@@ -93,14 +94,14 @@ export type PojoTypeAnnotation =
class PojoCollector {
_pojos: Map<string, Pojo> = 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<PropTypeAnnotation>,
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<PropTypeAnnotation>,
) {
this._insertPojo(namespace, pojoName, objectTypeAnnotation, true);
}
getAllPojos(): $ReadOnlyArray<Pojo> {
return [...this._pojos.values()];
}
@@ -55,7 +55,7 @@ module.exports = {
const {props} = component;
pojoCollector.process(
pojoCollector.processPojo(
capitalize(hasteModuleName),
`${capitalize(componentName)}Props`,
{