@react-native/codegen 0.72.1 → 0.72.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/lib/CodegenSchema.d.ts +348 -0
  2. package/lib/CodegenSchema.js.flow +31 -3
  3. package/lib/SchemaValidator.d.ts +11 -0
  4. package/lib/cli/combine/combine-js-to-schema.js +8 -8
  5. package/lib/cli/combine/combine-js-to-schema.js.flow +8 -7
  6. package/lib/cli/parser/parser.js +8 -15
  7. package/lib/cli/parser/parser.js.flow +8 -14
  8. package/lib/generators/Utils.js +16 -0
  9. package/lib/generators/Utils.js.flow +20 -0
  10. package/lib/generators/__test_fixtures__/fixtures.js +2 -1
  11. package/lib/generators/__test_fixtures__/fixtures.js.flow +2 -1
  12. package/lib/generators/components/ComponentsGeneratorUtils.js +10 -1
  13. package/lib/generators/components/ComponentsGeneratorUtils.js.flow +12 -2
  14. package/lib/generators/components/CppHelpers.js +8 -15
  15. package/lib/generators/components/CppHelpers.js.flow +8 -19
  16. package/lib/generators/components/GenerateEventEmitterCpp.js +7 -3
  17. package/lib/generators/components/GenerateEventEmitterCpp.js.flow +7 -3
  18. package/lib/generators/components/GenerateEventEmitterH.js +2 -2
  19. package/lib/generators/components/GenerateEventEmitterH.js.flow +1 -2
  20. package/lib/generators/components/GeneratePropsH.js +3 -4
  21. package/lib/generators/components/GeneratePropsH.js.flow +2 -4
  22. package/lib/generators/components/GeneratePropsJavaDelegate.js +2 -0
  23. package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +2 -0
  24. package/lib/generators/components/GeneratePropsJavaInterface.js +3 -0
  25. package/lib/generators/components/GeneratePropsJavaInterface.js.flow +3 -0
  26. package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +8 -0
  27. package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +12 -0
  28. package/lib/generators/components/GenerateTests.js +3 -2
  29. package/lib/generators/components/GenerateTests.js.flow +3 -1
  30. package/lib/generators/components/GenerateViewConfigJs.js +5 -2
  31. package/lib/generators/components/GenerateViewConfigJs.js.flow +5 -2
  32. package/lib/generators/components/JavaHelpers.js +9 -0
  33. package/lib/generators/components/JavaHelpers.js.flow +12 -1
  34. package/lib/generators/components/__test_fixtures__/fixtures.js +41 -0
  35. package/lib/generators/components/__test_fixtures__/fixtures.js.flow +42 -0
  36. package/lib/generators/modules/GenerateModuleCpp.js +9 -4
  37. package/lib/generators/modules/GenerateModuleCpp.js.flow +11 -3
  38. package/lib/generators/modules/GenerateModuleH.js +185 -33
  39. package/lib/generators/modules/GenerateModuleH.js.flow +203 -25
  40. package/lib/generators/modules/GenerateModuleJavaSpec.js +2 -2
  41. package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +5 -5
  42. package/lib/generators/modules/GenerateModuleJniCpp.js +2 -2
  43. package/lib/generators/modules/GenerateModuleJniCpp.js.flow +5 -5
  44. package/lib/generators/modules/GenerateModuleObjCpp/index.js +2 -2
  45. package/lib/generators/modules/GenerateModuleObjCpp/index.js.flow +2 -2
  46. package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +2 -2
  47. package/lib/generators/modules/Utils.js +4 -0
  48. package/lib/generators/modules/Utils.js.flow +6 -0
  49. package/lib/generators/modules/__test_fixtures__/fixtures.js +152 -10
  50. package/lib/generators/modules/__test_fixtures__/fixtures.js.flow +152 -10
  51. package/lib/parsers/error-utils.js +42 -19
  52. package/lib/parsers/error-utils.js.flow +46 -19
  53. package/lib/parsers/errors.d.ts +10 -0
  54. package/lib/parsers/errors.js +9 -29
  55. package/lib/parsers/errors.js.flow +2 -20
  56. package/lib/parsers/flow/Visitor.js +37 -0
  57. package/lib/parsers/flow/Visitor.js.flow +41 -0
  58. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +20 -2
  59. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +20 -2
  60. package/lib/parsers/flow/components/commands.js +3 -0
  61. package/lib/parsers/flow/components/commands.js.flow +2 -1
  62. package/lib/parsers/flow/components/componentsUtils.js +10 -0
  63. package/lib/parsers/flow/components/componentsUtils.js.flow +11 -1
  64. package/lib/parsers/flow/components/events.js.flow +1 -1
  65. package/lib/parsers/flow/components/extends.js.flow +1 -1
  66. package/lib/parsers/flow/components/index.js +16 -20
  67. package/lib/parsers/flow/components/index.js.flow +8 -7
  68. package/lib/parsers/flow/components/options.js.flow +1 -1
  69. package/lib/parsers/flow/components/props.js.flow +1 -1
  70. package/lib/parsers/flow/modules/__test_fixtures__/failures.js +54 -0
  71. package/lib/parsers/flow/modules/__test_fixtures__/failures.js.flow +56 -0
  72. package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +64 -1
  73. package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +66 -1
  74. package/lib/parsers/flow/modules/index.js +112 -48
  75. package/lib/parsers/flow/modules/index.js.flow +115 -44
  76. package/lib/parsers/flow/parser.d.ts +10 -0
  77. package/lib/parsers/flow/parser.js +173 -32
  78. package/lib/parsers/flow/parser.js.flow +201 -24
  79. package/lib/parsers/flow/utils.js +33 -57
  80. package/lib/parsers/flow/utils.js.flow +37 -60
  81. package/lib/parsers/parser.d.ts +17 -0
  82. package/lib/parsers/parser.js.flow +124 -17
  83. package/lib/parsers/parserMock.js +101 -30
  84. package/lib/parsers/parserMock.js.flow +139 -24
  85. package/lib/parsers/parsers-commons.js +121 -119
  86. package/lib/parsers/parsers-commons.js.flow +136 -131
  87. package/lib/parsers/parsers-primitives.js +75 -12
  88. package/lib/parsers/parsers-primitives.js.flow +92 -13
  89. package/lib/parsers/schema/index.d.ts +10 -0
  90. package/lib/parsers/{typescript/components/schema.js.flow → schema.js.flow} +1 -1
  91. package/lib/parsers/typescript/Visitor.js +42 -0
  92. package/lib/parsers/typescript/Visitor.js.flow +47 -0
  93. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +22 -0
  94. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +22 -0
  95. package/lib/parsers/typescript/components/commands.js +3 -0
  96. package/lib/parsers/typescript/components/commands.js.flow +2 -1
  97. package/lib/parsers/typescript/components/componentsUtils.js +5 -0
  98. package/lib/parsers/typescript/components/componentsUtils.js.flow +6 -1
  99. package/lib/parsers/typescript/components/events.js.flow +1 -1
  100. package/lib/parsers/typescript/components/extends.js.flow +1 -1
  101. package/lib/parsers/typescript/components/index.js +15 -19
  102. package/lib/parsers/typescript/components/index.js.flow +8 -7
  103. package/lib/parsers/typescript/components/options.js.flow +1 -1
  104. package/lib/parsers/typescript/components/props.js.flow +1 -1
  105. package/lib/parsers/typescript/modules/__test_fixtures__/failures.js +52 -0
  106. package/lib/parsers/typescript/modules/__test_fixtures__/failures.js.flow +54 -0
  107. package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js +142 -0
  108. package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js.flow +146 -0
  109. package/lib/parsers/typescript/modules/index.js +276 -110
  110. package/lib/parsers/typescript/modules/index.js.flow +237 -68
  111. package/lib/parsers/typescript/parser.d.ts +10 -0
  112. package/lib/parsers/typescript/parser.js +175 -25
  113. package/lib/parsers/typescript/parser.js.flow +184 -26
  114. package/lib/parsers/typescript/utils.js +40 -51
  115. package/lib/parsers/typescript/utils.js.flow +44 -55
  116. package/lib/parsers/utils.js +0 -125
  117. package/lib/parsers/utils.js.flow +3 -70
  118. package/package.json +4 -1
  119. package/lib/parsers/flow/components/schema.js +0 -106
  120. package/lib/parsers/flow/components/schema.js.flow +0 -62
  121. package/lib/parsers/flow/index.js +0 -80
  122. package/lib/parsers/flow/index.js.flow +0 -85
  123. package/lib/parsers/typescript/index.js +0 -85
  124. package/lib/parsers/typescript/index.js.flow +0 -94
  125. /package/lib/parsers/{typescript/components/schema.js → schema.js} +0 -0
