Skip to content

Commit f3b366b

Browse files
Pranav-yadavOlimpiaZurek
authored andcommitted
Merge Flow and TS Parsers' IncorrectModuleRegistryCallArgumentTypeParserError error-emitting code (facebook#36252)
Summary: > [Codegen 83 - assigned to Pranav-yadav] Create a function throwIfIncorrectModuleRegistryCallArgumnent function in the errors.js file and factor together the code from [Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L407-L415) and [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L513-L521). Update the Parsers to return the right Literl type Merged `Flow` and `TS` Parsers' `IncorrectModuleRegistryCallArgumentTypeParserError` error-emitting code - into `throwIfIncorrectModuleRegistryCallArgument` fun in `error-utils.js` ## Changelog [INTERNAL] [CHANGED] - Merge `Flow` and `TS` Parsers' `IncorrectModuleRegistryCallArgumentTypeParserError` error-emitting code Pull Request resolved: facebook#36252 Test Plan: - `yarn test` Reviewed By: rshest Differential Revision: D43502843 Pulled By: cipolleschi fbshipit-source-id: eb63b40d433f90134a80c02c8f750257c82104a3
1 parent 3bf7745 commit f3b366b

File tree

4 files changed

+84
-20
lines changed

4 files changed

+84
-20
lines changed

packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
throwIfUnusedModuleInterfaceParserError,
1919
throwIfWrongNumberOfCallExpressionArgs,
2020
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
21+
throwIfIncorrectModuleRegistryCallArgument,
2122
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
2223
throwIfMoreThanOneModuleInterfaceParserError,
2324
throwIfModuleTypeIsUnsupported,
@@ -33,6 +34,7 @@ const {
3334
UnusedModuleInterfaceParserError,
3435
IncorrectModuleRegistryCallArityParserError,
3536
IncorrectModuleRegistryCallTypeParameterParserError,
37+
IncorrectModuleRegistryCallArgumentTypeParserError,
3638
UnsupportedFunctionReturnTypeAnnotationParserError,
3739
UntypedModuleRegistryCallParserError,
3840
MoreThanOneModuleInterfaceParserError,
@@ -461,6 +463,55 @@ describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
461463
});
462464
});
463465

