@react-native/codegen 0.72.4 → 0.73.0

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 (28) hide show
  1. package/lib/generators/components/GenerateEventEmitterCpp.js +33 -44
  2. package/lib/generators/components/GenerateEventEmitterCpp.js.flow +35 -44
  3. package/lib/generators/components/GenerateEventEmitterH.js +0 -2
  4. package/lib/generators/components/GenerateEventEmitterH.js.flow +0 -2
  5. package/lib/generators/modules/GenerateModuleCpp.js +3 -3
  6. package/lib/generators/modules/GenerateModuleCpp.js.flow +3 -3
  7. package/lib/generators/modules/GenerateModuleJavaSpec.js +1 -2
  8. package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +1 -2
  9. package/lib/parsers/flow/components/index.js +19 -57
  10. package/lib/parsers/flow/components/index.js.flow +20 -63
  11. package/lib/parsers/flow/modules/index.js +16 -26
  12. package/lib/parsers/flow/modules/index.js.flow +17 -26
  13. package/lib/parsers/flow/parser.js +16 -1
  14. package/lib/parsers/flow/parser.js.flow +13 -1
  15. package/lib/parsers/parser.js.flow +19 -0
  16. package/lib/parsers/parserMock.js +9 -0
  17. package/lib/parsers/parserMock.js.flow +12 -0
  18. package/lib/parsers/parsers-commons.js +36 -0
  19. package/lib/parsers/parsers-commons.js.flow +51 -1
  20. package/lib/parsers/parsers-primitives.js +37 -35
  21. package/lib/parsers/parsers-primitives.js.flow +41 -35
  22. package/lib/parsers/typescript/components/index.js +19 -57
  23. package/lib/parsers/typescript/components/index.js.flow +18 -63
  24. package/lib/parsers/typescript/modules/index.js +16 -26
  25. package/lib/parsers/typescript/modules/index.js.flow +17 -25
  26. package/lib/parsers/typescript/parser.js +28 -1
  27. package/lib/parsers/typescript/parser.js.flow +26 -1
  28. package/package.json +1 -1
@@ -26,8 +26,11 @@ const _require6 = require('../../error-utils'),
26
26
  const _require7 = require('../../parsers-commons'),
27
27
  createComponentConfig = _require7.createComponentConfig,
28
28
  findNativeComponentType = _require7.findNativeComponentType,
29
+ propertyNames = _require7.propertyNames,
29
30
  getCommandOptions = _require7.getCommandOptions,
30
- getOptions = _require7.getOptions;
31
+ getOptions = _require7.getOptions,
32
+ getCommandTypeNameAndOptionsExpression =
33
+ _require7.getCommandTypeNameAndOptionsExpression;
31
34
 
32
35
  // $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
33
36
  function findComponentConfig(ast, parser) {
@@ -49,67 +52,33 @@ function findComponentConfig(ast, parser) {
49
52
  node => node.type === 'ExportNamedDeclaration',
50
53
  );
51
54
  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
- })
55
+ .map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
82
56
  .filter(Boolean);
83
57
  throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
84
58
  return createComponentConfig(foundConfig, commandsTypeNames);
