@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,60 @@
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-local
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ const {
14
+ flattenProperties,
15
+ getSchemaInfo,
16
+ getTypeAnnotation,
17
+ } = require('./componentsUtils.js');
18
+
19
+ import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js';
20
+ import type {TypeDeclarationMap} from '../../utils';
21
+
22
+ // $FlowFixMe[unclear-type] there's no flowtype for ASTs
23
+ type PropAST = Object;
24
+
25
+ function buildPropSchema(
26
+ property: PropAST,
27
+ types: TypeDeclarationMap,
28
+ ): ?NamedShape<PropTypeAnnotation> {
29
+ const info = getSchemaInfo(property, types);
30
+ if (info == null) {
31
+ return null;
32
+ }
33
+ const {name, optional, typeAnnotation, defaultValue, withNullDefault} = info;
34
+
35
+ return {
36
+ name,
37
+ optional,
38
+ typeAnnotation: getTypeAnnotation(
39
+ name,
40
+ typeAnnotation,
41
+ defaultValue,
42
+ withNullDefault,
43
+ types,
44
+ buildPropSchema,
45
+ ),
46
+ };
47
+ }
48
+
49
+ function getProps(
50
+ typeDefinition: $ReadOnlyArray<PropAST>,
51
+ types: TypeDeclarationMap,
52
+ ): $ReadOnlyArray<NamedShape<PropTypeAnnotation>> {
53
+ return flattenProperties(typeDefinition, types)
54
+ .map(property => buildPropSchema(property, types))
55
+ .filter(Boolean);
56
+ }
57
+
58
+ module.exports = {
59
+ getProps,
60
+ };
@@ -0,0 +1,106 @@
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
+ function ownKeys(object, enumerableOnly) {
14
+ var keys = Object.keys(object);
15
+ if (Object.getOwnPropertySymbols) {
16
+ var symbols = Object.getOwnPropertySymbols(object);
17
+ enumerableOnly &&
18
+ (symbols = symbols.filter(function (sym) {
19
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
20
+ })),
21
+ keys.push.apply(keys, symbols);
22
+ }
23
+ return keys;
24
+ }
25
+ function _objectSpread(target) {
26
+ for (var i = 1; i < arguments.length; i++) {
27
+ var source = null != arguments[i] ? arguments[i] : {};
28
+ i % 2
29
+ ? ownKeys(Object(source), !0).forEach(function (key) {
30
+ _defineProperty(target, key, source[key]);
31
+ })
32
+ : Object.getOwnPropertyDescriptors
33
+ ? Object.defineProperties(
34
+ target,
35
+ Object.getOwnPropertyDescriptors(source),
36
+ )
37
+ : ownKeys(Object(source)).forEach(function (key) {
38
+ Object.defineProperty(
39
+ target,
40
+ key,
41
+ Object.getOwnPropertyDescriptor(source, key),
42
+ );
43
+ });
44
+ }
45
+ return target;
46
+ }
47
+ function _defineProperty(obj, key, value) {
48
+ key = _toPropertyKey(key);
49
+ if (key in obj) {
50
+ Object.defineProperty(obj, key, {
51
+ value: value,
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ });
56
+ } else {
57
+ obj[key] = value;
58
+ }
59
+ return obj;
60
+ }
61
+ function _toPropertyKey(arg) {
62
+ var key = _toPrimitive(arg, 'string');
63
+ return typeof key === 'symbol' ? key : String(key);
64
+ }
65
+ function _toPrimitive(input, hint) {
66
+ if (typeof input !== 'object' || input === null) return input;
67
+ var prim = input[Symbol.toPrimitive];
68
+ if (prim !== undefined) {
69
+ var res = prim.call(input, hint || 'default');
70
+ if (typeof res !== 'object') return res;
71
+ throw new TypeError('@@toPrimitive must return a primitive value.');
72
+ }
73
+ return (hint === 'string' ? String : Number)(input);
74
+ }
75
+ function wrapComponentSchema({
76
+ filename,
77
+ componentName,
78
+ extendsProps,
79
+ events,
80
+ props,
81
+ options,
82
+ commands,
83
+ }) {
84
+ return {
85
+ modules: {
86
+ [filename]: {
87
+ type: 'Component',
88
+ components: {
89
+ [componentName]: _objectSpread(
90
+ _objectSpread({}, options || {}),
91
+ {},
92
+ {
93
+ extendsProps,
94
+ events,
95
+ props,
96
+ commands,
97
+ },
98
+ ),
99
+ },
100
+ },
101
+ },
102
+ };
103
+ }
104
+ module.exports = {
105
+ wrapComponentSchema,
106
+ };
@@ -0,0 +1,62 @@
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 {
14
+ EventTypeShape,
15
+ NamedShape,
16
+ CommandTypeAnnotation,
17
+ PropTypeAnnotation,
18
+ ExtendsPropsShape,
19
+ SchemaType,
20
+ OptionsShape,
21
+ } from '../../../CodegenSchema.js';
22
+
23
+ export type ComponentSchemaBuilderConfig = $ReadOnly<{
24
+ filename: string,
25
+ componentName: string,
26
+ extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
27
+ events: $ReadOnlyArray<EventTypeShape>,
28
+ props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
29
+ commands: $ReadOnlyArray<NamedShape<CommandTypeAnnotation>>,
30
+ options?: ?OptionsShape,
31
+ }>;
32
+
33
+ function wrapComponentSchema({
34
+ filename,
35
+ componentName,
36
+ extendsProps,
37
+ events,
38
+ props,
39
+ options,
40
+ commands,
41
+ }: ComponentSchemaBuilderConfig): SchemaType {
42
+ return {
43
+ modules: {
44
+ [filename]: {
45
+ type: 'Component',
46
+ components: {
47
+ [componentName]: {
48
+ ...(options || {}),
49
+ extendsProps,
50
+ events,
51
+ props,
52
+ commands,
53
+ },
54
+ },
55
+ },
56
+ },
57
+ };
58
+ }
59
+
60
+ module.exports = {
61
+ wrapComponentSchema,
62
+ };
@@ -0,0 +1,80 @@
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
+ // $FlowFixMe[untyped-import] there's no flowtype flow-parser
14
+ const flowParser = require('flow-parser');
15
+ const fs = require('fs');
16
+ const _require = require('../utils'),
17
+ buildSchemaFromConfigType = _require.buildSchemaFromConfigType,
18
+ getConfigType = _require.getConfigType,
19
+ isModuleRegistryCall = _require.isModuleRegistryCall;
20
+ const _require2 = require('./components'),
21
+ buildComponentSchema = _require2.buildComponentSchema;
22
+ const _require3 = require('./components/schema'),
23
+ wrapComponentSchema = _require3.wrapComponentSchema;
24
+ const _require4 = require('./modules'),
25
+ buildModuleSchema = _require4.buildModuleSchema;
26
+ function Visitor(infoMap) {
27
+ return {
28
+ CallExpression(node) {
29
+ if (
30
+ node.callee.type === 'Identifier' &&
31
+ node.callee.name === 'codegenNativeComponent'
32
+ ) {
33
+ infoMap.isComponent = true;
34
+ }
35
+ if (isModuleRegistryCall(node)) {
36
+ infoMap.isModule = true;
37
+ }
38
+ },
39
+ InterfaceExtends(node) {
40
+ if (node.id.name === 'TurboModule') {
41
+ infoMap.isModule = true;
42
+ }
43
+ },
44
+ };
45
+ }
46
+ function buildSchema(contents, filename) {
47
+ // Early return for non-Spec JavaScript files
48
+ if (
49
+ !contents.includes('codegenNativeComponent') &&
50
+ !contents.includes('TurboModule')
51
+ ) {
52
+ return {
53
+ modules: {},
54
+ };
55
+ }
56
+ const ast = flowParser.parse(contents, {
57
+ enums: true,
58
+ });
59
+ const configType = getConfigType(ast, Visitor);
60
+ return buildSchemaFromConfigType(
61
+ configType,
62
+ filename,
63
+ ast,
64
+ wrapComponentSchema,
65
+ buildComponentSchema,
66
+ buildModuleSchema,
67
+ );
68
+ }
69
+ function parseModuleFixture(filename) {
70
+ const contents = fs.readFileSync(filename, 'utf8');
71
+ return buildSchema(contents, 'path/NativeSampleTurboModule.js');
72
+ }
73
+ function parseString(contents, filename) {
74
+ return buildSchema(contents, filename);
75
+ }
76
+ module.exports = {
77
+ buildSchema,
78
+ parseModuleFixture,
79
+ parseString,
80
+ };
@@ -0,0 +1,85 @@
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-local
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {SchemaType} from '../../CodegenSchema.js';
14
+
15
+ // $FlowFixMe[untyped-import] there's no flowtype flow-parser
16
+ const flowParser = require('flow-parser');
17
+ const fs = require('fs');
18
+ const {
19
+ buildSchemaFromConfigType,
20
+ getConfigType,
21
+ isModuleRegistryCall,
22
+ } = require('../utils');
23
+ const {buildComponentSchema} = require('./components');
24
+ const {wrapComponentSchema} = require('./components/schema');
25
+ const {buildModuleSchema} = require('./modules');
26
+
27
+ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) {
28
+ return {
29
+ CallExpression(node: $FlowFixMe) {
30
+ if (
31
+ node.callee.type === 'Identifier' &&
32
+ node.callee.name === 'codegenNativeComponent'
33
+ ) {
34
+ infoMap.isComponent = true;
35
+ }
36
+
37
+ if (isModuleRegistryCall(node)) {
38
+ infoMap.isModule = true;
39
+ }
40
+ },
41
+ InterfaceExtends(node: $FlowFixMe) {
42
+ if (node.id.name === 'TurboModule') {
43
+ infoMap.isModule = true;
44
+ }
45
+ },
46
+ };
47
+ }
48
+
49
+ function buildSchema(contents: string, filename: ?string): SchemaType {
50
+ // Early return for non-Spec JavaScript files
51
+ if (
52
+ !contents.includes('codegenNativeComponent') &&
53
+ !contents.includes('TurboModule')
54
+ ) {
55
+ return {modules: {}};
56
+ }
57
+
58
+ const ast = flowParser.parse(contents, {enums: true});
59
+ const configType = getConfigType(ast, Visitor);
60
+
61
+ return buildSchemaFromConfigType(
62
+ configType,
63
+ filename,
64
+ ast,
65
+ wrapComponentSchema,
66
+ buildComponentSchema,
67
+ buildModuleSchema,
68
+ );
69
+ }
70
+
71
+ function parseModuleFixture(filename: string): SchemaType {
72
+ const contents = fs.readFileSync(filename, 'utf8');
73
+
74
+ return buildSchema(contents, 'path/NativeSampleTurboModule.js');
75
+ }
76
+
77
+ function parseString(contents: string, filename: ?string): SchemaType {
78
+ return buildSchema(contents, filename);
79
+ }
80
+
81
+ module.exports = {
82
+ buildSchema,
83
+ parseModuleFixture,
84
+ parseString,
85
+ };
@@ -0,0 +1,215 @@
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
+ // @licenselint-loose-mode
12
+
13
+ 'use strict';
14
+
15
+ const NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT = `
16
+ /**
17
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
18
+ *
19
+ * This source code is licensed under the MIT license found in the
20
+ * LICENSE file in the root directory of this source tree.
21
+ *
22
+ * @flow strict-local
23
+ * @format
24
+ */
25
+
26
+ 'use strict';
27
+
28
+ import type {TurboModule} from '../RCTExport';
29
+ import * as TurboModuleRegistry from '../TurboModuleRegistry';
30
+
31
+ export interface Spec extends TurboModule {
32
+ getString: (arg: string) => Array;
33
+ }
34
+
35
+ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
36
+
37
+ `;
38
+ const NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT_AS_PARAM = `
39
+ /**
40
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
41
+ *
42
+ * This source code is licensed under the MIT license found in the
43
+ * LICENSE file in the root directory of this source tree.
44
+ *
45
+ * @flow strict-local
46
+ * @format
47
+ */
48
+
49
+ 'use strict';
50
+
51
+ import type {TurboModule} from '../RCTExport';
52
+ import * as TurboModuleRegistry from '../TurboModuleRegistry';
53
+
54
+ export interface Spec extends TurboModule {
55
+ getString: (arg : Array) => string;
56
+ }
57
+
58
+ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
59
+
60
+ `;
61
+ const NATIVE_MODULES_WITH_READ_ONLY_OBJECT_NO_TYPE_FOR_CONTENT = `
62
+ /**
63
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
64
+ *
65
+ * This source code is licensed under the MIT license found in the
66
+ * LICENSE file in the root directory of this source tree.
67
+ *
68
+ * @flow strict-local
69
+ * @format
70
+ */
71
+
72
+ 'use strict';
73
+
74
+ import type {TurboModule} from '../RCTExport';
75
+ import * as TurboModuleRegistry from '../TurboModuleRegistry';
76
+
77
+ export interface Spec extends TurboModule {
78
+ getString: (arg : $ReadOnly<>) => string;
79
+ }
80
+
81
+ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
82
+
83
+ `;
84
+ const NATIVE_MODULES_WITH_NOT_ONLY_METHODS = `
85
+ /**
86
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
87
+ *
88
+ * This source code is licensed under the MIT license found in the
89
+ * LICENSE file in the root directory of this source tree.
90
+ *
91
+ * @flow strict-local
92
+ * @format
93
+ */
94
+
95
+ 'use strict';
96
+
97
+ import type {TurboModule} from '../RCTExport';
98
+ import * as TurboModuleRegistry from '../TurboModuleRegistry';
99
+
100
+ export interface Spec extends TurboModule {
101
+ +getBool: (arg: boolean) => boolean;
102
+ +getNumber: (arg: number) => number;
103
+ +getString: (arg: string) => string;
104
+ sampleBool: boolean,
105
+
106
+ }
107
+
108
+ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
109
+
110
+ `;
111
+ const NATIVE_MODULES_WITH_UNNAMED_PARAMS = `
112
+ /**
113
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
114
+ *
115
+ * This source code is licensed under the MIT license found in the
116
+ * LICENSE file in the root directory of this source tree.
117
+ *
118
+ * @flow strict-local
119
+ * @format
120
+ */
121
+
122
+ 'use strict';
123
+
124
+ import type {TurboModule} from '../RCTExport';
125
+ import * as TurboModuleRegistry from '../TurboModuleRegistry';
126
+
127
+ export interface Spec extends TurboModule {
128
+ +getBool: (boolean) => boolean;
129
+
130
+ }
131
+
132
+ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
133
+
134
+ `;
135
+ const NATIVE_MODULES_WITH_PROMISE_WITHOUT_TYPE = `
136
+ /**
137
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
138
+ *
139
+ * This source code is licensed under the MIT license found in the
140
+ * LICENSE file in the root directory of this source tree.
141
+ *
142
+ * @flow strict-local
143
+ * @format
144
+ */
145
+
146
+ 'use strict';
147
+
148
+ import type {TurboModule} from '../RCTExport';
149
+ import * as TurboModuleRegistry from '../TurboModuleRegistry';
150
+
151
+ export interface Spec extends TurboModule {
152
+ +getBool: (arg: boolean) => Promise;
153
+
154
+ }
155
+
156
+ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
157
+
158
+ `;
159
+ const TWO_NATIVE_MODULES_EXPORTED_WITH_DEFAULT = `
160
+ /**
161
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
162
+ *
163
+ * This source code is licensed under the MIT license found in the
164
+ * LICENSE file in the root directory of this source tree.
165
+ *
166
+ * @flow strict-local
167
+ * @format
168
+ */
169
+
170
+ 'use strict';
171
+
172
+ import type {TurboModule} from '../RCTExport';
173
+ import * as TurboModuleRegistry from '../TurboModuleRegistry';
174
+
175
+
176
+ export default TurboModuleRegistry.getEnforcing<Spec1>('SampleTurboModule1');
177
+ export default TurboModuleRegistry.getEnforcing<Spec2>('SampleTurboModule2');
178
+
179
+ `;
180
+ const TWO_NATIVE_EXTENDING_TURBO_MODULE = `
181
+ /**
182
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
183
+ *
184
+ * This source code is licensed under the MIT license found in the
185
+ * LICENSE file in the root directory of this source tree.
186
+ *
187
+ * @flow strict-local
188
+ * @format
189
+ */
190
+
191
+ 'use strict';
192
+
193
+ import type {TurboModule} from '../RCTExport';
194
+ import * as TurboModuleRegistry from '../TurboModuleRegistry';
195
+
196
+ export interface Spec extends TurboModule {
197
+ +getSth: (a : ?number) => void
198
+ }
199
+
200
+ export interface Spec2 extends TurboModule {
201
+ +getSth: (a : ?number) => void
202
+ }
203
+
204
+
205
+ `;
206
+ module.exports = {
207
+ NATIVE_MODULES_WITH_READ_ONLY_OBJECT_NO_TYPE_FOR_CONTENT,
208
+ NATIVE_MODULES_WITH_UNNAMED_PARAMS,
209
+ NATIVE_MODULES_WITH_PROMISE_WITHOUT_TYPE,
210
+ NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT_AS_PARAM,
211
+ NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT,
212
+ TWO_NATIVE_MODULES_EXPORTED_WITH_DEFAULT,
213
+ NATIVE_MODULES_WITH_NOT_ONLY_METHODS,
214
+ TWO_NATIVE_EXTENDING_TURBO_MODULE,
215
+ };