Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,6 +23,7 @@ const {
throwIfModuleTypeIsUnsupported,
throwIfUntypedModule,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
} = require('../error-utils');
const {
UnsupportedModulePropertyParserError,
Expand DownExpand 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);
});
});
26 changes: 26 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ const {
UnsupportedModulePropertyParserError,
MoreThanOneModuleInterfaceParserError,
UnsupportedFunctionParamTypeAnnotationParserError,
UnsupportedArrayElementTypeAnnotationParserError,
} = require('./errors.js');

function throwIfModuleInterfaceIsMisnamed(
Expand DownExpand Up@@ -250,6 +251,30 @@ function throwIfUnsupportedFunctionParamTypeAnnotationParserError(
);
}

function throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName: string,
flowElementType: $FlowFixMe,
flowArrayType: 'Array' | '$ReadOnlyArray' | 'ReadonlyArray',
type: string,
language: ParserType,
) {
const TypeMap = {
FunctionTypeAnnotation: 'FunctionTypeAnnotation',
VoidTypeAnnotation: 'void',
PromiseTypeAnnotation: 'Promise',
};

if (type in TypeMap) {
throw new UnsupportedArrayElementTypeAnnotationParserError(
hasteModuleName,
flowElementType,
flowArrayType,
TypeMap[type],
language,
);
}
}

module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
Expand All@@ -263,4 +288,5 @@ module.exports = {
throwIfModuleTypeIsUnsupported,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
};
49 changes: 13 additions & 36 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,6 @@
import type {
NamedShape,
NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModuleTypeAnnotation,
NativeModulePropertyShape,
Expand DownExpand Up@@ -56,13 +55,16 @@ const {

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

const {
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfPropertyValueTypeIsUnsupported,
throwIfArrayElementTypeAnnotationIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfMoreThanOneModuleRegistryCalls,
Expand DownExpand Up@@ -110,44 +112,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 numberDiff line numberDiff line change
Expand Up@@ -56,6 +56,7 @@ const {

const {
UnsupportedArrayElementTypeAnnotationParserError,
UnnamedFunctionParamParserError,
UnsupportedGenericParserError,
UnsupportedTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
Expand All@@ -70,6 +71,8 @@ const {
throwIfMoreThanOneModuleRegistryCalls,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
} = require('../../error-utils');

const {TypeScriptParser} = require('../parser');
Expand DownExpand Up@@ -111,56 +114,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