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
Convert @babel/template
from Flow to TS
#12317
Convert @babel/template
from Flow to TS
#12317
Conversation
// Until we have converted every package, we cannot store | ||
// .d.ts files inside lib/ because it causes conflicts | ||
// with Babel-related type definitions in node_modules/@types | ||
outDir: "./dts", |
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 is because, until all of our packages have .ts
files, we:
- Rely on some packages that are still
.js
filed and have the correspondingnode_moduels/@types/
definitions - Those packages rely on our already converted packages
- They load the
.d.ts
files (only if we store them inlib
) - TS refuses to update the
.d.ts
files because they are used as inputs
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 is a tricky one, the issue is happening only when having kinda circular dependency - in other words, generated d.ts
files in a package are somehow imported by itself while compiling. And this is something that needs to be fixed, while migrating.
I would suggest, @babel/template
is just not the right starting point - dependencies of it can be migrated first, and that would solve the issue.
state.isLegacyRef.value = false; | ||
} | ||
} else if (state.isLegacyRef.value === false || state.syntacticPlaceholders) { | ||
return; | ||
} else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) { | ||
name = ((node: any): BabelNodeIdentifier).name; | ||
name = node.name; |
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.
Here TS is better than flow at refining node
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.
it can be even better - if to first convert @babel/types
and @babel/traverse
packages (there are many types missing or to be improved comparing to definitely typed)
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/32124/ |
export function parse(input: string, options?: ParserOptions): import('@babel/types').File; | ||
export function parse( | ||
input: string, | ||
options?: ParserOptions | ||
): import("@babel/types").File; |
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 file is just reformatted, since I enabled Prettier on TS files.
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 4c8ea55:
|
@@ -1,31 +0,0 @@ | |||
// @flow |
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.
to avoid loosing file edits history, I would suggest having two separate commits (one to rename the files, and other to convert the content)
I agree that #11578, if considering entire thing at once - it is too big to be properly reviewed. But a very big part of the PR is automatically converted from the flow sources, and during that it is already verified to remain exactly the same after types are removed - and so, it does not actually needs to be reviewed, it is safe to merge as is. Except that automatic step - there are just a few bigger things that would take some time to review, but most of the commits there it a fairly trivial(that is on purpose - I avoided much refactor, trying to have only minimal type fixes, specifically to allow migration to be fast) And so, I am not sure it worth to go really gradually keeping both flow and typescript type coverage at the same time - I would suggest that would add too much overhead. And instead, I would suggest following bigger steps:
The biggest drawback would be - for a brief time there would be no type check in the repository, but given flow coverage is not good anyway - so, I do not really worry about that. |
c90459c
to
dfe15bd
Compare
I noticed that the @RyanCavanaugh (TS team) was looking into circular project references (microsoft/TypeScript#33685), and the issue is marked as Awaiting More Feedback. Maybe our repository can provide an example of use case 😄
Yeah, probably the best starting point would have been something like
I don't think that running both Flow and TS at the same time adds too much overhead. We run type checking as part of CI, and it's definitely not one of the slowest jobs. In the end, both strategies (by "conceptual" chunks as you are proposing, or by groups of packages as I'm proposing) will give the same result, so the decision should just be about what's easier for us to manage during the migration. I prefer the approach I'm proposing because, even if the automated transform works from a theoretical point of view, there are different things that we might still want to change.
All those things are really nitpicks, but they improve the code quality and are harder to notice in a big automated PR. @JLHwung / @existentialism / @hzoo / @kaicataldo Do you have any opinion on this? |
If we aren't comfortable with automated approach (I think if we were we would of already done it, and we'd have to review it either way), then just doing it slowly manually seems fine, esp if given someone is looking at it you can catch things along the way. If we start this, maybe just need to be disciplined in actually reviewing/merging and having some plan to move things over. |
Actually, without @zxbodya's tool migrating would be order of magnitudes harder. The automated tool gives us a very good basis, but the output still needs to be tweaked (and thus, reviewed). What I did in this PR was cherry-picking files from the other PR, and then reviewing and slightly adjusting them. |
💯 for the current approach. LGTM as well as type check is good. We can always iterate later. (I have updated the colour of "Flow -> TS" label to match the theme colour of |
Thanks, I had that intention but just guessed since I was too lazy to check what it should have been 😂 |
dfe15bd
to
1c1e3be
Compare
I mean extra work needed to keep flow working for remaining packages - like adding
When converting, the tool literally checks that output when using flow preset and output using typescript preset on converted code are the same - so it should be pretty safe from perspective of not breaking anything except the type checks. Except automated part - there are some manual changes required to fix the differences between type systems, to make type check passing - that indeed needs a review, but can be done separately (if we are ok, to have some broken types for migration time)
I agree there are many things that would be nice to be tweaked, but I am not sure how much of it should be done during initial migration - my feeling is, better to have as much of it as possible separately from the initial step:
In #11578, I tired to avoid changes that are not really necessary and instead adding Also on type improvements, I would suggest after initial migration it would be nice to consider adding strict type checking in tsconfig, maybe that can be combined with other type improvements… About migration approach - I do not really have a strong preference. Both look fine for me, the biggest difference I see - doing each package separately - would be a bit more work, but would also result in more detailed review. I see two main options:
Lets decide on one - and then, I can split #11578 into multiple PRs(thinking to make a script for that),
Anther thing to consider, is how to deal with existing types in definitely typed, like here: I would suggest - postpone this, and and do not publish generated types, while not everything is migrated Once everything is in typescript - where possible update sources to match types from DefinitelyTyped(often types there are more detailed, and more importantly already used by external projects (like jest). And only then start publishing the types, and to deprecate DefinitelyTyped for newer versions. |
{ | ||
files: [], | ||
references: tsPkgs.map(({ dir }) => ({ | ||
path: path.relative(root, dir), |
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.
to keep in mind - this is not always correct: some dev packages may need to not be included(because will cause cyclical dependency)
also in many cases - @babel/types
, @babel/traverse
would need to be added
so, I would say - this should be used only once to generate initial configs which then to be committed
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'm not tracking devDependencies
here, because they are not used in src
folders (this also avoids cycles, at least for the non-plugin packages).
@babel/types
has its own type building logic (it already has its published .d.ts
file) and it isn't build using tsc
, so tsc
already picks up the type definitions.
I'd be glad to help with the Flow definitions! It shouldn't be too much work, and it will give me a clear idea of our API surface which is good.
100% agree 😄
Yeah I agree, we shouldn't publish types yet. I think we can keep checking DefinitelyTyped (and maybe testing our types on the Jest source) as the last step.
Since some packages are very small, we'll go with a "mixed" approach. I have prepared a list of "batches" (they are still a small number of packages, but it's a good starting point): they don't have cycles, and they have the correct size to be properly reviewed. I'll cross-post this list (if it looks good to you) in your draft PR, and I'll start preparing Flow definitions for the first batch. Batch 1:
Batch 2:
Batch 3:
Batch 4: (depends on 2 and 3)
Batch 5:
Batch 6: (depends on 3 and 4)
|
Co-authored-by: Bogdan Savluk <savluk.bogdan@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
1c1e3be
to
d6d9063
Compare
As an option to help with flow type definitions - it should be possible to use compilation result after converting to TypeScript. It is possible to bundle btw, I rebased #11578 so it can be used for latest code version.
I would say - we should not use it for now, and only later to use type tests from it to align type definitions later… (if to use it - there would be weird issues because, like - inability to generate files, because they are used as input 😆, or need to rename things(which later, with typescript support can be much easier leveraging IDE features)) about a ways to exclude
(basically resolving specific type packages to something non-existing…) Also, as a first steps -- I would say, we should include |
Co-authored-by: Bogdan Savluk <savluk.bogdan@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
d6d9063
to
c2d1852
Compare
I got some type errors while rebasing, so I added |
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
c2d1852
to
4c8ea55
Compare
If you think that something is needed in a different order than what I proposed feel free to do it. However, we already generate types for Btw, since this is a lot of work feel free to ask me to continue when you don't want to. |
Co-authored-by: Bogdan Savluk <savluk.bogdan@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This PR is extracted from #11578. Since #11578 is so big that it's almost impossible to review, I'm attempting to enable a gradual migration (package by package) from Flow to TS.
make tscheck
only works after building the monorepo. We can avoid this by migrating packages in topological order if possible.tsconfig.json
files are gitignored: it's easy to compute them inmake tscheck
, so I think we can avoid committing one new "noise" file per folder.Btw, while working on this PR I already noticed that TS in VSCode is definitely more helpful than Flow 😁
cc @zxbodya