

JS 2 CoffeeScript Compiler - franze
http://js2coffee.org

======
jashkenas
Amazingly enough, the meat of it is less than 1,000 lines of code -- and shows
how you can integrate with Narcissus to parse JavaScript from JavaScript:

[https://github.com/rstacruz/js2coffee/blob/master/lib/js2cof...](https://github.com/rstacruz/js2coffee/blob/master/lib/js2coffee.coffee)

~~~
Groxx
It should probably be noted that this is <1000 lines of _CoffeeScript_.
Impressive nonetheless, thanks for the link :)

------
fooyc
It fails to return nothing here:

e.onclick = function() { false; } // returns nothing

Translates to:

e.onclick = -> false // returns false

~~~
rgbrgb
What's the purpose of the 'false;' statement? I believe CoffeeScript always
returns the last line of a function.

~~~
fooyc
An example.

This could be any statement evaluating to false.

Coffee script returns the result of the last statement. Javascript don't. So,
failing to take this into account produces coffeescript that do not have the
same behavior than the original javascript.

This is particularly important for iterators that return false for stopping
iteration. Or for DOM event handlers that return false for stopping the
propagation of the event.

~~~
rgbrgb
While I totally agree with sid0 that it would be nice for 'academic' reasons
if the compiler translated js->cs perfectly, I guess I'm having trouble
understanding where this would actually come up as a problem. In my opinion,
js functions should always return a value (even if it's just 'this'). Maybe a
real code example of where your program would fail because of this?

~~~
fooyc
> would be nice for 'academic' reasons if the compiler translated js->cs
> perfectly

You should expect this from a compiler, actually

> In my opinion, js functions should always return a value

Sounds wrong

> real code example of where your program would fail because of this?

Simple:

    
    
        // That's perfectly fine; why would I return anything from this ?
        link.onclick = function() {
            doSomething();
        };
    
        // Now, translated in coffeescript:
        link.onclick = ->
            return doSomething(); // if this returns false, the "click" event is canceled

~~~
fooyc
An other good example is constructors; say you have a function like this:

    
    
        function MyClass(x){
            this.x = x;
        }
    

In Coffeescript this translates to

    
    
        MyClass = (x) ->
            @x = x
    

`x` is returned; and if you return something from a constructor, something is
returned instead of the constructed object (`new MyClass({})` returns `{}`).

------
riffraff
I like the way

    
    
        if (!foo) bar;
    

gets turned into

    
    
        bar unless foo
    

:)

~~~
nolok
Am I really the only one who finds it actually a wee bit harder to read when
"eyeing" through a lot of code ? It might look nicer, but you read it like "do
this, ah no cancel it if that" forcing you to rollback in your head.

~~~
TrevorBurnham
It looks weird at first, but it's great when used like so:

    
    
        return x unless err
        return y if condition
        ...
        return z
    

Keeping all of your "return"s at the start of the line makes it much easier to
see, at a glance, what the possible return values of your function are. (And,
of course, when you see a "return" in the middle of your function, you know
there _must_ be a condition attached.)

~~~
baddox
The traditional alternative also puts your returns at the beginnings of lines.
It just uses a lot more lines.

    
    
        if (!err) {
          return x;
        }
        if (condition) {
          return y;
        }
        ...

~~~
ww520
Not if you do

    
    
        if (!err)       return x;
        if (condition)  return y;

------
bergie
I've used this to convert some old JS code to CoffeeScript for better
maintainability. Usually requires some manual touch-up afterwards, but is
still a lot faster than converting by hand.

------
akx
Some critique:

I tried with a couple lengthier bits of code that JSLint happily eats and I
get an "Illegal token" error with no coordinates or anything to help me. Ah
well.

Also not very happy with the custom scrolling in the source text area -- my
mouse wheel is set up to scroll 6 lines per wheel click. The source text area
scrolls approximately 1.5 at a time.

~~~
bchallenor
It appears that it doesn't like CRLF. Maybe the author didn't test it on
Windows. ;)

~~~
bchallenor
I raised an issue on GitHub.

<https://github.com/rstacruz/js2coffee/issues/81>

------
ch0wn
This works really well. I actually learned some syntax elements I did not know
before, like :: for accessing an object's prototype (Array::slice).

------
ktusznio
I used this to port jQuery.data implementation and its corresponding tests
into Batman's internals[1]. Worked wonderfully and saved me hours of manual
rewriting.

Kudos to the author!

1:
[https://github.com/Shopify/batman/commit/6b421b32c2d28bc7e41...](https://github.com/Shopify/batman/commit/6b421b32c2d28bc7e41deab5eacea7827afa7a8d)

------
wccrawford
Interesting, but has issues with classes.

I turned that find function into a class with a method, and converted it back
and forth and the JS->Coffee came out oddly. Definitely not nice CoffeeScript,
even if it worked.

------
markokocic
Nice tool if you want to convert existing JS code to CoffeeScript. Wonder how
stable it is.

~~~
alnayyir
It's not. At all.

------
jobeirne
Does anyone else find CoffeeScript insufferably unreadable? Why take a
basically nice-looking language like JavaScript and bathe it in special forms?

~~~
rauljara
Any language is unreadable if you are unfamiliar with it, or languages that
share syntax with it. As someone with more of a ruby/python background, I find
CoffeeScript incredibly readable. I imagine someone who has only ever learned
lisp would find both syntaxes wretched.

I do think, however, that many of the ruby-ish syntax tweaks are more
expressive once you get used to them. That is, I learned C-like syntaxes
before I learned ruby and I still find I'm able to parse ruby-like code much
faster than c-like code. There is more to learn in terms of symbols,
constructions, but once you know them, they communicate more information
faster.

~~~
silverbax88
But most programmers are familiar with a structure and layout that is common
to other programming languages.

~~~
alecbenzer
What? CoffeeScript is similar to python and ruby just like Javascript might be
considered more similar to C.

~~~
silverbax88
I never said they weren't.

~~~
alecbenzer
I'm not sure what you mean by "they", but your post made it sound like
Javascript syntax was better then CoffeeScript because "most programmers are
familiar with a structure and layout that is common to other programming
languages", which would imply that Javascript syntax is similar to other
languages but that CoffeeScript isn't.

~~~
silverbax88
I'm sorry, but you are inferring a lot from a simple comment.

~~~
alecbenzer
I don't think I'm inferring any more than I thought seemed necessary to make
sense of your comment given the context. What did you mean by your original
comment?

