Reputation: 248
Is it possible to turn the AST back into CoffeeScript instead of into JavaScript? A project I am working basically requires turning all the CoffeeScript into the AST, analysing the AST, then turning portions of the AST back into CoffeeScript. I understand the transformation back would be 'lossy' with regards to the original code, but this is okay.
I would really like to avoid having to compile those portions to JavaScript and then back to CoffeeScript.
Upvotes: 6
Views: 1351
Reputation: 146
See round_trip.coffee in CoffeeCoffee project (I'm the author):
I don't cover every case, but it's a start.
Upvotes: 4
Reputation: 5374
It is possible, but it requires you to play with Coffeescript's source code. You need to get familiar with the way Coffeescript does the tokenization, then how it turns tokens into the AST. The source code in question is in coffee-script/src on Github.
If you look into the file coffee-script/src/nodes.coffee, you'll see a bunch of Node subclasses, each with their own compilation functions (like compile()
, compileNode()
, etc.). These define how the tree is turned into javascript code. If you want to generate Coffeescript code instead, you need to override all of them, on your own fork of the project.
Upvotes: 3