Extracted IncorrectModuleRegistryCallTypeParameterParserError to throwIfIncorrectModuleRegistryCallTypeParameterParserError (#34941)

Summary:
This PR is part of https://github.com/facebook/react-native/issues/34872.
This PR extracts `IncorrectModuleRegistryCallTypeParameterParserError` exception to a separate function inside an `error-utils.js` file

## Changelog

[Internal] [Changed] - Extract `IncorrectModuleRegistryCallTypeParameterParserError` to a seperate function inside `error-utils.js`

Pull Request resolved: https://github.com/facebook/react-native/pull/34941

Test Plan:
```sh
yarn jest react-native-codegen
```
Added unit case in `error-utils-test.js` file

<img width="940" alt="Screenshot 2022-10-11 at 4 42 03 PM" src="https://user-images.githubusercontent.com/86605635/195076564-3b023c17-661c-4330-805c-0216c4391d59.png">

Reviewed By: dmytrorykun

Differential Revision: D40296642

Pulled By: cipolleschi

fbshipit-source-id: 7c7bba6a4f68e9b8fa4729a7651f22cce6d7ca6e
This commit is contained in:
harshsiriah
2022-10-14 03:09:51 -07:00
committed by Facebook GitHub Bot
parent 76c5b6f7bf
commit bb519ecccb
4 changed files with 309 additions and 30 deletions
@@ -16,12 +16,14 @@ const {
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../error-utils');
const {
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
} = require('../errors');
describe('throwIfModuleInterfaceNotFound', () => {
@@ -147,3 +149,254 @@ describe('throwErrorIfWrongNumberOfCallExpressionArgs', () => {
}).not.toThrow(IncorrectModuleRegistryCallArityParserError);
});
});
describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
const nativeModuleName = 'moduleName';
const methodName = 'methodName';
const moduleName = 'moduleName';
it('throw error if flowTypeArguments type is incorrect', () => {
const flowTypeArguments = {
type: '',
params: [
{
type: 'GenericTypeAnnotation',
id: {
name: 'Spec',
},
},
],
};
const parserType = 'Flow';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('throw error if flowTypeArguments params length is not 1', () => {
const flowTypeArguments = {
type: 'TypeParameterInstantiation',
params: [],
};
const parserType = 'Flow';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('throw error if flowTypeArguments params type is not GenericTypeAnnotation', () => {
const flowTypeArguments = {
type: 'TypeParameterInstantiation',
params: [
{
type: '',
id: {
name: 'Spec',
},
},
],
};
const parserType = 'Flow';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('throw error if flowTypeArguments params id name is not Spec', () => {
const flowTypeArguments = {
type: 'TypeParameterInstantiation',
params: [
{
type: 'GenericTypeAnnotation',
id: {
name: '',
},
},
],
};
const parserType = 'Flow';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('do not throw error if flowTypeArguments are correct', () => {
const flowTypeArguments = {
type: 'TypeParameterInstantiation',
params: [
{
type: 'GenericTypeAnnotation',
id: {
name: 'Spec',
},
},
],
};
const parserType = 'Flow';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('throw error if typeScriptTypeArguments type not correct', () => {
const typeScriptTypeArguments = {
type: '',
params: [
{
type: 'TSTypeReference',
typeName: {
name: 'Spec',
},
},
],
};
const parserType = 'TypeScript';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('throw error if typeScriptTypeArguments params length is not equal to 1', () => {
const typeScriptTypeArguments = {
type: 'TSTypeParameterInstantiation',
params: [],
};
const parserType = 'TypeScript';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('throw error if typeScriptTypeArguments params type is not TSTypeReference', () => {
const typeScriptTypeArguments = {
type: 'TSTypeParameterInstantiation',
params: [
{
type: '',
typeName: {
name: 'Spec',
},
},
],
};
const parserType = 'TypeScript';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('throw error if typeScriptTypeArguments params typeName name is not Spec', () => {
const typeScriptTypeArguments = {
type: 'TSTypeParameterInstantiation',
params: [
{
type: 'TSTypeReference',
typeName: {
name: '',
},
},
],
};
const parserType = 'TypeScript';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
it('do not throw error if typeScriptTypeArguments are correct', () => {
const typeScriptTypeArguments = {
type: 'TSTypeParameterInstantiation',
params: [
{
type: 'TSTypeReference',
typeName: {
name: 'Spec',
},
},
],
};
const parserType = 'TypeScript';
expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
});
@@ -17,6 +17,7 @@ const {
MoreThanOneModuleRegistryCallsParserError,
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
} = require('./errors.js');
function throwIfModuleInterfaceNotFound(
@@ -83,9 +84,48 @@ function throwIfWrongNumberOfCallExpressionArgs(
}
}
function throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName: string,
typeArguments: $FlowFixMe,
methodName: string,
moduleName: string,
language: ParserType,
) {
function throwError() {
throw new IncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeArguments,
methodName,
moduleName,
language,
);
}
if (language === 'Flow') {
if (
typeArguments.type !== 'TypeParameterInstantiation' ||
typeArguments.params.length !== 1 ||
typeArguments.params[0].type !== 'GenericTypeAnnotation' ||
typeArguments.params[0].id.name !== 'Spec'
) {
throwError();
}
} else if (language === 'TypeScript') {
if (
typeArguments.type !== 'TSTypeParameterInstantiation' ||
typeArguments.params.length !== 1 ||
typeArguments.params[0].type !== 'TSTypeReference' ||
typeArguments.params[0].typeName.name !== 'Spec'
) {
throwError();
}
}
}
module.exports = {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
};
@@ -64,7 +64,6 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
@@ -72,6 +71,7 @@ const {
throwIfModuleInterfaceNotFound,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../../error-utils');
const language = 'Flow';
@@ -667,20 +667,13 @@ function buildModuleSchema(
);
}
if (
typeArguments.type !== 'TypeParameterInstantiation' ||
typeArguments.params.length !== 1 ||
typeArguments.params[0].type !== 'GenericTypeAnnotation' ||
typeArguments.params[0].id.name !== 'Spec'
) {
throw new IncorrectModuleRegistryCallTypeParameterParserError(
hasteModuleName,
typeArguments,
methodName,
$moduleName,
language,
);
}
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
hasteModuleName,
typeArguments,
methodName,
$moduleName,
language,
);
return $moduleName;
});
@@ -64,7 +64,6 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
@@ -72,6 +71,7 @@ const {
throwIfUnusedModuleInterfaceParserError,
throwIfModuleInterfaceNotFound,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../../error-utils');
const language = 'TypeScript';
@@ -701,20 +701,13 @@ function buildModuleSchema(
);
}
if (
typeParameters.type !== 'TSTypeParameterInstantiation' ||
typeParameters.params.length !== 1 ||
typeParameters.params[0].type !== 'TSTypeReference' ||
typeParameters.params[0].typeName.name !== 'Spec'
) {
throw new IncorrectModuleRegistryCallTypeParameterParserError(
hasteModuleName,
typeParameters,
methodName,
$moduleName,
language,
);
}
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
hasteModuleName,
typeParameters,
methodName,
$moduleName,
language,
);
return $moduleName;
});