mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
refactor(codegen): extract throwIfArgumentPropsAreNull function in error-utils (#37252)
Summary: This PR contains task 117 from https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/pull/37252 Test Plan: I tested using Jest and Flow commands. Reviewed By: dmytrorykun Differential Revision: D45865671 Pulled By: cipolleschi fbshipit-source-id: 6711dbed0a5ccd56075e0d13ffa13b222979b8c7
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b5c01ee945
commit
f05252ae4e
@@ -35,6 +35,7 @@ const {
|
||||
throwIfMoreThanOneCodegenNativecommands,
|
||||
throwIfEventHasNoName,
|
||||
throwIfBubblingTypeIsNull,
|
||||
throwIfArgumentPropsAreNull,
|
||||
} = require('../error-utils');
|
||||
const {
|
||||
UnsupportedModulePropertyParserError,
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
+15
-1
@@ -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 = {
|
||||
@@ -352,4 +365,5 @@ module.exports = {
|
||||
throwIfMoreThanOneConfig,
|
||||
throwIfEventHasNoName,
|
||||
throwIfBubblingTypeIsNull,
|
||||
throwIfArgumentPropsAreNull,
|
||||
};
|
||||
|
||||
@@ -19,6 +19,7 @@ import type {Parser} from '../../parser';
|
||||
const {
|
||||
throwIfEventHasNoName,
|
||||
throwIfBubblingTypeIsNull,
|
||||
throwIfArgumentPropsAreNull,
|
||||
} = require('../../error-utils');
|
||||
const {getEventArgument} = require('../../parsers-commons');
|
||||
|
||||
@@ -296,24 +297,22 @@ 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,
|
||||
),
|
||||
@@ -321,20 +320,14 @@ function buildEventSchema(
|
||||
};
|
||||
}
|
||||
|
||||
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,
|
||||
),
|
||||
|
||||
+24
-23
@@ -22,6 +22,7 @@ const {parseTopLevelType} = require('../parseTopLevelType');
|
||||
const {
|
||||
throwIfEventHasNoName,
|
||||
throwIfBubblingTypeIsNull,
|
||||
throwIfArgumentPropsAreNull,
|
||||
} = require('../../error-utils');
|
||||
const {getEventArgument} = require('../../parsers-commons');
|
||||
function getPropertyType(
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user