Extract the UnsupportedArrayElementTypeAnnotationParserError in its o… (#35167)

Summary:
This PR is part of https://github.com/facebook/react-native/issues/34872
This PR extracts the [UnsupportedArrayElementTypeAnnotationParserError](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L132) in its own throwing function.

## Changelog
[Internal] [Changed] - Extract the UnsupportedArrayElementTypeAnnotationParserError in its own throwing function

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

Test Plan: <img width="454" alt="Screen Shot 2022-11-02 at 15 21 15" src="https://user-images.githubusercontent.com/57004457/199582495-23e4e3be-cb7e-41e8-a1fa-0250e127993c.png">

Reviewed By: cipolleschi

Differential Revision: D41437971

Pulled By: rshest

fbshipit-source-id: 14a6e09297d96f3b57568e0303e5cafff76e6f32
This commit is contained in:
matiassalles99
2022-11-22 06:16:24 -08:00
committed by Facebook GitHub Bot
parent 285a98084e
commit cc311ff01a
4 changed files with 108 additions and 85 deletions
@@ -23,6 +23,7 @@ const {
throwIfModuleTypeIsUnsupported,
throwIfUntypedModule,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
} = require('../error-utils');
const {
UnsupportedModulePropertyParserError,
@@ -637,3 +638,59 @@ describe('throwIfUnsupportedFunctionParamTypeAnnotationParserError', () => {
}).toThrow(UnsupportedFunctionParamTypeAnnotationParserError);
});
});
describe('throwIfArrayElementTypeAnnotationIsUnsupported', () => {
const {
UnsupportedArrayElementTypeAnnotationParserError,
} = require('../errors.js');
const moduleName = 'moduleName';
const language = 'Flow';
it('throws the error if it is the type is void type annotation', () => {
expect(() => {
throwIfArrayElementTypeAnnotationIsUnsupported(
moduleName,
undefined,
'Array',
'VoidTypeAnnotation',
language,
);
}).toThrow(UnsupportedArrayElementTypeAnnotationParserError);
});
it('throws the error if it is the type is promise type annotation', () => {
expect(() => {
throwIfArrayElementTypeAnnotationIsUnsupported(
moduleName,
undefined,
'Array',
'PromiseTypeAnnotation',
language,
);
}).toThrow(UnsupportedArrayElementTypeAnnotationParserError);
});
it('throws the error if it is the type is function type annotation', () => {
expect(() => {
throwIfArrayElementTypeAnnotationIsUnsupported(
moduleName,
undefined,
'Array',
'FunctionTypeAnnotation',
language,
);
}).toThrow(UnsupportedArrayElementTypeAnnotationParserError);
});
it('does not throw the error if the type is NativeModuleTypeAnnotation', () => {
expect(() => {
throwIfArrayElementTypeAnnotationIsUnsupported(
moduleName,
undefined,
'Array',
'StringTypeAnnotation',
language,
);
}).not.toThrow(UnsupportedArrayElementTypeAnnotationParserError);
});
});
@@ -27,6 +27,7 @@ const {
UnsupportedModulePropertyParserError,
MoreThanOneModuleInterfaceParserError,
UnsupportedFunctionParamTypeAnnotationParserError,
UnsupportedArrayElementTypeAnnotationParserError,
} = require('./errors.js');
function throwIfModuleInterfaceIsMisnamed(
@@ -250,6 +251,33 @@ function throwIfUnsupportedFunctionParamTypeAnnotationParserError(
);
}
function throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName: string,
flowElementType: $FlowFixMe,
flowArrayType: 'Array' | '$ReadOnlyArray' | 'ReadonlyArray',
type: string,
language: ParserType,
) {
const TypeMap = {
FunctionTypeAnnotation: 'FunctionTypeAnnotation',
VoidTypeAnnotation: 'void',
PromiseTypeAnnotation: 'Promise',
// TODO: Added as a work-around for now until TupleTypeAnnotation are fully supported in both flow and TS
// Right now they are partially treated as UnionTypeAnnotation
UnionTypeAnnotation: 'UnionTypeAnnotation',
};
if (type in TypeMap) {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
flowElementType,
flowArrayType,
TypeMap[type],
language,
);
}
}
module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
@@ -263,4 +291,5 @@ module.exports = {
throwIfModuleTypeIsUnsupported,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
};
@@ -13,7 +13,6 @@
import type {
NamedShape,
NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModuleTypeAnnotation,
NativeModulePropertyShape,
@@ -55,7 +54,6 @@ const {
} = require('../../parsers-primitives');
const {
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
@@ -63,6 +61,7 @@ const {
const {
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfArrayElementTypeAnnotationIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfMoreThanOneModuleRegistryCalls,
@@ -110,44 +109,19 @@ function translateArrayTypeAnnotation(
),
);
if (elementType.type === 'VoidTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
flowElementType,
flowArrayType,
'void',
language,
);
}
throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName,
flowElementType,
flowArrayType,
elementType.type,
language,
);
if (elementType.type === 'PromiseTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
flowElementType,
flowArrayType,
'Promise',
language,
);
}
if (elementType.type === 'FunctionTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
flowElementType,
flowArrayType,
'FunctionTypeAnnotation',
language,
);
}
const finalTypeAnnotation: NativeModuleArrayTypeAnnotation<
Nullable<NativeModuleBaseTypeAnnotation>,
> = {
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
// $FlowFixMe[incompatible-call]
elementType: wrapNullable(isElementTypeNullable, elementType),
};
return wrapNullable(nullable, finalTypeAnnotation);
});
} catch (ex) {
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
@@ -13,7 +13,6 @@
import type {
NamedShape,
NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModulePropertyShape,
NativeModuleTypeAnnotation,
@@ -70,6 +69,7 @@ const {
throwIfMoreThanOneModuleRegistryCalls,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
} = require('../../error-utils');
const {TypeScriptParser} = require('../parser');
@@ -111,56 +111,19 @@ function translateArrayTypeAnnotation(
),
);
if (elementType.type === 'VoidTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
tsElementType,
tsArrayType,
'void',
language,
);
}
throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName,
tsElementType,
tsArrayType,
elementType.type,
language,
);
if (elementType.type === 'PromiseTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
tsElementType,
tsArrayType,
'Promise',
language,
);
}
if (elementType.type === 'FunctionTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
tsElementType,
tsArrayType,
'FunctionTypeAnnotation',
language,
);
}
// TODO: Added as a work-around for now until TupleTypeAnnotation are fully supported in both flow and TS
// Right now they are partially treated as UnionTypeAnnotation
if (elementType.type === 'UnionTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
tsElementType,
tsArrayType,
'UnionTypeAnnotation',
language,
);
}
const finalTypeAnnotation: NativeModuleArrayTypeAnnotation<
Nullable<NativeModuleBaseTypeAnnotation>,
> = {
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
// $FlowFixMe[incompatible-call]
elementType: wrapNullable(isElementTypeNullable, elementType),
};
return wrapNullable(nullable, finalTypeAnnotation);
});
} catch (ex) {
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',