Skip to content

Conversation

@AntoineDoubovetzky
Copy link

Summary

This PR is a task from #34872:

Extract the UnsupportedFunctionParamTypeAnnotationParserError in its own throwing function (if it does not exists already) and reuse that function passing a proper type. Then, refactor the code using a dictionary and avoiding the three different ifs in both parsers.

Changelog

[Internal] [Changed] - Extract the UnsupportedFunctionParamTypeAnnotationParserError in its own throwing function in error-utils

Test Plan

@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 23, 2022
@AntoineDoubovetzky AntoineDoubovetzky force-pushed the refactor/extract-throwIfUnsupportedFunctionParamTypeAnnotationParserError-function branch from 2ca7834 to 3e1326d Compare October 23, 2022 18:22
@AntoineDoubovetzky
Copy link
Author

@cipolleschi I still need to do the second part of the task ("Then, refactor the code using a dictionary and avoiding the three different ifs in both parsers."), but I have some troubles with Flow.

Flow doesn't understand anymore that paramTypeAnnotation can't be of type VoidTypeAnnotation nor PromiseTypeAnnotation.

Capture d’écran 2022-10-23 à 20 22 51

I tried to use predicate functions but I didn't manage to make it work in this case. I don't like the idea of adding a $FlowFixMe comment.
Do you have any idea?

@analysis-bot
Copy link

analysis-bot commented Oct 23, 2022

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,066,076 +73,448
android hermes armeabi-v7a 6,470,155 +101,198
android hermes x86 7,377,755 -27,147
android hermes x86_64 7,348,765 +79,576
android jsc arm64-v8a 8,923,088 +63,147
android jsc armeabi-v7a 7,689,029 +90,911
android jsc x86 8,869,884 -47,661
android jsc x86_64 9,462,806 +61,643

Base commit: cf5addf
Branch: main

@analysis-bot
Copy link

analysis-bot commented Oct 23, 2022

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

Base commit: cf5addf
Branch: main

@pull-bot
Copy link

PR build artifact for 3e1326d is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@AntoineDoubovetzky
Copy link
Author

I searched if there were other places where we encountered the same problem. I believe it is the case in this PR #34917, and I see that the if(XXX) is not in the throwIfXXX function, but I feel like it's losing the point of extracting the function.

@cipolleschi
Copy link
Contributor

Hi @AntoineDoubovetzky, thanks for the comment.

I understand what you are trying to say and I completely agree. However, I think we have two orders of problems:

  1. We have the repetition of the if (type == 'something') then throw for each of the invalid types
  2. The logic you highlighted not in the throwIfXXX function.

To keep things as simple as possible, I tried to split the problem in two steps: the PR you linked was addressing only the first one and I think it's fine: we can have another task for the second step.

Does this reasoning make sense to you?

For your task, you can tackle both points together, if you feel it! 😊

@AntoineDoubovetzky
Copy link
Author

AntoineDoubovetzky commented Oct 24, 2022

I'll do the first one, for the second one I have a Flow error that I didn't manage to fix yet (#35057 (comment)).

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.

Amazing, thank you for taking this and to look through the codebase.
I left a comment to improve it further and it could perhaps solve the other problem as well.

Comment on lines 461 to 467
throwIfUnsupportedFunctionParamTypeAnnotationParserError(
hasteModuleName,
flowParam.typeAnnotation,
paramTypeAnnotation.type,
paramName,
'PromiseTypeAnnotation',
);
Copy link
Contributor

Choose a reason for hiding this comment

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

What about saving some more lines (deleting line from 453 to 459)with this:

Suggested change
throwIfUnsupportedFunctionParamTypeAnnotationParserError(
hasteModuleName,
flowParam.typeAnnotation,
paramTypeAnnotation.type,
paramName,
'PromiseTypeAnnotation',
);
const forbiddenTypes = new Set(['VoidTypeAnnotation', 'PromiseTypeAnnotation']);
const currentType = paramTypeAnnotation.type;
if (forbiddenTypes.has(currentType)) {
throwIfUnsupportedFunctionParamTypeAnnotationParserError(
hasteModuleName,
flowParam.typeAnnotation,
paramTypeAnnotation.type,
paramName,
currentType,
);
}

This is better because, if in the future we want to forbid more type, we can just work on the Set instead of make one call for each.
Perhaps, with this approach, you may also be able to solve the Flow problem.

What do you think?

Copy link
Author

@AntoineDoubovetzky AntoineDoubovetzky Oct 26, 2022

Choose a reason for hiding this comment

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

I tried to use a Set but Flow couldn't infer that paramTypeAnnotation.type could not equal VoidTypeAnnotation or PromiseTypeAnnotation so I had the same Flow error when calling wrappNullable as before (#35057 (comment)).

The only solution I found is to replace the Set with multiple OR conditions and to add an early return.

@AntoineDoubovetzky AntoineDoubovetzky force-pushed the refactor/extract-throwIfUnsupportedFunctionParamTypeAnnotationParserError-function branch from 3e1326d to 232c852 Compare October 26, 2022 14:34
@AntoineDoubovetzky AntoineDoubovetzky marked this pull request as ready for review October 26, 2022 14:40
@AntoineDoubovetzky
Copy link
Author

@cipolleschi I updated the PR, I think this is now ready to merge

@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.

@pull-bot
Copy link

PR build artifact for 232c852 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @AntoineDoubovetzky in 8c69b6c.

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 28, 2022
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
…tion (facebook#35057)

Summary:
This PR is a task from facebook#34872:

> Extract the UnsupportedFunctionParamTypeAnnotationParserError in its own throwing function (if it does not exists already) and reuse that function passing a proper type. Then, refactor the code using a dictionary and avoiding the three different ifs in both parsers.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal] [Changed] - Extract the UnsupportedFunctionParamTypeAnnotationParserError in its own throwing function in error-utils

Pull Request resolved: facebook#35057

Reviewed By: lunaleaps

Differential Revision: D40721099

Pulled By: cipolleschi

fbshipit-source-id: af5e4cd275d0049047d35660559b94a27e660e40
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