@react-native/codegen 0.72.1 → 0.72.2
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/lib/CodegenSchema.d.ts +348 -0
- package/lib/CodegenSchema.js.flow +31 -3
- package/lib/SchemaValidator.d.ts +11 -0
- package/lib/cli/combine/combine-js-to-schema.js +8 -8
- package/lib/cli/combine/combine-js-to-schema.js.flow +8 -7
- package/lib/cli/parser/parser.js +8 -15
- package/lib/cli/parser/parser.js.flow +8 -14
- package/lib/generators/Utils.js +16 -0
- package/lib/generators/Utils.js.flow +20 -0
- package/lib/generators/__test_fixtures__/fixtures.js +2 -1
- package/lib/generators/__test_fixtures__/fixtures.js.flow +2 -1
- package/lib/generators/components/ComponentsGeneratorUtils.js +10 -1
- package/lib/generators/components/ComponentsGeneratorUtils.js.flow +12 -2
- package/lib/generators/components/CppHelpers.js +8 -15
- package/lib/generators/components/CppHelpers.js.flow +8 -19
- package/lib/generators/components/GenerateEventEmitterCpp.js +7 -3
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +7 -3
- package/lib/generators/components/GenerateEventEmitterH.js +2 -2
- package/lib/generators/components/GenerateEventEmitterH.js.flow +1 -2
- package/lib/generators/components/GeneratePropsH.js +3 -4
- package/lib/generators/components/GeneratePropsH.js.flow +2 -4
- package/lib/generators/components/GeneratePropsJavaDelegate.js +2 -0
- package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +2 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js +3 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js.flow +3 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +8 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +12 -0
- package/lib/generators/components/GenerateTests.js +3 -2
- package/lib/generators/components/GenerateTests.js.flow +3 -1
- package/lib/generators/components/GenerateViewConfigJs.js +5 -2
- package/lib/generators/components/GenerateViewConfigJs.js.flow +5 -2
- package/lib/generators/components/JavaHelpers.js +9 -0
- package/lib/generators/components/JavaHelpers.js.flow +12 -1
- package/lib/generators/components/__test_fixtures__/fixtures.js +41 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js.flow +42 -0
- package/lib/generators/modules/GenerateModuleCpp.js +9 -4
- package/lib/generators/modules/GenerateModuleCpp.js.flow +11 -3
- package/lib/generators/modules/GenerateModuleH.js +185 -33
- package/lib/generators/modules/GenerateModuleH.js.flow +203 -25
- package/lib/generators/modules/GenerateModuleJavaSpec.js +2 -2
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +5 -5
- package/lib/generators/modules/GenerateModuleJniCpp.js +2 -2
- package/lib/generators/modules/GenerateModuleJniCpp.js.flow +5 -5
- package/lib/generators/modules/GenerateModuleObjCpp/index.js +2 -2
- package/lib/generators/modules/GenerateModuleObjCpp/index.js.flow +2 -2
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +2 -2
- package/lib/generators/modules/Utils.js +4 -0
- package/lib/generators/modules/Utils.js.flow +6 -0
- package/lib/generators/modules/__test_fixtures__/fixtures.js +152 -10
- package/lib/generators/modules/__test_fixtures__/fixtures.js.flow +152 -10
- package/lib/parsers/error-utils.js +1 -19
- package/lib/parsers/error-utils.js.flow +1 -19
- package/lib/parsers/errors.d.ts +10 -0
- package/lib/parsers/errors.js +9 -29
- package/lib/parsers/errors.js.flow +2 -20
- package/lib/parsers/flow/Visitor.js +37 -0
- package/lib/parsers/flow/Visitor.js.flow +41 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +20 -2
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +20 -2
- package/lib/parsers/flow/components/commands.js +3 -0
- package/lib/parsers/flow/components/commands.js.flow +2 -1
- package/lib/parsers/flow/components/componentsUtils.js +10 -0
- package/lib/parsers/flow/components/componentsUtils.js.flow +11 -1
- package/lib/parsers/flow/components/events.js.flow +1 -1
- package/lib/parsers/flow/components/extends.js.flow +1 -1
- package/lib/parsers/flow/components/index.js.flow +1 -1
- package/lib/parsers/flow/components/options.js.flow +1 -1
- package/lib/parsers/flow/components/props.js.flow +1 -1
- package/lib/parsers/flow/modules/__test_fixtures__/failures.js +54 -0
- package/lib/parsers/flow/modules/__test_fixtures__/failures.js.flow +56 -0
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +64 -1
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +66 -1
- package/lib/parsers/flow/modules/index.js +99 -25
- package/lib/parsers/flow/modules/index.js.flow +106 -22
- package/lib/parsers/flow/parser.d.ts +10 -0
- package/lib/parsers/flow/parser.js +122 -32
- package/lib/parsers/flow/parser.js.flow +144 -24
- package/lib/parsers/flow/utils.js +33 -19
- package/lib/parsers/flow/utils.js.flow +37 -22
- package/lib/parsers/parser.d.ts +17 -0
- package/lib/parsers/parser.js.flow +102 -17
- package/lib/parsers/parserMock.js +87 -30
- package/lib/parsers/parserMock.js.flow +118 -24
- package/lib/parsers/parsers-commons.js +121 -119
- package/lib/parsers/parsers-commons.js.flow +130 -131
- package/lib/parsers/parsers-primitives.js +75 -12
- package/lib/parsers/parsers-primitives.js.flow +92 -13
- package/lib/parsers/schema/index.d.ts +10 -0
- package/lib/parsers/typescript/Visitor.js +42 -0
- package/lib/parsers/typescript/Visitor.js.flow +47 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +22 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +22 -0
- package/lib/parsers/typescript/components/commands.js +3 -0
- package/lib/parsers/typescript/components/commands.js.flow +2 -1
- package/lib/parsers/typescript/components/componentsUtils.js +5 -0
- package/lib/parsers/typescript/components/componentsUtils.js.flow +6 -1
- package/lib/parsers/typescript/components/events.js.flow +1 -1
- package/lib/parsers/typescript/components/extends.js.flow +1 -1
- package/lib/parsers/typescript/components/index.js.flow +1 -1
- package/lib/parsers/typescript/components/options.js.flow +1 -1
- package/lib/parsers/typescript/components/props.js.flow +1 -1
- package/lib/parsers/typescript/modules/__test_fixtures__/failures.js +52 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/failures.js.flow +54 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js +142 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js.flow +146 -0
- package/lib/parsers/typescript/modules/index.js +266 -87
- package/lib/parsers/typescript/modules/index.js.flow +230 -48
- package/lib/parsers/typescript/parser.d.ts +10 -0
- package/lib/parsers/typescript/parser.js +131 -25
- package/lib/parsers/typescript/parser.js.flow +137 -26
- package/lib/parsers/typescript/utils.js +40 -20
- package/lib/parsers/typescript/utils.js.flow +44 -24
- package/lib/parsers/utils.js +0 -125
- package/lib/parsers/utils.js.flow +3 -70
- package/package.json +4 -1
- package/lib/parsers/flow/index.js +0 -80
- package/lib/parsers/flow/index.js.flow +0 -85
- package/lib/parsers/typescript/index.js +0 -85
- package/lib/parsers/typescript/index.js.flow +0 -94
|
@@ -10,10 +10,29 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {
|
|
13
|
+
import type {
|
|
14
|
+
UnionTypeAnnotationMemberType,
|
|
15
|
+
SchemaType,
|
|
16
|
+
NamedShape,
|
|
17
|
+
Nullable,
|
|
18
|
+
NativeModuleParamTypeAnnotation,
|
|
19
|
+
NativeModuleEnumMembers,
|
|
20
|
+
NativeModuleEnumMemberType,
|
|
21
|
+
} from '../../CodegenSchema';
|
|
14
22
|
import type {ParserType} from '../errors';
|
|
15
23
|
import type {Parser} from '../parser';
|
|
16
24
|
|
|
25
|
+
// $FlowFixMe[untyped-import] Use flow-types for @babel/parser
|
|
26
|
+
const babelParser = require('@babel/parser');
|
|
27
|
+
|
|
28
|
+
const {buildSchema} = require('../parsers-commons');
|
|
29
|
+
const {Visitor} = require('./Visitor');
|
|
30
|
+
const {buildComponentSchema} = require('./components');
|
|
31
|
+
const {wrapComponentSchema} = require('./components/schema');
|
|
32
|
+
const {buildModuleSchema} = require('./modules');
|
|
33
|
+
|
|
34
|
+
const fs = require('fs');
|
|
35
|
+
|
|
17
36
|
const {
|
|
18
37
|
UnsupportedObjectPropertyTypeAnnotationParserError,
|
|
19
38
|
} = require('../errors');
|
|
@@ -21,34 +40,20 @@ const {
|
|
|
21
40
|
class TypeScriptParser implements Parser {
|
|
22
41
|
typeParameterInstantiation: string = 'TSTypeParameterInstantiation';
|
|
23
42
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
case 'TSPropertySignature':
|
|
27
|
-
return propertyOrIndex.key.name;
|
|
28
|
-
case 'TSIndexSignature':
|
|
29
|
-
return propertyOrIndex.parameters[0].name;
|
|
30
|
-
default:
|
|
31
|
-
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
|
|
32
|
-
hasteModuleName,
|
|
33
|
-
propertyOrIndex,
|
|
34
|
-
propertyOrIndex.type,
|
|
35
|
-
this.language(),
|
|
36
|
-
);
|
|
37
|
-
}
|
|
43
|
+
isProperty(property: $FlowFixMe): boolean {
|
|
44
|
+
return property.type === 'TSPropertySignature';
|
|
38
45
|
}
|
|
39
46
|
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
getKeyName(property: $FlowFixMe, hasteModuleName: string): string {
|
|
48
|
+
if (!this.isProperty(property)) {
|
|
49
|
+
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
|
|
50
|
+
hasteModuleName,
|
|
51
|
+
property,
|
|
52
|
+
property.type,
|
|
53
|
+
this.language(),
|
|
54
|
+
);
|
|
45
55
|
}
|
|
46
|
-
|
|
47
|
-
return 'StringTypeAnnotation';
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean {
|
|
51
|
-
return maybeEnumDeclaration.type === 'TSEnumDeclaration';
|
|
56
|
+
return property.key.name;
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
language(): ParserType {
|
|
@@ -81,6 +86,112 @@ class TypeScriptParser implements Parser {
|
|
|
81
86
|
|
|
82
87
|
return [...new Set(membersTypes.map(remapLiteral))];
|
|
83
88
|
}
|
|
89
|
+
|
|
90
|
+
parseFile(filename: string): SchemaType {
|
|
91
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
92
|
+
|
|
93
|
+
return this.parseString(contents, filename);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
parseString(contents: string, filename: ?string): SchemaType {
|
|
97
|
+
return buildSchema(
|
|
98
|
+
contents,
|
|
99
|
+
filename,
|
|
100
|
+
wrapComponentSchema,
|
|
101
|
+
buildComponentSchema,
|
|
102
|
+
buildModuleSchema,
|
|
103
|
+
Visitor,
|
|
104
|
+
this,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
parseModuleFixture(filename: string): SchemaType {
|
|
109
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
110
|
+
|
|
111
|
+
return this.parseString(contents, 'path/NativeSampleTurboModule.ts');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
getAst(contents: string): $FlowFixMe {
|
|
115
|
+
return babelParser.parse(contents, {
|
|
116
|
+
sourceType: 'module',
|
|
117
|
+
plugins: ['typescript'],
|
|
118
|
+
}).program;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
getFunctionTypeAnnotationParameters(
|
|
122
|
+
functionTypeAnnotation: $FlowFixMe,
|
|
123
|
+
): $ReadOnlyArray<$FlowFixMe> {
|
|
124
|
+
return functionTypeAnnotation.parameters;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getFunctionNameFromParameter(
|
|
128
|
+
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
|
|
129
|
+
): $FlowFixMe {
|
|
130
|
+
return parameter.typeAnnotation;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
getParameterName(parameter: $FlowFixMe): string {
|
|
134
|
+
return parameter.name;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
getParameterTypeAnnotation(parameter: $FlowFixMe): $FlowFixMe {
|
|
138
|
+
return parameter.typeAnnotation.typeAnnotation;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
getFunctionTypeAnnotationReturnType(
|
|
142
|
+
functionTypeAnnotation: $FlowFixMe,
|
|
143
|
+
): $FlowFixMe {
|
|
144
|
+
return functionTypeAnnotation.typeAnnotation.typeAnnotation;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
parseEnumMembersType(typeAnnotation: $FlowFixMe): NativeModuleEnumMemberType {
|
|
148
|
+
const enumInitializer = typeAnnotation.members[0]?.initializer;
|
|
149
|
+
const enumMembersType: ?NativeModuleEnumMemberType =
|
|
150
|
+
!enumInitializer || enumInitializer.type === 'StringLiteral'
|
|
151
|
+
? 'StringTypeAnnotation'
|
|
152
|
+
: enumInitializer.type === 'NumericLiteral'
|
|
153
|
+
? 'NumberTypeAnnotation'
|
|
154
|
+
: null;
|
|
155
|
+
if (!enumMembersType) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
'Enum values must be either blank, number, or string values.',
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return enumMembersType;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
validateEnumMembersSupported(
|
|
164
|
+
typeAnnotation: $FlowFixMe,
|
|
165
|
+
enumMembersType: NativeModuleEnumMemberType,
|
|
166
|
+
): void {
|
|
167
|
+
if (!typeAnnotation.members || typeAnnotation.members.length === 0) {
|
|
168
|
+
throw new Error('Enums should have at least one member.');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const enumInitializerType =
|
|
172
|
+
enumMembersType === 'StringTypeAnnotation'
|
|
173
|
+
? 'StringLiteral'
|
|
174
|
+
: enumMembersType === 'NumberTypeAnnotation'
|
|
175
|
+
? 'NumericLiteral'
|
|
176
|
+
: null;
|
|
177
|
+
|
|
178
|
+
typeAnnotation.members.forEach(member => {
|
|
179
|
+
if (
|
|
180
|
+
(member.initializer?.type ?? 'StringLiteral') !== enumInitializerType
|
|
181
|
+
) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
'Enum values can not be mixed. They all must be either blank, number, or string values.',
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
parseEnumMembers(typeAnnotation: $FlowFixMe): NativeModuleEnumMembers {
|
|
190
|
+
return typeAnnotation.members.map(member => ({
|
|
191
|
+
name: member.id.name,
|
|
192
|
+
value: member.initializer?.value ?? member.id.name,
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
84
195
|
}
|
|
85
196
|
module.exports = {
|
|
86
197
|
TypeScriptParser,
|
|
@@ -16,7 +16,6 @@ const _require = require('./parseTopLevelType'),
|
|
|
16
16
|
/**
|
|
17
17
|
* TODO(T108222691): Use flow-types for @babel/parser
|
|
18
18
|
*/
|
|
19
|
-
|
|
20
19
|
function getTypes(ast) {
|
|
21
20
|
return ast.body.reduce((types, node) => {
|
|
22
21
|
switch (node.type) {
|
|
@@ -61,38 +60,59 @@ function resolveTypeAnnotation(
|
|
|
61
60
|
? typeAnnotation.typeAnnotation
|
|
62
61
|
: typeAnnotation;
|
|
63
62
|
let nullable = false;
|
|
64
|
-
let
|
|
63
|
+
let typeResolutionStatus = {
|
|
65
64
|
successful: false,
|
|
66
65
|
};
|
|
67
66
|
for (;;) {
|
|
68
67
|
const topLevelType = parseTopLevelType(node);
|
|
69
68
|
nullable = nullable || topLevelType.optional;
|
|
70
69
|
node = topLevelType.type;
|
|
71
|
-
if (node.type
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
70
|
+
if (node.type !== 'TSTypeReference') {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
const resolvedTypeAnnotation = types[node.typeName.name];
|
|
74
|
+
if (resolvedTypeAnnotation == null) {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
switch (resolvedTypeAnnotation.type) {
|
|
78
|
+
case 'TSTypeAliasDeclaration': {
|
|
79
|
+
typeResolutionStatus = {
|
|
80
|
+
successful: true,
|
|
81
|
+
type: 'alias',
|
|
82
|
+
name: node.typeName.name,
|
|
83
|
+
};
|
|
84
|
+
node = resolvedTypeAnnotation.typeAnnotation;
|
|
81
85
|
break;
|
|
82
86
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
case 'TSInterfaceDeclaration': {
|
|
88
|
+
typeResolutionStatus = {
|
|
89
|
+
successful: true,
|
|
90
|
+
type: 'alias',
|
|
91
|
+
name: node.typeName.name,
|
|
92
|
+
};
|
|
93
|
+
node = resolvedTypeAnnotation;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
case 'TSEnumDeclaration': {
|
|
97
|
+
typeResolutionStatus = {
|
|
98
|
+
successful: true,
|
|
99
|
+
type: 'enum',
|
|
100
|
+
name: node.typeName.name,
|
|
101
|
+
};
|
|
102
|
+
node = resolvedTypeAnnotation;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
default: {
|
|
106
|
+
throw new TypeError(
|
|
107
|
+
`A non GenericTypeAnnotation must be a type declaration ('TSTypeAliasDeclaration'), an interface ('TSInterfaceDeclaration'), or enum ('TSEnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
90
110
|
}
|
|
91
111
|
}
|
|
92
112
|
return {
|
|
93
113
|
nullable: nullable,
|
|
94
114
|
typeAnnotation: node,
|
|
95
|
-
|
|
115
|
+
typeResolutionStatus,
|
|
96
116
|
};
|
|
97
117
|
}
|
|
98
118
|
module.exports = {
|
|
@@ -10,14 +10,13 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {
|
|
13
|
+
import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';
|
|
14
14
|
|
|
15
15
|
const {parseTopLevelType} = require('./parseTopLevelType');
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* TODO(T108222691): Use flow-types for @babel/parser
|
|
19
19
|
*/
|
|
20
|
-
|
|
21
20
|
function getTypes(ast: $FlowFixMe): TypeDeclarationMap {
|
|
22
21
|
return ast.body.reduce((types, node) => {
|
|
23
22
|
switch (node.type) {
|
|
@@ -57,7 +56,7 @@ function resolveTypeAnnotation(
|
|
|
57
56
|
): {
|
|
58
57
|
nullable: boolean,
|
|
59
58
|
typeAnnotation: $FlowFixMe,
|
|
60
|
-
|
|
59
|
+
typeResolutionStatus: TypeResolutionStatus,
|
|
61
60
|
} {
|
|
62
61
|
invariant(
|
|
63
62
|
typeAnnotation != null,
|
|
@@ -69,7 +68,7 @@ function resolveTypeAnnotation(
|
|
|
69
68
|
? typeAnnotation.typeAnnotation
|
|
70
69
|
: typeAnnotation;
|
|
71
70
|
let nullable = false;
|
|
72
|
-
let
|
|
71
|
+
let typeResolutionStatus: TypeResolutionStatus = {
|
|
73
72
|
successful: false,
|
|
74
73
|
};
|
|
75
74
|
|
|
@@ -78,34 +77,55 @@ function resolveTypeAnnotation(
|
|
|
78
77
|
nullable = nullable || topLevelType.optional;
|
|
79
78
|
node = topLevelType.type;
|
|
80
79
|
|
|
81
|
-
if (node.type
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
aliasName: node.typeName.name,
|
|
85
|
-
};
|
|
86
|
-
const resolvedTypeAnnotation = types[node.typeName.name];
|
|
87
|
-
if (
|
|
88
|
-
resolvedTypeAnnotation == null ||
|
|
89
|
-
resolvedTypeAnnotation.type === 'TSEnumDeclaration'
|
|
90
|
-
) {
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
invariant(
|
|
95
|
-
resolvedTypeAnnotation.type === 'TSTypeAliasDeclaration',
|
|
96
|
-
`GenericTypeAnnotation '${node.typeName.name}' must resolve to a TSTypeAliasDeclaration. Instead, it resolved to a '${resolvedTypeAnnotation.type}'`,
|
|
97
|
-
);
|
|
80
|
+
if (node.type !== 'TSTypeReference') {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
98
83
|
|
|
99
|
-
|
|
100
|
-
|
|
84
|
+
const resolvedTypeAnnotation = types[node.typeName.name];
|
|
85
|
+
if (resolvedTypeAnnotation == null) {
|
|
101
86
|
break;
|
|
102
87
|
}
|
|
88
|
+
|
|
89
|
+
switch (resolvedTypeAnnotation.type) {
|
|
90
|
+
case 'TSTypeAliasDeclaration': {
|
|
91
|
+
typeResolutionStatus = {
|
|
92
|
+
successful: true,
|
|
93
|
+
type: 'alias',
|
|
94
|
+
name: node.typeName.name,
|
|
95
|
+
};
|
|
96
|
+
node = resolvedTypeAnnotation.typeAnnotation;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case 'TSInterfaceDeclaration': {
|
|
100
|
+
typeResolutionStatus = {
|
|
101
|
+
successful: true,
|
|
102
|
+
type: 'alias',
|
|
103
|
+
name: node.typeName.name,
|
|
104
|
+
};
|
|
105
|
+
node = resolvedTypeAnnotation;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case 'TSEnumDeclaration': {
|
|
109
|
+
typeResolutionStatus = {
|
|
110
|
+
successful: true,
|
|
111
|
+
type: 'enum',
|
|
112
|
+
name: node.typeName.name,
|
|
113
|
+
};
|
|
114
|
+
node = resolvedTypeAnnotation;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
default: {
|
|
118
|
+
throw new TypeError(
|
|
119
|
+
`A non GenericTypeAnnotation must be a type declaration ('TSTypeAliasDeclaration'), an interface ('TSInterfaceDeclaration'), or enum ('TSEnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
103
123
|
}
|
|
104
124
|
|
|
105
125
|
return {
|
|
106
126
|
nullable: nullable,
|
|
107
127
|
typeAnnotation: node,
|
|
108
|
-
|
|
128
|
+
typeResolutionStatus,
|
|
109
129
|
};
|
|
110
130
|
}
|
|
111
131
|
|
package/lib/parsers/utils.js
CHANGED
|
@@ -10,81 +10,9 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
function _slicedToArray(arr, i) {
|
|
14
|
-
return (
|
|
15
|
-
_arrayWithHoles(arr) ||
|
|
16
|
-
_iterableToArrayLimit(arr, i) ||
|
|
17
|
-
_unsupportedIterableToArray(arr, i) ||
|
|
18
|
-
_nonIterableRest()
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
function _nonIterableRest() {
|
|
22
|
-
throw new TypeError(
|
|
23
|
-
'Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
function _unsupportedIterableToArray(o, minLen) {
|
|
27
|
-
if (!o) return;
|
|
28
|
-
if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
|
|
29
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
30
|
-
if (n === 'Object' && o.constructor) n = o.constructor.name;
|
|
31
|
-
if (n === 'Map' || n === 'Set') return Array.from(o);
|
|
32
|
-
if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
|
|
33
|
-
return _arrayLikeToArray(o, minLen);
|
|
34
|
-
}
|
|
35
|
-
function _arrayLikeToArray(arr, len) {
|
|
36
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
37
|
-
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
38
|
-
return arr2;
|
|
39
|
-
}
|
|
40
|
-
function _iterableToArrayLimit(arr, i) {
|
|
41
|
-
var _i =
|
|
42
|
-
null == arr
|
|
43
|
-
? null
|
|
44
|
-
: ('undefined' != typeof Symbol && arr[Symbol.iterator]) ||
|
|
45
|
-
arr['@@iterator'];
|
|
46
|
-
if (null != _i) {
|
|
47
|
-
var _s,
|
|
48
|
-
_e,
|
|
49
|
-
_x,
|
|
50
|
-
_r,
|
|
51
|
-
_arr = [],
|
|
52
|
-
_n = !0,
|
|
53
|
-
_d = !1;
|
|
54
|
-
try {
|
|
55
|
-
if (((_x = (_i = _i.call(arr)).next), 0 === i)) {
|
|
56
|
-
if (Object(_i) !== _i) return;
|
|
57
|
-
_n = !1;
|
|
58
|
-
} else
|
|
59
|
-
for (
|
|
60
|
-
;
|
|
61
|
-
!(_n = (_s = _x.call(_i)).done) &&
|
|
62
|
-
(_arr.push(_s.value), _arr.length !== i);
|
|
63
|
-
_n = !0
|
|
64
|
-
);
|
|
65
|
-
} catch (err) {
|
|
66
|
-
(_d = !0), (_e = err);
|
|
67
|
-
} finally {
|
|
68
|
-
try {
|
|
69
|
-
if (!_n && null != _i.return && ((_r = _i.return()), Object(_r) !== _r))
|
|
70
|
-
return;
|
|
71
|
-
} finally {
|
|
72
|
-
if (_d) throw _e;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return _arr;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
function _arrayWithHoles(arr) {
|
|
79
|
-
if (Array.isArray(arr)) return arr;
|
|
80
|
-
}
|
|
81
13
|
const _require = require('./errors'),
|
|
82
14
|
ParserError = _require.ParserError;
|
|
83
|
-
const _require2 = require('./parsers-commons'),
|
|
84
|
-
wrapModuleSchema = _require2.wrapModuleSchema;
|
|
85
|
-
const fs = require('fs');
|
|
86
15
|
const path = require('path');
|
|
87
|
-
const invariant = require('invariant');
|
|
88
16
|
function extractNativeModuleName(filename) {
|
|
89
17
|
// this should drop everything after the file name. For Example it will drop:
|
|
90
18
|
// .android.js, .android.ts, .android.tsx, .ios.js, .ios.ts, .ios.tsx, .js, .ts, .tsx
|
|
@@ -130,10 +58,6 @@ function verifyPlatforms(hasteModuleName, moduleName) {
|
|
|
130
58
|
excludedPlatforms: Array.from(excludedPlatforms),
|
|
131
59
|
};
|
|
132
60
|
}
|
|
133
|
-
function parseFile(filename, callback) {
|
|
134
|
-
const contents = fs.readFileSync(filename, 'utf8');
|
|
135
|
-
return callback(contents, filename);
|
|
136
|
-
}
|
|
137
61
|
|
|
138
62
|
// TODO(T108222691): Use flow-types for @babel/parser
|
|
139
63
|
function visit(astNode, visitor) {
|
|
@@ -156,53 +80,6 @@ function visit(astNode, visitor) {
|
|
|
156
80
|
}
|
|
157
81
|
}
|
|
158
82
|
}
|
|
159
|
-
function buildSchemaFromConfigType(
|
|
160
|
-
configType,
|
|
161
|
-
filename,
|
|
162
|
-
ast,
|
|
163
|
-
wrapComponentSchema,
|
|
164
|
-
buildComponentSchema,
|
|
165
|
-
buildModuleSchema,
|
|
166
|
-
) {
|
|
167
|
-
switch (configType) {
|
|
168
|
-
case 'component': {
|
|
169
|
-
return wrapComponentSchema(buildComponentSchema(ast));
|
|
170
|
-
}
|
|
171
|
-
case 'module': {
|
|
172
|
-
if (filename === undefined || filename === null) {
|
|
173
|
-
throw new Error('Filepath expected while parasing a module');
|
|
174
|
-
}
|
|
175
|
-
const nativeModuleName = extractNativeModuleName(filename);
|
|
176
|
-
const _createParserErrorCap = createParserErrorCapturer(),
|
|
177
|
-
_createParserErrorCap2 = _slicedToArray(_createParserErrorCap, 2),
|
|
178
|
-
parsingErrors = _createParserErrorCap2[0],
|
|
179
|
-
tryParse = _createParserErrorCap2[1];
|
|
180
|
-
const schema = tryParse(() =>
|
|
181
|
-
buildModuleSchema(nativeModuleName, ast, tryParse),
|
|
182
|
-
);
|
|
183
|
-
if (parsingErrors.length > 0) {
|
|
184
|
-
/**
|
|
185
|
-
* TODO(T77968131): We have two options:
|
|
186
|
-
* - Throw the first error, but indicate there are more then one errors.
|
|
187
|
-
* - Display all errors, nicely formatted.
|
|
188
|
-
*
|
|
189
|
-
* For the time being, we're just throw the first error.
|
|
190
|
-
**/
|
|
191
|
-
|
|
192
|
-
throw parsingErrors[0];
|
|
193
|
-
}
|
|
194
|
-
invariant(
|
|
195
|
-
schema != null,
|
|
196
|
-
'When there are no parsing errors, the schema should not be null',
|
|
197
|
-
);
|
|
198
|
-
return wrapModuleSchema(schema, nativeModuleName);
|
|
199
|
-
}
|
|
200
|
-
default:
|
|
201
|
-
return {
|
|
202
|
-
modules: {},
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
83
|
function getConfigType(
|
|
207
84
|
// TODO(T71778680): Flow-type this node.
|
|
208
85
|
ast,
|
|
@@ -266,8 +143,6 @@ module.exports = {
|
|
|
266
143
|
extractNativeModuleName,
|
|
267
144
|
createParserErrorCapturer,
|
|
268
145
|
verifyPlatforms,
|
|
269
|
-
parseFile,
|
|
270
146
|
visit,
|
|
271
|
-
buildSchemaFromConfigType,
|
|
272
147
|
isModuleRegistryCall,
|
|
273
148
|
};
|
|
@@ -10,21 +10,17 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {ComponentSchemaBuilderConfig} from './flow/components/schema';
|
|
14
|
-
import type {NativeModuleSchema, SchemaType} from '../CodegenSchema';
|
|
15
13
|
const {ParserError} = require('./errors');
|
|
16
|
-
const {wrapModuleSchema} = require('./parsers-commons');
|
|
17
14
|
|
|
18
|
-
const fs = require('fs');
|
|
19
15
|
const path = require('path');
|
|
20
|
-
const invariant = require('invariant');
|
|
21
16
|
|
|
22
17
|
export type TypeDeclarationMap = {[declarationName: string]: $FlowFixMe};
|
|
23
18
|
|
|
24
|
-
export type
|
|
19
|
+
export type TypeResolutionStatus =
|
|
25
20
|
| $ReadOnly<{
|
|
21
|
+
type: 'alias' | 'enum',
|
|
26
22
|
successful: true,
|
|
27
|
-
|
|
23
|
+
name: string,
|
|
28
24
|
}>
|
|
29
25
|
| $ReadOnly<{
|
|
30
26
|
successful: false,
|
|
@@ -95,15 +91,6 @@ function verifyPlatforms(
|
|
|
95
91
|
};
|
|
96
92
|
}
|
|
97
93
|
|
|
98
|
-
function parseFile(
|
|
99
|
-
filename: string,
|
|
100
|
-
callback: (contents: string, filename: string) => SchemaType,
|
|
101
|
-
): SchemaType {
|
|
102
|
-
const contents = fs.readFileSync(filename, 'utf8');
|
|
103
|
-
|
|
104
|
-
return callback(contents, filename);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
94
|
// TODO(T108222691): Use flow-types for @babel/parser
|
|
108
95
|
function visit(
|
|
109
96
|
astNode: $FlowFixMe,
|
|
@@ -133,58 +120,6 @@ function visit(
|
|
|
133
120
|
}
|
|
134
121
|
}
|
|
135
122
|
|
|
136
|
-
function buildSchemaFromConfigType(
|
|
137
|
-
configType: 'module' | 'component' | 'none',
|
|
138
|
-
filename: ?string,
|
|
139
|
-
ast: $FlowFixMe,
|
|
140
|
-
wrapComponentSchema: (config: ComponentSchemaBuilderConfig) => SchemaType,
|
|
141
|
-
buildComponentSchema: (ast: $FlowFixMe) => ComponentSchemaBuilderConfig,
|
|
142
|
-
buildModuleSchema: (
|
|
143
|
-
hasteModuleName: string,
|
|
144
|
-
ast: $FlowFixMe,
|
|
145
|
-
tryParse: ParserErrorCapturer,
|
|
146
|
-
) => NativeModuleSchema,
|
|
147
|
-
): SchemaType {
|
|
148
|
-
switch (configType) {
|
|
149
|
-
case 'component': {
|
|
150
|
-
return wrapComponentSchema(buildComponentSchema(ast));
|
|
151
|
-
}
|
|
152
|
-
case 'module': {
|
|
153
|
-
if (filename === undefined || filename === null) {
|
|
154
|
-
throw new Error('Filepath expected while parasing a module');
|
|
155
|
-
}
|
|
156
|
-
const nativeModuleName = extractNativeModuleName(filename);
|
|
157
|
-
|
|
158
|
-
const [parsingErrors, tryParse] = createParserErrorCapturer();
|
|
159
|
-
|
|
160
|
-
const schema = tryParse(() =>
|
|
161
|
-
buildModuleSchema(nativeModuleName, ast, tryParse),
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
if (parsingErrors.length > 0) {
|
|
165
|
-
/**
|
|
166
|
-
* TODO(T77968131): We have two options:
|
|
167
|
-
* - Throw the first error, but indicate there are more then one errors.
|
|
168
|
-
* - Display all errors, nicely formatted.
|
|
169
|
-
*
|
|
170
|
-
* For the time being, we're just throw the first error.
|
|
171
|
-
**/
|
|
172
|
-
|
|
173
|
-
throw parsingErrors[0];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
invariant(
|
|
177
|
-
schema != null,
|
|
178
|
-
'When there are no parsing errors, the schema should not be null',
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
return wrapModuleSchema(schema, nativeModuleName);
|
|
182
|
-
}
|
|
183
|
-
default:
|
|
184
|
-
return {modules: {}};
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
123
|
function getConfigType(
|
|
189
124
|
// TODO(T71778680): Flow-type this node.
|
|
190
125
|
ast: $FlowFixMe,
|
|
@@ -259,8 +194,6 @@ module.exports = {
|
|
|
259
194
|
extractNativeModuleName,
|
|
260
195
|
createParserErrorCapturer,
|
|
261
196
|
verifyPlatforms,
|
|
262
|
-
parseFile,
|
|
263
197
|
visit,
|
|
264
|
-
buildSchemaFromConfigType,
|
|
265
198
|
isModuleRegistryCall,
|
|
266
199
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native/codegen",
|
|
3
|
-
"version": "0.72.
|
|
3
|
+
"version": "0.72.2",
|
|
4
4
|
"description": "⚛️ Code generation tools for React Native",
|
|
5
5
|
"homepage": "https://github.com/facebook/react-native/tree/HEAD/packages/react-native-codegen",
|
|
6
6
|
"repository": {
|
|
@@ -41,5 +41,8 @@
|
|
|
41
41
|
"mkdirp": "^0.5.1",
|
|
42
42
|
"prettier": "^2.4.1",
|
|
43
43
|
"rimraf": "^3.0.2"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@babel/preset-env": "^7.1.6"
|
|
44
47
|
}
|
|
45
48
|
}
|