@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,267 @@
|
|
|
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
|
+
const _require = require('../utils'),
|
|
76
|
+
getTypes = _require.getTypes;
|
|
77
|
+
const _require2 = require('./commands'),
|
|
78
|
+
getCommands = _require2.getCommands;
|
|
79
|
+
const _require3 = require('./events'),
|
|
80
|
+
getEvents = _require3.getEvents;
|
|
81
|
+
const _require4 = require('./extends'),
|
|
82
|
+
getExtendsProps = _require4.getExtendsProps,
|
|
83
|
+
removeKnownExtends = _require4.removeKnownExtends;
|
|
84
|
+
const _require5 = require('./options'),
|
|
85
|
+
getCommandOptions = _require5.getCommandOptions,
|
|
86
|
+
getOptions = _require5.getOptions;
|
|
87
|
+
const _require6 = require('./props'),
|
|
88
|
+
getProps = _require6.getProps;
|
|
89
|
+
const _require7 = require('./componentsUtils.js'),
|
|
90
|
+
getProperties = _require7.getProperties;
|
|
91
|
+
|
|
92
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
93
|
+
* LTI update could not be added via codemod */
|
|
94
|
+
function findComponentConfig(ast) {
|
|
95
|
+
const foundConfigs = [];
|
|
96
|
+
const defaultExports = ast.body.filter(
|
|
97
|
+
node => node.type === 'ExportDefaultDeclaration',
|
|
98
|
+
);
|
|
99
|
+
defaultExports.forEach(statement => {
|
|
100
|
+
let declaration = statement.declaration;
|
|
101
|
+
|
|
102
|
+
// codegenNativeComponent can be nested inside a cast
|
|
103
|
+
// expression so we need to go one level deeper
|
|
104
|
+
if (declaration.type === 'TypeCastExpression') {
|
|
105
|
+
declaration = declaration.expression;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
if (declaration.callee.name === 'codegenNativeComponent') {
|
|
109
|
+
const typeArgumentParams = declaration.typeArguments.params;
|
|
110
|
+
const funcArgumentParams = declaration.arguments;
|
|
111
|
+
const nativeComponentType = {
|
|
112
|
+
propsTypeName: typeArgumentParams[0].id.name,
|
|
113
|
+
componentName: funcArgumentParams[0].value,
|
|
114
|
+
};
|
|
115
|
+
if (funcArgumentParams.length > 1) {
|
|
116
|
+
nativeComponentType.optionsExpression = funcArgumentParams[1];
|
|
117
|
+
}
|
|
118
|
+
foundConfigs.push(nativeComponentType);
|
|
119
|
+
}
|
|
120
|
+
} catch (e) {
|
|
121
|
+
// ignore
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
if (foundConfigs.length === 0) {
|
|
125
|
+
throw new Error('Could not find component config for native component');
|
|
126
|
+
}
|
|
127
|
+
if (foundConfigs.length > 1) {
|
|
128
|
+
throw new Error('Only one component is supported per file');
|
|
129
|
+
}
|
|
130
|
+
const foundConfig = foundConfigs[0];
|
|
131
|
+
const namedExports = ast.body.filter(
|
|
132
|
+
node => node.type === 'ExportNamedDeclaration',
|
|
133
|
+
);
|
|
134
|
+
const commandsTypeNames = namedExports
|
|
135
|
+
.map(statement => {
|
|
136
|
+
let callExpression;
|
|
137
|
+
let calleeName;
|
|
138
|
+
try {
|
|
139
|
+
callExpression = statement.declaration.declarations[0].init;
|
|
140
|
+
calleeName = callExpression.callee.name;
|
|
141
|
+
} catch (e) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (calleeName !== 'codegenNativeCommands') {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// const statement.declaration.declarations[0].init
|
|
149
|
+
if (callExpression.arguments.length !== 1) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
'codegenNativeCommands must be passed options including the supported commands',
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
const typeArgumentParam = callExpression.typeArguments.params[0];
|
|
155
|
+
if (typeArgumentParam.type !== 'GenericTypeAnnotation') {
|
|
156
|
+
throw new Error(
|
|
157
|
+
"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
commandTypeName: typeArgumentParam.id.name,
|
|
162
|
+
commandOptionsExpression: callExpression.arguments[0],
|
|
163
|
+
};
|
|
164
|
+
})
|
|
165
|
+
.filter(Boolean);
|
|
166
|
+
if (commandsTypeNames.length > 1) {
|
|
167
|
+
throw new Error('codegenNativeCommands may only be called once in a file');
|
|
168
|
+
}
|
|
169
|
+
return _objectSpread(
|
|
170
|
+
_objectSpread({}, foundConfig),
|
|
171
|
+
{},
|
|
172
|
+
{
|
|
173
|
+
commandTypeName:
|
|
174
|
+
commandsTypeNames[0] == null
|
|
175
|
+
? null
|
|
176
|
+
: commandsTypeNames[0].commandTypeName,
|
|
177
|
+
commandOptionsExpression:
|
|
178
|
+
commandsTypeNames[0] == null
|
|
179
|
+
? null
|
|
180
|
+
: commandsTypeNames[0].commandOptionsExpression,
|
|
181
|
+
},
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
function getCommandProperties(
|
|
185
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
186
|
+
* LTI update could not be added via codemod */
|
|
187
|
+
commandTypeName,
|
|
188
|
+
types,
|
|
189
|
+
commandOptions,
|
|
190
|
+
) {
|
|
191
|
+
if (commandTypeName == null) {
|
|
192
|
+
return [];
|
|
193
|
+
}
|
|
194
|
+
const typeAlias = types[commandTypeName];
|
|
195
|
+
if (typeAlias.type !== 'InterfaceDeclaration') {
|
|
196
|
+
throw new Error(
|
|
197
|
+
`The type argument for codegenNativeCommands must be an interface, received ${typeAlias.type}`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
let properties;
|
|
201
|
+
try {
|
|
202
|
+
properties = typeAlias.body.properties;
|
|
203
|
+
} catch (e) {
|
|
204
|
+
throw new Error(
|
|
205
|
+
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen flow file`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
const flowPropertyNames = properties
|
|
209
|
+
.map(property => property && property.key && property.key.name)
|
|
210
|
+
.filter(Boolean);
|
|
211
|
+
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
if (
|
|
217
|
+
commandOptions.supportedCommands.length !== flowPropertyNames.length ||
|
|
218
|
+
!commandOptions.supportedCommands.every(supportedCommand =>
|
|
219
|
+
flowPropertyNames.includes(supportedCommand),
|
|
220
|
+
)
|
|
221
|
+
) {
|
|
222
|
+
throw new Error(
|
|
223
|
+
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${flowPropertyNames.join(
|
|
224
|
+
', ',
|
|
225
|
+
)}`,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
return properties;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
|
|
232
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
233
|
+
* LTI update could not be added via codemod */
|
|
234
|
+
function buildComponentSchema(ast) {
|
|
235
|
+
const _findComponentConfig = findComponentConfig(ast),
|
|
236
|
+
componentName = _findComponentConfig.componentName,
|
|
237
|
+
propsTypeName = _findComponentConfig.propsTypeName,
|
|
238
|
+
commandTypeName = _findComponentConfig.commandTypeName,
|
|
239
|
+
commandOptionsExpression = _findComponentConfig.commandOptionsExpression,
|
|
240
|
+
optionsExpression = _findComponentConfig.optionsExpression;
|
|
241
|
+
const types = getTypes(ast);
|
|
242
|
+
const propProperties = getProperties(propsTypeName, types);
|
|
243
|
+
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
244
|
+
const commandProperties = getCommandProperties(
|
|
245
|
+
commandTypeName,
|
|
246
|
+
types,
|
|
247
|
+
commandOptions,
|
|
248
|
+
);
|
|
249
|
+
const extendsProps = getExtendsProps(propProperties, types);
|
|
250
|
+
const options = getOptions(optionsExpression);
|
|
251
|
+
const nonExtendsProps = removeKnownExtends(propProperties, types);
|
|
252
|
+
const props = getProps(nonExtendsProps, types);
|
|
253
|
+
const events = getEvents(propProperties, types);
|
|
254
|
+
const commands = getCommands(commandProperties, types);
|
|
255
|
+
return {
|
|
256
|
+
filename: componentName,
|
|
257
|
+
componentName,
|
|
258
|
+
options,
|
|
259
|
+
extendsProps,
|
|
260
|
+
events,
|
|
261
|
+
props,
|
|
262
|
+
commands,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
module.exports = {
|
|
266
|
+
buildComponentSchema,
|
|
267
|
+
};
|
|
@@ -0,0 +1,226 @@
|
|
|
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
|
+
import type {TypeDeclarationMap} from '../../utils';
|
|
13
|
+
import type {CommandOptions} from './options';
|
|
14
|
+
import type {ComponentSchemaBuilderConfig} from './schema.js';
|
|
15
|
+
|
|
16
|
+
const {getTypes} = require('../utils');
|
|
17
|
+
const {getCommands} = require('./commands');
|
|
18
|
+
const {getEvents} = require('./events');
|
|
19
|
+
const {getExtendsProps, removeKnownExtends} = require('./extends');
|
|
20
|
+
const {getCommandOptions, getOptions} = require('./options');
|
|
21
|
+
const {getProps} = require('./props');
|
|
22
|
+
const {getProperties} = require('./componentsUtils.js');
|
|
23
|
+
|
|
24
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
25
|
+
* LTI update could not be added via codemod */
|
|
26
|
+
function findComponentConfig(ast) {
|
|
27
|
+
const foundConfigs = [];
|
|
28
|
+
|
|
29
|
+
const defaultExports = ast.body.filter(
|
|
30
|
+
node => node.type === 'ExportDefaultDeclaration',
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
defaultExports.forEach(statement => {
|
|
34
|
+
let declaration = statement.declaration;
|
|
35
|
+
|
|
36
|
+
// codegenNativeComponent can be nested inside a cast
|
|
37
|
+
// expression so we need to go one level deeper
|
|
38
|
+
if (declaration.type === 'TypeCastExpression') {
|
|
39
|
+
declaration = declaration.expression;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
if (declaration.callee.name === 'codegenNativeComponent') {
|
|
44
|
+
const typeArgumentParams = declaration.typeArguments.params;
|
|
45
|
+
const funcArgumentParams = declaration.arguments;
|
|
46
|
+
|
|
47
|
+
const nativeComponentType: {[string]: string} = {
|
|
48
|
+
propsTypeName: typeArgumentParams[0].id.name,
|
|
49
|
+
componentName: funcArgumentParams[0].value,
|
|
50
|
+
};
|
|
51
|
+
if (funcArgumentParams.length > 1) {
|
|
52
|
+
nativeComponentType.optionsExpression = funcArgumentParams[1];
|
|
53
|
+
}
|
|
54
|
+
foundConfigs.push(nativeComponentType);
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
// ignore
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (foundConfigs.length === 0) {
|
|
62
|
+
throw new Error('Could not find component config for native component');
|
|
63
|
+
}
|
|
64
|
+
if (foundConfigs.length > 1) {
|
|
65
|
+
throw new Error('Only one component is supported per file');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const foundConfig = foundConfigs[0];
|
|
69
|
+
|
|
70
|
+
const namedExports = ast.body.filter(
|
|
71
|
+
node => node.type === 'ExportNamedDeclaration',
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const commandsTypeNames = namedExports
|
|
75
|
+
.map(statement => {
|
|
76
|
+
let callExpression;
|
|
77
|
+
let calleeName;
|
|
78
|
+
try {
|
|
79
|
+
callExpression = statement.declaration.declarations[0].init;
|
|
80
|
+
calleeName = callExpression.callee.name;
|
|
81
|
+
} catch (e) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (calleeName !== 'codegenNativeCommands') {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// const statement.declaration.declarations[0].init
|
|
90
|
+
if (callExpression.arguments.length !== 1) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
'codegenNativeCommands must be passed options including the supported commands',
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const typeArgumentParam = callExpression.typeArguments.params[0];
|
|
97
|
+
|
|
98
|
+
if (typeArgumentParam.type !== 'GenericTypeAnnotation') {
|
|
99
|
+
throw new Error(
|
|
100
|
+
"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
commandTypeName: typeArgumentParam.id.name,
|
|
106
|
+
commandOptionsExpression: callExpression.arguments[0],
|
|
107
|
+
};
|
|
108
|
+
})
|
|
109
|
+
.filter(Boolean);
|
|
110
|
+
|
|
111
|
+
if (commandsTypeNames.length > 1) {
|
|
112
|
+
throw new Error('codegenNativeCommands may only be called once in a file');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
...foundConfig,
|
|
117
|
+
commandTypeName:
|
|
118
|
+
commandsTypeNames[0] == null
|
|
119
|
+
? null
|
|
120
|
+
: commandsTypeNames[0].commandTypeName,
|
|
121
|
+
commandOptionsExpression:
|
|
122
|
+
commandsTypeNames[0] == null
|
|
123
|
+
? null
|
|
124
|
+
: commandsTypeNames[0].commandOptionsExpression,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function getCommandProperties(
|
|
129
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
130
|
+
* LTI update could not be added via codemod */
|
|
131
|
+
commandTypeName,
|
|
132
|
+
types: TypeDeclarationMap,
|
|
133
|
+
commandOptions: ?CommandOptions,
|
|
134
|
+
) {
|
|
135
|
+
if (commandTypeName == null) {
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const typeAlias = types[commandTypeName];
|
|
140
|
+
|
|
141
|
+
if (typeAlias.type !== 'InterfaceDeclaration') {
|
|
142
|
+
throw new Error(
|
|
143
|
+
`The type argument for codegenNativeCommands must be an interface, received ${typeAlias.type}`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let properties;
|
|
148
|
+
try {
|
|
149
|
+
properties = typeAlias.body.properties;
|
|
150
|
+
} catch (e) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen flow file`,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const flowPropertyNames = properties
|
|
157
|
+
.map(property => property && property.key && property.key.name)
|
|
158
|
+
.filter(Boolean);
|
|
159
|
+
|
|
160
|
+
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (
|
|
167
|
+
commandOptions.supportedCommands.length !== flowPropertyNames.length ||
|
|
168
|
+
!commandOptions.supportedCommands.every(supportedCommand =>
|
|
169
|
+
flowPropertyNames.includes(supportedCommand),
|
|
170
|
+
)
|
|
171
|
+
) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${flowPropertyNames.join(
|
|
174
|
+
', ',
|
|
175
|
+
)}`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return properties;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
|
|
183
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
184
|
+
* LTI update could not be added via codemod */
|
|
185
|
+
function buildComponentSchema(ast): ComponentSchemaBuilderConfig {
|
|
186
|
+
const {
|
|
187
|
+
componentName,
|
|
188
|
+
propsTypeName,
|
|
189
|
+
commandTypeName,
|
|
190
|
+
commandOptionsExpression,
|
|
191
|
+
optionsExpression,
|
|
192
|
+
} = findComponentConfig(ast);
|
|
193
|
+
|
|
194
|
+
const types = getTypes(ast);
|
|
195
|
+
|
|
196
|
+
const propProperties = getProperties(propsTypeName, types);
|
|
197
|
+
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
198
|
+
|
|
199
|
+
const commandProperties = getCommandProperties(
|
|
200
|
+
commandTypeName,
|
|
201
|
+
types,
|
|
202
|
+
commandOptions,
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const extendsProps = getExtendsProps(propProperties, types);
|
|
206
|
+
const options = getOptions(optionsExpression);
|
|
207
|
+
|
|
208
|
+
const nonExtendsProps = removeKnownExtends(propProperties, types);
|
|
209
|
+
const props = getProps(nonExtendsProps, types);
|
|
210
|
+
const events = getEvents(propProperties, types);
|
|
211
|
+
const commands = getCommands(commandProperties, types);
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
filename: componentName,
|
|
215
|
+
componentName,
|
|
216
|
+
options,
|
|
217
|
+
extendsProps,
|
|
218
|
+
events,
|
|
219
|
+
props,
|
|
220
|
+
commands,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
module.exports = {
|
|
225
|
+
buildComponentSchema,
|
|
226
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
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[unclear-type] there's no flowtype for ASTs
|
|
14
|
+
|
|
15
|
+
function getCommandOptions(commandOptionsExpression) {
|
|
16
|
+
if (commandOptionsExpression == null) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
let foundOptions;
|
|
20
|
+
try {
|
|
21
|
+
foundOptions = commandOptionsExpression.properties.reduce(
|
|
22
|
+
(options, prop) => {
|
|
23
|
+
options[prop.key.name] = (
|
|
24
|
+
(prop && prop.value && prop.value.elements) ||
|
|
25
|
+
[]
|
|
26
|
+
).map(element => element && element.value);
|
|
27
|
+
return options;
|
|
28
|
+
},
|
|
29
|
+
{},
|
|
30
|
+
);
|
|
31
|
+
} catch (e) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
'Failed to parse command options, please check that they are defined correctly',
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
return foundOptions;
|
|
37
|
+
}
|
|
38
|
+
function getOptions(optionsExpression) {
|
|
39
|
+
if (!optionsExpression) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
let foundOptions;
|
|
43
|
+
try {
|
|
44
|
+
foundOptions = optionsExpression.properties.reduce((options, prop) => {
|
|
45
|
+
if (prop.value.type === 'ArrayExpression') {
|
|
46
|
+
options[prop.key.name] = prop.value.elements.map(
|
|
47
|
+
element => element.value,
|
|
48
|
+
);
|
|
49
|
+
} else {
|
|
50
|
+
options[prop.key.name] = prop.value.value;
|
|
51
|
+
}
|
|
52
|
+
return options;
|
|
53
|
+
}, {});
|
|
54
|
+
} catch (e) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
'Failed to parse codegen options, please check that they are defined correctly',
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
if (
|
|
60
|
+
foundOptions.paperComponentName &&
|
|
61
|
+
foundOptions.paperComponentNameDeprecated
|
|
62
|
+
) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
'Failed to parse codegen options, cannot use both paperComponentName and paperComponentNameDeprecated',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return foundOptions;
|
|
68
|
+
}
|
|
69
|
+
module.exports = {
|
|
70
|
+
getCommandOptions,
|
|
71
|
+
getOptions,
|
|
72
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
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 {OptionsShape} from '../../../CodegenSchema.js';
|
|
14
|
+
|
|
15
|
+
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
|
|
16
|
+
type OptionsAST = Object;
|
|
17
|
+
|
|
18
|
+
export type CommandOptions = $ReadOnly<{
|
|
19
|
+
supportedCommands: $ReadOnlyArray<string>,
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
function getCommandOptions(
|
|
23
|
+
commandOptionsExpression: OptionsAST,
|
|
24
|
+
): ?CommandOptions {
|
|
25
|
+
if (commandOptionsExpression == null) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let foundOptions;
|
|
30
|
+
try {
|
|
31
|
+
foundOptions = commandOptionsExpression.properties.reduce(
|
|
32
|
+
(options, prop) => {
|
|
33
|
+
options[prop.key.name] = (
|
|
34
|
+
(prop && prop.value && prop.value.elements) ||
|
|
35
|
+
[]
|
|
36
|
+
).map(element => element && element.value);
|
|
37
|
+
return options;
|
|
38
|
+
},
|
|
39
|
+
{},
|
|
40
|
+
);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
'Failed to parse command options, please check that they are defined correctly',
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return foundOptions;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
|
|
51
|
+
if (!optionsExpression) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
let foundOptions;
|
|
55
|
+
try {
|
|
56
|
+
foundOptions = optionsExpression.properties.reduce((options, prop) => {
|
|
57
|
+
if (prop.value.type === 'ArrayExpression') {
|
|
58
|
+
options[prop.key.name] = prop.value.elements.map(
|
|
59
|
+
element => element.value,
|
|
60
|
+
);
|
|
61
|
+
} else {
|
|
62
|
+
options[prop.key.name] = prop.value.value;
|
|
63
|
+
}
|
|
64
|
+
return options;
|
|
65
|
+
}, {});
|
|
66
|
+
} catch (e) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
'Failed to parse codegen options, please check that they are defined correctly',
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
foundOptions.paperComponentName &&
|
|
74
|
+
foundOptions.paperComponentNameDeprecated
|
|
75
|
+
) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
'Failed to parse codegen options, cannot use both paperComponentName and paperComponentNameDeprecated',
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return foundOptions;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = {
|
|
85
|
+
getCommandOptions,
|
|
86
|
+
getOptions,
|
|
87
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
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('./componentsUtils.js'),
|
|
14
|
+
flattenProperties = _require.flattenProperties,
|
|
15
|
+
getSchemaInfo = _require.getSchemaInfo,
|
|
16
|
+
getTypeAnnotation = _require.getTypeAnnotation;
|
|
17
|
+
function buildPropSchema(property, types) {
|
|
18
|
+
const info = getSchemaInfo(property, types);
|
|
19
|
+
if (info == null) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const name = info.name,
|
|
23
|
+
optional = info.optional,
|
|
24
|
+
typeAnnotation = info.typeAnnotation,
|
|
25
|
+
defaultValue = info.defaultValue,
|
|
26
|
+
withNullDefault = info.withNullDefault;
|
|
27
|
+
return {
|
|
28
|
+
name,
|
|
29
|
+
optional,
|
|
30
|
+
typeAnnotation: getTypeAnnotation(
|
|
31
|
+
name,
|
|
32
|
+
typeAnnotation,
|
|
33
|
+
defaultValue,
|
|
34
|
+
withNullDefault,
|
|
35
|
+
types,
|
|
36
|
+
buildPropSchema,
|
|
37
|
+
),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function getProps(typeDefinition, types) {
|
|
41
|
+
return flattenProperties(typeDefinition, types)
|
|
42
|
+
.map(property => buildPropSchema(property, types))
|
|
43
|
+
.filter(Boolean);
|
|
44
|
+
}
|
|
45
|
+
module.exports = {
|
|
46
|
+
getProps,
|
|
47
|
+
};
|