News for hackers. Stuff that matters.
Re:JS 2 CoffeeScript Compiler by jashkenas on Wednesday September 14, 2011 @09:17AM (#2996120)
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...

Re:JS 2 CoffeeScript Compiler by Groxx on Wednesday September 14, 2011 @12:29PM (#2996836)
It should probably be noted that this is <1000 lines of CoffeeScript. Impressive nonetheless, thanks for the link :)
Re:JS 2 CoffeeScript Compiler by fooyc on Wednesday September 14, 2011 @09:59AM (#2996260)
It fails to return nothing here:

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

Translates to:

e.onclick = -> false // returns false

Re:JS 2 CoffeeScript Compiler by walrus on Wednesday September 14, 2011 @01:14PM (#2997026)
I don't understand why this is being downvoted. The compiler's behavior is definitely wrong here, as you showed with your example. This issue could even come up in practice. Suppose you have a checkbox with the id 'cb' in your DOM. Consider the following script:

    var list = [true, false, true, true, false];
    document.getElementById('cb').onclick = function() {list.pop();};
When the checkbox is clicked, it should remove the last item in the list, then put a checkmark into the box. Run this through the Js2coffee compiler and you will get

    list = [ true, false, true, true, false ]
    document.getElementById("cb").onclick = -> list.pop()
Run this back through the CoffeeScript compiler to get: (modified slightly for brevity)

    var list = [true, false, true, true, false];
    document.getElementById("cb").onclick = function() {return list.pop();};
Notice that it now returns the result of list.pop(). Since the last item in the list is false, this will prevent the default browser behavior, so the checkbox will not be checked when it is clicked the first time.
Re:JS 2 CoffeeScript Compiler by rgbrgb on Wednesday September 14, 2011 @10:46AM (#2996440)
What's the purpose of the 'false;' statement? I believe CoffeeScript always returns the last line of a function.
Re:JS 2 CoffeeScript Compiler by fooyc on Wednesday September 14, 2011 @11:56AM (#2996711)
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.

Re:JS 2 CoffeeScript Compiler by rgbrgb on Wednesday September 14, 2011 @12:55PM (#2996952)
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?
Re:JS 2 CoffeeScript Compiler by fooyc on Wednesday September 14, 2011 @01:08PM (#2997003)
> 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
Re:JS 2 CoffeeScript Compiler by fooyc on Wednesday September 14, 2011 @03:40PM (#2997564)
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 `{}`).
Re:JS 2 CoffeeScript Compiler by lmkg on Wednesday September 14, 2011 @02:26PM (#2997327)
> In my opinion, js functions should always return a value (even if it's just 'this').

Yeah, well, you know, that's just, like, your opinion, man.

It's perfectly valid for JavaScript functions to not return something. Your opinion about always returning something (which I agree with, btw) is only a personal stylistic preference, and from the code I've seen in the wild, it's not really that widespread.

As fooyc points out, the behavior of onClick events in the DOM changes depending on what return value is received. As I understand it, when an onClick executes a script, the truthiness of the value it returns determines whether the event continues to bubble up the DOM after the onClick has executed. At a guess, 75% of the onClick handler functions I've seen in the wild do not return a value; whether they realize it or not, all of those handlers implicitly rely on the appropriate truthiness of not returning a value for the rest of the page to behave correctly. Suddenly giving those functions a return value, especially one the developer didn't intentionally choose and didn't expect to have any effect, could cause quite a bit of unexpected behavior.

This also seems like an easy issue to address. No return statement in the JavaScript? Add an empty return in the CoffeeScript.

Re:JS 2 CoffeeScript Compiler by sid0 on Wednesday September 14, 2011 @11:29AM (#2996589)
The purpose of the false statement is irrelevant. I'm generally not a big fan of perfection in programming, but compilers are one instance it's worth aiming for.
Re:JS 2 CoffeeScript Compiler by skeptical on Wednesday September 14, 2011 @10:15AM (#2996335)
Just out of curiosity, how does one returns nothing in coffeescript?
Re:JS 2 CoffeeScript Compiler by endtwist on Wednesday September 14, 2011 @10:35AM (#2996402)

    fn = -> return
Re:JS 2 CoffeeScript Compiler by riffraff on Wednesday September 14, 2011 @08:53AM (#2996039)
I like the way

    if (!foo) bar;
gets turned into

    bar unless foo
:)
Re:JS 2 CoffeeScript Compiler by nolok on Wednesday September 14, 2011 @09:56AM (#2996255)
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.
Re:JS 2 CoffeeScript Compiler by TrevorBurnham on Wednesday September 14, 2011 @10:57AM (#2996486)
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.)
Re:JS 2 CoffeeScript Compiler by baddox on Wednesday September 14, 2011 @02:06PM (#2997257)
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;
    }
    ...
Re:JS 2 CoffeeScript Compiler by ww520 on Thursday September 15, 2011 @12:36AM (#2998931)
Not if you do

    if (!err)       return x;
    if (condition)  return y;
Re:JS 2 CoffeeScript Compiler by Groxx on Wednesday September 14, 2011 @12:11PM (#2996765)
I tend to like it if the left-hand-side is overwhelmingly the default code-path, or to keep all assignments aligned (ones with caveats then stick out like a sore thumb).

How do you feel then about things like this?

  variable = if something
      val
    else
      another_val
    end
(insert your personal indentation choice, of course)
Re:JS 2 CoffeeScript Compiler by aaronblohowiak on Wednesday September 14, 2011 @01:35PM (#2997112)
This does not cause any "backtracking" when tracing the code execution with the gray interpreter.
Re:JS 2 CoffeeScript Compiler by Groxx on Wednesday September 14, 2011 @04:30PM (#2997759)
>'with the gray interpreter'

sorry, ya lost me there. Are you referring to a language interpreter? In that case, which one?

The only difference I can see for interpreters is that it necessitates parsing the entire line before acting, which all but the most highly-tuned (and designed for this purpose) interpreted-languages probably already do.

Re:JS 2 CoffeeScript Compiler by damncabbage on Thursday September 15, 2011 @10:19AM (#3000429)
I'm guessing that he's referring to his brain.
Re:JS 2 CoffeeScript Compiler by Groxx on Thursday September 15, 2011 @10:39PM (#3002981)
Ahahaha, of course, that makes sense. Thanks, I may have to wake up the hamster in mine :)
Re:JS 2 CoffeeScript Compiler by skeptical on Wednesday September 14, 2011 @10:08AM (#2996305)
I always had that opinion about that feature too. It's present in quite a few languages. I think people like to point out that they can do things that you can't in other languages and end up going ridiculous paths. As if the real power of a programming language could be inferred from it's syntax.

Don't get me wrong, I like coffee script and I find the tool linked on this thread quite useful, but come on... rewriting all the 'ifs' as 'unless' jut because you can looks pointless and silly to me. Just my opinion.

Re:JS 2 CoffeeScript Compiler by bergie on Wednesday September 14, 2011 @08:45AM (#2996005)
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.
Re:JS 2 CoffeeScript Compiler by akx on Wednesday September 14, 2011 @11:49AM (#2996685)
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.

Re:JS 2 CoffeeScript Compiler by bchallenor on Wednesday September 14, 2011 @01:31PM (#2997094)
It appears that it doesn't like CRLF. Maybe the author didn't test it on Windows. ;)
Re:JS 2 CoffeeScript Compiler by bchallenor on Wednesday September 14, 2011 @01:48PM (#2997175)
Re:JS 2 CoffeeScript Compiler by ch0wn on Wednesday September 14, 2011 @08:05AM (#2995874)
This works really well. I actually learned some syntax elements I did not know before, like :: for accessing an object's prototype (Array::slice).
Re:JS 2 CoffeeScript Compiler by ktusznio on Wednesday September 14, 2011 @04:43PM (#2997803)
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...

Re:JS 2 CoffeeScript Compiler by wccrawford on Wednesday September 14, 2011 @08:15AM (#2995907)
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.

Re:JS 2 CoffeeScript Compiler by markokocic on Wednesday September 14, 2011 @08:10AM (#2995891)
Nice tool if you want to convert existing JS code to CoffeeScript. Wonder how stable it is.
Re:JS 2 CoffeeScript Compiler by alnayyir on Wednesday September 14, 2011 @07:00PM (#2998197)
It's not. At all.
Re:JS 2 CoffeeScript Compiler by jobeirne on Wednesday September 14, 2011 @09:47AM (#2996219)
Does anyone else find CoffeeScript insufferably unreadable? Why take a basically nice-looking language like JavaScript and bathe it in special forms?
Re:JS 2 CoffeeScript Compiler by rauljara on Wednesday September 14, 2011 @10:13AM (#2996330)
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.

Re:JS 2 CoffeeScript Compiler by dcvetinovic on Wednesday September 14, 2011 @10:27AM (#2996372)
I've been programming in ruby last 3-4 years most of my time. Despite of that (or may be just because of that) I still find JS code more clear and elegant for reading/writing (under condition it is well written, i.e. w/o "bad parts"), although it is way more verbose, has camel-case convention which I really dislike etc...
Re:JS 2 CoffeeScript Compiler by silverbax88 on Wednesday September 14, 2011 @11:34AM (#2996615)
But most programmers are familiar with a structure and layout that is common to other programming languages.
Re:JS 2 CoffeeScript Compiler by alecbenzer on Wednesday September 14, 2011 @12:00PM (#2996726)
What? CoffeeScript is similar to python and ruby just like Javascript might be considered more similar to C.
Re:JS 2 CoffeeScript Compiler by silverbax88 on Wednesday September 14, 2011 @05:58PM (#2998025)
I never said they weren't.
Re:JS 2 CoffeeScript Compiler by alecbenzer on Wednesday September 14, 2011 @06:50PM (#2998170)
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.
Re:JS 2 CoffeeScript Compiler by silverbax88 on Wednesday September 14, 2011 @07:35PM (#2998275)
I'm sorry, but you are inferring a lot from a simple comment.
Re:JS 2 CoffeeScript Compiler by alecbenzer on Wednesday September 14, 2011 @09:18PM (#2998513)
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?
Re:JS 2 CoffeeScript Compiler by dcvetinovic on Wednesday September 14, 2011 @09:58AM (#2996258)
I also find correctly written (i.e. without "bad parts") JS code more clear and readable, with syntax which better articulates programmers intentions. Although it may be that I got used to common JS's idioms and patterns. Anyways I still don't find enough incentive to try to write in CoffeeScript.
Re:JS 2 CoffeeScript Compiler by kennystone on Wednesday September 14, 2011 @10:20AM (#2996348)
Nah, you just need to learn some languages without C-style syntax.