-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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 codegen output for object with indexer #35344
Conversation
Base commit: 0f089ea |
I fear this will conflict with #35098 and won't pass our internal builds as we rely on parsing indexed properties for (std::)map data structures in C++ TM. I agree that mixing properties and indexed types is not ideal - but it was a cheap initial solution. If we want to 'Fix codegen output for object with indexer' I would expect that this change:
I don't think the current PR can be landed as it - as it simply removes existing logic. For the current sample we would then output something as:
|
Base commit: a3c47d3 |
PR build artifact for 32bc4f8 is ready. |
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.
Thank you for taking the time to solve this semantic issue.
I left a few comments to improve the code maintainability, let me know what do you think.
Functionality-wise, it looks good to me.
packages/react-native-codegen/src/parsers/flow/modules/index.js
Outdated
Show resolved
Hide resolved
packages/react-native-codegen/src/parsers/typescript/modules/index.js
Outdated
Show resolved
Hide resolved
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Hi @christophpurrer . As I mentioned in the description, the current behavior parses I think making a But @cipolleschi I think here is exactly a useful use case. I am totally fine if we could add a new type here. Otherwise, the only correct way will be treating it like an object, and code using |
@ZihanChen-MSFT > What is your main motivation for this PR? Are you fixing an actual issue / bug on your side OR you 'just' want to make things semantically correct? |
in By having the codegen treat My original idea was to add a |
PR build artifact for 8a559d6 is ready. |
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
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.
I applied the comments @christophpurrer added internally.
I think that they are valid, so I forwarded them here.
Thank you for taking care of these PRs.
).length > 0 | ||
) { | ||
// no need to do further checking | ||
return emitObject(nullable); |
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.
from @christophpurrer:
What if we emit a new type here?
Instead ofGenericObjectTypeAnnotation
we can useIndexedObjectTypeAnnotation
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.
I think this is the idea of DictionaryObjectTypeAnnotation
but you said you don't want me to add that type am I right?
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.
I don't have a strong opinion of adding or not adding a new type for indexed objects at this point.
If we would add a new type we should keep its name as close as possible to the internal representation in flow-parser (ObjectTypeIndexer) or babel-parser with the TypeScript plugin (TSIndexSignature)
Dictionary is not a term used in either of the 2 parsers.
packages/react-native-codegen/src/parsers/flow/modules/index.js
Outdated
Show resolved
Hide resolved
PR build artifact for eb28c31 is ready. |
/rebase |
eb28c31
to
aba3568
Compare
PR build artifact for aba3568 is ready. |
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.
I left a couple of comments for some unused return values.
I'll import it to see if something breaks
if (indexSignatures.length > 0) { | ||
// check the property type to prevent developers from using unsupported types | ||
const propertyType = indexSignatures[0].typeAnnotation; | ||
translateTypeAnnotation( |
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.
We are not using the returned value of translateTypeAnnotation
. Is it right?
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.
The return value is ignored, since we don't really have DictionaryTypeAnnotation
, there is no place to return it. But I call this function to check if the type is supported.
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Unfortunately, things are breaking internally. We have a bunch of This is the most likely cause of the failure. In a TM we have this export
So I think that the Flow |
@cipolleschi https://flow.org/en/docs/types/mixed/ Looks like
So I could generate case 'MixedTypeAnnotation': {
if (cxxOnly) {
return emitMixedTypeAnnotation(nullable);
} else {
return emitObject(nullable);
}
} |
PR build artifact for 099766b is ready. |
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
099766b
to
56f69b8
Compare
PR build artifact for 56f69b8 is ready. |
56f69b8
to
49c8252
Compare
PR build artifact for 49c8252 is ready. |
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@cipolleschi merged this pull request in f07490b. |
Summary: The current implementation think `{[key:T]:U}` and `{key:object}` are the same type, which is semantically wrong. This pull request fixes the problem and return `{type:'GenericObjectTypeAnnotation'}`, so that `{[key:T]:U}` is `Object`. The current schema cannot represent dictionary type, `Object` is the closest one. The previous incorrect implementation actually bring in code with logic that doesn't make sense (treating indexer as property), those and related unit test are all undone. ## Changelog [General] [Changed] - Fix codegen output for object with indexer Pull Request resolved: facebook#35344 Test Plan: `yarn jest react-native-codegen` passed Reviewed By: rshest Differential Revision: D41304475 Pulled By: cipolleschi fbshipit-source-id: caab8e458d83f9850c5c28b67cc561a764738372
Summary
The current implementation think
{[key:T]:U}
and{key:object}
are the same type, which is semantically wrong.This pull request fixes the problem and return
{type:'GenericObjectTypeAnnotation'}
, so that{[key:T]:U}
isObject
. The current schema cannot represent dictionary type,Object
is the closest one.The previous incorrect implementation actually bring in code with logic that doesn't make sense (treating indexer as property), those and related unit test are all undone.
Changelog
[General] [Changed] - Fix codegen output for object with indexer
Test Plan
yarn jest react-native-codegen
passed