Pythonic JavaScript to CoffeeScript compiler

I am rewriting some of my old JavaScript code into CoffeeScript. I feel more happy in CoffeeScript and I think I produce more readable and more maintainable code. It also offers some great features such as classes. To help me on this transition I use js2coffee which compiles JavaScript to CoffeeScript. I have made an extension of js2coffee that is a bit more Pythonic (produces Python looking CoffeeScript code :-)).

I have before covered CoffeeScript and you should read my post if you don't know what CoffeeScript is:

The Pythonic extension of js2coffee

js2coffee is great, but I don't like some of the code it produces as it's more similar to Ruby than Python. I have forked js2coffee and provided a --pythonic option which does following things:

  • Uses 4 spaces for each step of indent
  • Uses parenthesized function calls - - I really dislike Ruby's optional parenthesizes on function calls, it produces unceccary bugs and makes code harder to understand (at least, when having a Python background)
  • Does not use implicit returns
  • Does not use unless - - for me, code that uses unless is much harder to understand

Here's a little showcase of what --pythonic does different:

$ ~/> cat test.'s

The JavaScript code we are compiling to CoffeeScript:

function helloWorld(some_value) {
    if(some_value != 'hello')
        return;
    aFunctionCall('hello');
    return "value";
}

$ js2coffee test.js

Compiling it via the standard js2coffee produces following code:

helloWorld = (some_value) ->
  return  unless some_value is "hello"
  aFunctionCall "hello"
  "value"

$ js2coffee --pythonic test.js

Compiling it with the --pythonic option produced following code - - which for me is easier to understand given my Python background:

helloWorld = (some_value) ->
    return if some_value isnt "hello"
    aFunctionCall("hello")
    return "value"

Google Closure + CoffeeScript?

Another project I am currently working on is to improve CoffeeScript's compiler so it can work with Google Closure. What this enables is to write high-level code in CoffeeScript that gets minimized and optimized by Google Closure's compiler (which does non-trivial code transformations and optimizations).

There's already a project that does something similar to this:

Code · Code improvement · Code rewrite · Design · JavaScript · node.js 28. Dec 2011
© Amir Salihefendic. Powered by Skeletonz.