Skip to content

Conversation

@dhruvtailor7
Copy link
Contributor

Summary

This PR is a part of #34872.
Extracted the UnsupportedObjectPropertyValueTypeAnnotationParserError in its own throwing function and reuse that function passing a proper type.

Changelog

[Internal] [Changed] - Extract the UnsupportedObjectPropertyValueTypeAnnotationParserError in its own throwing function and reuse that function passing a proper type.

Test Plan

Output of yarn jest react-native-codegen.
Screenshot 2022-10-10 at 12 55 39 PM

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 10, 2022
@analysis-bot
Copy link

analysis-bot commented Oct 10, 2022

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,778,150 +0
android hermes armeabi-v7a 7,179,206 +0
android hermes x86 8,091,069 +0
android hermes x86_64 8,062,841 +0
android jsc arm64-v8a 9,637,352 +0
android jsc armeabi-v7a 8,401,647 +0
android jsc x86 9,586,544 +0
android jsc x86_64 10,179,709 +0

Base commit: 629e8e0
Branch: main

@analysis-bot
Copy link

analysis-bot commented Oct 10, 2022

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 629e8e0
Branch: main

Copy link
Contributor

@cipolleschi cipolleschi left a comment

Choose a reason for hiding this comment

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

Hi @dhruvtailor7! Thanks a lot for taking this task and for the amazing job! 👏

I left some comments to suggest further improvements in the codebase. Could you update the PR with these changes? 🙏

Comment on lines 71 to 90
const UnsupportedObjectPropertyTypeToInvalidPropertyValueTypeMap = {
FunctionTypeAnnotation: 'FunctionTypeAnnotation',
VoidTypeAnnotation: 'void',
PromiseTypeAnnotation: 'Promise',
};

function throwUnsupportedObjectPropertyValueTypeAnnotationParserError(
moduleName,
propertyValue,
propertyKey,
type,
) {
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
moduleName,
propertyValue,
propertyKey,
type,
);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This can be moved into and error-utils.js file which is shared between typescript and flow. As you can see, the logic is exactly the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I was waiting for the other PR to be merged first.

Comment on lines +303 to +307
if (
propertyTypeAnnotation.type === 'FunctionTypeAnnotation' ||
propertyTypeAnnotation.type === 'PromiseTypeAnnotation' ||
propertyTypeAnnotation.type === 'VoidTypeAnnotation'
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use const keys = Object.keys(UnsupportedObjectPropertyTypeToInvalidPropertyValueTypeMap) to obtain an array with all the keys. Then, you can create a new const invalidKeys = new Set(keys); to create a set from it and, finally use the set with something like:

if (invalidKeys.has(propertyTypeAnnotation.type)) {
  throw new error
}

This avoids to explicitly list the three items. Imagine that we have to unsupport other objects, we will have to add other OR clauses which is not very elegant. 😉

propertyKey,
type,
) {
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
Copy link
Contributor

Choose a reason for hiding this comment

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

I will put also the if logic in the function, s we can factor out more code.

Also, we can have a better name. Something like: throwIfPropertyValueTypeIsUnsupported. What do you think?

'Promise',
invalidPropertyValueType,
);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need the else. If the function throws, the execution flow changes, so we can safely remove this indentation! ;)

Comment on lines 71 to 90
const UnsupportedObjectPropertyTypeToInvalidPropertyValueTypeMap = {
FunctionTypeAnnotation: 'FunctionTypeAnnotation',
VoidTypeAnnotation: 'void',
PromiseTypeAnnotation: 'Promise',
};

function throwUnsupportedObjectPropertyValueTypeAnnotationParserError(
moduleName,
propertyValue,
propertyKey,
type,
) {
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
moduleName,
propertyValue,
propertyKey,
type,
);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Theoreticaly, this code should appear only in a single place. It is exactly the same of the Flow one. 😉

Comment on lines 338 to 363
if (
propertyTypeAnnotation.type === 'FunctionTypeAnnotation' ||
propertyTypeAnnotation.type === 'PromiseTypeAnnotation' ||
propertyTypeAnnotation.type === 'VoidTypeAnnotation'
) {
const invalidPropertyValueType =
UnsupportedObjectPropertyTypeToInvalidPropertyValueTypeMap[
propertyTypeAnnotation.type
];

throwUnsupportedObjectPropertyValueTypeAnnotationParserError(
hasteModuleName,
property.typeAnnotation.typeAnnotation,
property.key,
'Promise',
invalidPropertyValueType,
);
} else {
return {
name: key.name,
optional,
typeAnnotation: wrapNullable(
isPropertyNullable,
propertyTypeAnnotation,
),
};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

with the suggested changes, this code will just become:

throwIfPropertyValueTypeIsUnsupported(/*params*/)
return {
  name: key.name,
  optional,
  typeAnnotation: wrapNullable(
    isPropertyNullable,
    propertyTypeAnnotation,
  ),
};

Copy link
Contributor Author

@dhruvtailor7 dhruvtailor7 Oct 12, 2022

Choose a reason for hiding this comment

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

Hello @cipolleschi, I did the suggested changes but after doing them I get an error with flow as typeAnnotation expects a value of type NativeModuleBaseTypeAnnotation but the type returned by wrapNullable is of type NativeModuleTypeAnnotation. This was also the reason i used else statement before.

It was working before due to flow's type refinement because of the if condition and throw statement. I looked into the Flow documentation but couldn’t find a solution to it. Do you have any suggestions on how I should proceed?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's... weird. :/
Let's keep the else, then..

@cipolleschi
Copy link
Contributor

After the suggested refactoring, it would be great to have some unit tests, ideally, one for each unsupported case + 1 for a supported case where we check that the function does not throw.

@cipolleschi
Copy link
Contributor

@dhruvtailor7 could you also rebase and resolve the conflicts, please? 🙏

@cipolleschi
Copy link
Contributor

/rebase

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @dhruvtailor7 in aba6be6.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Oct 19, 2022
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
…throwing function (facebook#34917)

Summary:
This PR is a part of facebook#34872.
Extracted the UnsupportedObjectPropertyValueTypeAnnotationParserError in its own throwing function and reuse that function passing a proper type.

## Changelog

[Internal] [Changed] - Extract the UnsupportedObjectPropertyValueTypeAnnotationParserError in its own throwing function and reuse that function passing a proper type.

Pull Request resolved: facebook#34917

Test Plan:
Output of yarn jest react-native-codegen.
<img width="451" alt="Screenshot 2022-10-10 at 12 55 39 PM" src="https://user-images.githubusercontent.com/32268377/194816863-5220dbaa-3b63-42bf-8e62-9d7b915f7cbd.png">

Reviewed By: cortinico

Differential Revision: D40424885

Pulled By: cipolleschi

fbshipit-source-id: 08d4d13ee3959391261fe13c190a4bb893970757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. hacktoberfest-accepted Merged This PR has been merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants