Skip to content

Commit

Permalink
Extracted IncorrectModuleRegistryCallTypeParameterParserError to thro…
Browse files Browse the repository at this point in the history
…wIfIncorrectModuleRegistryCallTypeParameterParserError (#34941)

Summary:
This PR is part of #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: #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
  • Loading branch information
harsh-siriah authored and facebook-github-bot committed Oct 14, 2022
1 parent 76c5b6f commit bb519ec
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const {
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../error-utils');
const {
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
} = require('../errors');

describe('throwIfModuleInterfaceNotFound', () => {
Expand Down Expand Up @@ -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);
});
});
40 changes: 40 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
MoreThanOneModuleRegistryCallsParserError,
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
} = require('./errors.js');

function throwIfModuleInterfaceNotFound(
Expand Down Expand Up @@ -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,
};
23 changes: 8 additions & 15 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {
throwIfModuleInterfaceNotFound,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../../error-utils');

const language = 'Flow';
Expand Down Expand Up @@ -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;
});
Expand Down
Loading

0 comments on commit bb519ec

Please sign in to comment.