Skip to content

Commit

Permalink
Extracted UnsupportedModulePropertyParserError into throwIfModuleType…
Browse files Browse the repository at this point in the history
…IsUnsupported function in error-utils.js file (#34966)

Summary:
This PR is part of #34872
This PR extracts `UnsupportedModulePropertyParserError` exception to `throwIfModuleTypeIsUnsupported` function inside `error-utils.js` file

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal] [Changed] - Extract `UnsupportedModulePropertyParserError` to `throwIfModuleTypeIsUnsupported` function inside `error-utils.js`

Pull Request resolved: #34966

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

<img width="939" alt="Screenshot 2022-10-13 at 12 14 19 PM" src="https://user-images.githubusercontent.com/86604753/195521643-6a197b51-7038-48f1-8b92-2c8c2786d66b.png">

Reviewed By: christophpurrer

Differential Revision: D40337936

Pulled By: cipolleschi

fbshipit-source-id: 74fb116b48634c5e3b6c11f8b71ad59f290d959e
  • Loading branch information
mohitcharkha committed Oct 17, 2022
1 parent 15f974f commit c519fa0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,80 @@ describe('throwIfUntypedModule', () => {
).not.toThrowError(UntypedModuleRegistryCallParserError);
});
});

describe('throwIfMoreThanOneModuleRegistryCalls', () => {
const {throwIfModuleTypeIsUnsupported} = require('../error-utils.js');
const {UnsupportedModulePropertyParserError} = require('../errors.js');
const hasteModuleName = 'moduleName';
const property = {value: 'value', key: {name: 'name'}};
it("don't throw error if module type is FunctionTypeAnnotation in Flow", () => {
const value = {type: 'FunctionTypeAnnotation'};
const language = 'Flow';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it('throw error if module type is unsupported in Flow', () => {
const value = {type: ''};
const language = 'Flow';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);
}).toThrow(UnsupportedModulePropertyParserError);
});
it("don't throw error if module type is TSFunctionType in TypeScript", () => {
const value = {type: 'TSFunctionType'};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it("don't throw error if module type is TSMethodSignature in TypeScript", () => {
const value = {type: 'TSMethodSignature'};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it('throw error if module type is unsupported in TypeScript', () => {
const value = {type: ''};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);
}).toThrow(UnsupportedModulePropertyParserError);
});
});
32 changes: 32 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
UntypedModuleRegistryCallParserError,
UnsupportedModulePropertyParserError,
} = require('./errors.js');

function throwIfModuleInterfaceIsMisnamed(
Expand Down Expand Up @@ -157,6 +158,36 @@ function throwIfUntypedModule(
}
}

function throwIfModuleTypeIsUnsupported(
nativeModuleName: string,
propertyValue: $FlowFixMe,
propertyName: string,
propertyValueType: string,
language: ParserType,
) {
if (language === 'Flow' && propertyValueType !== 'FunctionTypeAnnotation') {
throw new UnsupportedModulePropertyParserError(
nativeModuleName,
propertyValue,
propertyName,
propertyValueType,
language,
);
} else if (
language === 'TypeScript' &&
propertyValueType !== 'TSFunctionType' &&
propertyValueType !== 'TSMethodSignature'
) {
throw new UnsupportedModulePropertyParserError(
nativeModuleName,
propertyValue,
propertyName,
propertyValueType,
language,
);
}
}

module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfModuleInterfaceNotFound,
Expand All @@ -165,4 +196,5 @@ module.exports = {
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
};
20 changes: 9 additions & 11 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ const {
UnsupportedFunctionReturnTypeAnnotationParserError,
UnsupportedEnumDeclarationParserError,
UnsupportedUnionTypeAnnotationParserError,
UnsupportedModulePropertyParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
const {throwIfUntypedModule} = require('../../error-utils');

const {
throwIfModuleInterfaceNotFound,
Expand All @@ -75,6 +73,8 @@ const {
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
} = require('../../error-utils');

const language = 'Flow';
Expand Down Expand Up @@ -548,15 +548,13 @@ function buildPropertySchema(

({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));

if (value.type !== 'FunctionTypeAnnotation') {
throw new UnsupportedModulePropertyParserError(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);
}
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);

return {
name: methodName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ const {
UnsupportedFunctionReturnTypeAnnotationParserError,
UnsupportedEnumDeclarationParserError,
UnsupportedUnionTypeAnnotationParserError,
UnsupportedModulePropertyParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfModuleInterfaceIsMisnamed,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../../error-utils');
Expand Down Expand Up @@ -561,16 +562,13 @@ function buildPropertySchema(
const methodName: string = key.name;

({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));

if (value.type !== 'TSFunctionType' && value.type !== 'TSMethodSignature') {
throw new UnsupportedModulePropertyParserError(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);
}
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);

return {
name: methodName,
Expand Down

0 comments on commit c519fa0

Please sign in to comment.