@react-native/codegen 0.72.1

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.
Files changed (186) hide show
  1. package/README.md +21 -0
  2. package/lib/CodegenSchema.js +11 -0
  3. package/lib/CodegenSchema.js.flow +351 -0
  4. package/lib/SchemaValidator.js +54 -0
  5. package/lib/SchemaValidator.js.flow +67 -0
  6. package/lib/cli/combine/combine-js-to-schema-cli.js +50 -0
  7. package/lib/cli/combine/combine-js-to-schema-cli.js.flow +49 -0
  8. package/lib/cli/combine/combine-js-to-schema.js +110 -0
  9. package/lib/cli/combine/combine-js-to-schema.js.flow +48 -0
  10. package/lib/cli/combine/combine-utils.js +118 -0
  11. package/lib/cli/combine/combine-utils.js.flow +85 -0
  12. package/lib/cli/generators/generate-all.js +65 -0
  13. package/lib/cli/generators/generate-all.js.flow +66 -0
  14. package/lib/cli/parser/parser-cli.js +55 -0
  15. package/lib/cli/parser/parser-cli.js.flow +18 -0
  16. package/lib/cli/parser/parser.js +35 -0
  17. package/lib/cli/parser/parser.js.flow +37 -0
  18. package/lib/cli/parser/parser.sh +15 -0
  19. package/lib/generators/RNCodegen.js +207 -0
  20. package/lib/generators/RNCodegen.js.flow +284 -0
  21. package/lib/generators/Utils.js +31 -0
  22. package/lib/generators/Utils.js.flow +33 -0
  23. package/lib/generators/__test_fixtures__/fixtures.js +79 -0
  24. package/lib/generators/__test_fixtures__/fixtures.js.flow +82 -0
  25. package/lib/generators/components/ComponentsGeneratorUtils.js +217 -0
  26. package/lib/generators/components/ComponentsGeneratorUtils.js.flow +304 -0
  27. package/lib/generators/components/CppHelpers.js +188 -0
  28. package/lib/generators/components/CppHelpers.js.flow +228 -0
  29. package/lib/generators/components/GenerateComponentDescriptorH.js +75 -0
  30. package/lib/generators/components/GenerateComponentDescriptorH.js.flow +95 -0
  31. package/lib/generators/components/GenerateComponentHObjCpp.js +327 -0
  32. package/lib/generators/components/GenerateComponentHObjCpp.js.flow +418 -0
  33. package/lib/generators/components/GenerateEventEmitterCpp.js +199 -0
  34. package/lib/generators/components/GenerateEventEmitterCpp.js.flow +273 -0
  35. package/lib/generators/components/GenerateEventEmitterH.js +224 -0
  36. package/lib/generators/components/GenerateEventEmitterH.js.flow +315 -0
  37. package/lib/generators/components/GeneratePropsCpp.js +126 -0
  38. package/lib/generators/components/GeneratePropsCpp.js.flow +161 -0
  39. package/lib/generators/components/GeneratePropsH.js +606 -0
  40. package/lib/generators/components/GeneratePropsH.js.flow +778 -0
  41. package/lib/generators/components/GeneratePropsJavaDelegate.js +287 -0
  42. package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +348 -0
  43. package/lib/generators/components/GeneratePropsJavaInterface.js +237 -0
  44. package/lib/generators/components/GeneratePropsJavaInterface.js.flow +288 -0
  45. package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js +157 -0
  46. package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +187 -0
  47. package/lib/generators/components/GeneratePropsJavaPojo/index.js +66 -0
  48. package/lib/generators/components/GeneratePropsJavaPojo/index.js.flow +80 -0
  49. package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +273 -0
  50. package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +296 -0
  51. package/lib/generators/components/GenerateShadowNodeCpp.js +73 -0
  52. package/lib/generators/components/GenerateShadowNodeCpp.js.flow +95 -0
  53. package/lib/generators/components/GenerateShadowNodeH.js +91 -0
  54. package/lib/generators/components/GenerateShadowNodeH.js.flow +124 -0
  55. package/lib/generators/components/GenerateStateCpp.js +70 -0
  56. package/lib/generators/components/GenerateStateCpp.js.flow +92 -0
  57. package/lib/generators/components/GenerateStateH.js +93 -0
  58. package/lib/generators/components/GenerateStateH.js.flow +111 -0
  59. package/lib/generators/components/GenerateTests.js +165 -0
  60. package/lib/generators/components/GenerateTests.js.flow +219 -0
  61. package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js +91 -0
  62. package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js.flow +107 -0
  63. package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js +89 -0
  64. package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js.flow +105 -0
  65. package/lib/generators/components/GenerateViewConfigJs.js +410 -0
  66. package/lib/generators/components/GenerateViewConfigJs.js.flow +484 -0
  67. package/lib/generators/components/JavaHelpers.js +87 -0
  68. package/lib/generators/components/JavaHelpers.js.flow +119 -0
  69. package/lib/generators/components/__test_fixtures__/fixtures.js +1641 -0
  70. package/lib/generators/components/__test_fixtures__/fixtures.js.flow +1672 -0
  71. package/lib/generators/modules/GenerateModuleCpp.js +299 -0
  72. package/lib/generators/modules/GenerateModuleCpp.js.flow +278 -0
  73. package/lib/generators/modules/GenerateModuleH.js +373 -0
  74. package/lib/generators/modules/GenerateModuleH.js.flow +355 -0
  75. package/lib/generators/modules/GenerateModuleJavaSpec.js +571 -0
  76. package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +553 -0
  77. package/lib/generators/modules/GenerateModuleJniCpp.js +505 -0
  78. package/lib/generators/modules/GenerateModuleJniCpp.js.flow +523 -0
  79. package/lib/generators/modules/GenerateModuleJniH.js +138 -0
  80. package/lib/generators/modules/GenerateModuleJniH.js.flow +148 -0
  81. package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js +262 -0
  82. package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js.flow +208 -0
  83. package/lib/generators/modules/GenerateModuleObjCpp/Utils.js +25 -0
  84. package/lib/generators/modules/GenerateModuleObjCpp/Utils.js.flow +32 -0
  85. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js +337 -0
  86. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js.flow +288 -0
  87. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js +330 -0
  88. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js.flow +279 -0
  89. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeStruct.js +25 -0
  90. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeStruct.js.flow +35 -0
  91. package/lib/generators/modules/GenerateModuleObjCpp/index.js +185 -0
  92. package/lib/generators/modules/GenerateModuleObjCpp/index.js.flow +219 -0
  93. package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js +529 -0
  94. package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +510 -0
  95. package/lib/generators/modules/GenerateModuleObjCpp/source/serializeModule.js +96 -0
  96. package/lib/generators/modules/GenerateModuleObjCpp/source/serializeModule.js.flow +121 -0
  97. package/lib/generators/modules/Utils.js +34 -0
  98. package/lib/generators/modules/Utils.js.flow +53 -0
  99. package/lib/generators/modules/__test_fixtures__/fixtures.js +1658 -0
  100. package/lib/generators/modules/__test_fixtures__/fixtures.js.flow +1669 -0
  101. package/lib/parsers/consistency/compareSnaps.js +84 -0
  102. package/lib/parsers/error-utils.js +284 -0
  103. package/lib/parsers/error-utils.js.flow +295 -0
  104. package/lib/parsers/errors.js +350 -0
  105. package/lib/parsers/errors.js.flow +439 -0
  106. package/lib/parsers/flow/components/__test_fixtures__/failures.js +583 -0
  107. package/lib/parsers/flow/components/__test_fixtures__/failures.js.flow +600 -0
  108. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +1001 -0
  109. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +1016 -0
  110. package/lib/parsers/flow/components/commands.js +104 -0
  111. package/lib/parsers/flow/components/commands.js.flow +123 -0
  112. package/lib/parsers/flow/components/componentsUtils.js +446 -0
  113. package/lib/parsers/flow/components/componentsUtils.js.flow +496 -0
  114. package/lib/parsers/flow/components/events.js +233 -0
  115. package/lib/parsers/flow/components/events.js.flow +262 -0
  116. package/lib/parsers/flow/components/extends.js +52 -0
  117. package/lib/parsers/flow/components/extends.js.flow +66 -0
  118. package/lib/parsers/flow/components/index.js +267 -0
  119. package/lib/parsers/flow/components/index.js.flow +226 -0
  120. package/lib/parsers/flow/components/options.js +72 -0
  121. package/lib/parsers/flow/components/options.js.flow +87 -0
  122. package/lib/parsers/flow/components/props.js +47 -0
  123. package/lib/parsers/flow/components/props.js.flow +60 -0
  124. package/lib/parsers/flow/components/schema.js +106 -0
  125. package/lib/parsers/flow/components/schema.js.flow +62 -0
  126. package/lib/parsers/flow/index.js +80 -0
  127. package/lib/parsers/flow/index.js.flow +85 -0
  128. package/lib/parsers/flow/modules/__test_fixtures__/failures.js +215 -0
  129. package/lib/parsers/flow/modules/__test_fixtures__/failures.js.flow +223 -0
  130. package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +724 -0
  131. package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +747 -0
  132. package/lib/parsers/flow/modules/index.js +528 -0
  133. package/lib/parsers/flow/modules/index.js.flow +420 -0
  134. package/lib/parsers/flow/parser.js +109 -0
  135. package/lib/parsers/flow/parser.js.flow +83 -0
  136. package/lib/parsers/flow/utils.js +108 -0
  137. package/lib/parsers/flow/utils.js.flow +123 -0
  138. package/lib/parsers/parser.js +11 -0
  139. package/lib/parsers/parser.js.flow +70 -0
  140. package/lib/parsers/parserMock.js +96 -0
  141. package/lib/parsers/parserMock.js.flow +68 -0
  142. package/lib/parsers/parsers-commons.js +448 -0
  143. package/lib/parsers/parsers-commons.js.flow +447 -0
  144. package/lib/parsers/parsers-primitives.js +387 -0
  145. package/lib/parsers/parsers-primitives.js.flow +383 -0
  146. package/lib/parsers/parsers-utils.js +18 -0
  147. package/lib/parsers/parsers-utils.js.flow +19 -0
  148. package/lib/parsers/schema/index.js +23 -0
  149. package/lib/parsers/schema/index.js.flow +26 -0
  150. package/lib/parsers/typescript/components/__test_fixtures__/failures.js +489 -0
  151. package/lib/parsers/typescript/components/__test_fixtures__/failures.js.flow +505 -0
  152. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +1151 -0
  153. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +1168 -0
  154. package/lib/parsers/typescript/components/commands.js +132 -0
  155. package/lib/parsers/typescript/components/commands.js.flow +151 -0
  156. package/lib/parsers/typescript/components/componentsUtils.js +471 -0
  157. package/lib/parsers/typescript/components/componentsUtils.js.flow +521 -0
  158. package/lib/parsers/typescript/components/events.js +240 -0
  159. package/lib/parsers/typescript/components/events.js.flow +262 -0
  160. package/lib/parsers/typescript/components/extends.js +88 -0
  161. package/lib/parsers/typescript/components/extends.js.flow +103 -0
  162. package/lib/parsers/typescript/components/index.js +277 -0
  163. package/lib/parsers/typescript/components/index.js.flow +239 -0
  164. package/lib/parsers/typescript/components/options.js +72 -0
  165. package/lib/parsers/typescript/components/options.js.flow +87 -0
  166. package/lib/parsers/typescript/components/props.js +39 -0
  167. package/lib/parsers/typescript/components/props.js.flow +48 -0
  168. package/lib/parsers/typescript/components/schema.js +106 -0
  169. package/lib/parsers/typescript/components/schema.js.flow +62 -0
  170. package/lib/parsers/typescript/index.js +85 -0
  171. package/lib/parsers/typescript/index.js.flow +94 -0
  172. package/lib/parsers/typescript/modules/__test_fixtures__/failures.js +160 -0
  173. package/lib/parsers/typescript/modules/__test_fixtures__/failures.js.flow +167 -0
  174. package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js +701 -0
  175. package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js.flow +727 -0
  176. package/lib/parsers/typescript/modules/index.js +543 -0
  177. package/lib/parsers/typescript/modules/index.js.flow +435 -0
  178. package/lib/parsers/typescript/parseTopLevelType.js +203 -0
  179. package/lib/parsers/typescript/parseTopLevelType.js.flow +243 -0
  180. package/lib/parsers/typescript/parser.js +105 -0
  181. package/lib/parsers/typescript/parser.js.flow +87 -0
  182. package/lib/parsers/typescript/utils.js +101 -0
  183. package/lib/parsers/typescript/utils.js.flow +115 -0
  184. package/lib/parsers/utils.js +273 -0
  185. package/lib/parsers/utils.js.flow +266 -0
  186. package/package.json +45 -0
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ // File path -> contents
14
+
15
+ const FileTemplate = ({libraryName, stateClasses}) => `
16
+ /**
17
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
18
+ *
19
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
20
+ * once the code is regenerated.
21
+ *
22
+ * ${'@'}generated by codegen project: GenerateStateCpp.js
23
+ */
24
+ #include <react/renderer/components/${libraryName}/States.h>
25
+
26
+ namespace facebook {
27
+ namespace react {
28
+
29
+ ${stateClasses}
30
+
31
+ } // namespace react
32
+ } // namespace facebook
33
+ `;
34
+ const StateTemplate = ({stateName}) => '';
35
+ module.exports = {
36
+ generate(libraryName, schema, packageName, assumeNonnull = false) {
37
+ const fileName = 'States.cpp';
38
+ const stateClasses = Object.keys(schema.modules)
39
+ .map(moduleName => {
40
+ const module = schema.modules[moduleName];
41
+ if (module.type !== 'Component') {
42
+ return;
43
+ }
44
+ const components = module.components;
45
+ // No components in this module
46
+ if (components == null) {
47
+ return null;
48
+ }
49
+ return Object.keys(components)
50
+ .map(componentName => {
51
+ const component = components[componentName];
52
+ if (component.interfaceOnly === true) {
53
+ return null;
54
+ }
55
+ return StateTemplate({
56
+ stateName: `${componentName}State`,
57
+ });
58
+ })
59
+ .filter(Boolean)
60
+ .join('\n');
61
+ })
62
+ .filter(Boolean)
63
+ .join('\n');
64
+ const replacedTemplate = FileTemplate({
65
+ libraryName,
66
+ stateClasses,
67
+ });
68
+ return new Map([[fileName, replacedTemplate]]);
69
+ },
70
+ };
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {SchemaType} from '../../CodegenSchema';
14
+
15
+ // File path -> contents
16
+ type FilesOutput = Map<string, string>;
17
+
18
+ const FileTemplate = ({
19
+ libraryName,
20
+ stateClasses,
21
+ }: {
22
+ libraryName: string,
23
+ stateClasses: string,
24
+ }) => `
25
+ /**
26
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
27
+ *
28
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
29
+ * once the code is regenerated.
30
+ *
31
+ * ${'@'}generated by codegen project: GenerateStateCpp.js
32
+ */
33
+ #include <react/renderer/components/${libraryName}/States.h>
34
+
35
+ namespace facebook {
36
+ namespace react {
37
+
38
+ ${stateClasses}
39
+
40
+ } // namespace react
41
+ } // namespace facebook
42
+ `;
43
+
44
+ const StateTemplate = ({stateName}: {stateName: string}) => '';
45
+
46
+ module.exports = {
47
+ generate(
48
+ libraryName: string,
49
+ schema: SchemaType,
50
+ packageName?: string,
51
+ assumeNonnull: boolean = false,
52
+ ): FilesOutput {
53
+ const fileName = 'States.cpp';
54
+
55
+ const stateClasses = Object.keys(schema.modules)
56
+ .map(moduleName => {
57
+ const module = schema.modules[moduleName];
58
+ if (module.type !== 'Component') {
59
+ return;
60
+ }
61
+
62
+ const {components} = module;
63
+ // No components in this module
64
+ if (components == null) {
65
+ return null;
66
+ }
67
+
68
+ return Object.keys(components)
69
+ .map(componentName => {
70
+ const component = components[componentName];
71
+ if (component.interfaceOnly === true) {
72
+ return null;
73
+ }
74
+
75
+ return StateTemplate({
76
+ stateName: `${componentName}State`,
77
+ });
78
+ })
79
+ .filter(Boolean)
80
+ .join('\n');
81
+ })
82
+ .filter(Boolean)
83
+ .join('\n');
84
+
85
+ const replacedTemplate = FileTemplate({
86
+ libraryName,
87
+ stateClasses,
88
+ });
89
+
90
+ return new Map([[fileName, replacedTemplate]]);
91
+ },
92
+ };
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ // File path -> contents
14
+
15
+ const FileTemplate = ({libraryName, stateClasses}) =>
16
+ `
17
+ /**
18
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
19
+ *
20
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
21
+ * once the code is regenerated.
22
+ *
23
+ * ${'@'}generated by codegen project: GenerateStateH.js
24
+ */
25
+ #pragma once
26
+
27
+ #ifdef ANDROID
28
+ #include <folly/dynamic.h>
29
+ #include <react/renderer/mapbuffer/MapBuffer.h>
30
+ #include <react/renderer/mapbuffer/MapBufferBuilder.h>
31
+ #endif
32
+
33
+ namespace facebook {
34
+ namespace react {
35
+
36
+ ${stateClasses}
37
+
38
+ } // namespace react
39
+ } // namespace facebook
40
+ `.trim();
41
+ const StateTemplate = ({stateName}) =>
42
+ `
43
+ class ${stateName}State {
44
+ public:
45
+ ${stateName}State() = default;
46
+
47
+ #ifdef ANDROID
48
+ ${stateName}State(${stateName}State const &previousState, folly::dynamic data){};
49
+ folly::dynamic getDynamic() const {
50
+ return {};
51
+ };
52
+ MapBuffer getMapBuffer() const {
53
+ return MapBufferBuilder::EMPTY();
54
+ };
55
+ #endif
56
+ };
57
+ `.trim();
58
+ module.exports = {
59
+ generate(libraryName, schema, packageName, assumeNonnull = false) {
60
+ const fileName = 'States.h';
61
+ const stateClasses = Object.keys(schema.modules)
62
+ .map(moduleName => {
63
+ const module = schema.modules[moduleName];
64
+ if (module.type !== 'Component') {
65
+ return;
66
+ }
67
+ const components = module.components;
68
+ // No components in this module
69
+ if (components == null) {
70
+ return null;
71
+ }
72
+ return Object.keys(components)
73
+ .map(componentName => {
74
+ const component = components[componentName];
75
+ if (component.interfaceOnly === true) {
76
+ return null;
77
+ }
78
+ return StateTemplate({
79
+ stateName: componentName,
80
+ });
81
+ })
82
+ .filter(Boolean)
83
+ .join('\n\n');
84
+ })
85
+ .filter(Boolean)
86
+ .join('\n\n');
87
+ const template = FileTemplate({
88
+ libraryName,
89
+ stateClasses,
90
+ });
91
+ return new Map([[fileName, template]]);
92
+ },
93
+ };
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {SchemaType} from '../../CodegenSchema';
14
+
15
+ // File path -> contents
16
+ type FilesOutput = Map<string, string>;
17
+
18
+ const FileTemplate = ({
19
+ libraryName,
20
+ stateClasses,
21
+ }: {
22
+ libraryName: string,
23
+ stateClasses: string,
24
+ }) =>
25
+ `
26
+ /**
27
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
28
+ *
29
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
30
+ * once the code is regenerated.
31
+ *
32
+ * ${'@'}generated by codegen project: GenerateStateH.js
33
+ */
34
+ #pragma once
35
+
36
+ #ifdef ANDROID
37
+ #include <folly/dynamic.h>
38
+ #include <react/renderer/mapbuffer/MapBuffer.h>
39
+ #include <react/renderer/mapbuffer/MapBufferBuilder.h>
40
+ #endif
41
+
42
+ namespace facebook {
43
+ namespace react {
44
+
45
+ ${stateClasses}
46
+
47
+ } // namespace react
48
+ } // namespace facebook
49
+ `.trim();
50
+
51
+ const StateTemplate = ({stateName}: {stateName: string}) =>
52
+ `
53
+ class ${stateName}State {
54
+ public:
55
+ ${stateName}State() = default;
56
+
57
+ #ifdef ANDROID
58
+ ${stateName}State(${stateName}State const &previousState, folly::dynamic data){};
59
+ folly::dynamic getDynamic() const {
60
+ return {};
61
+ };
62
+ MapBuffer getMapBuffer() const {
63
+ return MapBufferBuilder::EMPTY();
64
+ };
65
+ #endif
66
+ };
67
+ `.trim();
68
+
69
+ module.exports = {
70
+ generate(
71
+ libraryName: string,
72
+ schema: SchemaType,
73
+ packageName?: string,
74
+ assumeNonnull: boolean = false,
75
+ ): FilesOutput {
76
+ const fileName = 'States.h';
77
+
78
+ const stateClasses = Object.keys(schema.modules)
79
+ .map(moduleName => {
80
+ const module = schema.modules[moduleName];
81
+ if (module.type !== 'Component') {
82
+ return;
83
+ }
84
+
85
+ const {components} = module;
86
+ // No components in this module
87
+ if (components == null) {
88
+ return null;
89
+ }
90
+
91
+ return Object.keys(components)
92
+ .map(componentName => {
93
+ const component = components[componentName];
94
+ if (component.interfaceOnly === true) {
95
+ return null;
96
+ }
97
+ return StateTemplate({stateName: componentName});
98
+ })
99
+ .filter(Boolean)
100
+ .join('\n\n');
101
+ })
102
+ .filter(Boolean)
103
+ .join('\n\n');
104
+
105
+ const template = FileTemplate({
106
+ libraryName,
107
+ stateClasses,
108
+ });
109
+ return new Map([[fileName, template]]);
110
+ },
111
+ };
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ const _require = require('./CppHelpers'),
14
+ getImports = _require.getImports,
15
+ toSafeCppString = _require.toSafeCppString;
16
+ const FileTemplate = ({libraryName, imports, componentTests}) =>
17
+ `
18
+ /**
19
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
20
+ *
21
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
22
+ * once the code is regenerated.
23
+ *
24
+ * ${'@'}generated by codegen project: GenerateTests.js
25
+ * */
26
+
27
+ #include <gtest/gtest.h>
28
+ #include <react/renderer/core/PropsParserContext.h>
29
+ #include <react/renderer/components/${libraryName}/Props.h>
30
+ ${imports}
31
+
32
+ using namespace facebook::react;
33
+ ${componentTests}
34
+ `.trim();
35
+ const TestTemplate = ({componentName, testName, propName, propValue}) => `
36
+ TEST(${componentName}_${testName}, etc) {
37
+ auto propParser = RawPropsParser();
38
+ propParser.prepare<${componentName}>();
39
+ auto const &sourceProps = ${componentName}();
40
+ auto const &rawProps = RawProps(folly::dynamic::object("${propName}", ${propValue}));
41
+
42
+ ContextContainer contextContainer{};
43
+ PropsParserContext parserContext{-1, contextContainer};
44
+
45
+ rawProps.parse(propParser, parserContext);
46
+ ${componentName}(parserContext, sourceProps, rawProps);
47
+ }
48
+ `;
49
+ function getTestCasesForProp(propName, typeAnnotation) {
50
+ const cases = [];
51
+ if (typeAnnotation.type === 'StringEnumTypeAnnotation') {
52
+ typeAnnotation.options.forEach(option =>
53
+ cases.push({
54
+ propName,
55
+ testName: `${propName}_${toSafeCppString(option)}`,
56
+ propValue: option,
57
+ }),
58
+ );
59
+ } else if (typeAnnotation.type === 'StringTypeAnnotation') {
60
+ cases.push({
61
+ propName,
62
+ propValue:
63
+ typeAnnotation.default != null && typeAnnotation.default !== ''
64
+ ? typeAnnotation.default
65
+ : 'foo',
66
+ });
67
+ } else if (typeAnnotation.type === 'BooleanTypeAnnotation') {
68
+ cases.push({
69
+ propName: propName,
70
+ propValue: typeAnnotation.default != null ? typeAnnotation.default : true,
71
+ });
72
+ // $FlowFixMe[incompatible-type]
73
+ } else if (typeAnnotation.type === 'IntegerTypeAnnotation') {
74
+ cases.push({
75
+ propName,
76
+ propValue: typeAnnotation.default || 10,
77
+ });
78
+ } else if (typeAnnotation.type === 'FloatTypeAnnotation') {
79
+ cases.push({
80
+ propName,
81
+ propValue: typeAnnotation.default != null ? typeAnnotation.default : 0.1,
82
+ });
83
+ } else if (typeAnnotation.type === 'ReservedPropTypeAnnotation') {
84
+ if (typeAnnotation.name === 'ColorPrimitive') {
85
+ cases.push({
86
+ propName,
87
+ propValue: 1,
88
+ });
89
+ } else if (typeAnnotation.name === 'PointPrimitive') {
90
+ cases.push({
91
+ propName,
92
+ propValue: 'folly::dynamic::object("x", 1)("y", 1)',
93
+ raw: true,
94
+ });
95
+ } else if (typeAnnotation.name === 'ImageSourcePrimitive') {
96
+ cases.push({
97
+ propName,
98
+ propValue: 'folly::dynamic::object("url", "testurl")',
99
+ raw: true,
100
+ });
101
+ }
102
+ }
103
+ return cases;
104
+ }
105
+ function generateTestsString(name, component) {
106
+ function createTest({testName, propName, propValue, raw = false}) {
107
+ const value =
108
+ !raw && typeof propValue === 'string' ? `"${propValue}"` : propValue;
109
+ return TestTemplate({
110
+ componentName: name,
111
+ testName: testName != null ? testName : propName,
112
+ propName,
113
+ propValue: String(value),
114
+ });
115
+ }
116
+ const testCases = component.props.reduce((cases, prop) => {
117
+ return cases.concat(getTestCasesForProp(prop.name, prop.typeAnnotation));
118
+ }, []);
119
+ const baseTest = {
120
+ testName: 'DoesNotDie',
121
+ propName: 'xx_invalid_xx',
122
+ propValue: 'xx_invalid_xx',
123
+ };
124
+ return [baseTest, ...testCases].map(createTest).join('');
125
+ }
126
+ module.exports = {
127
+ generate(libraryName, schema, packageName, assumeNonnull = false) {
128
+ const fileName = 'Tests.cpp';
129
+ const allImports = new Set([
130
+ '#include <react/renderer/core/propsConversions.h>',
131
+ '#include <react/renderer/core/RawProps.h>',
132
+ '#include <react/renderer/core/RawPropsParser.h>',
133
+ ]);
134
+ const componentTests = Object.keys(schema.modules)
135
+ .map(moduleName => {
136
+ const module = schema.modules[moduleName];
137
+ if (module.type !== 'Component') {
138
+ return;
139
+ }
140
+ const components = module.components;
141
+ if (components == null) {
142
+ return null;
143
+ }
144
+ return Object.keys(components)
145
+ .map(componentName => {
146
+ const component = components[componentName];
147
+ const name = `${componentName}Props`;
148
+ const imports = getImports(component.props);
149
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
150
+ imports.forEach(allImports.add, allImports);
151
+ return generateTestsString(name, component);
152
+ })
153
+ .join('');
154
+ })
155
+ .filter(Boolean)
156
+ .join('');
157
+ const imports = Array.from(allImports).sort().join('\n').trim();
158
+ const replacedTemplate = FileTemplate({
159
+ imports,
160
+ libraryName,
161
+ componentTests,
162
+ });
163
+ return new Map([[fileName, replacedTemplate]]);
164
+ },
165
+ };