466+
describe('throwIfIncorrectModuleRegistryCallArgument', () => {
467+
const nativeModuleName = 'moduleName';
468+
const methodName = 'methodName';
469+
470+
it('throw error if callExpressionArg type is unsupported in Flow', () => {
471+
const callExpressionArg = {type: 'NotLiteral'};
472+
expect(() => {
473+
throwIfIncorrectModuleRegistryCallArgument(
474+
nativeModuleName,
475+
callExpressionArg,
476+
methodName,
477+
);
478+
}).toThrow(IncorrectModuleRegistryCallArgumentTypeParserError);
479+
});
480+
481+
it("don't throw error if callExpressionArg type is `Literal` in Flow", () => {
482+
const callExpressionArg = {type: 'Literal'};
483+
expect(() => {
484+
throwIfIncorrectModuleRegistryCallArgument(
485+
nativeModuleName,
486+
callExpressionArg,
487+
methodName,
488+
);
489+
}).not.toThrow(IncorrectModuleRegistryCallArgumentTypeParserError);
490+
});
491+
492+
it('throw error if callExpressionArg type is unsupported in TypeScript', () => {
493+
const callExpressionArg = {type: 'NotStringLiteral'};
494+
expect(() => {
495+
throwIfIncorrectModuleRegistryCallArgument(
496+
nativeModuleName,
497+
callExpressionArg,
498+
methodName,
499+
);
500+
}).toThrow(IncorrectModuleRegistryCallArgumentTypeParserError);
501+
});
502+
503+
it("don't throw error if callExpressionArg type is `StringLiteral` in TypeScript", () => {
504+
const callExpressionArg = {type: 'StringLiteral'};
505+
expect(() => {
506+
throwIfIncorrectModuleRegistryCallArgument(
507+
nativeModuleName,
508+
callExpressionArg,
509+
methodName,
510+
);
511+
}).not.toThrow(IncorrectModuleRegistryCallArgumentTypeParserError);
512+
});
513+
});
514+
464515
describe('throwIfUntypedModule', () => {
465516
const hasteModuleName = 'moduleName';
466517
const methodName = 'methodName';

packages/react-native-codegen/src/parsers/error-utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
UnusedModuleInterfaceParserError,
2323
IncorrectModuleRegistryCallArityParserError,
2424
IncorrectModuleRegistryCallTypeParameterParserError,
25+
IncorrectModuleRegistryCallArgumentTypeParserError,
2526
UnsupportedObjectPropertyValueTypeAnnotationParserError,
2627
UntypedModuleRegistryCallParserError,
2728
UnsupportedModulePropertyParserError,
@@ -260,6 +261,25 @@ function throwIfArrayElementTypeAnnotationIsUnsupported(
260261
}
261262
}
262263

264+
function throwIfIncorrectModuleRegistryCallArgument(
265+
nativeModuleName: string,
266+
callExpressionArg: $FlowFixMe,
267+
methodName: string,
268+
) {
269+
if (
270+
callExpressionArg.type !== 'StringLiteral' &&
271+
callExpressionArg.type !== 'Literal'
272+
) {
273+
const {type} = callExpressionArg;
274+
throw new IncorrectModuleRegistryCallArgumentTypeParserError(
275+
nativeModuleName,
276+
callExpressionArg,
277+
methodName,
278+
type,
279+
);
280+
}
281+
}
282+
263283
module.exports = {
264284
throwIfModuleInterfaceIsMisnamed,
265285
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
@@ -274,4 +294,5 @@ module.exports = {
274294
throwIfMoreThanOneModuleInterfaceParserError,
275295
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
276296
throwIfArrayElementTypeAnnotationIsUnsupported,
297+
throwIfIncorrectModuleRegistryCallArgument,
277298
};

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ const {
5656

5757
const {
5858
UnsupportedTypeAnnotationParserError,
59-
IncorrectModuleRegistryCallArgumentTypeParserError,
6059
UnsupportedGenericParserError,
6160
} = require('../../errors');
6261

@@ -67,6 +66,7 @@ const {
6766
throwIfWrongNumberOfCallExpressionArgs,
6867
throwIfMoreThanOneModuleRegistryCalls,
6968
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
69+
throwIfIncorrectModuleRegistryCallArgument,
7070
throwIfUntypedModule,
7171
throwIfMoreThanOneModuleInterfaceParserError,
7272
} = require('../../error-utils');
@@ -395,15 +395,11 @@ function buildModuleSchema(
395395
callExpression.arguments.length,
396396
);
397397

398-
if (callExpression.arguments[0].type !== 'Literal') {
399-
const {type} = callExpression.arguments[0];
400-
throw new IncorrectModuleRegistryCallArgumentTypeParserError(
401-
hasteModuleName,
402-
callExpression.arguments[0],
403-
methodName,
404-
type,
405-
);
406-
}
398+
throwIfIncorrectModuleRegistryCallArgument(
399+
hasteModuleName,
400+
callExpression.arguments[0],
401+
methodName,
402+
);
407403

408404
const $moduleName = callExpression.arguments[0].value;
409405

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ const {
6363
const {
6464
UnsupportedGenericParserError,
6565
UnsupportedTypeAnnotationParserError,
66-
IncorrectModuleRegistryCallArgumentTypeParserError,
6766
} = require('../../errors');
6867

6968
const {
@@ -75,6 +74,7 @@ const {
7574
throwIfMoreThanOneModuleRegistryCalls,
7675
throwIfMoreThanOneModuleInterfaceParserError,
7776
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
77+
throwIfIncorrectModuleRegistryCallArgument,
7878
} = require('../../error-utils');
7979

8080
const language = 'TypeScript';
@@ -501,15 +501,11 @@ function buildModuleSchema(
501501
callExpression.arguments.length,
502502
);
503503

504-
if (callExpression.arguments[0].type !== 'StringLiteral') {
505-
const {type} = callExpression.arguments[0];
506-
throw new IncorrectModuleRegistryCallArgumentTypeParserError(
507-
hasteModuleName,
508-
callExpression.arguments[0],
509-
methodName,
510-
type,
511-
);
512-
}
504+
throwIfIncorrectModuleRegistryCallArgument(
505+
hasteModuleName,
506+
callExpression.arguments[0],
507+
methodName,
508+
);
513509

514510
const $moduleName = callExpression.arguments[0].value;
515511

0 commit comments

Comments
 (0)