Skip to content

Commit 1e15e21

Browse files
dhruvtailor7facebook-github-bot
authored andcommitted
extracted UntypedModuleRegistryCallParserError to a throwing function in error-utils.js (#34953)
Summary: This PR is part of #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 ## Changelog [Internal] [Changed] - Extract `UntypedModuleRegistryCallParserError` to a separate function inside `error-utils.js` file. Pull Request resolved: #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 b87d371 commit 1e15e21

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
@@ -400,3 +400,46 @@ describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
400400
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
401401
});
402402
});
403+
404+
describe('throwIfUntypedModule', () => {
405+
const {throwIfUntypedModule} = require('../error-utils');
406+
const {UntypedModuleRegistryCallParserError} = require('../errors');
407+
const hasteModuleName = 'moduleName';
408+
const methodName = 'methodName';
409+
const moduleName = 'moduleName';
410+
const callExpressions = [];
411+
412+
it('should throw error if module does not have a type', () => {
413+
const typeArguments = null;
414+
const language = 'Flow';
415+
expect(() =>
416+
throwIfUntypedModule(
417+
typeArguments,
418+
hasteModuleName,
419+
callExpressions,
420+
methodName,
421+
moduleName,
422+
language,
423+
),
424+
).toThrowError(UntypedModuleRegistryCallParserError);
425+
});
426+
427+
it('should not throw error if module have a type', () => {
428+
const typeArguments = {
429+
type: 'TSTypeParameterInstantiations',
430+
params: [],
431+
};
432+
433+
const language = 'TypeScript';
434+
expect(() =>
435+
throwIfUntypedModule(
436+
typeArguments,
437+
hasteModuleName,
438+
callExpressions,
439+
methodName,
440+
moduleName,
441+
language,
442+
),
443+
).not.toThrowError(UntypedModuleRegistryCallParserError);
444+
});
445+
});

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

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

2324
function throwIfModuleInterfaceNotFound(
@@ -122,10 +123,30 @@ function throwIfIncorrectModuleRegistryCallTypeParameterParserError(
122123
}
123124
}
124125

126+
function throwIfUntypedModule(
127+
typeArguments: $FlowFixMe,
128+
hasteModuleName: string,
129+
callExpression: $FlowFixMe,
130+
methodName: string,
131+
$moduleName: string,
132+
language: ParserType,
133+
) {
134+
if (typeArguments == null) {
135+
throw new UntypedModuleRegistryCallParserError(
136+
hasteModuleName,
137+
callExpression,
138+
methodName,
139+
$moduleName,
140+
language,
141+
);
142+
}
143+
}
144+
125145
module.exports = {
126146
throwIfModuleInterfaceNotFound,
127147
throwIfMoreThanOneModuleRegistryCalls,
128148
throwIfUnusedModuleInterfaceParserError,
129149
throwIfWrongNumberOfCallExpressionArgs,
130150
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
151+
throwIfUntypedModule,
131152
};

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ const {
6666
UnsupportedModulePropertyParserError,
6767
UnsupportedObjectPropertyTypeAnnotationParserError,
6868
UnsupportedObjectPropertyValueTypeAnnotationParserError,
69-
UntypedModuleRegistryCallParserError,
7069
IncorrectModuleRegistryCallArgumentTypeParserError,
7170
} = require('../../errors.js');
71+
const {throwIfUntypedModule} = require('../../error-utils');
7272

7373
const {
7474
throwIfModuleInterfaceNotFound,
@@ -673,15 +673,14 @@ function buildModuleSchema(
673673

674674
const $moduleName = callExpression.arguments[0].value;
675675

676-
if (typeArguments == null) {
677-
throw new UntypedModuleRegistryCallParserError(
678-
hasteModuleName,
679-
callExpression,
680-
methodName,
681-
$moduleName,
682-
language,
683-
);
684-
}
676+
throwIfUntypedModule(
677+
typeArguments,
678+
hasteModuleName,
679+
callExpression,
680+
methodName,
681+
$moduleName,
682+
language,
683+
);
685684

686685
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
687686
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
@@ -66,9 +66,9 @@ const {
6666
UnsupportedModulePropertyParserError,
6767
UnsupportedObjectPropertyTypeAnnotationParserError,
6868
UnsupportedObjectPropertyValueTypeAnnotationParserError,
69-
UntypedModuleRegistryCallParserError,
7069
IncorrectModuleRegistryCallArgumentTypeParserError,
7170
} = require('../../errors.js');
71+
const {throwIfUntypedModule} = require('../../error-utils');
7272

7373
const {
7474
throwIfUnusedModuleInterfaceParserError,
@@ -688,15 +688,14 @@ function buildModuleSchema(
688688

689689
const $moduleName = callExpression.arguments[0].value;
690690

691-
if (typeParameters == null) {
692-
throw new UntypedModuleRegistryCallParserError(
693-
hasteModuleName,
694-
callExpression,
695-
methodName,
696-
$moduleName,
697-
language,
698-
);
699-
}
691+
throwIfUntypedModule(
692+
typeParameters,
693+
hasteModuleName,
694+
callExpression,
695+
methodName,
696+
$moduleName,
697+
language,
698+
);
700699

701700
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
702701
hasteModuleName,

0 commit comments

Comments
 (0)