@@ -10,9 +10,29 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema.js';
13
+ import type {
14
+ UnionTypeAnnotationMemberType,
15
+ SchemaType,
16
+ NamedShape,
17
+ Nullable,
18
+ NativeModuleParamTypeAnnotation,
19
+ NativeModuleEnumMemberType,
20
+ NativeModuleEnumMembers,
21
+ } from '../../CodegenSchema';
14
22
  import type {ParserType} from '../errors';
15
23
  import type {Parser} from '../parser';
24
+ import type {TypeDeclarationMap} from '../utils';
25
+
26
+ // $FlowFixMe[untyped-import] there's no flowtype flow-parser
27
+ const flowParser = require('flow-parser');
28
+
29
+ const {buildSchema} = require('../parsers-commons');
30
+ const {Visitor} = require('./Visitor');
31
+ const {buildComponentSchema} = require('./components');
32
+ const {wrapComponentSchema} = require('../schema.js');
33
+ const {buildModuleSchema} = require('./modules');
34
+
35
+ const fs = require('fs');
16
36
 
17
37
  const {
18
38
  UnsupportedObjectPropertyTypeAnnotationParserError,
@@ -21,31 +41,20 @@ const {
21
41
  class FlowParser implements Parser {
22
42
  typeParameterInstantiation: string = 'TypeParameterInstantiation';
23
43
 
24
- getKeyName(propertyOrIndex: $FlowFixMe, hasteModuleName: string): string {
25
- switch (propertyOrIndex.type) {
26
- case 'ObjectTypeProperty':
27
- return propertyOrIndex.key.name;
28
- case 'ObjectTypeIndexer':
29
- // flow index name is optional
30
- return propertyOrIndex.id?.name ?? 'key';
31
- default:
32
- throw new UnsupportedObjectPropertyTypeAnnotationParserError(
33
- hasteModuleName,
34
- propertyOrIndex,
35
- propertyOrIndex.type,
36
- this.language(),
37
- );
38
- }
39
- }
40
-
41
- getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string {
42
- return maybeEnumDeclaration.body.type
43
- .replace('EnumNumberBody', 'NumberTypeAnnotation')
44
- .replace('EnumStringBody', 'StringTypeAnnotation');
44
+ isProperty(property: $FlowFixMe): boolean {
45
+ return property.type === 'ObjectTypeProperty';
45
46
  }
46
47
 
47
- isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean {
48
- return maybeEnumDeclaration.type === 'EnumDeclaration';
48
+ getKeyName(property: $FlowFixMe, hasteModuleName: string): string {
49
+ if (!this.isProperty(property)) {
50
+ throw new UnsupportedObjectPropertyTypeAnnotationParserError(
51
+ hasteModuleName,
52
+ property,
53
+ property.type,
54
+ this.language(),
55
+ );
56
+ }
57
+ return property.key.name;
49
58
  }
50
59
 
51
60
  language(): ParserType {
@@ -76,6 +85,174 @@ class FlowParser implements Parser {
76
85
 
77
86
  return [...new Set(membersTypes.map(remapLiteral))];
78
87
  }
88
+
89
+ parseFile(filename: string): SchemaType {
90
+ const contents = fs.readFileSync(filename, 'utf8');
91
+
92
+ return this.parseString(contents, filename);
93
+ }
94
+
95
+ parseString(contents: string, filename: ?string): SchemaType {
96
+ return buildSchema(
97
+ contents,
98
+ filename,
99
+ wrapComponentSchema,
100
+ buildComponentSchema,
101
+ buildModuleSchema,
102
+ Visitor,
103
+ this,
104
+ );
105
+ }
106
+
107
+ parseModuleFixture(filename: string): SchemaType {
108
+ const contents = fs.readFileSync(filename, 'utf8');
109
+
110
+ return this.parseString(contents, 'path/NativeSampleTurboModule.js');
111
+ }
112
+
113
+ getAst(contents: string): $FlowFixMe {
114
+ return flowParser.parse(contents, {
115
+ enums: true,
116
+ });
117
+ }
118
+
119
+ getFunctionTypeAnnotationParameters(
120
+ functionTypeAnnotation: $FlowFixMe,
121
+ ): $ReadOnlyArray<$FlowFixMe> {
122
+ return functionTypeAnnotation.params;
123
+ }
124
+
125
+ getFunctionNameFromParameter(
126
+ parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
127
+ ): $FlowFixMe {
128
+ return parameter.name;
129
+ }
130
+
131
+ getParameterName(parameter: $FlowFixMe): string {
132
+ return parameter.name.name;
133
+ }
134
+
135
+ getParameterTypeAnnotation(parameter: $FlowFixMe): $FlowFixMe {
136
+ return parameter.typeAnnotation;
137
+ }
138
+
139
+ getFunctionTypeAnnotationReturnType(
140
+ functionTypeAnnotation: $FlowFixMe,
141
+ ): $FlowFixMe {
142
+ return functionTypeAnnotation.returnType;
143
+ }
144
+
145
+ parseEnumMembersType(typeAnnotation: $FlowFixMe): NativeModuleEnumMemberType {
146
+ const enumMembersType: ?NativeModuleEnumMemberType =
147
+ typeAnnotation.type === 'EnumStringBody'
148
+ ? 'StringTypeAnnotation'
149
+ : typeAnnotation.type === 'EnumNumberBody'
150
+ ? 'NumberTypeAnnotation'
151
+ : null;
152
+ if (!enumMembersType) {
153
+ throw new Error(
154
+ `Unknown enum type annotation type. Got: ${typeAnnotation.type}. Expected: EnumStringBody or EnumNumberBody.`,
155
+ );
156
+ }
157
+ return enumMembersType;
158
+ }
159
+
160
+ validateEnumMembersSupported(
161
+ typeAnnotation: $FlowFixMe,
162
+ enumMembersType: NativeModuleEnumMemberType,
163
+ ): void {
164
+ if (!typeAnnotation.members || typeAnnotation.members.length === 0) {
165
+ // passing mixed members to flow would result in a flow error
166
+ // if the tool is launched ignoring that error, the enum would appear like not having enums
167
+ throw new Error(
168
+ 'Enums should have at least one member and member values can not be mixed- they all must be either blank, number, or string values.',
169
+ );
170
+ }
171
+
172
+ typeAnnotation.members.forEach(member => {
173
+ if (
174
+ enumMembersType === 'StringTypeAnnotation' &&
175
+ (!member.init || typeof member.init.value === 'string')
176
+ ) {
177
+ return;
178
+ }
179
+
180
+ if (
181
+ enumMembersType === 'NumberTypeAnnotation' &&
182
+ member.init &&
183
+ typeof member.init.value === 'number'
184
+ ) {
185
+ return;
186
+ }
187
+
188
+ throw new Error(
189
+ 'Enums can not be mixed- they all must be either blank, number, or string values.',
190
+ );
191
+ });
192
+ }
193
+
194
+ parseEnumMembers(typeAnnotation: $FlowFixMe): NativeModuleEnumMembers {
195
+ return typeAnnotation.members.map(member => ({
196
+ name: member.id.name,
197
+ value: member.init?.value ?? member.id.name,
198
+ }));
199
+ }
200
+
201
+ isModuleInterface(node: $FlowFixMe): boolean {
202
+ return (
203
+ node.type === 'InterfaceDeclaration' &&
204
+ node.extends.length === 1 &&
205
+ node.extends[0].type === 'InterfaceExtends' &&
206
+ node.extends[0].id.name === 'TurboModule'
207
+ );
208
+ }
209
+
210
+ extractAnnotatedElement(
211
+ typeAnnotation: $FlowFixMe,
212
+ types: TypeDeclarationMap,
213
+ ): $FlowFixMe {
214
+ return types[typeAnnotation.typeParameters.params[0].id.name];
215
+ }
216
+
217
+ /**
218
+ * This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias
219
+ * declaration type. Unfortunately, we don't have those types, because flow-parser
220
+ * generates them, and flow-parser is not type-safe. In the future, we should find
221
+ * a way to get these types from our flow parser library.
222
+ *
223
+ * TODO(T71778680): Flow type AST Nodes
224
+ */
225
+
226
+ getTypes(ast: $FlowFixMe): TypeDeclarationMap {
227
+ return ast.body.reduce((types, node) => {
228
+ if (
229
+ node.type === 'ExportNamedDeclaration' &&
230
+ node.exportKind === 'type'
231
+ ) {
232
+ if (
233
+ node.declaration != null &&
234
+ (node.declaration.type === 'TypeAlias' ||
235
+ node.declaration.type === 'InterfaceDeclaration')
236
+ ) {
237
+ types[node.declaration.id.name] = node.declaration;
238
+ }
239
+ } else if (
240
+ node.type === 'ExportNamedDeclaration' &&
241
+ node.exportKind === 'value' &&
242
+ node.declaration &&
243
+ node.declaration.type === 'EnumDeclaration'
244
+ ) {
245
+ types[node.declaration.id.name] = node.declaration;
246
+ } else if (
247
+ node.type === 'TypeAlias' ||
248
+ node.type === 'InterfaceDeclaration' ||
249
+ node.type === 'EnumDeclaration'
250
+ ) {
251
+ types[node.id.name] = node;
252
+ }
253
+ return types;
254
+ }, {});
255
+ }
79
256
  }
80
257
 
81
258
  module.exports = {
@@ -10,43 +10,6 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- /**
14
- * This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias
15
- * declaration type. Unfortunately, we don't have those types, because flow-parser
16
- * generates them, and flow-parser is not type-safe. In the future, we should find
17
- * a way to get these types from our flow parser library.
18
- *
19
- * TODO(T71778680): Flow type AST Nodes
20
- */
21
-
22
- function getTypes(ast) {
23
- return ast.body.reduce((types, node) => {
24
- if (node.type === 'ExportNamedDeclaration' && node.exportKind === 'type') {
25
- if (
26
- node.declaration != null &&
27
- (node.declaration.type === 'TypeAlias' ||
28
- node.declaration.type === 'InterfaceDeclaration')
29
- ) {
30
- types[node.declaration.id.name] = node.declaration;
31
- }
32
- } else if (
33
- node.type === 'ExportNamedDeclaration' &&
34
- node.exportKind === 'value' &&
35
- node.declaration &&
36
- node.declaration.type === 'EnumDeclaration'
37
- ) {
38
- types[node.declaration.id.name] = node.declaration;
39
- } else if (
40
- node.type === 'TypeAlias' ||
41
- node.type === 'InterfaceDeclaration' ||
42
- node.type === 'EnumDeclaration'
43
- ) {
44
- types[node.id.name] = node;
45
- }
46
- return types;
47
- }, {});
48
- }
49
-
50
13
  // $FlowFixMe[unclear-type] there's no flowtype for ASTs
51
14
 
52
15
  const invariant = require('invariant');
@@ -61,38 +24,52 @@ function resolveTypeAnnotation(
61
24
  );
62
25
  let node = typeAnnotation;
63
26
  let nullable = false;
64
- let typeAliasResolutionStatus = {
27
+ let typeResolutionStatus = {
65
28
  successful: false,
66
29
  };
67
30
  for (;;) {
68
31
  if (node.type === 'NullableTypeAnnotation') {
69
32
  nullable = true;
70
33
  node = node.typeAnnotation;
71
- } else if (node.type === 'GenericTypeAnnotation') {
72
- typeAliasResolutionStatus = {
73
- successful: true,
74
- aliasName: node.id.name,
75
- };
76
- const resolvedTypeAnnotation = types[node.id.name];
77
- if (
78
- resolvedTypeAnnotation == null ||
79
- resolvedTypeAnnotation.type === 'EnumDeclaration'
80
- ) {
34
+ continue;
35
+ }
36
+ if (node.type !== 'GenericTypeAnnotation') {
37
+ break;
38
+ }
39
+ const resolvedTypeAnnotation = types[node.id.name];
40
+ if (resolvedTypeAnnotation == null) {
41
+ break;
42
+ }
43
+ switch (resolvedTypeAnnotation.type) {
44
+ case 'TypeAlias': {
45
+ typeResolutionStatus = {
46
+ successful: true,
47
+ type: 'alias',
48
+ name: node.id.name,
49
+ };
50
+ node = resolvedTypeAnnotation.right;
81
51
  break;
82
52
  }
83
- invariant(
84
- resolvedTypeAnnotation.type === 'TypeAlias',
85
- `GenericTypeAnnotation '${node.id.name}' must resolve to a TypeAlias. Instead, it resolved to a '${resolvedTypeAnnotation.type}'`,
86
- );
87
- node = resolvedTypeAnnotation.right;
88
- } else {
89
- break;
53
+ case 'EnumDeclaration': {
54
+ typeResolutionStatus = {
55
+ successful: true,
56
+ type: 'enum',
57
+ name: node.id.name,
58
+ };
59
+ node = resolvedTypeAnnotation.body;
60
+ break;
61
+ }
62
+ default: {
63
+ throw new TypeError(
64
+ `A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
65
+ );
66
+ }
90
67
  }
91
68
  }
92
69
  return {
93
70
  nullable: nullable,
94
71
  typeAnnotation: node,
95
- typeAliasResolutionStatus,
72
+ typeResolutionStatus,
96
73
  };
97
74
  }
98
75
  function getValueFromTypes(value, types) {
@@ -104,5 +81,4 @@ function getValueFromTypes(value, types) {
104
81
  module.exports = {
105
82
  getValueFromTypes,
106
83
  resolveTypeAnnotation,
107
- getTypes,
108
84
  };
@@ -10,44 +10,7 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils';
14
-
15
- /**
16
- * This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias
17
- * declaration type. Unfortunately, we don't have those types, because flow-parser
18
- * generates them, and flow-parser is not type-safe. In the future, we should find
19
- * a way to get these types from our flow parser library.
20
- *
21
- * TODO(T71778680): Flow type AST Nodes
22
- */
23
-
24
- function getTypes(ast: $FlowFixMe): TypeDeclarationMap {
25
- return ast.body.reduce((types, node) => {
26
- if (node.type === 'ExportNamedDeclaration' && node.exportKind === 'type') {
27
- if (
28
- node.declaration != null &&
29
- (node.declaration.type === 'TypeAlias' ||
30
- node.declaration.type === 'InterfaceDeclaration')
31
- ) {
32
- types[node.declaration.id.name] = node.declaration;
33
- }
34
- } else if (
35
- node.type === 'ExportNamedDeclaration' &&
36
- node.exportKind === 'value' &&
37
- node.declaration &&
38
- node.declaration.type === 'EnumDeclaration'
39
- ) {
40
- types[node.declaration.id.name] = node.declaration;
41
- } else if (
42
- node.type === 'TypeAlias' ||
43
- node.type === 'InterfaceDeclaration' ||
44
- node.type === 'EnumDeclaration'
45
- ) {
46
- types[node.id.name] = node;
47
- }
48
- return types;
49
- }, {});
50
- }
13
+ import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';
51
14
 
52
15
  // $FlowFixMe[unclear-type] there's no flowtype for ASTs
53
16
  export type ASTNode = Object;
@@ -61,7 +24,7 @@ function resolveTypeAnnotation(
61
24
  ): {
62
25
  nullable: boolean,
63
26
  typeAnnotation: $FlowFixMe,
64
- typeAliasResolutionStatus: TypeAliasResolutionStatus,
27
+ typeResolutionStatus: TypeResolutionStatus,
65
28
  } {
66
29
  invariant(
67
30
  typeAnnotation != null,
@@ -70,7 +33,7 @@ function resolveTypeAnnotation(
70
33
 
71
34
  let node = typeAnnotation;
72
35
  let nullable = false;
73
- let typeAliasResolutionStatus: TypeAliasResolutionStatus = {
36
+ let typeResolutionStatus: TypeResolutionStatus = {
74
37
  successful: false,
75
38
  };
76
39
 
@@ -78,34 +41,49 @@ function resolveTypeAnnotation(
78
41
  if (node.type === 'NullableTypeAnnotation') {
79
42
  nullable = true;
80
43
  node = node.typeAnnotation;
81
- } else if (node.type === 'GenericTypeAnnotation') {
82
- typeAliasResolutionStatus = {
83
- successful: true,
84
- aliasName: node.id.name,
85
- };
86
- const resolvedTypeAnnotation = types[node.id.name];
87
- if (
88
- resolvedTypeAnnotation == null ||
89
- resolvedTypeAnnotation.type === 'EnumDeclaration'
90
- ) {
91
- break;
92
- }
44
+ continue;
45
+ }
93
46
 
94
- invariant(
95
- resolvedTypeAnnotation.type === 'TypeAlias',
96
- `GenericTypeAnnotation '${node.id.name}' must resolve to a TypeAlias. Instead, it resolved to a '${resolvedTypeAnnotation.type}'`,
97
- );
47
+ if (node.type !== 'GenericTypeAnnotation') {
48
+ break;
49
+ }
98
50
 
99
- node = resolvedTypeAnnotation.right;
100
- } else {
51
+ const resolvedTypeAnnotation = types[node.id.name];
52
+ if (resolvedTypeAnnotation == null) {
101
53
  break;
102
54
  }
55
+
56
+ switch (resolvedTypeAnnotation.type) {
57
+ case 'TypeAlias': {
58
+ typeResolutionStatus = {
59
+ successful: true,
60
+ type: 'alias',
61
+ name: node.id.name,
62
+ };
63
+ node = resolvedTypeAnnotation.right;
64
+ break;
65
+ }
66
+ case 'EnumDeclaration': {
67
+ typeResolutionStatus = {
68
+ successful: true,
69
+ type: 'enum',
70
+ name: node.id.name,
71
+ };
72
+ node = resolvedTypeAnnotation.body;
73
+ break;
74
+ }
75
+ default: {
76
+ throw new TypeError(
77
+ `A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
78
+ );
79
+ }
80
+ }
103
81
  }
104
82
 
105
83
  return {
106
84
  nullable: nullable,
107
85
  typeAnnotation: node,
108
- typeAliasResolutionStatus,
86
+ typeResolutionStatus,
109
87
  };
110
88
  }
111
89
 
@@ -119,5 +97,4 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
119
97
  module.exports = {
120
98
  getValueFromTypes,
121
99
  resolveTypeAnnotation,
122
- getTypes,
123
100
  };
@@ -0,0 +1,17 @@
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
+ import type { SchemaType } from "../CodegenSchema";
9
+ import type { ParserType } from "./errors";
10
+
11
+ // useful members only for downstream
12
+ export interface Parser {
13
+ language(): ParserType;
14
+ parseFile(filename: string): SchemaType;
15
+ parseString(contents: string, filename?: string): SchemaType;
16
+ parseModuleFixture(filename: string): SchemaType;
17
+ }
@@ -10,8 +10,17 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {UnionTypeAnnotationMemberType} from '../CodegenSchema.js';
13
+ import type {
14
+ UnionTypeAnnotationMemberType,
15
+ SchemaType,
16
+ NamedShape,
17
+ Nullable,
18
+ NativeModuleParamTypeAnnotation,
19
+ NativeModuleEnumMemberType,
20
+ NativeModuleEnumMembers,
21
+ } from '../CodegenSchema';
14
22
  import type {ParserType} from './errors';
23
+ import type {TypeDeclarationMap} from './utils';
15
24
 
16
25
  /**
17
26
  * This is the main interface for Parsers of various languages.
@@ -24,25 +33,17 @@ export interface Parser {
24
33
  typeParameterInstantiation: string;
25
34
 
26
35
  /**
27
- * Given a property or an index declaration, it returns the key name.
28
- * @parameter propertyOrIndex: an object containing a property or an index declaration.
29
- * @parameter hasteModuleName: a string with the native module name.
30
- * @returns: the key name.
31
- * @throws if propertyOrIndex does not contain a property or an index declaration.
32
- */
33
- getKeyName(propertyOrIndex: $FlowFixMe, hasteModuleName: string): string;
34
- /**
35
- * Given a type declaration, it possibly returns the name of the Enum type.
36
- * @parameter maybeEnumDeclaration: an object possibly containing an Enum declaration.
37
- * @returns: the name of the Enum type.
36
+ * Given a declaration, it returns true if it is a property
38
37
  */
39
- getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string;
38
+ isProperty(property: $FlowFixMe): boolean;
40
39
  /**
41
- * Given a type declaration, it returns a boolean specifying if is an Enum declaration.
42
- * @parameter maybeEnumDeclaration: an object possibly containing an Enum declaration.
43
- * @returns: a boolean specifying if is an Enum declaration.
40
+ * Given a property declaration, it returns the key name.
41
+ * @parameter property: an object containing a property declaration.
42
+ * @parameter hasteModuleName: a string with the native module name.
43
+ * @returns: the key name.
44
+ * @throws if property does not contain a property declaration.
44
45
  */
45
- isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean;
46
+ getKeyName(property: $FlowFixMe, hasteModuleName: string): string;
46
47
  /**
47
48
  * @returns: the Parser language.
48
49
  */
@@ -67,4 +68,110 @@ export interface Parser {
67
68
  remapUnionTypeAnnotationMemberNames(
68
69
  types: $FlowFixMe,
69
70
  ): UnionTypeAnnotationMemberType[];
71
+ /**
72
+ * Given the name of a file, it returns a Schema.
73
+ * @parameter filename: the name of the file.
74
+ * @returns: the Schema of the file.
75
+ */
76
+ parseFile(filename: string): SchemaType;
77
+ /**
78
+ * Given the content of a file, it returns a Schema.
79
+ * @parameter contents: the content of the file.
80
+ * @parameter filename: the name of the file.
81
+ * @returns: the Schema of the file.
82
+ */
83
+ parseString(contents: string, filename: ?string): SchemaType;
84
+ /**
85
+ * Given the name of a file, it returns a Schema.
86
+ * @parameter filename: the name of the file.
87
+ * @returns: the Schema of the file.
88
+ */
89
+ parseModuleFixture(filename: string): SchemaType;
90
+
91
+ /**
92
+ * Given the content of a file, it returns an AST.
93
+ * @parameter contents: the content of the file.
94
+ * @returns: the AST of the file.
95
+ */
96
+ getAst(contents: string): $FlowFixMe;
97
+
98
+ /**
99
+ * Given a FunctionTypeAnnotation, it returns an array of its parameters.
100
+ * @parameter functionTypeAnnotation: a FunctionTypeAnnotation
101
+ * @returns: the parameters of the FunctionTypeAnnotation.
102
+ */
103
+ getFunctionTypeAnnotationParameters(
104
+ functionTypeAnnotation: $FlowFixMe,
105
+ ): $ReadOnlyArray<$FlowFixMe>;
106
+
107
+ /**
108
+ * Given a parameter, it returns the function name of the parameter.
109
+ * @parameter parameter: a parameter of a FunctionTypeAnnotation.
110
+ * @returns: the function name of the parameter.
111
+ */
112
+ getFunctionNameFromParameter(
113
+ parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
114
+ ): $FlowFixMe;
115
+
116
+ /**
117
+ * Given a parameter, it returns its name.
118
+ * @parameter parameter: a parameter of a FunctionTypeAnnotation.
119
+ * @returns: the name of the parameter.
120
+ */
121
+ getParameterName(parameter: $FlowFixMe): string;
122
+
123
+ /**
124
+ * Given a parameter, it returns its typeAnnotation.
125
+ * @parameter parameter: a parameter of a FunctionTypeAnnotation.
126
+ * @returns: the typeAnnotation of the parameter.
127
+ */
128
+ getParameterTypeAnnotation(param: $FlowFixMe): $FlowFixMe;
129
+
130
+ /**
131
+ * Given a FunctionTypeAnnotation, it returns its returnType.
132
+ * @parameter functionTypeAnnotation: a FunctionTypeAnnotation
133
+ * @returns: the returnType of the FunctionTypeAnnotation.
134
+ */
135
+ getFunctionTypeAnnotationReturnType(
136
+ functionTypeAnnotation: $FlowFixMe,
137
+ ): $FlowFixMe;
138
+
139
+ /**
140
+ * Calculates an enum's members type
141
+ */
142
+ parseEnumMembersType(typeAnnotation: $FlowFixMe): NativeModuleEnumMemberType;
143
+
144
+ /**
145
+ * Throws if enum mebers are not supported
146
+ */
147
+ validateEnumMembersSupported(
148
+ typeAnnotation: $FlowFixMe,
149
+ enumMembersType: NativeModuleEnumMemberType,
150
+ ): void;
151
+
152
+ /**
153
+ * Calculates enum's members
154
+ */
155
+ parseEnumMembers(typeAnnotation: $FlowFixMe): NativeModuleEnumMembers;
156
+
157
+ /**
158
+ * Given a node, it returns true if it is a module interface
159
+ */
160
+ isModuleInterface(node: $FlowFixMe): boolean;
161
+
162
+ /**
163
+ * Given a typeAnnotation, it returns the annotated element.
164
+ * @paramater typeAnnotation: the annotation for a type.
165
+ * @paramater types: a map of type declarations.
166
+ * @returns: the annotated element.
167
+ */
168
+ extractAnnotatedElement(
169
+ typeAnnotation: $FlowFixMe,
170
+ types: TypeDeclarationMap,
171
+ ): $FlowFixMe;
172
+
173
+ /**
174
+ * Given the AST, returns the TypeDeclarationMap
175
+ */
176
+ getTypes(ast: $FlowFixMe): TypeDeclarationMap;
70
177
  }