-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Re-add support for local Flow bindings (TypeAlias, OpaqueTypeAlias and Interface) #7900
Conversation
3016216
to
d8a8d15
Compare
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/7999/ |
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.
This makes sense to me
The TL;DR for bindings & Flow is
|
Could one of you verify how exactly flow differentiates "global" and "module" in cases like Babel? I was under the impression that something like |
This is correct. It's unusual to use The intention is that |
Does Flow ever treat .js files as non-ESM/CommonJS, or is it safe to assume that it'd only every be files loaded by In my mind, part of the reason we just removed this is that it wasn't clear that it worked right anyway. None of our transforms really expect Flowtypes to exist, so they usually end up corrupting the type definitions. That or we end up with issues like #5395 where people expect top-level flow declarations to be global. I think it's all made more complicated because there's no clear specification anywhere defining what Flow expects for this kind of thing. If I |
Yes, this is correct. Only files loaded by
I'm not sure I understand that issue entirely, but it's not correct to assume that a top-level
Sorry the specification is unclear. Hopefully this is clear:
It's declaring a new binding, which shadows the |
Alright, that's good to know, so the user's usecase is not one that Flow actually supports/expects. In that case, the user was using a module-level declare to define the type of a global variable. Since the Would it make sense for Flow to error in cases like this? If you assign to something declared with |
IMO we should just forbid |
Fair enough! |
I write this as a TypeScript user, but there are times I do want to use I usually do this with modules that import global data into local state; everything else should refer to the local state. |
@hzoo can we please merge this? |
Given the conversation above, do we actually want the "Global" distinction included here? It's not super clear to me what distinction that is trying to draw. |
I agree that the Global distinction is unclear, and the original FlowDeclaration name better describes how Flow understand the pre-transform code. Otherwise, this is fine with me. |
@loganfsmyth I made the distinction because that's why the Flow bindings were removed in the first place. I can update the PR to remove the distinction so it matches what Flow actually interprets in code files (vs library files), if you're OK with that. Edit: updated to remove the distinction. |
4853c4a
to
cfd75b3
Compare
cfd75b3
to
82dd23e
Compare
Flow binding support was completely removed in #6528. While the reason behind that change was totally valid:
I think it should only be applied to global Flow declarations. Local Flow declarations (like
TypeAlias
,OpaqueTypeAlias
andInterface
) create local type bindings that exist in the same namespace as value bindings. E.g.:When using Flow,
T
referenced bystr
is bound to the type defined infoo
instead of theconst
defined outside. Same for the value used bybaz
, which causes a Flow error because it references a type in that scope.This PR adds support for those local Flow bindings.