-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
[Codegen] feat: handle negative values in enums #47452
[Codegen] feat: handle negative values in enums #47452
Conversation
2c7d75d
to
667fa99
Compare
667fa99
to
12b7143
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yep, this is definitely something that needs to be supported.
You'll need to add support for this to Flow too.
Please add an example that exercises this code path to the parser fixture file here: https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js
You'll also need to modify the same fixture in the same way for Flow here: https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js
the fixtures should stay in sync between Flow and TypeScript.
Also, since unary types aren't just negative, can you add a case to the failure fixture file in the same directory for something to exercise the failure case, like
enum Foo {
BAD = !4
}
let enumMembersType: ?NativeModuleEnumMemberType = null; | ||
|
||
switch (enumInitializerType) { | ||
case undefined: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you leave a comment explaining this case, because it's very very strange to see undefined
as a case in a switch. It's almost worth pulling it out into its own if statement above the switch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, I've extracted this to a separate if statement above the switch
@elicwhite Thanks for your review! I applied your suggestions and added test cases for both Flow and Typescript. Looks like Flow handles this case without any changes. Let me know if I should fix anything else |
d993309
to
d38fffd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making the changes!
I think this is the last thing before I will import.
packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js
Outdated
Show resolved
Hide resolved
...codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap
Outdated
Show resolved
Hide resolved
91ed5e0
to
94c5486
Compare
@elicwhite has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@elicwhite merged this pull request in 177bf4d. |
This pull request was successfully merged by @okwasniewski in 177bf4d When will my fix make it into a release? | How to file a pick request? |
Summary:
This PR adds support for negative values in enums.
Currently when we try to use an enum with negative value:
It will fail:
This is because negative values are parsed as
UnaryExpressions
which have-
operator in front and value as argument.With the new approach codegen properly generates enums with negative values.
Changelog:
[GENERAL] [ADDED] - Support negative values in enums
Test Plan:
I've added tests to see if everything is working properly