Skip to content

Commit

Permalink
Extract the UnsupportedArrayElementTypeAnnotationParserError in its o… (
Browse files Browse the repository at this point in the history
#35167)

Summary:
This PR is part of #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: #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
  • Loading branch information
matiassalles99 authored and facebook-github-bot committed Nov 22, 2022
1 parent 285a980 commit cc311ff
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
throwIfModuleTypeIsUnsupported,
throwIfUntypedModule,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
} = require('../error-utils');
const {
UnsupportedModulePropertyParserError,
Expand Down Expand Up @@ -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);
});
});
29 changes: 29 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
UnsupportedModulePropertyParserError,
MoreThanOneModuleInterfaceParserError,
UnsupportedFunctionParamTypeAnnotationParserError,
UnsupportedArrayElementTypeAnnotationParserError,
} = require('./errors.js');

function throwIfModuleInterfaceIsMisnamed(
Expand Down Expand Up @@ -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,
Expand All @@ -263,4 +291,5 @@ module.exports = {
throwIfModuleTypeIsUnsupported,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
};
48 changes: 11 additions & 37 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import type {
NamedShape,
NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModuleTypeAnnotation,
NativeModulePropertyShape,
Expand Down Expand Up @@ -55,14 +54,14 @@ const {
} = require('../../parsers-primitives');

const {
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfArrayElementTypeAnnotationIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfMoreThanOneModuleRegistryCalls,
Expand Down Expand Up @@ -110,44 +109,19 @@ function translateArrayTypeAnnotation(
),
);

if (elementType.type === 'VoidTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
flowElementType,
flowArrayType,
'void',
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,
);
}
throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName,
flowElementType,
flowArrayType,
elementType.type,
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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import type {
NamedShape,
NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModulePropertyShape,
NativeModuleTypeAnnotation,
Expand Down Expand Up @@ -70,6 +69,7 @@ const {
throwIfMoreThanOneModuleRegistryCalls,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
} = require('../../error-utils');

const {TypeScriptParser} = require('../parser');
Expand Down Expand Up @@ -111,56 +111,19 @@ function translateArrayTypeAnnotation(
),
);

if (elementType.type === 'VoidTypeAnnotation') {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
tsElementType,
tsArrayType,
'void',
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,
);
}
throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName,
tsElementType,
tsArrayType,
elementType.type,
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',
Expand Down

0 comments on commit cc311ff

Please sign in to comment.