Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile CoffeeScript AST into CoffeeScript instead of JavaScript?

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.

like image 522
gkz Avatar asked Nov 24 '11 09:11

gkz


People also ask

Is CoffeeScript same as JavaScript?

One crucial difference between the two languages is that TypeScript is the superset of JavaScript while CoffeeScript is a language which is an enhanced version of JavaScript. Not just these two languages but there are other languages such as Dart, Kotlin, etc. which can be compiled into JavaScript.

How can we convert CoffeeScript code in JavaScript?

Compiling CoffeeScript into JavaScript usually results in JS that is fairly readable. You can convert snippets on the fly on the "Try CoffeeScript" tab of the CoffeeScript homepage, or via the CoffeeScript command line tool. There are also tools like decaffeinate that convert CoffeeScript source to modern JavaScript.

Why should you use CoffeeScript instead of JavaScript?

CoffeeScript is something that makes even good JavaScript code better. CoffeeScript compiled code can do everything that natively written JavaScript code can, only the code produced by using CoffeeScript is way shorter, and much easier to read.


Video Answer


1 Answers

See round_trip.coffee in CoffeeCoffee project (I'm the author):

I don't cover every case, but it's a start.

like image 151
Steve Howell Avatar answered Nov 15 '22 09:11

Steve Howell