Skip to content

Commit 15f974f

Browse files
dhruvtailor7mohitcharkha
authored andcommitted
extracted UntypedModuleRegistryCallParserError to a throwing function in error-utils.js (facebook#34953)
Summary: This PR is part of facebook#34872. As a part of this PR, `UntypedModuleRegistryCallParserError` is separated into its own throwing function in `error-utils.js` file and is used in both Flow and TypeScript parsers [Internal] [Changed] - Extract `UntypedModuleRegistryCallParserError` to a separate function inside `error-utils.js` file. Pull Request resolved: facebook#34953 Test Plan: Added unit case in error-utils-test.js file to test the new function. Output of `yarn jest react-native-codegen` ensures all passed test cases. <img width="450" alt="Screenshot 2022-10-12 at 12 44 36 PM" src="https://user-images.githubusercontent.com/32268377/195277708-97340db3-f3d8-48a3-9a59-95d2747c67b0.png"> Reviewed By: christophpurrer Differential Revision: D40297017 Pulled By: cipolleschi fbshipit-source-id: b02dcf0e110ab903a0d1831783194ae4a543075b
1 parent 918cc1b commit 15f974f

File tree

4 files changed

+82
-20
lines changed

4 files changed

+82
-20
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,46 @@ describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
423423
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
424424
});
425425
});
426+
427+
describe('throwIfUntypedModule', () => {
428+
const {throwIfUntypedModule} = require('../error-utils');
429+
const {UntypedModuleRegistryCallParserError} = require('../errors');
430+
const hasteModuleName = 'moduleName';
431+
const methodName = 'methodName';
432+
const moduleName = 'moduleName';
433+
const callExpressions = [];
434+
435+
it('should throw error if module does not have a type', () => {
436+
const typeArguments = null;
437+
const language = 'Flow';
438+
expect(() =>
439+
throwIfUntypedModule(
440+
typeArguments,
441+
hasteModuleName,
442+
callExpressions,
443+
methodName,
444+
moduleName,
445+
language,
446+
),
447+
).toThrowError(UntypedModuleRegistryCallParserError);
448+
});
449+
450+
it('should not throw error if module have a type', () => {
451+
const typeArguments = {
452+
type: 'TSTypeParameterInstantiations',
453+
params: [],
454+
};
455+
456+
const language = 'TypeScript';
457+
expect(() =>
458+
throwIfUntypedModule(
459+
typeArguments,
460+
hasteModuleName,
461+
callExpressions,
462+
methodName,
463+
moduleName,
464+
language,
465+
),
466+
).not.toThrowError(UntypedModuleRegistryCallParserError);
467+
});
468+
});

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
UnusedModuleInterfaceParserError,
2020
IncorrectModuleRegistryCallArityParserError,
2121
IncorrectModuleRegistryCallTypeParameterParserError,
22+
UntypedModuleRegistryCallParserError,
2223
} = require('./errors.js');
2324

2425
function throwIfModuleInterfaceIsMisnamed(
@@ -137,11 +138,31 @@ function throwIfIncorrectModuleRegistryCallTypeParameterParserError(
137138
}
138139
}
139140

141+
function throwIfUntypedModule(
142+
typeArguments: $FlowFixMe,
143+
hasteModuleName: string,
144+
callExpression: $FlowFixMe,
145+
methodName: string,
146+
$moduleName: string,
147+
language: ParserType,
148+
) {
149+
if (typeArguments == null) {
150+
throw new UntypedModuleRegistryCallParserError(
151+
hasteModuleName,
152+
callExpression,
153+
methodName,
154+
$moduleName,
155+
language,
156+
);
157+
}
158+
}
159+
140160
module.exports = {
141161
throwIfModuleInterfaceIsMisnamed,
142162
throwIfModuleInterfaceNotFound,
143163
throwIfMoreThanOneModuleRegistryCalls,
144164
throwIfUnusedModuleInterfaceParserError,
145165
throwIfWrongNumberOfCallExpressionArgs,
146166
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
167+
throwIfUntypedModule,
147168
};

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ const {
6464
UnsupportedModulePropertyParserError,
6565
UnsupportedObjectPropertyTypeAnnotationParserError,
6666
UnsupportedObjectPropertyValueTypeAnnotationParserError,
67-
UntypedModuleRegistryCallParserError,
6867
IncorrectModuleRegistryCallArgumentTypeParserError,
6968
} = require('../../errors.js');
69+
const {throwIfUntypedModule} = require('../../error-utils');
7070

7171
const {
7272
throwIfModuleInterfaceNotFound,
@@ -667,15 +667,14 @@ function buildModuleSchema(
667667

668668
const $moduleName = callExpression.arguments[0].value;
669669

670-
if (typeArguments == null) {
671-
throw new UntypedModuleRegistryCallParserError(
672-
hasteModuleName,
673-
callExpression,
674-
methodName,
675-
$moduleName,
676-
language,
677-
);
678-
}
670+
throwIfUntypedModule(
671+
typeArguments,
672+
hasteModuleName,
673+
callExpression,
674+
methodName,
675+
$moduleName,
676+
language,
677+
);
679678

680679
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
681680
hasteModuleName,

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ const {
6464
UnsupportedModulePropertyParserError,
6565
UnsupportedObjectPropertyTypeAnnotationParserError,
6666
UnsupportedObjectPropertyValueTypeAnnotationParserError,
67-
UntypedModuleRegistryCallParserError,
6867
IncorrectModuleRegistryCallArgumentTypeParserError,
6968
} = require('../../errors.js');
7069
const {
70+
throwIfUntypedModule,
7171
throwIfModuleInterfaceNotFound,
7272
throwIfMoreThanOneModuleRegistryCalls,
7373
throwIfModuleInterfaceIsMisnamed,
@@ -681,15 +681,14 @@ function buildModuleSchema(
681681

682682
const $moduleName = callExpression.arguments[0].value;
683683

684-
if (typeParameters == null) {
685-
throw new UntypedModuleRegistryCallParserError(
686-
hasteModuleName,
687-
callExpression,
688-
methodName,
689-
$moduleName,
690-
language,
691-
);
692-
}
684+
throwIfUntypedModule(
685+
typeParameters,
686+
hasteModuleName,
687+
callExpression,
688+
methodName,
689+
$moduleName,
690+
language,
691+
);
693692

694693
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
695694
hasteModuleName,

0 commit comments

Comments
 (0)