@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.
- package/README.md +21 -0
- package/lib/CodegenSchema.js +11 -0
- package/lib/CodegenSchema.js.flow +351 -0
- package/lib/SchemaValidator.js +54 -0
- package/lib/SchemaValidator.js.flow +67 -0
- package/lib/cli/combine/combine-js-to-schema-cli.js +50 -0
- package/lib/cli/combine/combine-js-to-schema-cli.js.flow +49 -0
- package/lib/cli/combine/combine-js-to-schema.js +110 -0
- package/lib/cli/combine/combine-js-to-schema.js.flow +48 -0
- package/lib/cli/combine/combine-utils.js +118 -0
- package/lib/cli/combine/combine-utils.js.flow +85 -0
- package/lib/cli/generators/generate-all.js +65 -0
- package/lib/cli/generators/generate-all.js.flow +66 -0
- package/lib/cli/parser/parser-cli.js +55 -0
- package/lib/cli/parser/parser-cli.js.flow +18 -0
- package/lib/cli/parser/parser.js +35 -0
- package/lib/cli/parser/parser.js.flow +37 -0
- package/lib/cli/parser/parser.sh +15 -0
- package/lib/generators/RNCodegen.js +207 -0
- package/lib/generators/RNCodegen.js.flow +284 -0
- package/lib/generators/Utils.js +31 -0
- package/lib/generators/Utils.js.flow +33 -0
- package/lib/generators/__test_fixtures__/fixtures.js +79 -0
- package/lib/generators/__test_fixtures__/fixtures.js.flow +82 -0
- package/lib/generators/components/ComponentsGeneratorUtils.js +217 -0
- package/lib/generators/components/ComponentsGeneratorUtils.js.flow +304 -0
- package/lib/generators/components/CppHelpers.js +188 -0
- package/lib/generators/components/CppHelpers.js.flow +228 -0
- package/lib/generators/components/GenerateComponentDescriptorH.js +75 -0
- package/lib/generators/components/GenerateComponentDescriptorH.js.flow +95 -0
- package/lib/generators/components/GenerateComponentHObjCpp.js +327 -0
- package/lib/generators/components/GenerateComponentHObjCpp.js.flow +418 -0
- package/lib/generators/components/GenerateEventEmitterCpp.js +199 -0
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +273 -0
- package/lib/generators/components/GenerateEventEmitterH.js +224 -0
- package/lib/generators/components/GenerateEventEmitterH.js.flow +315 -0
- package/lib/generators/components/GeneratePropsCpp.js +126 -0
- package/lib/generators/components/GeneratePropsCpp.js.flow +161 -0
- package/lib/generators/components/GeneratePropsH.js +606 -0
- package/lib/generators/components/GeneratePropsH.js.flow +778 -0
- package/lib/generators/components/GeneratePropsJavaDelegate.js +287 -0
- package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +348 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js +237 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js.flow +288 -0
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js +157 -0
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +187 -0
- package/lib/generators/components/GeneratePropsJavaPojo/index.js +66 -0
- package/lib/generators/components/GeneratePropsJavaPojo/index.js.flow +80 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +273 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +296 -0
- package/lib/generators/components/GenerateShadowNodeCpp.js +73 -0
- package/lib/generators/components/GenerateShadowNodeCpp.js.flow +95 -0
- package/lib/generators/components/GenerateShadowNodeH.js +91 -0
- package/lib/generators/components/GenerateShadowNodeH.js.flow +124 -0
- package/lib/generators/components/GenerateStateCpp.js +70 -0
- package/lib/generators/components/GenerateStateCpp.js.flow +92 -0
- package/lib/generators/components/GenerateStateH.js +93 -0
- package/lib/generators/components/GenerateStateH.js.flow +111 -0
- package/lib/generators/components/GenerateTests.js +165 -0
- package/lib/generators/components/GenerateTests.js.flow +219 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js +91 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js.flow +107 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js +89 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js.flow +105 -0
- package/lib/generators/components/GenerateViewConfigJs.js +410 -0
- package/lib/generators/components/GenerateViewConfigJs.js.flow +484 -0
- package/lib/generators/components/JavaHelpers.js +87 -0
- package/lib/generators/components/JavaHelpers.js.flow +119 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js +1641 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js.flow +1672 -0
- package/lib/generators/modules/GenerateModuleCpp.js +299 -0
- package/lib/generators/modules/GenerateModuleCpp.js.flow +278 -0
- package/lib/generators/modules/GenerateModuleH.js +373 -0
- package/lib/generators/modules/GenerateModuleH.js.flow +355 -0
- package/lib/generators/modules/GenerateModuleJavaSpec.js +571 -0
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +553 -0
- package/lib/generators/modules/GenerateModuleJniCpp.js +505 -0
- package/lib/generators/modules/GenerateModuleJniCpp.js.flow +523 -0
- package/lib/generators/modules/GenerateModuleJniH.js +138 -0
- package/lib/generators/modules/GenerateModuleJniH.js.flow +148 -0
- package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js +262 -0
- package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js.flow +208 -0
- package/lib/generators/modules/GenerateModuleObjCpp/Utils.js +25 -0
- package/lib/generators/modules/GenerateModuleObjCpp/Utils.js.flow +32 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js +337 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js.flow +288 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js +330 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js.flow +279 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeStruct.js +25 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeStruct.js.flow +35 -0
- package/lib/generators/modules/GenerateModuleObjCpp/index.js +185 -0
- package/lib/generators/modules/GenerateModuleObjCpp/index.js.flow +219 -0
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js +529 -0
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +510 -0
- package/lib/generators/modules/GenerateModuleObjCpp/source/serializeModule.js +96 -0
- package/lib/generators/modules/GenerateModuleObjCpp/source/serializeModule.js.flow +121 -0
- package/lib/generators/modules/Utils.js +34 -0
- package/lib/generators/modules/Utils.js.flow +53 -0
- package/lib/generators/modules/__test_fixtures__/fixtures.js +1658 -0
- package/lib/generators/modules/__test_fixtures__/fixtures.js.flow +1669 -0
- package/lib/parsers/consistency/compareSnaps.js +84 -0
- package/lib/parsers/error-utils.js +284 -0
- package/lib/parsers/error-utils.js.flow +295 -0
- package/lib/parsers/errors.js +350 -0
- package/lib/parsers/errors.js.flow +439 -0
- package/lib/parsers/flow/components/__test_fixtures__/failures.js +583 -0
- package/lib/parsers/flow/components/__test_fixtures__/failures.js.flow +600 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +1001 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +1016 -0
- package/lib/parsers/flow/components/commands.js +104 -0
- package/lib/parsers/flow/components/commands.js.flow +123 -0
- package/lib/parsers/flow/components/componentsUtils.js +446 -0
- package/lib/parsers/flow/components/componentsUtils.js.flow +496 -0
- package/lib/parsers/flow/components/events.js +233 -0
- package/lib/parsers/flow/components/events.js.flow +262 -0
- package/lib/parsers/flow/components/extends.js +52 -0
- package/lib/parsers/flow/components/extends.js.flow +66 -0
- package/lib/parsers/flow/components/index.js +267 -0
- package/lib/parsers/flow/components/index.js.flow +226 -0
- package/lib/parsers/flow/components/options.js +72 -0
- package/lib/parsers/flow/components/options.js.flow +87 -0
- package/lib/parsers/flow/components/props.js +47 -0
- package/lib/parsers/flow/components/props.js.flow +60 -0
- package/lib/parsers/flow/components/schema.js +106 -0
- package/lib/parsers/flow/components/schema.js.flow +62 -0
- package/lib/parsers/flow/index.js +80 -0
- package/lib/parsers/flow/index.js.flow +85 -0
- package/lib/parsers/flow/modules/__test_fixtures__/failures.js +215 -0
- package/lib/parsers/flow/modules/__test_fixtures__/failures.js.flow +223 -0
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +724 -0
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +747 -0
- package/lib/parsers/flow/modules/index.js +528 -0
- package/lib/parsers/flow/modules/index.js.flow +420 -0
- package/lib/parsers/flow/parser.js +109 -0
- package/lib/parsers/flow/parser.js.flow +83 -0
- package/lib/parsers/flow/utils.js +108 -0
- package/lib/parsers/flow/utils.js.flow +123 -0
- package/lib/parsers/parser.js +11 -0
- package/lib/parsers/parser.js.flow +70 -0
- package/lib/parsers/parserMock.js +96 -0
- package/lib/parsers/parserMock.js.flow +68 -0
- package/lib/parsers/parsers-commons.js +448 -0
- package/lib/parsers/parsers-commons.js.flow +447 -0
- package/lib/parsers/parsers-primitives.js +387 -0
- package/lib/parsers/parsers-primitives.js.flow +383 -0
- package/lib/parsers/parsers-utils.js +18 -0
- package/lib/parsers/parsers-utils.js.flow +19 -0
- package/lib/parsers/schema/index.js +23 -0
- package/lib/parsers/schema/index.js.flow +26 -0
- package/lib/parsers/typescript/components/__test_fixtures__/failures.js +489 -0
- package/lib/parsers/typescript/components/__test_fixtures__/failures.js.flow +505 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +1151 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +1168 -0
- package/lib/parsers/typescript/components/commands.js +132 -0
- package/lib/parsers/typescript/components/commands.js.flow +151 -0
- package/lib/parsers/typescript/components/componentsUtils.js +471 -0
- package/lib/parsers/typescript/components/componentsUtils.js.flow +521 -0
- package/lib/parsers/typescript/components/events.js +240 -0
- package/lib/parsers/typescript/components/events.js.flow +262 -0
- package/lib/parsers/typescript/components/extends.js +88 -0
- package/lib/parsers/typescript/components/extends.js.flow +103 -0
- package/lib/parsers/typescript/components/index.js +277 -0
- package/lib/parsers/typescript/components/index.js.flow +239 -0
- package/lib/parsers/typescript/components/options.js +72 -0
- package/lib/parsers/typescript/components/options.js.flow +87 -0
- package/lib/parsers/typescript/components/props.js +39 -0
- package/lib/parsers/typescript/components/props.js.flow +48 -0
- package/lib/parsers/typescript/components/schema.js +106 -0
- package/lib/parsers/typescript/components/schema.js.flow +62 -0
- package/lib/parsers/typescript/index.js +85 -0
- package/lib/parsers/typescript/index.js.flow +94 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/failures.js +160 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/failures.js.flow +167 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js +701 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js.flow +727 -0
- package/lib/parsers/typescript/modules/index.js +543 -0
- package/lib/parsers/typescript/modules/index.js.flow +435 -0
- package/lib/parsers/typescript/parseTopLevelType.js +203 -0
- package/lib/parsers/typescript/parseTopLevelType.js.flow +243 -0
- package/lib/parsers/typescript/parser.js +105 -0
- package/lib/parsers/typescript/parser.js.flow +87 -0
- package/lib/parsers/typescript/utils.js +101 -0
- package/lib/parsers/typescript/utils.js.flow +115 -0
- package/lib/parsers/utils.js +273 -0
- package/lib/parsers/utils.js.flow +266 -0
- package/package.json +45 -0
|
@@ -0,0 +1,148 @@
|
|
|
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
|
+
type FilesOutput = Map<string, string>;
|
|
16
|
+
|
|
17
|
+
const {getModules} = require('./Utils');
|
|
18
|
+
|
|
19
|
+
const ModuleClassDeclarationTemplate = ({
|
|
20
|
+
hasteModuleName,
|
|
21
|
+
}: $ReadOnly<{hasteModuleName: string}>) => {
|
|
22
|
+
return `/**
|
|
23
|
+
* JNI C++ class for module '${hasteModuleName}'
|
|
24
|
+
*/
|
|
25
|
+
class JSI_EXPORT ${hasteModuleName}SpecJSI : public JavaTurboModule {
|
|
26
|
+
public:
|
|
27
|
+
${hasteModuleName}SpecJSI(const JavaTurboModule::InitParams ¶ms);
|
|
28
|
+
};
|
|
29
|
+
`;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const HeaderFileTemplate = ({
|
|
33
|
+
modules,
|
|
34
|
+
libraryName,
|
|
35
|
+
}: $ReadOnly<{modules: string, libraryName: string}>) => {
|
|
36
|
+
return `
|
|
37
|
+
/**
|
|
38
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
39
|
+
*
|
|
40
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
41
|
+
* once the code is regenerated.
|
|
42
|
+
*
|
|
43
|
+
* ${'@'}generated by codegen project: GenerateModuleJniH.js
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
#pragma once
|
|
47
|
+
|
|
48
|
+
#include <ReactCommon/JavaTurboModule.h>
|
|
49
|
+
#include <ReactCommon/TurboModule.h>
|
|
50
|
+
#include <jsi/jsi.h>
|
|
51
|
+
|
|
52
|
+
namespace facebook {
|
|
53
|
+
namespace react {
|
|
54
|
+
|
|
55
|
+
${modules}
|
|
56
|
+
|
|
57
|
+
JSI_EXPORT
|
|
58
|
+
std::shared_ptr<TurboModule> ${libraryName}_ModuleProvider(const std::string &moduleName, const JavaTurboModule::InitParams ¶ms);
|
|
59
|
+
|
|
60
|
+
} // namespace react
|
|
61
|
+
} // namespace facebook
|
|
62
|
+
`;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Note: this CMakeLists.txt template includes dependencies for both NativeModule and components.
|
|
66
|
+
const CMakeListsTemplate = ({
|
|
67
|
+
libraryName,
|
|
68
|
+
}: $ReadOnly<{libraryName: string}>) => {
|
|
69
|
+
return `# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
70
|
+
#
|
|
71
|
+
# This source code is licensed under the MIT license found in the
|
|
72
|
+
# LICENSE file in the root directory of this source tree.
|
|
73
|
+
|
|
74
|
+
cmake_minimum_required(VERSION 3.13)
|
|
75
|
+
set(CMAKE_VERBOSE_MAKEFILE on)
|
|
76
|
+
|
|
77
|
+
file(GLOB react_codegen_SRCS CONFIGURE_DEPENDS *.cpp react/renderer/components/${libraryName}/*.cpp)
|
|
78
|
+
|
|
79
|
+
add_library(
|
|
80
|
+
react_codegen_${libraryName}
|
|
81
|
+
SHARED
|
|
82
|
+
\${react_codegen_SRCS}
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
target_include_directories(react_codegen_${libraryName} PUBLIC . react/renderer/components/${libraryName})
|
|
86
|
+
|
|
87
|
+
target_link_libraries(
|
|
88
|
+
react_codegen_${libraryName}
|
|
89
|
+
fbjni
|
|
90
|
+
folly_runtime
|
|
91
|
+
glog
|
|
92
|
+
jsi
|
|
93
|
+
${libraryName !== 'rncore' ? 'react_codegen_rncore' : ''}
|
|
94
|
+
react_debug
|
|
95
|
+
react_nativemodule_core
|
|
96
|
+
react_render_core
|
|
97
|
+
react_render_debug
|
|
98
|
+
react_render_graphics
|
|
99
|
+
react_render_imagemanager
|
|
100
|
+
rrc_image
|
|
101
|
+
rrc_view
|
|
102
|
+
turbomodulejsijni
|
|
103
|
+
yoga
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
target_compile_options(
|
|
107
|
+
react_codegen_${libraryName}
|
|
108
|
+
PRIVATE
|
|
109
|
+
-DLOG_TAG=\\"ReactNative\\"
|
|
110
|
+
-fexceptions
|
|
111
|
+
-frtti
|
|
112
|
+
-std=c++17
|
|
113
|
+
-Wall
|
|
114
|
+
)
|
|
115
|
+
`;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
module.exports = {
|
|
119
|
+
generate(
|
|
120
|
+
libraryName: string,
|
|
121
|
+
schema: SchemaType,
|
|
122
|
+
packageName?: string,
|
|
123
|
+
assumeNonnull: boolean = false,
|
|
124
|
+
): FilesOutput {
|
|
125
|
+
const nativeModules = getModules(schema);
|
|
126
|
+
const modules = Object.keys(nativeModules)
|
|
127
|
+
.filter(hasteModuleName => {
|
|
128
|
+
const module = nativeModules[hasteModuleName];
|
|
129
|
+
return !(
|
|
130
|
+
module.excludedPlatforms != null &&
|
|
131
|
+
module.excludedPlatforms.includes('android')
|
|
132
|
+
);
|
|
133
|
+
})
|
|
134
|
+
.sort()
|
|
135
|
+
.map(hasteModuleName => ModuleClassDeclarationTemplate({hasteModuleName}))
|
|
136
|
+
.join('\n');
|
|
137
|
+
|
|
138
|
+
const fileName = `${libraryName}.h`;
|
|
139
|
+
const replacedTemplate = HeaderFileTemplate({
|
|
140
|
+
modules: modules,
|
|
141
|
+
libraryName: libraryName.replace(/-/g, '_'),
|
|
142
|
+
});
|
|
143
|
+
return new Map([
|
|
144
|
+
[`jni/${fileName}`, replacedTemplate],
|
|
145
|
+
['jni/CMakeLists.txt', CMakeListsTemplate({libraryName: libraryName})],
|
|
146
|
+
]);
|
|
147
|
+
},
|
|
148
|
+
};
|
|
@@ -0,0 +1,262 @@
|
|
|
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 _slicedToArray(arr, i) {
|
|
48
|
+
return (
|
|
49
|
+
_arrayWithHoles(arr) ||
|
|
50
|
+
_iterableToArrayLimit(arr, i) ||
|
|
51
|
+
_unsupportedIterableToArray(arr, i) ||
|
|
52
|
+
_nonIterableRest()
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
function _nonIterableRest() {
|
|
56
|
+
throw new TypeError(
|
|
57
|
+
'Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
61
|
+
if (!o) return;
|
|
62
|
+
if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
|
|
63
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
64
|
+
if (n === 'Object' && o.constructor) n = o.constructor.name;
|
|
65
|
+
if (n === 'Map' || n === 'Set') return Array.from(o);
|
|
66
|
+
if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
|
|
67
|
+
return _arrayLikeToArray(o, minLen);
|
|
68
|
+
}
|
|
69
|
+
function _arrayLikeToArray(arr, len) {
|
|
70
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
71
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
72
|
+
return arr2;
|
|
73
|
+
}
|
|
74
|
+
function _iterableToArrayLimit(arr, i) {
|
|
75
|
+
var _i =
|
|
76
|
+
null == arr
|
|
77
|
+
? null
|
|
78
|
+
: ('undefined' != typeof Symbol && arr[Symbol.iterator]) ||
|
|
79
|
+
arr['@@iterator'];
|
|
80
|
+
if (null != _i) {
|
|
81
|
+
var _s,
|
|
82
|
+
_e,
|
|
83
|
+
_x,
|
|
84
|
+
_r,
|
|
85
|
+
_arr = [],
|
|
86
|
+
_n = !0,
|
|
87
|
+
_d = !1;
|
|
88
|
+
try {
|
|
89
|
+
if (((_x = (_i = _i.call(arr)).next), 0 === i)) {
|
|
90
|
+
if (Object(_i) !== _i) return;
|
|
91
|
+
_n = !1;
|
|
92
|
+
} else
|
|
93
|
+
for (
|
|
94
|
+
;
|
|
95
|
+
!(_n = (_s = _x.call(_i)).done) &&
|
|
96
|
+
(_arr.push(_s.value), _arr.length !== i);
|
|
97
|
+
_n = !0
|
|
98
|
+
);
|
|
99
|
+
} catch (err) {
|
|
100
|
+
(_d = !0), (_e = err);
|
|
101
|
+
} finally {
|
|
102
|
+
try {
|
|
103
|
+
if (!_n && null != _i.return && ((_r = _i.return()), Object(_r) !== _r))
|
|
104
|
+
return;
|
|
105
|
+
} finally {
|
|
106
|
+
if (_d) throw _e;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return _arr;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function _arrayWithHoles(arr) {
|
|
113
|
+
if (Array.isArray(arr)) return arr;
|
|
114
|
+
}
|
|
115
|
+
function _defineProperty(obj, key, value) {
|
|
116
|
+
key = _toPropertyKey(key);
|
|
117
|
+
if (key in obj) {
|
|
118
|
+
Object.defineProperty(obj, key, {
|
|
119
|
+
value: value,
|
|
120
|
+
enumerable: true,
|
|
121
|
+
configurable: true,
|
|
122
|
+
writable: true,
|
|
123
|
+
});
|
|
124
|
+
} else {
|
|
125
|
+
obj[key] = value;
|
|
126
|
+
}
|
|
127
|
+
return obj;
|
|
128
|
+
}
|
|
129
|
+
function _toPropertyKey(arg) {
|
|
130
|
+
var key = _toPrimitive(arg, 'string');
|
|
131
|
+
return typeof key === 'symbol' ? key : String(key);
|
|
132
|
+
}
|
|
133
|
+
function _toPrimitive(input, hint) {
|
|
134
|
+
if (typeof input !== 'object' || input === null) return input;
|
|
135
|
+
var prim = input[Symbol.toPrimitive];
|
|
136
|
+
if (prim !== undefined) {
|
|
137
|
+
var res = prim.call(input, hint || 'default');
|
|
138
|
+
if (typeof res !== 'object') return res;
|
|
139
|
+
throw new TypeError('@@toPrimitive must return a primitive value.');
|
|
140
|
+
}
|
|
141
|
+
return (hint === 'string' ? String : Number)(input);
|
|
142
|
+
}
|
|
143
|
+
const _require = require('../../Utils'),
|
|
144
|
+
capitalize = _require.capitalize;
|
|
145
|
+
const _require2 = require('../../../parsers/parsers-commons'),
|
|
146
|
+
unwrapNullable = _require2.unwrapNullable,
|
|
147
|
+
wrapNullable = _require2.wrapNullable;
|
|
148
|
+
class StructCollector {
|
|
149
|
+
constructor() {
|
|
150
|
+
_defineProperty(this, '_structs', new Map());
|
|
151
|
+
}
|
|
152
|
+
process(structName, structContext, resolveAlias, nullableTypeAnnotation) {
|
|
153
|
+
const _unwrapNullable = unwrapNullable(nullableTypeAnnotation),
|
|
154
|
+
_unwrapNullable2 = _slicedToArray(_unwrapNullable, 2),
|
|
155
|
+
typeAnnotation = _unwrapNullable2[0],
|
|
156
|
+
nullable = _unwrapNullable2[1];
|
|
157
|
+
switch (typeAnnotation.type) {
|
|
158
|
+
case 'ObjectTypeAnnotation': {
|
|
159
|
+
this._insertStruct(
|
|
160
|
+
structName,
|
|
161
|
+
structContext,
|
|
162
|
+
resolveAlias,
|
|
163
|
+
typeAnnotation,
|
|
164
|
+
);
|
|
165
|
+
return wrapNullable(nullable, {
|
|
166
|
+
type: 'TypeAliasTypeAnnotation',
|
|
167
|
+
name: structName,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
case 'ArrayTypeAnnotation': {
|
|
171
|
+
if (typeAnnotation.elementType == null) {
|
|
172
|
+
return wrapNullable(nullable, {
|
|
173
|
+
type: 'ArrayTypeAnnotation',
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return wrapNullable(nullable, {
|
|
177
|
+
type: 'ArrayTypeAnnotation',
|
|
178
|
+
elementType: this.process(
|
|
179
|
+
structName + 'Element',
|
|
180
|
+
structContext,
|
|
181
|
+
resolveAlias,
|
|
182
|
+
typeAnnotation.elementType,
|
|
183
|
+
),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
case 'TypeAliasTypeAnnotation': {
|
|
187
|
+
this._insertAlias(typeAnnotation.name, structContext, resolveAlias);
|
|
188
|
+
return wrapNullable(nullable, typeAnnotation);
|
|
189
|
+
}
|
|
190
|
+
case 'EnumDeclaration':
|
|
191
|
+
return wrapNullable(nullable, typeAnnotation);
|
|
192
|
+
case 'MixedTypeAnnotation':
|
|
193
|
+
throw new Error('Mixed types are unsupported in structs');
|
|
194
|
+
case 'UnionTypeAnnotation':
|
|
195
|
+
throw new Error('Union types are unsupported in structs');
|
|
196
|
+
default: {
|
|
197
|
+
return wrapNullable(nullable, typeAnnotation);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
_insertAlias(aliasName, structContext, resolveAlias) {
|
|
202
|
+
const usedStruct = this._structs.get(aliasName);
|
|
203
|
+
if (usedStruct == null) {
|
|
204
|
+
this._insertStruct(
|
|
205
|
+
aliasName,
|
|
206
|
+
structContext,
|
|
207
|
+
resolveAlias,
|
|
208
|
+
resolveAlias(aliasName),
|
|
209
|
+
);
|
|
210
|
+
} else if (usedStruct.context !== structContext) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`Tried to use alias '${aliasName}' in a getConstants() return type and inside a regular struct.`,
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
_insertStruct(structName, structContext, resolveAlias, objectTypeAnnotation) {
|
|
217
|
+
// $FlowFixMe[missing-type-arg]
|
|
218
|
+
const properties = objectTypeAnnotation.properties.map(property => {
|
|
219
|
+
const propertyStructName = structName + capitalize(property.name);
|
|
220
|
+
return _objectSpread(
|
|
221
|
+
_objectSpread({}, property),
|
|
222
|
+
{},
|
|
223
|
+
{
|
|
224
|
+
typeAnnotation: this.process(
|
|
225
|
+
propertyStructName,
|
|
226
|
+
structContext,
|
|
227
|
+
resolveAlias,
|
|
228
|
+
property.typeAnnotation,
|
|
229
|
+
),
|
|
230
|
+
},
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
switch (structContext) {
|
|
234
|
+
case 'REGULAR':
|
|
235
|
+
this._structs.set(structName, {
|
|
236
|
+
name: structName,
|
|
237
|
+
context: 'REGULAR',
|
|
238
|
+
properties: properties,
|
|
239
|
+
});
|
|
240
|
+
break;
|
|
241
|
+
case 'CONSTANTS':
|
|
242
|
+
this._structs.set(structName, {
|
|
243
|
+
name: structName,
|
|
244
|
+
context: 'CONSTANTS',
|
|
245
|
+
properties: properties,
|
|
246
|
+
});
|
|
247
|
+
break;
|
|
248
|
+
default:
|
|
249
|
+
structContext;
|
|
250
|
+
throw new Error(`Detected an invalid struct context: ${structContext}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
getAllStructs() {
|
|
254
|
+
return [...this._structs.values()];
|
|
255
|
+
}
|
|
256
|
+
getStruct(name) {
|
|
257
|
+
return this._structs.get(name);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
module.exports = {
|
|
261
|
+
StructCollector,
|
|
262
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
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
|
+
Nullable,
|
|
15
|
+
NativeModuleObjectTypeAnnotation,
|
|
16
|
+
NativeModuleStringTypeAnnotation,
|
|
17
|
+
NativeModuleNumberTypeAnnotation,
|
|
18
|
+
NativeModuleInt32TypeAnnotation,
|
|
19
|
+
NativeModuleDoubleTypeAnnotation,
|
|
20
|
+
NativeModuleFloatTypeAnnotation,
|
|
21
|
+
NativeModuleBooleanTypeAnnotation,
|
|
22
|
+
NativeModuleEnumDeclaration,
|
|
23
|
+
NativeModuleGenericObjectTypeAnnotation,
|
|
24
|
+
ReservedTypeAnnotation,
|
|
25
|
+
NativeModuleTypeAliasTypeAnnotation,
|
|
26
|
+
NativeModuleArrayTypeAnnotation,
|
|
27
|
+
NativeModuleBaseTypeAnnotation,
|
|
28
|
+
} from '../../../CodegenSchema';
|
|
29
|
+
|
|
30
|
+
import type {AliasResolver} from '../Utils';
|
|
31
|
+
|
|
32
|
+
const {capitalize} = require('../../Utils');
|
|
33
|
+
const {
|
|
34
|
+
unwrapNullable,
|
|
35
|
+
wrapNullable,
|
|
36
|
+
} = require('../../../parsers/parsers-commons');
|
|
37
|
+
|
|
38
|
+
type StructContext = 'CONSTANTS' | 'REGULAR';
|
|
39
|
+
|
|
40
|
+
export type RegularStruct = $ReadOnly<{
|
|
41
|
+
context: 'REGULAR',
|
|
42
|
+
name: string,
|
|
43
|
+
properties: $ReadOnlyArray<StructProperty>,
|
|
44
|
+
}>;
|
|
45
|
+
|
|
46
|
+
export type ConstantsStruct = $ReadOnly<{
|
|
47
|
+
context: 'CONSTANTS',
|
|
48
|
+
name: string,
|
|
49
|
+
properties: $ReadOnlyArray<StructProperty>,
|
|
50
|
+
}>;
|
|
51
|
+
|
|
52
|
+
export type Struct = RegularStruct | ConstantsStruct;
|
|
53
|
+
|
|
54
|
+
export type StructProperty = $ReadOnly<{
|
|
55
|
+
name: string,
|
|
56
|
+
optional: boolean,
|
|
57
|
+
typeAnnotation: Nullable<StructTypeAnnotation>,
|
|
58
|
+
}>;
|
|
59
|
+
|
|
60
|
+
export type StructTypeAnnotation =
|
|
61
|
+
| NativeModuleStringTypeAnnotation
|
|
62
|
+
| NativeModuleNumberTypeAnnotation
|
|
63
|
+
| NativeModuleInt32TypeAnnotation
|
|
64
|
+
| NativeModuleDoubleTypeAnnotation
|
|
65
|
+
| NativeModuleFloatTypeAnnotation
|
|
66
|
+
| NativeModuleBooleanTypeAnnotation
|
|
67
|
+
| NativeModuleEnumDeclaration
|
|
68
|
+
| NativeModuleGenericObjectTypeAnnotation
|
|
69
|
+
| ReservedTypeAnnotation
|
|
70
|
+
| NativeModuleTypeAliasTypeAnnotation
|
|
71
|
+
| NativeModuleArrayTypeAnnotation<Nullable<StructTypeAnnotation>>;
|
|
72
|
+
|
|
73
|
+
class StructCollector {
|
|
74
|
+
_structs: Map<string, Struct> = new Map();
|
|
75
|
+
|
|
76
|
+
process(
|
|
77
|
+
structName: string,
|
|
78
|
+
structContext: StructContext,
|
|
79
|
+
resolveAlias: AliasResolver,
|
|
80
|
+
nullableTypeAnnotation: Nullable<NativeModuleBaseTypeAnnotation>,
|
|
81
|
+
): Nullable<StructTypeAnnotation> {
|
|
82
|
+
const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation);
|
|
83
|
+
switch (typeAnnotation.type) {
|
|
84
|
+
case 'ObjectTypeAnnotation': {
|
|
85
|
+
this._insertStruct(
|
|
86
|
+
structName,
|
|
87
|
+
structContext,
|
|
88
|
+
resolveAlias,
|
|
89
|
+
typeAnnotation,
|
|
90
|
+
);
|
|
91
|
+
return wrapNullable(nullable, {
|
|
92
|
+
type: 'TypeAliasTypeAnnotation',
|
|
93
|
+
name: structName,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
case 'ArrayTypeAnnotation': {
|
|
97
|
+
if (typeAnnotation.elementType == null) {
|
|
98
|
+
return wrapNullable(nullable, {
|
|
99
|
+
type: 'ArrayTypeAnnotation',
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return wrapNullable(nullable, {
|
|
104
|
+
type: 'ArrayTypeAnnotation',
|
|
105
|
+
elementType: this.process(
|
|
106
|
+
structName + 'Element',
|
|
107
|
+
structContext,
|
|
108
|
+
resolveAlias,
|
|
109
|
+
typeAnnotation.elementType,
|
|
110
|
+
),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
case 'TypeAliasTypeAnnotation': {
|
|
114
|
+
this._insertAlias(typeAnnotation.name, structContext, resolveAlias);
|
|
115
|
+
return wrapNullable(nullable, typeAnnotation);
|
|
116
|
+
}
|
|
117
|
+
case 'EnumDeclaration':
|
|
118
|
+
return wrapNullable(nullable, typeAnnotation);
|
|
119
|
+
case 'MixedTypeAnnotation':
|
|
120
|
+
throw new Error('Mixed types are unsupported in structs');
|
|
121
|
+
case 'UnionTypeAnnotation':
|
|
122
|
+
throw new Error('Union types are unsupported in structs');
|
|
123
|
+
default: {
|
|
124
|
+
return wrapNullable(nullable, typeAnnotation);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
_insertAlias(
|
|
130
|
+
aliasName: string,
|
|
131
|
+
structContext: StructContext,
|
|
132
|
+
resolveAlias: AliasResolver,
|
|
133
|
+
): void {
|
|
134
|
+
const usedStruct = this._structs.get(aliasName);
|
|
135
|
+
if (usedStruct == null) {
|
|
136
|
+
this._insertStruct(
|
|
137
|
+
aliasName,
|
|
138
|
+
structContext,
|
|
139
|
+
resolveAlias,
|
|
140
|
+
resolveAlias(aliasName),
|
|
141
|
+
);
|
|
142
|
+
} else if (usedStruct.context !== structContext) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Tried to use alias '${aliasName}' in a getConstants() return type and inside a regular struct.`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
_insertStruct(
|
|
150
|
+
structName: string,
|
|
151
|
+
structContext: StructContext,
|
|
152
|
+
resolveAlias: AliasResolver,
|
|
153
|
+
objectTypeAnnotation: NativeModuleObjectTypeAnnotation,
|
|
154
|
+
): void {
|
|
155
|
+
// $FlowFixMe[missing-type-arg]
|
|
156
|
+
const properties = objectTypeAnnotation.properties.map<
|
|
157
|
+
$ReadOnly<{
|
|
158
|
+
name: string,
|
|
159
|
+
optional: boolean,
|
|
160
|
+
typeAnnotation: Nullable<StructTypeAnnotation>,
|
|
161
|
+
}>,
|
|
162
|
+
>(property => {
|
|
163
|
+
const propertyStructName = structName + capitalize(property.name);
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
...property,
|
|
167
|
+
typeAnnotation: this.process(
|
|
168
|
+
propertyStructName,
|
|
169
|
+
structContext,
|
|
170
|
+
resolveAlias,
|
|
171
|
+
property.typeAnnotation,
|
|
172
|
+
),
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
switch (structContext) {
|
|
177
|
+
case 'REGULAR':
|
|
178
|
+
this._structs.set(structName, {
|
|
179
|
+
name: structName,
|
|
180
|
+
context: 'REGULAR',
|
|
181
|
+
properties: properties,
|
|
182
|
+
});
|
|
183
|
+
break;
|
|
184
|
+
case 'CONSTANTS':
|
|
185
|
+
this._structs.set(structName, {
|
|
186
|
+
name: structName,
|
|
187
|
+
context: 'CONSTANTS',
|
|
188
|
+
properties: properties,
|
|
189
|
+
});
|
|
190
|
+
break;
|
|
191
|
+
default:
|
|
192
|
+
(structContext: empty);
|
|
193
|
+
throw new Error(`Detected an invalid struct context: ${structContext}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
getAllStructs(): $ReadOnlyArray<Struct> {
|
|
198
|
+
return [...this._structs.values()];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
getStruct(name: string): ?Struct {
|
|
202
|
+
return this._structs.get(name);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
module.exports = {
|
|
207
|
+
StructCollector,
|
|
208
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
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 getSafePropertyName(property) {
|
|
14
|
+
if (property.name === 'id') {
|
|
15
|
+
return `${property.name}_`;
|
|
16
|
+
}
|
|
17
|
+
return property.name;
|
|
18
|
+
}
|
|
19
|
+
function getNamespacedStructName(hasteModuleName, structName) {
|
|
20
|
+
return `JS::${hasteModuleName}::${structName}`;
|
|
21
|
+
}
|
|
22
|
+
module.exports = {
|
|
23
|
+
getSafePropertyName,
|
|
24
|
+
getNamespacedStructName,
|
|
25
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
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 {StructProperty} from './StructCollector';
|
|
14
|
+
|
|
15
|
+
function getSafePropertyName(property: StructProperty): string {
|
|
16
|
+
if (property.name === 'id') {
|
|
17
|
+
return `${property.name}_`;
|
|
18
|
+
}
|
|
19
|
+
return property.name;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getNamespacedStructName(
|
|
23
|
+
hasteModuleName: string,
|
|
24
|
+
structName: string,
|
|
25
|
+
): string {
|
|
26
|
+
return `JS::${hasteModuleName}::${structName}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = {
|
|
30
|
+
getSafePropertyName,
|
|
31
|
+
getNamespacedStructName,
|
|
32
|
+
};
|