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 number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
throwIfPartialWithMoreParameter,
throwIfMoreThanOneCodegenNativecommands,
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
} = require('../error-utils');
const {
UnsupportedModulePropertyParserError,
Expand Down Expand Up @@ -950,3 +951,25 @@ describe('throwIfEventHasNoName', () => {
}).not.toThrow();
});
});

describe('throwIfBubblingTypeIsNull', () => {
it('throw an error if unable to determine event bubbling type', () => {
const bubblingType = null;
const eventName = 'Event';

expect(() => {
throwIfBubblingTypeIsNull(bubblingType, eventName);
}).toThrowError(
`Unable to determine event bubbling type for "${eventName}"`,
);
});

it('does not throw an error if able to determine event bubbling type', () => {
const bubblingType = 'direct';
const eventName = 'Event';

expect(() => {
throwIfBubblingTypeIsNull(bubblingType, eventName);
}).not.toThrow();
});
});
12 changes: 12 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ function throwIfEventHasNoName(typeAnnotation: $FlowFixMe, parser: Parser) {
}
}

function throwIfBubblingTypeIsNull(
bubblingType: ?('direct' | 'bubble'),
eventName: string,
) {
if (!bubblingType) {
throw new Error(
`Unable to determine event bubbling type for "${eventName}"`,
);
}
}

module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
Expand All @@ -340,4 +351,5 @@ module.exports = {
throwIfConfigNotfound,
throwIfMoreThanOneConfig,
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import type {
EventTypeAnnotation,
} from '../../../CodegenSchema.js';
import type {Parser} from '../../parser';
const {throwIfEventHasNoName} = require('../../error-utils');
const {
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
} = require('../../error-utils');

function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
Expand Down Expand Up @@ -245,9 +248,7 @@ function buildEventSchema(
throw new Error(`Unable to determine event arguments for "${name}"`);
}

if (bubblingType === null) {
throw new Error(`Unable to determine event arguments for "${name}"`);
}
throwIfBubblingTypeIsNull(bubblingType, name);
}

// $FlowFixMe[unclear-type] there's no flowtype for ASTs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import type {TypeDeclarationMap} from '../../utils';
import type {Parser} from '../../parser';
const {flattenProperties} = require('./componentsUtils');
const {parseTopLevelType} = require('../parseTopLevelType');
const {throwIfEventHasNoName} = require('../../error-utils');
const {
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
} = require('../../error-utils');

function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
Expand Down Expand Up @@ -219,7 +222,7 @@ function buildEventSchema(
types: TypeDeclarationMap,
property: EventTypeAST,
parser: Parser,
): EventTypeShape {
): ?EventTypeShape {
// unpack WithDefault, (T) or T|U
const topLevelType = parseTopLevelType(
property.typeAnnotation.typeAnnotation,
Expand All @@ -235,7 +238,7 @@ function buildEventSchema(
if (!argumentProps) {
throw new Error(`Unable to determine event arguments for "${name}"`);
} else if (!bubblingType) {
throw new Error(`Unable to determine event bubbling type for "${name}"`);
throwIfBubblingTypeIsNull(bubblingType, name);
} else {
if (paperTopLevelNameDeprecated != null) {
return {
Expand Down Expand Up @@ -267,9 +270,9 @@ function getEvents(
types: TypeDeclarationMap,
parser: Parser,
): $ReadOnlyArray<EventTypeShape> {
return eventTypeAST.map(property =>
buildEventSchema(types, property, parser),
);
return eventTypeAST
.map(property => buildEventSchema(types, property, parser))
.filter(Boolean);
}

module.exports = {
Expand Down