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
Improve flow typing #12135
Improve flow typing #12135
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 33d7851:
|
f9d0f82
to
7c6b6b6
Compare
@MichaReiser Thanks for the writings. We have an RFC repo for this specific purpose, can you open an RFC PR on https://github.com/babel/rfcs? |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/32555/ |
RFC Discussion: babel/rfcs#7 |
See also: #11883 |
7c6b6b6
to
ee84ca3
Compare
ee84ca3
to
b424fb7
Compare
b424fb7
to
33d7851
Compare
@MichaReiser Could you try running |
@nicolo-ribaudo There's a performance difference when running a full-check on the repository. I haven't figured out how to measure the incremental performance which, in my view, is the more important performance. Flow 0.123
flow 0.137
I overall expected that the check performance would decrease and the results are around what I expected. Other types I tried had check times where a single file took more than 8s. |
The goal of this PR is to improve the
@babel/types
typing for Flow. This is a RFC because some of the changes are not backwards compatible.Current Flow Types
The current script generates classes for each node type and specifies type refinements (
%checks (node instanceof MemberExpression)
) for theis*
methods.That means, the following is possible.
Shortcomings
The todays system has mainly two shortcomings.
@babel/types
does not implement the node types as classes nor does it export any value for each type. Declaring the node types as classes might misguide users to check for a certain node type usingnode instanceof Identifier
or to create a new node using a constructornew Identifier()
.%checks
refinements on function declarations and only if the function is called as a method. For example,isIdentifier(node)
is supported butt.isIdentifier(node)
(method invocation) orpath.isIdentifier()
are not.Proposal
The proposal of this PR is to change the today's typing to
@babel/types
does not export a value for the node types.BabelNode
rather than a base class. This allows to refine on node types usingnode.type === 'Identifier'
.type
foris*
methodsExample Usage:
This will also allow flow to e.g. refine
Path
by usingpath.node.type === 'Identifier' && path.node.name === 'test';
Generated types
Regression
Changing the node types to classes means that custom
%checks
declaration must be rewritten from e.g.to
Other Changes
NODE_PREFIX
. The goal of this is to have shorter type names and allow consumers to explicitly import the types vs using the global declarations.super
andimport
functions to be declared asvar
instead of functions to work around a parsing error in some linters. This doesn't change the semantics.%checks
declaration for alias types, e.g.isLiteral