Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash by conditional value of aspectRatio style value (#35858) #35859

Closed
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
@@ -1,7 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`processAspectRatio should not accept invalid formats 1`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: 0a"`;
exports[`processAspectRatio should not accept invalid formats 1`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: 0a"`;

exports[`processAspectRatio should not accept invalid formats 2`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: 1 / 1 1"`;
exports[`processAspectRatio should not accept invalid formats 2`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: 1 / 1 1"`;

exports[`processAspectRatio should not accept invalid formats 3`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: auto 1/1"`;
exports[`processAspectRatio should not accept invalid formats 3`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: auto 1/1"`;

exports[`processAspectRatio should not accept non string truthy types 1`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: function () {}"`;

exports[`processAspectRatio should not accept non string truthy types 2`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: 1,2,3"`;

exports[`processAspectRatio should not accept non string truthy types 3`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: [object Object]"`;
14 changes: 14 additions & 0 deletions Libraries/StyleSheet/__tests__/processAspectRatio-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@ describe('processAspectRatio', () => {
expect(() => processAspectRatio('1 / 1 1')).toThrowErrorMatchingSnapshot();
expect(() => processAspectRatio('auto 1/1')).toThrowErrorMatchingSnapshot();
});

it('should ignore non string falsy types', () => {
const invalidThings = [undefined, null, false];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't null an allowed value for all properties?

Copy link
Contributor Author

@mym0404 mym0404 Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is allowed and ignored silently in this code.

do you mean typing also should be changed to value?: number? | string??

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could rename this test to it('should ignone non string falsy types', () => {

Copy link
Contributor Author

@mym0404 mym0404 Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done name is changed.

invalidThings.forEach(thing => {
expect(processAspectRatio(thing)).toBe(undefined);
});
});

it('should not accept non string truthy types', () => {
const invalidThings = [() => {}, [1, 2, 3], {}];
invalidThings.forEach(thing => {
expect(() => processAspectRatio(thing)).toThrowErrorMatchingSnapshot();
});
});
});
14 changes: 12 additions & 2 deletions Libraries/StyleSheet/processAspectRatio.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@

const invariant = require('invariant');

function processAspectRatio(aspectRatio: number | string): ?number {
function processAspectRatio(aspectRatio?: number | string): ?number {
if (typeof aspectRatio === 'number') {
return aspectRatio;
}
if (typeof aspectRatio !== 'string') {
NickGerleman marked this conversation as resolved.
Show resolved Hide resolved
if (__DEV__) {
invariant(
!aspectRatio,
'aspectRatio must either be a number, a ratio string or `auto`. You passed: %s',
aspectRatio,
);
}
return;
}

const matches = aspectRatio.split('/').map(s => s.trim());

Expand All @@ -34,7 +44,7 @@ function processAspectRatio(aspectRatio: number | string): ?number {
if (__DEV__) {
invariant(
!hasNonNumericValues && (matches.length === 1 || matches.length === 2),
'aspectRatio must either be a number, a ratio or `auto`. You passed: %s',
'aspectRatio must either be a number, a ratio string or `auto`. You passed: %s',
aspectRatio,
);
}
Expand Down