7 ms·
JS 2 CoffeeScript Compiler
- ch0wn 15y agoThis works really well. I actually learned some syntax elements I did not know before, like :: for accessing an object's prototype (Array::slice).
- markokocic 15y agoNice tool if you want to convert existing JS code to CoffeeScript. Wonder how stable it is.
- alnayyir 15y agoIt's not. At all.
- wccrawford 15y agoInteresting, 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.
- bergie 15y agoI'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.
- riffraff 15y agoI like the way if (!foo) bar; gets turned into bar unless foo :)
- nolok 15y agoAm 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.
- skeptical 15y agoI 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.
- TrevorBurnham 15y agoIt 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.)
- Groxx 15y agoI 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)
- jashkenas 15y agoAmazingly 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/js2coffee.coffee https://github.com/rstacruz/js2coffee/blob/master/lib/js2cof...
- Groxx 15y agoIt should probably be noted that this is <1000 lines of CoffeeScript. Impressive nonetheless, thanks for the link :)
- jobeirne 15y agoDoes anyone else find CoffeeScript insufferably unreadable? Why take a basically nice-looking language like JavaScript and bathe it in special forms?
- dcvetinovic 15y agoI 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.
- rauljara 15y agoAny 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.
- dcvetinovic 15y agoI'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...
- silverbax88 15y agoBut most programmers are familiar with a structure and layout that is common to other programming languages.
- 15y ago
- fooyc 15y agoIt fails to return nothing here: e.onclick = function() { false; } // returns nothing Translates to: e.onclick = -> false // returns false
- skeptical 15y agoJust out of curiosity, how does one returns nothing in coffeescript?
- endtwist 15y agofn = -> return
- rgbrgb 15y agoWhat's the purpose of the 'false;' statement? I believe CoffeeScript always returns the last line of a function.
- sid0 15y agoThe 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.
- fooyc 15y agoAn 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 15y agoWhile 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?
- akx 15y agoSome 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 15y agoIt appears that it doesn't like CRLF. Maybe the author didn't test it on Windows. ;)
- bchallenor 15y agoI raised an issue on GitHub. https://github.com/rstacruz/js2coffee/issues/81 https://github.com/rstacruz/js2coffee/issues/81
- ktusznio 15y agoI 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/6b421b32c2d28bc7e41deab5eacea7827afa7a8d https://github.com/Shopify/batman/commit/6b421b32c2d28bc7e41...