@react-native/codegen 0.81.0-nightly-20250526-5dbe32ee1 → 0.81.0-nightly-20250528-8f2e981ab
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/generators/components/GeneratePropsCpp.js +89 -3
- package/lib/generators/components/GeneratePropsCpp.js.flow +95 -2
- package/lib/generators/components/GeneratePropsH.js +4 -0
- package/lib/generators/components/GeneratePropsH.js.flow +4 -0
- package/lib/generators/modules/GenerateModuleJniH.js +1 -9
- package/lib/generators/modules/GenerateModuleJniH.js.flow +1 -9
- package/package.json +1 -1
|
@@ -42,16 +42,96 @@ ${componentClasses}
|
|
|
42
42
|
|
|
43
43
|
} // namespace facebook::react
|
|
44
44
|
`;
|
|
45
|
-
const ComponentTemplate = ({className, extendClasses, props}) =>
|
|
45
|
+
const ComponentTemplate = ({className, extendClasses, props, diffProps}) =>
|
|
46
46
|
`
|
|
47
47
|
${className}::${className}(
|
|
48
48
|
const PropsParserContext &context,
|
|
49
49
|
const ${className} &sourceProps,
|
|
50
50
|
const RawProps &rawProps):${extendClasses}
|
|
51
51
|
|
|
52
|
-
${props}
|
|
53
|
-
|
|
52
|
+
${props} {}
|
|
53
|
+
${diffProps}
|
|
54
|
+
|
|
54
55
|
`.trim();
|
|
56
|
+
function generatePropsDiffString(className, componentName, component) {
|
|
57
|
+
const diffProps = component.props
|
|
58
|
+
.map(prop => {
|
|
59
|
+
const typeAnnotation = prop.typeAnnotation;
|
|
60
|
+
switch (typeAnnotation.type) {
|
|
61
|
+
case 'StringTypeAnnotation':
|
|
62
|
+
case 'Int32TypeAnnotation':
|
|
63
|
+
case 'BooleanTypeAnnotation':
|
|
64
|
+
return `
|
|
65
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
66
|
+
result["${prop.name}"] = ${prop.name};
|
|
67
|
+
}`;
|
|
68
|
+
case 'DoubleTypeAnnotation':
|
|
69
|
+
case 'FloatTypeAnnotation':
|
|
70
|
+
return `
|
|
71
|
+
if ((${prop.name} != oldProps->${prop.name}) && !(std::isnan(${prop.name}) && std::isnan(oldProps->${prop.name}))) {
|
|
72
|
+
result["${prop.name}"] = ${prop.name};
|
|
73
|
+
}`;
|
|
74
|
+
case 'ReservedPropTypeAnnotation':
|
|
75
|
+
switch (typeAnnotation.name) {
|
|
76
|
+
case 'ColorPrimitive':
|
|
77
|
+
return `
|
|
78
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
79
|
+
result["${prop.name}"] = *${prop.name};
|
|
80
|
+
}`;
|
|
81
|
+
case 'ImageSourcePrimitive':
|
|
82
|
+
return `
|
|
83
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
84
|
+
result["${prop.name}"] = ${prop.name}.toDynamic();
|
|
85
|
+
}`;
|
|
86
|
+
case 'ImageRequestPrimitive':
|
|
87
|
+
// Shouldn't be used in props
|
|
88
|
+
throw new Error(
|
|
89
|
+
'ImageRequestPrimitive should not be used in Props',
|
|
90
|
+
);
|
|
91
|
+
case 'PointPrimitive':
|
|
92
|
+
return `
|
|
93
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
94
|
+
folly::dynamic pointResult = folly::dynamic::object();
|
|
95
|
+
pointResult["x"] = ${prop.name}.x;
|
|
96
|
+
pointResult["y"] = ${prop.name}.y;
|
|
97
|
+
result["${prop.name}"] = pointResult;
|
|
98
|
+
}`;
|
|
99
|
+
case 'EdgeInsetsPrimitive':
|
|
100
|
+
case 'DimensionPrimitive':
|
|
101
|
+
// TODO: Implement diffProps for complex types
|
|
102
|
+
return '';
|
|
103
|
+
default:
|
|
104
|
+
typeAnnotation.name;
|
|
105
|
+
throw new Error('Received unknown ReservedPropTypeAnnotation');
|
|
106
|
+
}
|
|
107
|
+
case 'ArrayTypeAnnotation':
|
|
108
|
+
case 'ObjectTypeAnnotation':
|
|
109
|
+
case 'StringEnumTypeAnnotation':
|
|
110
|
+
case 'Int32EnumTypeAnnotation':
|
|
111
|
+
case 'MixedTypeAnnotation':
|
|
112
|
+
default:
|
|
113
|
+
// TODO: Implement diffProps for complex types
|
|
114
|
+
return '';
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
.join('\n' + ' ');
|
|
118
|
+
return `
|
|
119
|
+
#ifdef RN_SERIALIZABLE_STATE
|
|
120
|
+
folly::dynamic ${className}::getDiffProps(
|
|
121
|
+
const Props* prevProps) const {
|
|
122
|
+
static const auto defaultProps = ${className}();
|
|
123
|
+
const ${className}* oldProps = prevProps == nullptr
|
|
124
|
+
? &defaultProps
|
|
125
|
+
: static_cast<const ${className}*>(prevProps);
|
|
126
|
+
if (this == oldProps) {
|
|
127
|
+
return folly::dynamic::object();
|
|
128
|
+
}
|
|
129
|
+
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
|
|
130
|
+
${diffProps}
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
#endif`;
|
|
134
|
+
}
|
|
55
135
|
function generatePropsString(componentName, component) {
|
|
56
136
|
return component.props
|
|
57
137
|
.map(prop => {
|
|
@@ -119,6 +199,11 @@ module.exports = {
|
|
|
119
199
|
const newName = `${componentName}Props`;
|
|
120
200
|
const propsString = generatePropsString(componentName, component);
|
|
121
201
|
const extendString = getClassExtendString(component);
|
|
202
|
+
const diffPropsString = generatePropsDiffString(
|
|
203
|
+
newName,
|
|
204
|
+
componentName,
|
|
205
|
+
component,
|
|
206
|
+
);
|
|
122
207
|
const imports = getImports(component.props);
|
|
123
208
|
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
|
|
124
209
|
imports.forEach(allImports.add, allImports);
|
|
@@ -126,6 +211,7 @@ module.exports = {
|
|
|
126
211
|
className: newName,
|
|
127
212
|
extendClasses: extendString,
|
|
128
213
|
props: propsString,
|
|
214
|
+
diffProps: diffPropsString,
|
|
129
215
|
});
|
|
130
216
|
return replacedTemplate;
|
|
131
217
|
})
|
|
@@ -55,10 +55,12 @@ const ComponentTemplate = ({
|
|
|
55
55
|
className,
|
|
56
56
|
extendClasses,
|
|
57
57
|
props,
|
|
58
|
+
diffProps,
|
|
58
59
|
}: {
|
|
59
60
|
className: string,
|
|
60
61
|
extendClasses: string,
|
|
61
62
|
props: string,
|
|
63
|
+
diffProps: string,
|
|
62
64
|
}) =>
|
|
63
65
|
`
|
|
64
66
|
${className}::${className}(
|
|
@@ -66,10 +68,95 @@ ${className}::${className}(
|
|
|
66
68
|
const ${className} &sourceProps,
|
|
67
69
|
const RawProps &rawProps):${extendClasses}
|
|
68
70
|
|
|
69
|
-
${props}
|
|
70
|
-
|
|
71
|
+
${props} {}
|
|
72
|
+
${diffProps}
|
|
73
|
+
|
|
71
74
|
`.trim();
|
|
72
75
|
|
|
76
|
+
function generatePropsDiffString(
|
|
77
|
+
className: string,
|
|
78
|
+
componentName: string,
|
|
79
|
+
component: ComponentShape,
|
|
80
|
+
) {
|
|
81
|
+
const diffProps = component.props
|
|
82
|
+
.map(prop => {
|
|
83
|
+
const typeAnnotation = prop.typeAnnotation;
|
|
84
|
+
switch (typeAnnotation.type) {
|
|
85
|
+
case 'StringTypeAnnotation':
|
|
86
|
+
case 'Int32TypeAnnotation':
|
|
87
|
+
case 'BooleanTypeAnnotation':
|
|
88
|
+
return `
|
|
89
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
90
|
+
result["${prop.name}"] = ${prop.name};
|
|
91
|
+
}`;
|
|
92
|
+
case 'DoubleTypeAnnotation':
|
|
93
|
+
case 'FloatTypeAnnotation':
|
|
94
|
+
return `
|
|
95
|
+
if ((${prop.name} != oldProps->${prop.name}) && !(std::isnan(${prop.name}) && std::isnan(oldProps->${prop.name}))) {
|
|
96
|
+
result["${prop.name}"] = ${prop.name};
|
|
97
|
+
}`;
|
|
98
|
+
case 'ReservedPropTypeAnnotation':
|
|
99
|
+
switch (typeAnnotation.name) {
|
|
100
|
+
case 'ColorPrimitive':
|
|
101
|
+
return `
|
|
102
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
103
|
+
result["${prop.name}"] = *${prop.name};
|
|
104
|
+
}`;
|
|
105
|
+
case 'ImageSourcePrimitive':
|
|
106
|
+
return `
|
|
107
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
108
|
+
result["${prop.name}"] = ${prop.name}.toDynamic();
|
|
109
|
+
}`;
|
|
110
|
+
case 'ImageRequestPrimitive':
|
|
111
|
+
// Shouldn't be used in props
|
|
112
|
+
throw new Error(
|
|
113
|
+
'ImageRequestPrimitive should not be used in Props',
|
|
114
|
+
);
|
|
115
|
+
case 'PointPrimitive':
|
|
116
|
+
return `
|
|
117
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
118
|
+
folly::dynamic pointResult = folly::dynamic::object();
|
|
119
|
+
pointResult["x"] = ${prop.name}.x;
|
|
120
|
+
pointResult["y"] = ${prop.name}.y;
|
|
121
|
+
result["${prop.name}"] = pointResult;
|
|
122
|
+
}`;
|
|
123
|
+
case 'EdgeInsetsPrimitive':
|
|
124
|
+
case 'DimensionPrimitive':
|
|
125
|
+
// TODO: Implement diffProps for complex types
|
|
126
|
+
return '';
|
|
127
|
+
default:
|
|
128
|
+
(typeAnnotation.name: empty);
|
|
129
|
+
throw new Error('Received unknown ReservedPropTypeAnnotation');
|
|
130
|
+
}
|
|
131
|
+
case 'ArrayTypeAnnotation':
|
|
132
|
+
case 'ObjectTypeAnnotation':
|
|
133
|
+
case 'StringEnumTypeAnnotation':
|
|
134
|
+
case 'Int32EnumTypeAnnotation':
|
|
135
|
+
case 'MixedTypeAnnotation':
|
|
136
|
+
default:
|
|
137
|
+
// TODO: Implement diffProps for complex types
|
|
138
|
+
return '';
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
.join('\n' + ' ');
|
|
142
|
+
|
|
143
|
+
return `
|
|
144
|
+
#ifdef RN_SERIALIZABLE_STATE
|
|
145
|
+
folly::dynamic ${className}::getDiffProps(
|
|
146
|
+
const Props* prevProps) const {
|
|
147
|
+
static const auto defaultProps = ${className}();
|
|
148
|
+
const ${className}* oldProps = prevProps == nullptr
|
|
149
|
+
? &defaultProps
|
|
150
|
+
: static_cast<const ${className}*>(prevProps);
|
|
151
|
+
if (this == oldProps) {
|
|
152
|
+
return folly::dynamic::object();
|
|
153
|
+
}
|
|
154
|
+
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
|
|
155
|
+
${diffProps}
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
#endif`;
|
|
159
|
+
}
|
|
73
160
|
function generatePropsString(componentName: string, component: ComponentShape) {
|
|
74
161
|
return component.props
|
|
75
162
|
.map(prop => {
|
|
@@ -144,6 +231,11 @@ module.exports = {
|
|
|
144
231
|
|
|
145
232
|
const propsString = generatePropsString(componentName, component);
|
|
146
233
|
const extendString = getClassExtendString(component);
|
|
234
|
+
const diffPropsString = generatePropsDiffString(
|
|
235
|
+
newName,
|
|
236
|
+
componentName,
|
|
237
|
+
component,
|
|
238
|
+
);
|
|
147
239
|
|
|
148
240
|
const imports = getImports(component.props);
|
|
149
241
|
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
|
|
@@ -153,6 +245,7 @@ module.exports = {
|
|
|
153
245
|
className: newName,
|
|
154
246
|
extendClasses: extendString,
|
|
155
247
|
props: propsString,
|
|
248
|
+
diffProps: diffPropsString,
|
|
156
249
|
});
|
|
157
250
|
|
|
158
251
|
return replacedTemplate;
|
|
@@ -55,6 +55,10 @@ class ${className} final${extendClasses} {
|
|
|
55
55
|
#pragma mark - Props
|
|
56
56
|
|
|
57
57
|
${props}
|
|
58
|
+
|
|
59
|
+
#ifdef RN_SERIALIZABLE_STATE
|
|
60
|
+
folly::dynamic getDiffProps(const Props* prevProps) const override;
|
|
61
|
+
#endif
|
|
58
62
|
};
|
|
59
63
|
`.trim();
|
|
60
64
|
const EnumTemplate = ({enumName, values, fromCases, toCases}) =>
|
|
@@ -82,15 +82,7 @@ target_link_libraries(
|
|
|
82
82
|
}
|
|
83
83
|
)
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
react_codegen_${libraryName}
|
|
87
|
-
PRIVATE
|
|
88
|
-
-DLOG_TAG=\\"ReactNative\\"
|
|
89
|
-
-fexceptions
|
|
90
|
-
-frtti
|
|
91
|
-
-std=c++20
|
|
92
|
-
-Wall
|
|
93
|
-
)
|
|
85
|
+
target_compile_reactnative_options(react_codegen_${libraryName} PRIVATE)
|
|
94
86
|
`;
|
|
95
87
|
};
|
|
96
88
|
module.exports = {
|
|
@@ -95,15 +95,7 @@ target_link_libraries(
|
|
|
95
95
|
}
|
|
96
96
|
)
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
react_codegen_${libraryName}
|
|
100
|
-
PRIVATE
|
|
101
|
-
-DLOG_TAG=\\"ReactNative\\"
|
|
102
|
-
-fexceptions
|
|
103
|
-
-frtti
|
|
104
|
-
-std=c++20
|
|
105
|
-
-Wall
|
|
106
|
-
)
|
|
98
|
+
target_compile_reactnative_options(react_codegen_${libraryName} PRIVATE)
|
|
107
99
|
`;
|
|
108
100
|
};
|
|
109
101
|
|