Apr 30, 2012

The CoffeeScript Compiler

The CoffeeScript compiler is written in what? CoffeeScript?

I just love this recent exchange on the #coffeescript IRC channel between CoffeeScript creator Jeremy Ashkenas and an unnamed visitor.

13:14 S: Hi, I have just a little question : In what language is written CoffeScript ? (I have see the GitHub but... I don't see a answer)
13:17 S: In src/ they have some .coffee script but CoffeeScript can be write in CoffeeScript :D
13:18 jashkenas: yes, yes it can.
13:18 jashkenas: It's written in CoffeeScript.
13:18 S: What ? I don't understand...
13:19 jashkenas: It was originally written in Ruby.
13:19 S: CoffeeScript can't be compileted by itselft ...
13:19 jashkenas: When that version of the compiler was complete enough.
13:19 jashkenas: It was ported over to CoffeeScript
13:19 S: Ok, I understand
13:20 jashkenas: Then that CoffeeScript source was passed through the Ruby compiler, producing JavaScript
13:20 jashkenas: Then that JavaScript recompiled the CoffeeScript
13:20 jashkenas: and now it recompiles itself.
13:20 S: Ok, nice :D
13:20 S: I little strange but why not :D
13:21 S: And by the way thx jashkenas for this :D I Love ! <3
13:21 jashkenas: cheers.

Meanwhile, Make a Better CoffeeScript Compiler on Kickstarter just reached its goal of $12,000, making it possible for heavyweight committer Michael Ficarra to devote four months of full-time work to the project.

If you haven't noticed, we appear to be entering a new era of fan-supported open-source software (OSS) development projects. The first such project to catch my attention was Yehuda Katz's rails.app, which is currently funded at almost 2x its initial goal. I think this is a welcome and healthy turn of events for open source, and will result in an explosion of activity as more and more contributors find it possible to carve out serious time without needing to reach the threshold of full-time, salaried employment with a corporate sponsor.




Apr 13, 2012

CoffeeScript Gets Strict Mode

What's strict mode? And more importantly, how does it work in CoffeeScript 1.3.x?

Mozilla Developer Network (MDN) defines ECMAScript 5 strict mode as "a way to opt in to a restricted variant of JavaScript." It basically provides a syntax error system to prevent you from creating trouble with JavaScript in a number of areas, ranging from the commonplace (global variable leaks) to the arcane (numeric octal literals). It also signals security issues. For further reading about strict mode and JavaScript, check out John Resig's post on strict mode.


CoffeeScript already does quite a bit to guard us from JavaScript pitfalls, so what additional help do we get from strict mode? Alex MacCaw has written this up nicely in the "Strict mode" section of The Little Book on CoffeeScript, Chapter 7, The Bad Parts.

For the final word on the feature, read the CoffeeScript project issue on GitHub. Jeremy Ashkenas' comment on the issue is noteworthy: "I think if we do this, we enforce our static strict mode restrictions everywhere, but we do not put any "use strict"directives into your code for you." In other words, the CoffeeScript compiler does the strict mode syntax checks itself, at compile time. This is good. You don't want to wait until you run the generated JavaScript output to discover your errors. 

Apr 3, 2012

Source Maps for CoffeeScript

One of the more serious concerns about CoffeeScript is that it lacks a first-class debugging environment. The code that you see in a browser debugger is the compiled JavaScript, not the CoffeeScript source. Thanks to JavaScript Source Maps, however, this long-running issue will likely be solved in the very near future.

JavaScript Source Maps is a system of references from JavaScript executing in the browser to original source files that have been minified, concatenated, compiled, or otherwise transformed. Chrome dev tools evangelist Paul Irish called Source Maps "the biggest thing to happen to JavaScript debugging in years." The Google Chrome team is pushing aggressively toward the release of Source Maps support (you can preview the feature in Chrome Canary).

You can read more about Source Maps in MozillaWiki feature page, in this tutorial, and in the formal proposal co-authored by Mozilla and Google. There is a Source map support plan discussion on the CoffeeScript Google Group.

Finally, Mozilla intern Nick Fitzgerald has produced a great what-I-did-this-summer video explaining the importance of Source Maps and of languages such as CoffeeScript. I recommend watching all 15 minutes of it, but for the TL;DR crowd, start around minute 11 for the sequence in which he demos an actual JavaScript console error with a link back to CoffeeScript source. Woo Hoo!

Update (2012/4/13)
Now that CoffeeScript 1.3 has shipped, Jeremy Ashkenas says that Source Maps support will be the focus of the next release of CoffeeScript. One comment on the announcment reads: 
"Source maps in Firefox and Chrome will be one of the most important pieces of software infrastructure since Javascript itself. It will be the realization of Stallman's 'one script to unite them all' vision for GNU GUILE from the 'Tcl wars.' A pity that it isn't Lisp, but there is enough goodness in Javascript (and Coffeescript) to make me very optimistic." 
Hyperbole? We'll see.

Update (2013/3/5)
Almost a year after my original post, CoffeeScript 1.6.1 now supports Source Maps via the --map compiler option!