-
Notifications
You must be signed in to change notification settings - Fork 350
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
Initial support for TypeScript in Recast via Babylon 7 parser #461
Conversation
For comparison, it might be useful to refer to Prettier's printer implementation, since Prettier can print TypeScript types, and Prettier was originally forked from the Recast pretty-printer, so their implementation should be more or less applicable here. They've changed some things, of course—e.g. they don't use |
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 looking really great. You’ve picked up the quirks and conventions of this codebase, and reused existing code as well as I could have hoped. I’m sure there will be some additional cases to implement after benjamn/ast-types#249 is merged, but the general approach here is 👌. Thanks for jumping into this!
lib/printer.js
Outdated
" = ", | ||
path.call(print, "initializer") | ||
); | ||
parts.push(); |
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.
What’s this empty .push()
doing?
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.
Absolutely nothing. Copy paste mistake :) Ill fix it as soon as I have some free time to get back into this later this week.
Oh also, when you get a chance, could you rebase against |
Naive question (and probably not in the right place to ask). I'm a little new to the GitHub/node ecosystem. If I wanted to test out the PRs for recast and ast-types from jscodeshift, is that possible using this assumed approach?
|
You can take a look at this comment on the JSCodeshift issue as someone has done some work on getting it working (I think there's a few tweaks on the jscodeshift side needed? The main thing is: you don't copy paste the code over. Use |
@blaster151 Instead of downloading the source and copying it into "dependencies": {
"recast": "https://github.com/Phoenixmatrix/recast/tarball/typescript-initial",
"ast-types": "https://github.com/Phoenixmatrix/ast-types/tarball/typescript-initial"
} Alternatively, you can install these versions using cd path/to/your/project
npm install https://github.com/Phoenixmatrix/ast-types/tarball/typescript-initial
npm install https://github.com/Phoenixmatrix/recast/tarball/typescript-initial However, since this version of |
As @Phoenixmatrix suggested, |
Wow, my mind is blown - I didn't even know npm could work with GitHub tarball URLs. Thanks so much! |
Thank you again, @benjamn and @Phoenixmatrix, for the direction. I got jscodeshift working (similarly to that poster on the thread you linked to - I hadn't seen that issue on jscodeshift's GitHub page and I'm assuming because it's currently closed). Giddy! |
Although I'm wondering if I should just be using recast straight-up. Is jscodeshift basically the most popular command-line "runner" for recast? |
Recast itself has a very simple (and old) |
df9e980
to
198498c
Compare
7f6f99a
to
f59bc54
Compare
33cbf7e
to
5d12678
Compare
Ok! I'm confident we've covered all the different kinds of TypeScript syntax that can be parsed by the Babylon 7 parser, and that we've implemented pretty-printer cases for all those new AST types. This means it's now possible to use Recast to transform TypeScript code, which is something that's never been possible before! Two areas for future improvement:
|
We'll be using this in the real world soon enough, so I'll keep an eye for opportunities to open PRs when we hit cases that could be pretty-printed better :) Thank you so much for helping with this! |
@benjamn
Would resolve #424
I think it's time I get an eye on this.
It also includes #458 and #460, but those are their own PRs and you won't hurt my feelings we have to remove them from this one :)
As discussed in the typescript issue, this uses the babylon parser and uses an initial (not complete by any mean) ast to provide basic TypeScript printing. For the scope of this PR I focused on supporting as many of the common ast nodes as I could and printing in the common/simple case. I don't yet take into consideration options other than tabWidth, and some more complex elements (like module bodies) are probably not complete, but this is enough to work with common Recast uses with TypeScript.
I still need to do a bit of cleanup, but would like your input on the general approach. Thanks! (Opening the ast-types PR shortly)
(build failing is just because ast-types isn't published)