@react-native/codegen 0.72.4 → 0.73.0-nightly-20230603-fd9e295be
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 +2 -2
- package/lib/CodegenSchema.d.ts +15 -4
- package/lib/CodegenSchema.js.flow +33 -21
- package/lib/generators/components/CppHelpers.js +39 -2
- package/lib/generators/components/CppHelpers.js.flow +54 -5
- package/lib/generators/components/GenerateEventEmitterCpp.js +238 -55
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +233 -55
- package/lib/generators/components/GenerateEventEmitterH.js +77 -28
- package/lib/generators/components/GenerateEventEmitterH.js.flow +87 -32
- package/lib/generators/components/GeneratePropsH.js +1 -2
- package/lib/generators/components/GeneratePropsH.js.flow +1 -2
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js +0 -1
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +3 -5
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js +4 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js.flow +4 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js +2 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js.flow +2 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js +114 -1
- package/lib/generators/components/__test_fixtures__/fixtures.js.flow +114 -1
- package/lib/generators/modules/GenerateModuleCpp.js +3 -3
- package/lib/generators/modules/GenerateModuleCpp.js.flow +3 -3
- package/lib/generators/modules/GenerateModuleH.js +1 -1
- package/lib/generators/modules/GenerateModuleH.js.flow +1 -1
- package/lib/generators/modules/GenerateModuleJavaSpec.js +1 -2
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +1 -2
- package/lib/parsers/error-utils.js +44 -0
- package/lib/parsers/error-utils.js.flow +59 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +51 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +51 -0
- package/lib/parsers/flow/components/componentsUtils.js +6 -6
- package/lib/parsers/flow/components/componentsUtils.js.flow +10 -10
- package/lib/parsers/flow/components/events.js +157 -78
- package/lib/parsers/flow/components/events.js.flow +158 -77
- package/lib/parsers/flow/components/index.js +28 -105
- package/lib/parsers/flow/components/index.js.flow +21 -110
- package/lib/parsers/flow/modules/index.js +39 -49
- package/lib/parsers/flow/modules/index.js.flow +22 -31
- package/lib/parsers/flow/parser.d.ts +9 -2
- package/lib/parsers/flow/parser.js +167 -16
- package/lib/parsers/flow/parser.js.flow +209 -6
- package/lib/parsers/flow/utils.js +0 -63
- package/lib/parsers/flow/utils.js.flow +1 -77
- package/lib/parsers/parser.js.flow +141 -1
- package/lib/parsers/parserMock.js +165 -2
- package/lib/parsers/parserMock.js.flow +216 -2
- package/lib/parsers/parsers-commons.js +128 -9
- package/lib/parsers/parsers-commons.js.flow +182 -12
- package/lib/parsers/parsers-primitives.js +84 -35
- package/lib/parsers/parsers-primitives.js.flow +110 -35
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +51 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +51 -0
- package/lib/parsers/typescript/components/componentsUtils.js +31 -7
- package/lib/parsers/typescript/components/componentsUtils.js.flow +31 -14
- package/lib/parsers/typescript/components/events.js +142 -67
- package/lib/parsers/typescript/components/events.js.flow +149 -68
- package/lib/parsers/typescript/components/extends.js +2 -49
- package/lib/parsers/typescript/components/extends.js.flow +1 -55
- package/lib/parsers/typescript/components/index.js +29 -109
- package/lib/parsers/typescript/components/index.js.flow +21 -116
- package/lib/parsers/typescript/modules/index.js +41 -50
- package/lib/parsers/typescript/modules/index.js.flow +23 -31
- package/lib/parsers/typescript/parser.d.ts +9 -2
- package/lib/parsers/typescript/parser.js +193 -7
- package/lib/parsers/typescript/parser.js.flow +235 -6
- package/lib/parsers/utils.js.flow +6 -0
- package/package.json +26 -15
- package/lib/parsers/flow/components/extends.js +0 -52
- package/lib/parsers/flow/components/extends.js.flow +0 -66
- package/lib/parsers/flow/components/props.js +0 -47
- package/lib/parsers/flow/components/props.js.flow +0 -60
- package/lib/parsers/typescript/components/props.js +0 -39
- package/lib/parsers/typescript/components/props.js.flow +0 -48
- package/lib/parsers/typescript/utils.js +0 -90
- package/lib/parsers/typescript/utils.js.flow +0 -104
|
@@ -12,28 +12,6 @@
|
|
|
12
12
|
|
|
13
13
|
const _require = require('../parseTopLevelType'),
|
|
14
14
|
parseTopLevelType = _require.parseTopLevelType;
|
|
15
|
-
const _require2 = require('./componentsUtils.js'),
|
|
16
|
-
flattenProperties = _require2.flattenProperties;
|
|
17
|
-
function extendsForProp(prop, types) {
|
|
18
|
-
if (!prop.expression) {
|
|
19
|
-
console.log('null', prop);
|
|
20
|
-
}
|
|
21
|
-
const name = prop.expression.name;
|
|
22
|
-
if (types[name] != null) {
|
|
23
|
-
// This type is locally defined in the file
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
switch (name) {
|
|
27
|
-
case 'ViewProps':
|
|
28
|
-
return {
|
|
29
|
-
type: 'ReactNativeBuiltInType',
|
|
30
|
-
knownTypeName: 'ReactNativeCoreViewProps',
|
|
31
|
-
};
|
|
32
|
-
default: {
|
|
33
|
-
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
15
|
function isEvent(typeAnnotation) {
|
|
38
16
|
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
39
17
|
return false;
|
|
@@ -41,35 +19,12 @@ function isEvent(typeAnnotation) {
|
|
|
41
19
|
const eventNames = new Set(['BubblingEventHandler', 'DirectEventHandler']);
|
|
42
20
|
return eventNames.has(typeAnnotation.typeName.name);
|
|
43
21
|
}
|
|
44
|
-
function isProp(name, typeAnnotation) {
|
|
45
|
-
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
const isStyle =
|
|
49
|
-
name === 'style' &&
|
|
50
|
-
typeAnnotation.type === 'GenericTypeAnnotation' &&
|
|
51
|
-
typeAnnotation.typeName.name === 'ViewStyleProp';
|
|
52
|
-
return !isStyle;
|
|
53
|
-
}
|
|
54
22
|
|
|
55
23
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
56
24
|
|
|
57
|
-
function categorizeProps(typeDefinition, types,
|
|
58
|
-
|
|
25
|
+
function categorizeProps(typeDefinition, types, events) {
|
|
26
|
+
// find events
|
|
59
27
|
for (const prop of typeDefinition) {
|
|
60
|
-
// find extends
|
|
61
|
-
if (prop.type === 'TSExpressionWithTypeArguments') {
|
|
62
|
-
const extend = extendsForProp(prop, types);
|
|
63
|
-
if (extend) {
|
|
64
|
-
extendsProps.push(extend);
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
remaining.push(prop);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// find events and props
|
|
72
|
-
for (const prop of flattenProperties(remaining, types)) {
|
|
73
28
|
if (prop.type === 'TSPropertySignature') {
|
|
74
29
|
const topLevelType = parseTopLevelType(
|
|
75
30
|
prop.typeAnnotation.typeAnnotation,
|
|
@@ -77,8 +32,6 @@ function categorizeProps(typeDefinition, types, extendsProps, props, events) {
|
|
|
77
32
|
);
|
|
78
33
|
if (isEvent(topLevelType.type)) {
|
|
79
34
|
events.push(prop);
|
|
80
|
-
} else if (isProp(prop.key.name, prop)) {
|
|
81
|
-
props.push(prop);
|
|
82
35
|
}
|
|
83
36
|
}
|
|
84
37
|
}
|
|
@@ -10,33 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
|
|
14
13
|
import type {TypeDeclarationMap} from '../../utils';
|
|
15
14
|
const {parseTopLevelType} = require('../parseTopLevelType');
|
|
16
|
-
const {flattenProperties} = require('./componentsUtils.js');
|
|
17
|
-
|
|
18
|
-
function extendsForProp(prop: PropsAST, types: TypeDeclarationMap) {
|
|
19
|
-
if (!prop.expression) {
|
|
20
|
-
console.log('null', prop);
|
|
21
|
-
}
|
|
22
|
-
const name = prop.expression.name;
|
|
23
|
-
|
|
24
|
-
if (types[name] != null) {
|
|
25
|
-
// This type is locally defined in the file
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
switch (name) {
|
|
30
|
-
case 'ViewProps':
|
|
31
|
-
return {
|
|
32
|
-
type: 'ReactNativeBuiltInType',
|
|
33
|
-
knownTypeName: 'ReactNativeCoreViewProps',
|
|
34
|
-
};
|
|
35
|
-
default: {
|
|
36
|
-
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
15
|
|
|
41
16
|
function isEvent(typeAnnotation: $FlowFixMe): boolean {
|
|
42
17
|
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
@@ -46,43 +21,16 @@ function isEvent(typeAnnotation: $FlowFixMe): boolean {
|
|
|
46
21
|
return eventNames.has(typeAnnotation.typeName.name);
|
|
47
22
|
}
|
|
48
23
|
|
|
49
|
-
function isProp(name: string, typeAnnotation: $FlowFixMe): boolean {
|
|
50
|
-
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
const isStyle =
|
|
54
|
-
name === 'style' &&
|
|
55
|
-
typeAnnotation.type === 'GenericTypeAnnotation' &&
|
|
56
|
-
typeAnnotation.typeName.name === 'ViewStyleProp';
|
|
57
|
-
return !isStyle;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
24
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
61
25
|
type PropsAST = Object;
|
|
62
26
|
|
|
63
27
|
function categorizeProps(
|
|
64
28
|
typeDefinition: $ReadOnlyArray<PropsAST>,
|
|
65
29
|
types: TypeDeclarationMap,
|
|
66
|
-
extendsProps: Array<ExtendsPropsShape>,
|
|
67
|
-
props: Array<PropsAST>,
|
|
68
30
|
events: Array<PropsAST>,
|
|
69
31
|
): void {
|
|
70
|
-
|
|
32
|
+
// find events
|
|
71
33
|
for (const prop of typeDefinition) {
|
|
72
|
-
// find extends
|
|
73
|
-
if (prop.type === 'TSExpressionWithTypeArguments') {
|
|
74
|
-
const extend = extendsForProp(prop, types);
|
|
75
|
-
if (extend) {
|
|
76
|
-
extendsProps.push(extend);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
remaining.push(prop);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// find events and props
|
|
85
|
-
for (const prop of flattenProperties(remaining, types)) {
|
|
86
34
|
if (prop.type === 'TSPropertySignature') {
|
|
87
35
|
const topLevelType = parseTopLevelType(
|
|
88
36
|
prop.typeAnnotation.typeAnnotation,
|
|
@@ -91,8 +39,6 @@ function categorizeProps(
|
|
|
91
39
|
|
|
92
40
|
if (isEvent(topLevelType.type)) {
|
|
93
41
|
events.push(prop);
|
|
94
|
-
} else if (isProp(prop.key.name, prop)) {
|
|
95
|
-
props.push(prop);
|
|
96
42
|
}
|
|
97
43
|
}
|
|
98
44
|
}
|
|
@@ -16,100 +16,33 @@ const _require2 = require('./events'),
|
|
|
16
16
|
getEvents = _require2.getEvents;
|
|
17
17
|
const _require3 = require('./extends'),
|
|
18
18
|
categorizeProps = _require3.categorizeProps;
|
|
19
|
-
const _require4 = require('./
|
|
20
|
-
|
|
21
|
-
const _require5 = require('
|
|
22
|
-
|
|
23
|
-
const _require6 = require('../../
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
|
|
33
|
-
function findComponentConfig(ast, parser) {
|
|
34
|
-
const foundConfigs = [];
|
|
35
|
-
const defaultExports = ast.body.filter(
|
|
36
|
-
node => node.type === 'ExportDefaultDeclaration',
|
|
37
|
-
);
|
|
38
|
-
defaultExports.forEach(statement =>
|
|
39
|
-
findNativeComponentType(statement, foundConfigs, parser),
|
|
40
|
-
);
|
|
41
|
-
if (foundConfigs.length === 0) {
|
|
42
|
-
throw new Error('Could not find component config for native component');
|
|
43
|
-
}
|
|
44
|
-
if (foundConfigs.length > 1) {
|
|
45
|
-
throw new Error('Only one component is supported per file');
|
|
46
|
-
}
|
|
47
|
-
const foundConfig = foundConfigs[0];
|
|
48
|
-
const namedExports = ast.body.filter(
|
|
49
|
-
node => node.type === 'ExportNamedDeclaration',
|
|
50
|
-
);
|
|
51
|
-
const commandsTypeNames = namedExports
|
|
52
|
-
.map(statement => {
|
|
53
|
-
let callExpression;
|
|
54
|
-
let calleeName;
|
|
55
|
-
try {
|
|
56
|
-
callExpression = statement.declaration.declarations[0].init;
|
|
57
|
-
calleeName = callExpression.callee.name;
|
|
58
|
-
} catch (e) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (calleeName !== 'codegenNativeCommands') {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// const statement.declaration.declarations[0].init
|
|
66
|
-
if (callExpression.arguments.length !== 1) {
|
|
67
|
-
throw new Error(
|
|
68
|
-
'codegenNativeCommands must be passed options including the supported commands',
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
const typeArgumentParam = callExpression.typeParameters.params[0];
|
|
72
|
-
if (typeArgumentParam.type !== 'TSTypeReference') {
|
|
73
|
-
throw new Error(
|
|
74
|
-
"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
commandTypeName: typeArgumentParam.typeName.name,
|
|
79
|
-
commandOptionsExpression: callExpression.arguments[0],
|
|
80
|
-
};
|
|
81
|
-
})
|
|
82
|
-
.filter(Boolean);
|
|
83
|
-
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
|
|
84
|
-
return createComponentConfig(foundConfig, commandsTypeNames);
|
|
85
|
-
}
|
|
86
|
-
function getCommandProperties(
|
|
87
|
-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
88
|
-
* LTI update could not be added via codemod */
|
|
89
|
-
commandTypeName,
|
|
90
|
-
types,
|
|
91
|
-
commandOptions,
|
|
92
|
-
) {
|
|
19
|
+
const _require4 = require('./componentsUtils.js'),
|
|
20
|
+
getProperties = _require4.getProperties;
|
|
21
|
+
const _require5 = require('../../error-utils'),
|
|
22
|
+
throwIfTypeAliasIsNotInterface = _require5.throwIfTypeAliasIsNotInterface;
|
|
23
|
+
const _require6 = require('../../parsers-commons'),
|
|
24
|
+
propertyNames = _require6.propertyNames,
|
|
25
|
+
getCommandOptions = _require6.getCommandOptions,
|
|
26
|
+
getOptions = _require6.getOptions,
|
|
27
|
+
findComponentConfig = _require6.findComponentConfig;
|
|
28
|
+
function getCommandProperties(ast, parser) {
|
|
29
|
+
const _findComponentConfig = findComponentConfig(ast, parser),
|
|
30
|
+
commandTypeName = _findComponentConfig.commandTypeName,
|
|
31
|
+
commandOptionsExpression = _findComponentConfig.commandOptionsExpression;
|
|
93
32
|
if (commandTypeName == null) {
|
|
94
33
|
return [];
|
|
95
34
|
}
|
|
35
|
+
const types = parser.getTypes(ast);
|
|
96
36
|
const typeAlias = types[commandTypeName];
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
let properties;
|
|
103
|
-
try {
|
|
104
|
-
properties = typeAlias.body.body;
|
|
105
|
-
} catch (e) {
|
|
37
|
+
throwIfTypeAliasIsNotInterface(typeAlias, parser);
|
|
38
|
+
const properties = parser.bodyProperties(typeAlias);
|
|
39
|
+
if (!properties) {
|
|
106
40
|
throw new Error(
|
|
107
41
|
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen typescript file`,
|
|
108
42
|
);
|
|
109
43
|
}
|
|
110
|
-
const typeScriptPropertyNames = properties
|
|
111
|
-
|
|
112
|
-
.filter(Boolean);
|
|
44
|
+
const typeScriptPropertyNames = propertyNames(properties);
|
|
45
|
+
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
113
46
|
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
114
47
|
throw new Error(
|
|
115
48
|
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
@@ -135,33 +68,20 @@ function getCommandProperties(
|
|
|
135
68
|
|
|
136
69
|
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
|
|
137
70
|
function buildComponentSchema(ast, parser) {
|
|
138
|
-
const
|
|
139
|
-
componentName =
|
|
140
|
-
propsTypeName =
|
|
141
|
-
|
|
142
|
-
commandOptionsExpression = _findComponentConfig.commandOptionsExpression,
|
|
143
|
-
optionsExpression = _findComponentConfig.optionsExpression;
|
|
71
|
+
const _findComponentConfig2 = findComponentConfig(ast, parser),
|
|
72
|
+
componentName = _findComponentConfig2.componentName,
|
|
73
|
+
propsTypeName = _findComponentConfig2.propsTypeName,
|
|
74
|
+
optionsExpression = _findComponentConfig2.optionsExpression;
|
|
144
75
|
const types = parser.getTypes(ast);
|
|
145
76
|
const propProperties = getProperties(propsTypeName, types);
|
|
146
|
-
const
|
|
147
|
-
const commandProperties = getCommandProperties(
|
|
148
|
-
commandTypeName,
|
|
149
|
-
types,
|
|
150
|
-
commandOptions,
|
|
151
|
-
);
|
|
77
|
+
const commandProperties = getCommandProperties(ast, parser);
|
|
152
78
|
const options = getOptions(optionsExpression);
|
|
153
|
-
const extendsProps = [];
|
|
154
|
-
const componentPropAsts = [];
|
|
155
79
|
const componentEventAsts = [];
|
|
156
|
-
categorizeProps(
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
extendsProps
|
|
160
|
-
|
|
161
|
-
componentEventAsts,
|
|
162
|
-
);
|
|
163
|
-
const props = getProps(componentPropAsts, types);
|
|
164
|
-
const events = getEvents(componentEventAsts, types);
|
|
80
|
+
categorizeProps(propProperties, types, componentEventAsts);
|
|
81
|
+
const _parser$getProps = parser.getProps(propProperties, types),
|
|
82
|
+
props = _parser$getProps.props,
|
|
83
|
+
extendsProps = _parser$getProps.extendsProps;
|
|
84
|
+
const events = getEvents(componentEventAsts, types, parser);
|
|
165
85
|
const commands = getCommands(commandProperties, types);
|
|
166
86
|
return {
|
|
167
87
|
filename: componentName,
|
|
@@ -9,124 +9,45 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
|
-
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
|
|
13
12
|
import type {Parser} from '../../parser';
|
|
14
|
-
import type {TypeDeclarationMap} from '../../utils';
|
|
15
|
-
import type {CommandOptions} from '../../parsers-commons';
|
|
16
13
|
import type {ComponentSchemaBuilderConfig} from '../../schema.js';
|
|
17
14
|
|
|
18
15
|
const {getCommands} = require('./commands');
|
|
19
16
|
const {getEvents} = require('./events');
|
|
20
17
|
const {categorizeProps} = require('./extends');
|
|
21
|
-
const {getProps} = require('./props');
|
|
22
18
|
const {getProperties} = require('./componentsUtils.js');
|
|
23
|
-
const {
|
|
19
|
+
const {throwIfTypeAliasIsNotInterface} = require('../../error-utils');
|
|
24
20
|
const {
|
|
25
|
-
|
|
26
|
-
findNativeComponentType,
|
|
21
|
+
propertyNames,
|
|
27
22
|
getCommandOptions,
|
|
28
23
|
getOptions,
|
|
24
|
+
findComponentConfig,
|
|
29
25
|
} = require('../../parsers-commons');
|
|
30
26
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const defaultExports = ast.body.filter(
|
|
36
|
-
node => node.type === 'ExportDefaultDeclaration',
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
defaultExports.forEach(statement =>
|
|
40
|
-
findNativeComponentType(statement, foundConfigs, parser),
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
if (foundConfigs.length === 0) {
|
|
44
|
-
throw new Error('Could not find component config for native component');
|
|
45
|
-
}
|
|
46
|
-
if (foundConfigs.length > 1) {
|
|
47
|
-
throw new Error('Only one component is supported per file');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const foundConfig = foundConfigs[0];
|
|
51
|
-
|
|
52
|
-
const namedExports = ast.body.filter(
|
|
53
|
-
node => node.type === 'ExportNamedDeclaration',
|
|
27
|
+
function getCommandProperties(ast: $FlowFixMe, parser: Parser) {
|
|
28
|
+
const {commandTypeName, commandOptionsExpression} = findComponentConfig(
|
|
29
|
+
ast,
|
|
30
|
+
parser,
|
|
54
31
|
);
|
|
55
|
-
|
|
56
|
-
const commandsTypeNames = namedExports
|
|
57
|
-
.map(statement => {
|
|
58
|
-
let callExpression;
|
|
59
|
-
let calleeName;
|
|
60
|
-
try {
|
|
61
|
-
callExpression = statement.declaration.declarations[0].init;
|
|
62
|
-
calleeName = callExpression.callee.name;
|
|
63
|
-
} catch (e) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (calleeName !== 'codegenNativeCommands') {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// const statement.declaration.declarations[0].init
|
|
72
|
-
if (callExpression.arguments.length !== 1) {
|
|
73
|
-
throw new Error(
|
|
74
|
-
'codegenNativeCommands must be passed options including the supported commands',
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const typeArgumentParam = callExpression.typeParameters.params[0];
|
|
79
|
-
|
|
80
|
-
if (typeArgumentParam.type !== 'TSTypeReference') {
|
|
81
|
-
throw new Error(
|
|
82
|
-
"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return {
|
|
87
|
-
commandTypeName: typeArgumentParam.typeName.name,
|
|
88
|
-
commandOptionsExpression: callExpression.arguments[0],
|
|
89
|
-
};
|
|
90
|
-
})
|
|
91
|
-
.filter(Boolean);
|
|
92
|
-
|
|
93
|
-
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
|
|
94
|
-
|
|
95
|
-
return createComponentConfig(foundConfig, commandsTypeNames);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function getCommandProperties(
|
|
99
|
-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
100
|
-
* LTI update could not be added via codemod */
|
|
101
|
-
commandTypeName,
|
|
102
|
-
types: TypeDeclarationMap,
|
|
103
|
-
commandOptions: ?CommandOptions,
|
|
104
|
-
) {
|
|
105
32
|
if (commandTypeName == null) {
|
|
106
33
|
return [];
|
|
107
34
|
}
|
|
108
35
|
|
|
36
|
+
const types = parser.getTypes(ast);
|
|
109
37
|
const typeAlias = types[commandTypeName];
|
|
110
38
|
|
|
111
|
-
|
|
112
|
-
throw new Error(
|
|
113
|
-
`The type argument for codegenNativeCommands must be an interface, received ${typeAlias.type}`,
|
|
114
|
-
);
|
|
115
|
-
}
|
|
39
|
+
throwIfTypeAliasIsNotInterface(typeAlias, parser);
|
|
116
40
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
properties = typeAlias.body.body;
|
|
120
|
-
} catch (e) {
|
|
41
|
+
const properties = parser.bodyProperties(typeAlias);
|
|
42
|
+
if (!properties) {
|
|
121
43
|
throw new Error(
|
|
122
44
|
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen typescript file`,
|
|
123
45
|
);
|
|
124
46
|
}
|
|
125
47
|
|
|
126
|
-
const typeScriptPropertyNames = properties
|
|
127
|
-
.map(property => property && property.key && property.key.name)
|
|
128
|
-
.filter(Boolean);
|
|
48
|
+
const typeScriptPropertyNames = propertyNames(properties);
|
|
129
49
|
|
|
50
|
+
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
130
51
|
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
131
52
|
throw new Error(
|
|
132
53
|
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
@@ -158,39 +79,23 @@ function buildComponentSchema(
|
|
|
158
79
|
ast: $FlowFixMe,
|
|
159
80
|
parser: Parser,
|
|
160
81
|
): ComponentSchemaBuilderConfig {
|
|
161
|
-
const {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
commandOptionsExpression,
|
|
166
|
-
optionsExpression,
|
|
167
|
-
} = findComponentConfig(ast, parser);
|
|
82
|
+
const {componentName, propsTypeName, optionsExpression} = findComponentConfig(
|
|
83
|
+
ast,
|
|
84
|
+
parser,
|
|
85
|
+
);
|
|
168
86
|
|
|
169
87
|
const types = parser.getTypes(ast);
|
|
170
88
|
|
|
171
89
|
const propProperties = getProperties(propsTypeName, types);
|
|
172
|
-
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
173
90
|
|
|
174
|
-
const commandProperties = getCommandProperties(
|
|
175
|
-
commandTypeName,
|
|
176
|
-
types,
|
|
177
|
-
commandOptions,
|
|
178
|
-
);
|
|
91
|
+
const commandProperties = getCommandProperties(ast, parser);
|
|
179
92
|
|
|
180
93
|
const options = getOptions(optionsExpression);
|
|
181
94
|
|
|
182
|
-
const extendsProps: Array<ExtendsPropsShape> = [];
|
|
183
|
-
const componentPropAsts: Array<PropsAST> = [];
|
|
184
95
|
const componentEventAsts: Array<PropsAST> = [];
|
|
185
|
-
categorizeProps(
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
extendsProps,
|
|
189
|
-
componentPropAsts,
|
|
190
|
-
componentEventAsts,
|
|
191
|
-
);
|
|
192
|
-
const props = getProps(componentPropAsts, types);
|
|
193
|
-
const events = getEvents(componentEventAsts, types);
|
|
96
|
+
categorizeProps(propProperties, types, componentEventAsts);
|
|
97
|
+
const {props, extendsProps} = parser.getProps(propProperties, types);
|
|
98
|
+
const events = getEvents(componentEventAsts, types, parser);
|
|
194
99
|
const commands = getCommands(commandProperties, types);
|
|
195
100
|
|
|
196
101
|
return {
|
|
@@ -14,30 +14,23 @@ const _require = require('../parseTopLevelType'),
|
|
|
14
14
|
flattenIntersectionType = _require.flattenIntersectionType;
|
|
15
15
|
const _require2 = require('../components/componentsUtils'),
|
|
16
16
|
flattenProperties = _require2.flattenProperties;
|
|
17
|
-
const _require3 = require('
|
|
18
|
-
|
|
19
|
-
const _require4 = require('../../parsers-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
emitUnion = _require5.emitUnion,
|
|
33
|
-
emitCommonTypes = _require5.emitCommonTypes,
|
|
34
|
-
typeAliasResolution = _require5.typeAliasResolution,
|
|
35
|
-
typeEnumResolution = _require5.typeEnumResolution,
|
|
36
|
-
translateArrayTypeAnnotation = _require5.translateArrayTypeAnnotation;
|
|
37
|
-
const _require6 = require('../../errors'),
|
|
38
|
-
UnsupportedGenericParserError = _require6.UnsupportedGenericParserError,
|
|
17
|
+
const _require3 = require('../../parsers-commons'),
|
|
18
|
+
parseObjectProperty = _require3.parseObjectProperty;
|
|
19
|
+
const _require4 = require('../../parsers-primitives'),
|
|
20
|
+
emitArrayType = _require4.emitArrayType,
|
|
21
|
+
emitFunction = _require4.emitFunction,
|
|
22
|
+
emitDictionary = _require4.emitDictionary,
|
|
23
|
+
emitPromise = _require4.emitPromise,
|
|
24
|
+
emitRootTag = _require4.emitRootTag,
|
|
25
|
+
emitUnion = _require4.emitUnion,
|
|
26
|
+
emitCommonTypes = _require4.emitCommonTypes,
|
|
27
|
+
typeAliasResolution = _require4.typeAliasResolution,
|
|
28
|
+
typeEnumResolution = _require4.typeEnumResolution,
|
|
29
|
+
translateArrayTypeAnnotation = _require4.translateArrayTypeAnnotation;
|
|
30
|
+
const _require5 = require('../../errors'),
|
|
31
|
+
UnsupportedGenericParserError = _require5.UnsupportedGenericParserError,
|
|
39
32
|
UnsupportedTypeAnnotationParserError =
|
|
40
|
-
|
|
33
|
+
_require5.UnsupportedTypeAnnotationParserError;
|
|
41
34
|
function translateObjectTypeAnnotation(
|
|
42
35
|
hasteModuleName,
|
|
43
36
|
/**
|
|
@@ -173,13 +166,16 @@ function translateTypeAnnotation(
|
|
|
173
166
|
cxxOnly,
|
|
174
167
|
parser,
|
|
175
168
|
) {
|
|
176
|
-
const
|
|
169
|
+
const _parser$getResolvedTy = parser.getResolvedTypeAnnotation(
|
|
177
170
|
typeScriptTypeAnnotation,
|
|
178
171
|
types,
|
|
172
|
+
parser,
|
|
179
173
|
),
|
|
180
|
-
nullable =
|
|
181
|
-
typeAnnotation =
|
|
182
|
-
typeResolutionStatus =
|
|
174
|
+
nullable = _parser$getResolvedTy.nullable,
|
|
175
|
+
typeAnnotation = _parser$getResolvedTy.typeAnnotation,
|
|
176
|
+
typeResolutionStatus = _parser$getResolvedTy.typeResolutionStatus;
|
|
177
|
+
const resolveTypeaAnnotationFn = parser.getResolveTypeAnnotationFN();
|
|
178
|
+
resolveTypeaAnnotationFn(typeScriptTypeAnnotation, types, parser);
|
|
183
179
|
switch (typeAnnotation.type) {
|
|
184
180
|
case 'TSArrayType': {
|
|
185
181
|
return translateArrayTypeAnnotation(
|
|
@@ -303,7 +299,7 @@ function translateTypeAnnotation(
|
|
|
303
299
|
// check the property type to prevent developers from using unsupported types
|
|
304
300
|
// the return value from `translateTypeAnnotation` is unused
|
|
305
301
|
const propertyType = indexSignatures[0].typeAnnotation;
|
|
306
|
-
translateTypeAnnotation(
|
|
302
|
+
const valueType = translateTypeAnnotation(
|
|
307
303
|
hasteModuleName,
|
|
308
304
|
propertyType,
|
|
309
305
|
types,
|
|
@@ -314,7 +310,7 @@ function translateTypeAnnotation(
|
|
|
314
310
|
parser,
|
|
315
311
|
);
|
|
316
312
|
// no need to do further checking
|
|
317
|
-
return
|
|
313
|
+
return emitDictionary(nullable, valueType);
|
|
318
314
|
}
|
|
319
315
|
}
|
|
320
316
|
return translateObjectTypeAnnotation(
|
|
@@ -341,18 +337,6 @@ function translateTypeAnnotation(
|
|
|
341
337
|
parser,
|
|
342
338
|
);
|
|
343
339
|
}
|
|
344
|
-
case 'TSBooleanKeyword': {
|
|
345
|
-
return emitBoolean(nullable);
|
|
346
|
-
}
|
|
347
|
-
case 'TSNumberKeyword': {
|
|
348
|
-
return emitNumber(nullable);
|
|
349
|
-
}
|
|
350
|
-
case 'TSVoidKeyword': {
|
|
351
|
-
return emitVoid(nullable);
|
|
352
|
-
}
|
|
353
|
-
case 'TSStringKeyword': {
|
|
354
|
-
return emitString(nullable);
|
|
355
|
-
}
|
|
356
340
|
case 'TSFunctionType': {
|
|
357
341
|
return emitFunction(
|
|
358
342
|
nullable,
|
|
@@ -370,19 +354,26 @@ function translateTypeAnnotation(
|
|
|
370
354
|
case 'TSUnionType': {
|
|
371
355
|
return emitUnion(nullable, hasteModuleName, typeAnnotation, parser);
|
|
372
356
|
}
|
|
373
|
-
case 'TSUnknownKeyword': {
|
|
374
|
-
if (cxxOnly) {
|
|
375
|
-
return emitMixed(nullable);
|
|
376
|
-
}
|
|
377
|
-
// Fallthrough
|
|
378
|
-
}
|
|
379
|
-
|
|
380
357
|
default: {
|
|
381
|
-
|
|
358
|
+
const commonType = emitCommonTypes(
|
|
382
359
|
hasteModuleName,
|
|
360
|
+
types,
|
|
383
361
|
typeAnnotation,
|
|
384
|
-
|
|
362
|
+
aliasMap,
|
|
363
|
+
enumMap,
|
|
364
|
+
tryParse,
|
|
365
|
+
cxxOnly,
|
|
366
|
+
nullable,
|
|
367
|
+
parser,
|
|
385
368
|
);
|
|
369
|
+
if (!commonType) {
|
|
370
|
+
throw new UnsupportedTypeAnnotationParserError(
|
|
371
|
+
hasteModuleName,
|
|
372
|
+
typeAnnotation,
|
|
373
|
+
parser.language(),
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
return commonType;
|
|
386
377
|
}
|
|
387
378
|
}
|
|
388
379
|
}
|