This repository was archived by the owner on Apr 26, 2024. It is now read-only.
React: rewrite unqualified React imports; update Fragment, Children and ChildrenArray transforms. #397
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR does a few React-related things.
Unqualified Imports
This PR supports transformations such as
to
Previously, rewriting names like this was only supported for unambiguously namespaced references like
React.Node
toReact.ReactNode
.The gist of the changes is:
react
There's two known issues with the current approach:
import { Node as RNode } from 'react';
) and will no-opI'm not sure if these tradeoffs are acceptable for the goals of the project. They can be worked around with more effort, but a complete solution will require multiple passes over the file (since we will want to find all imports before we make any decisions about which types to rewrite), which is at odds with the current architecture. The current implementation is optimistic that it won't cause collisions and does not check.
Fragment
andChildren
No Longer RenamedThese two types have equivalents in the TypeScript typings. Rewriting these names would break working code of the form
as both of those identifiers are runtime values with the same name as their corresponding types. There are no
React.ReactFragment
orReact.ReactChildren
runtime values, though there are types.ChildrenArray<T>
Replaced withT | T[]
I don't know if TypeScript ever had a type for this, but it doesn't now. If you look at the type definitions for the
React.Children
object, you'll see they don't bother with an intermediate React-aware children "array" type, and instead just take a union likeT | T[]
. So nowflow-to-ts
performs the same mapping, removing anyChildrenArray
import entirely.