85
59
  }
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
- ) {
60
+ function getCommandProperties(ast, parser) {
61
+ const _findComponentConfig = findComponentConfig(ast, parser),
62
+ commandTypeName = _findComponentConfig.commandTypeName,
63
+ commandOptionsExpression = _findComponentConfig.commandOptionsExpression;
93
64
  if (commandTypeName == null) {
94
65
  return [];
95
66
  }
67
+ const types = parser.getTypes(ast);
96
68
  const typeAlias = types[commandTypeName];
97
69
  if (typeAlias.type !== 'TSInterfaceDeclaration') {
98
70
  throw new Error(
99
71
  `The type argument for codegenNativeCommands must be an interface, received ${typeAlias.type}`,
100
72
  );
101
73
  }
102
- let properties;
103
- try {
104
- properties = typeAlias.body.body;
105
- } catch (e) {
74
+ const properties = parser.bodyProperties(typeAlias);
75
+ if (!properties) {
106
76
  throw new Error(
107
77
  `Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen typescript file`,
108
78
  );
109
79
  }
110
- const typeScriptPropertyNames = properties
111
- .map(property => property && property.key && property.key.name)
112
- .filter(Boolean);
80
+ const typeScriptPropertyNames = propertyNames(properties);
81
+ const commandOptions = getCommandOptions(commandOptionsExpression);
113
82
  if (commandOptions == null || commandOptions.supportedCommands == null) {
114
83
  throw new Error(
115
84
  'codegenNativeCommands must be given an options object with supportedCommands array',
@@ -135,20 +104,13 @@ function getCommandProperties(
135
104
 
136
105
  // $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
137
106
  function buildComponentSchema(ast, parser) {
138
- const _findComponentConfig = findComponentConfig(ast, parser),
139
- componentName = _findComponentConfig.componentName,
140
- propsTypeName = _findComponentConfig.propsTypeName,
141
- commandTypeName = _findComponentConfig.commandTypeName,
142
- commandOptionsExpression = _findComponentConfig.commandOptionsExpression,
143
- optionsExpression = _findComponentConfig.optionsExpression;
107
+ const _findComponentConfig2 = findComponentConfig(ast, parser),
108
+ componentName = _findComponentConfig2.componentName,
109
+ propsTypeName = _findComponentConfig2.propsTypeName,
110
+ optionsExpression = _findComponentConfig2.optionsExpression;
144
111
  const types = parser.getTypes(ast);
145
112
  const propProperties = getProperties(propsTypeName, types);
146
- const commandOptions = getCommandOptions(commandOptionsExpression);
147
- const commandProperties = getCommandProperties(
148
- commandTypeName,
149
- types,
150
- commandOptions,
151
- );
113
+ const commandProperties = getCommandProperties(ast, parser);
152
114
  const options = getOptions(optionsExpression);
153
115
  const extendsProps = [];
154
116
  const componentPropAsts = [];
@@ -11,8 +11,6 @@
11
11
  'use strict';
12
12
  import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
13
13
  import type {Parser} from '../../parser';
14
- import type {TypeDeclarationMap} from '../../utils';
15
- import type {CommandOptions} from '../../parsers-commons';
16
14
  import type {ComponentSchemaBuilderConfig} from '../../schema.js';
17
15
 
18
16
  const {getCommands} = require('./commands');
@@ -24,8 +22,10 @@ const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
24
22
  const {
25
23
  createComponentConfig,
26
24
  findNativeComponentType,
25
+ propertyNames,
27
26
  getCommandOptions,
28
27
  getOptions,
28
+ getCommandTypeNameAndOptionsExpression,
29
29
  } = require('../../parsers-commons');
30
30
 
31
31
  // $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
@@ -54,40 +54,7 @@ function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
54
54
  );
55
55
 
56
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
- })
57
+ .map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
91
58
  .filter(Boolean);
92
59
 
93
60
  throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
@@ -95,17 +62,16 @@ function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
95
62
  return createComponentConfig(foundConfig, commandsTypeNames);
96
63
  }
97
64
 
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
- ) {
65
+ function getCommandProperties(ast: $FlowFixMe, parser: Parser) {
66
+ const {commandTypeName, commandOptionsExpression} = findComponentConfig(
67
+ ast,
68
+ parser,
69
+ );
105
70
  if (commandTypeName == null) {
106
71
  return [];
107
72
  }
108
73
 
74
+ const types = parser.getTypes(ast);
109
75
  const typeAlias = types[commandTypeName];
110
76
 
111
77
  if (typeAlias.type !== 'TSInterfaceDeclaration') {
@@ -114,19 +80,16 @@ function getCommandProperties(
114
80
  );
115
81
  }
116
82
 
117
- let properties;
118
- try {
119
- properties = typeAlias.body.body;
120
- } catch (e) {
83
+ const properties = parser.bodyProperties(typeAlias);
84
+ if (!properties) {
121
85
  throw new Error(
122
86
  `Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen typescript file`,
123
87
  );
124
88
  }
125
89
 
126
- const typeScriptPropertyNames = properties
127
- .map(property => property && property.key && property.key.name)
128
- .filter(Boolean);
90
+ const typeScriptPropertyNames = propertyNames(properties);
129
91
 
92
+ const commandOptions = getCommandOptions(commandOptionsExpression);
130
93
  if (commandOptions == null || commandOptions.supportedCommands == null) {
131
94
  throw new Error(
132
95
  'codegenNativeCommands must be given an options object with supportedCommands array',
@@ -158,24 +121,16 @@ function buildComponentSchema(
158
121
  ast: $FlowFixMe,
159
122
  parser: Parser,
160
123
  ): ComponentSchemaBuilderConfig {
161
- const {
162
- componentName,
163
- propsTypeName,
164
- commandTypeName,
165
- commandOptionsExpression,
166
- optionsExpression,
167
- } = findComponentConfig(ast, parser);
124
+ const {componentName, propsTypeName, optionsExpression} = findComponentConfig(
125
+ ast,
126
+ parser,
127
+ );
168
128
 
169
129
  const types = parser.getTypes(ast);
170
130
 
171
131
  const propProperties = getProperties(propsTypeName, types);
172
- const commandOptions = getCommandOptions(commandOptionsExpression);
173
132
 
174
- const commandProperties = getCommandProperties(
175
- commandTypeName,
176
- types,
177
- commandOptions,
178
- );
133
+ const commandProperties = getCommandProperties(ast, parser);
179
134
 
180
135
  const options = getOptions(optionsExpression);
181
136
 
@@ -20,15 +20,10 @@ const _require4 = require('../../parsers-commons'),
20
20
  parseObjectProperty = _require4.parseObjectProperty;
21
21
  const _require5 = require('../../parsers-primitives'),
22
22
  emitArrayType = _require5.emitArrayType,
23
- emitBoolean = _require5.emitBoolean,
24
23
  emitFunction = _require5.emitFunction,
25
- emitNumber = _require5.emitNumber,
26
24
  emitGenericObject = _require5.emitGenericObject,
27
25
  emitPromise = _require5.emitPromise,
28
26
  emitRootTag = _require5.emitRootTag,
29
- emitVoid = _require5.emitVoid,
30
- emitString = _require5.emitString,
31
- emitMixed = _require5.emitMixed,
32
27
  emitUnion = _require5.emitUnion,
33
28
  emitCommonTypes = _require5.emitCommonTypes,
34
29
  typeAliasResolution = _require5.typeAliasResolution,
@@ -341,18 +336,6 @@ function translateTypeAnnotation(
341
336
  parser,
342
337
  );
343
338
  }
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
339
  case 'TSFunctionType': {
357
340
  return emitFunction(
358
341
  nullable,
@@ -370,19 +353,26 @@ function translateTypeAnnotation(
370
353
  case 'TSUnionType': {
371
354
  return emitUnion(nullable, hasteModuleName, typeAnnotation, parser);
372
355
  }
373
- case 'TSUnknownKeyword': {
374
- if (cxxOnly) {
375
- return emitMixed(nullable);
376
- }
377
- // Fallthrough
378
- }
379
-
380
356
  default: {
381
- throw new UnsupportedTypeAnnotationParserError(
357
+ const commonType = emitCommonTypes(
382
358
  hasteModuleName,
359
+ types,
383
360
  typeAnnotation,
384
- parser.language(),
361
+ aliasMap,
362
+ enumMap,
363
+ tryParse,
364
+ cxxOnly,
365
+ nullable,
366
+ parser,
385
367
  );
368
+ if (!commonType) {
369
+ throw new UnsupportedTypeAnnotationParserError(
370
+ hasteModuleName,
371
+ typeAnnotation,
372
+ parser.language(),
373
+ );
374
+ }
375
+ return commonType;
386
376
  }
387
377
  }
388
378
  }
@@ -34,15 +34,10 @@ const {parseObjectProperty} = require('../../parsers-commons');
34
34
 
35
35
  const {
36
36
  emitArrayType,
37
- emitBoolean,
38
37
  emitFunction,
39
- emitNumber,
40
38
  emitGenericObject,
41
39
  emitPromise,
42
40
  emitRootTag,
43
- emitVoid,
44
- emitString,
45
- emitMixed,
46
41
  emitUnion,
47
42
  emitCommonTypes,
48
43
  typeAliasResolution,
@@ -353,18 +348,6 @@ function translateTypeAnnotation(
353
348
  parser,
354
349
  );
355
350
  }
356
- case 'TSBooleanKeyword': {
357
- return emitBoolean(nullable);
358
- }
359
- case 'TSNumberKeyword': {
360
- return emitNumber(nullable);
361
- }
362
- case 'TSVoidKeyword': {
363
- return emitVoid(nullable);
364
- }
365
- case 'TSStringKeyword': {
366
- return emitString(nullable);
367
- }
368
351
  case 'TSFunctionType': {
369
352
  return emitFunction(
370
353
  nullable,
@@ -382,18 +365,27 @@ function translateTypeAnnotation(
382
365
  case 'TSUnionType': {
383
366
  return emitUnion(nullable, hasteModuleName, typeAnnotation, parser);
384
367
  }
385
- case 'TSUnknownKeyword': {
386
- if (cxxOnly) {
387
- return emitMixed(nullable);
388
- }
389
- // Fallthrough
390
- }
391
368
  default: {
392
- throw new UnsupportedTypeAnnotationParserError(
369
+ const commonType = emitCommonTypes(
393
370
  hasteModuleName,
371
+ types,
394
372
  typeAnnotation,
395
- parser.language(),
373
+ aliasMap,
374
+ enumMap,
375
+ tryParse,
376
+ cxxOnly,
377
+ nullable,
378
+ parser,
396
379
  );
380
+
381
+ if (!commonType) {
382
+ throw new UnsupportedTypeAnnotationParserError(
383
+ hasteModuleName,
384
+ typeAnnotation,
385
+ parser.language(),
386
+ );
387
+ }
388
+ return commonType;
397
389
  }
398
390
  }
399
391
  }
@@ -86,7 +86,13 @@ class TypeScriptParser {
86
86
  return 'TypeScript';
87
87
  }
88
88
  nameForGenericTypeAnnotation(typeAnnotation) {
89
- return typeAnnotation.typeName.name;
89
+ var _typeAnnotation$typeN;
90
+ return typeAnnotation === null || typeAnnotation === void 0
91
+ ? void 0
92
+ : (_typeAnnotation$typeN = typeAnnotation.typeName) === null ||
93
+ _typeAnnotation$typeN === void 0
94
+ ? void 0
95
+ : _typeAnnotation$typeN.name;
90
96
  }
91
97
  checkIfInvalidModule(typeArguments) {
92
98
  return (
@@ -224,6 +230,9 @@ class TypeScriptParser {
224
230
  node.extends[0].expression.name === 'TurboModule'
225
231
  );
226
232
  }
233
+ isGenericTypeAnnotation(type) {
234
+ return type === 'TSTypeReference';
235
+ }
227
236
  extractAnnotatedElement(typeAnnotation, types) {
228
237
  return types[typeAnnotation.typeParameters.params[0].typeName.name];
229
238
  }
@@ -306,6 +315,24 @@ class TypeScriptParser {
306
315
  getAnnotatedElementProperties(annotatedElement) {
307
316
  return annotatedElement.typeAnnotation.members;
308
317
  }
318
+ bodyProperties(typeAlias) {
319
+ return typeAlias.body.body;
320
+ }
321
+ convertKeywordToTypeAnnotation(keyword) {
322
+ switch (keyword) {
323
+ case 'TSBooleanKeyword':
324
+ return 'BooleanTypeAnnotation';
325
+ case 'TSNumberKeyword':
326
+ return 'NumberTypeAnnotation';
327
+ case 'TSVoidKeyword':
328
+ return 'VoidTypeAnnotation';
329
+ case 'TSStringKeyword':
330
+ return 'StringTypeAnnotation';
331
+ case 'TSUnknownKeyword':
332
+ return 'MixedTypeAnnotation';
333
+ }
334
+ return keyword;
335
+ }
309
336
  }
310
337
  module.exports = {
311
338
  TypeScriptParser,
@@ -67,7 +67,7 @@ class TypeScriptParser implements Parser {
67
67
  }
68
68
 
69
69
  nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
70
- return typeAnnotation.typeName.name;
70
+ return typeAnnotation?.typeName?.name;
71
71
  }
72
72
 
73
73
  checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {
@@ -210,6 +210,10 @@ class TypeScriptParser implements Parser {
210
210
  );
211
211
  }
212
212
 
213
+ isGenericTypeAnnotation(type: $FlowFixMe): boolean {
214
+ return type === 'TSTypeReference';
215
+ }
216
+
213
217
  extractAnnotatedElement(
214
218
  typeAnnotation: $FlowFixMe,
215
219
  types: TypeDeclarationMap,
@@ -303,6 +307,27 @@ class TypeScriptParser implements Parser {
303
307
  getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe {
304
308
  return annotatedElement.typeAnnotation.members;
305
309
  }
310
+
311
+ bodyProperties(typeAlias: TypeDeclarationMap): $ReadOnlyArray<$FlowFixMe> {
312
+ return typeAlias.body.body;
313
+ }
314
+
315
+ convertKeywordToTypeAnnotation(keyword: string): string {
316
+ switch (keyword) {
317
+ case 'TSBooleanKeyword':
318
+ return 'BooleanTypeAnnotation';
319
+ case 'TSNumberKeyword':
320
+ return 'NumberTypeAnnotation';
321
+ case 'TSVoidKeyword':
322
+ return 'VoidTypeAnnotation';
323
+ case 'TSStringKeyword':
324
+ return 'StringTypeAnnotation';
325
+ case 'TSUnknownKeyword':
326
+ return 'MixedTypeAnnotation';
327
+ }
328
+
329
+ return keyword;
330
+ }
306
331
  }
307
332
 
308
333
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native/codegen",
3
- "version": "0.72.4",
3
+ "version": "0.73.0",
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": {