Skip to content

Commit

Permalink
refactor(codegen): extract throwIfArgumentPropsAreNull function in er…
Browse files Browse the repository at this point in the history
…ror-utils (#37252)

Summary:
This PR contains task 117 from #34872:
> [Codegen 117] Extract the code that throws if argumentProps are null in a throwIfArgumentPropsAreNull function in the error-utils.js file. Use it in the [flow/components/events.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/flow/components/events.js#L240-L242) and in the [typescript/components/event.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/typescript/components/events.js#L230-L231) files

bypass-github-export-checks

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal] [Changed] - Extract throwIfArgumentPropsAreNull function in error-utils

Pull Request resolved: #37252

Test Plan: I tested using Jest and Flow commands.

Reviewed By: dmytrorykun

Differential Revision: D45865671

Pulled By: cipolleschi

fbshipit-source-id: 6711dbed0a5ccd56075e0d13ffa13b222979b8c7
  • Loading branch information
Antoine Doubovetzky authored and facebook-github-bot committed May 17, 2023
1 parent b5c01ee commit f05252a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
throwIfMoreThanOneCodegenNativecommands,
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
throwIfArgumentPropsAreNull,
} = require('../error-utils');
const {
UnsupportedModulePropertyParserError,
Expand Down Expand Up @@ -973,3 +974,23 @@ describe('throwIfBubblingTypeIsNull', () => {
}).not.toThrow();
});
});

describe('throwIfArgumentPropsAreNull', () => {
it('throws an error if unable to determine event arguments', () => {
const argumentProps = null;
const eventName = 'Event';

expect(() => {
throwIfArgumentPropsAreNull(argumentProps, eventName);
}).toThrowError(`Unable to determine event arguments for "${eventName}"`);
});

it('does not throw an error if able to determine event arguments', () => {
const argumentProps = [{}];
const eventName = 'Event';

expect(() => {
throwIfArgumentPropsAreNull(argumentProps, eventName);
}).not.toThrow();
});
});
16 changes: 15 additions & 1 deletion packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,25 @@ function throwIfEventHasNoName(typeAnnotation: $FlowFixMe, parser: Parser) {
function throwIfBubblingTypeIsNull(
bubblingType: ?('direct' | 'bubble'),
eventName: string,
) {
): 'direct' | 'bubble' {
if (!bubblingType) {
throw new Error(
`Unable to determine event bubbling type for "${eventName}"`,
);
}

return bubblingType;
}

function throwIfArgumentPropsAreNull(
argumentProps: ?$ReadOnlyArray<$FlowFixMe>,
eventName: string,
): $ReadOnlyArray<$FlowFixMe> {
if (!argumentProps) {
throw new Error(`Unable to determine event arguments for "${eventName}"`);
}

return argumentProps;
}

module.exports = {
Expand All @@ -352,4 +365,5 @@ module.exports = {
throwIfMoreThanOneConfig,
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
throwIfArgumentPropsAreNull,
};
27 changes: 10 additions & 17 deletions packages/react-native-codegen/src/parsers/flow/components/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {Parser} from '../../parser';
const {
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');

Expand Down Expand Up @@ -296,45 +297,37 @@ function buildEventSchema(
const {argumentProps, bubblingType, paperTopLevelNameDeprecated} =
findEventArgumentsAndType(parser, typeAnnotation, types);

if (!argumentProps) {
throw new Error(`Unable to determine event arguments for "${name}"`);
}

if (!bubblingType) {
throw new Error(`Unable to determine event arguments for "${name}"`);
}
const nonNullableArgumentProps = throwIfArgumentPropsAreNull(
argumentProps,
name,
);
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);

if (paperTopLevelNameDeprecated != null) {
return {
name,
optional,
bubblingType,
bubblingType: nonNullableBubblingType,
paperTopLevelNameDeprecated,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
argumentProps,
nonNullableArgumentProps,
buildPropertiesForEvent,
parser,
),
},
};
}

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

throwIfBubblingTypeIsNull(bubblingType, name);

return {
name,
optional,
bubblingType,
bubblingType: nonNullableBubblingType,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
argumentProps,
nonNullableArgumentProps,
buildPropertiesForEvent,
parser,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {parseTopLevelType} = require('../parseTopLevelType');
const {
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');
function getPropertyType(
Expand Down Expand Up @@ -307,42 +308,42 @@ function buildEventSchema(
const {argumentProps, bubblingType, paperTopLevelNameDeprecated} =
findEventArgumentsAndType(parser, typeAnnotation, types);

if (!argumentProps) {
throw new Error(`Unable to determine event arguments for "${name}"`);
} else if (!bubblingType) {
throwIfBubblingTypeIsNull(bubblingType, name);
} else {
if (paperTopLevelNameDeprecated != null) {
return {
name,
optional,
bubblingType,
paperTopLevelNameDeprecated,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
argumentProps,
buildPropertiesForEvent,
parser,
),
},
};
}
const nonNullableArgumentProps = throwIfArgumentPropsAreNull(
argumentProps,
name,
);
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);

if (paperTopLevelNameDeprecated != null) {
return {
name,
optional,
bubblingType,
bubblingType: nonNullableBubblingType,
paperTopLevelNameDeprecated,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
argumentProps,
nonNullableArgumentProps,
buildPropertiesForEvent,
parser,
),
},
};
}

return {
name,
optional,
bubblingType: nonNullableBubblingType,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
buildPropertiesForEvent,
parser,
),
},
};
}

function getEvents(
Expand Down

0 comments on commit f05252a

Please sign in to comment.