CoffeeScript 2 arrives with JavaScript syntax improvements

By Paul Krill Sep 20th 2017
CoffeeScript 2 arrives with JavaScript syntax improvements

Upgrades should require 'little or no refactoring' for most projects, but there are some breaking changes.

CoffeeScript, a simple languages that compiles to JavaScript and aims to make web developers’ lives easier, has just moved to a second major release, one that emphasizes syntax improvements. 

CoffeeScript 2, which had been in a beta stage since April, features a compiler that translates CoffeeScript code into modern JavaScript syntax. A CoffeeScript “class” is now output using the class keyword, for example. Version 2 also features support for async functions syntax, the future object destructuring syntax, and JSX, which is JavaScript with interspersed XML elements.

The primary goals of CoffeeScript 2 were to remove incompatibilities with JavaScript that might prevent CoffeScript from being used for a project, and to preserve backward compatibility to the greatest degree possible.

New capabilities were done with few breaking changes from the 1.x version, CoffeeScript developers said. Previously, the team had warned that CoffeeScript 2 would require breaking changes to conform with the ECMAScript 2015 specification and above. “Most current CoffeeScript projects should be able to upgrade with little or no refactoring necessary,” the developers said. The few breaking changes involve bound (fat arrow) functions, bound generator functions, and Literate CoffeeScript parsing, which has been refactored to be more careful about not treating indented lists as code blocks.

Modernizing of JavaScript syntax means developers may need to transpile the compiler’s output, with transpilation converting source code into equivalent but different source code. CoffeeScript documentation cites the case in which developers would want to convert modern JavaScript into older JavaScript to run in older versions of Node.js or older browsers. Built-in support for the Babel transpiler is included in CoffeeScript.

With version 2, CoffeeScript now outputs line comments, after having discarded them in version 1.1. Block comments are now permitted anywhere, enabling static type annotations. The coffee command-line tool has been improved in the upgrade as well.

Note that some JavaScript features have been intentionally omitted, such as let and var, named functions, and the get and set keywords. Whereas let and named functions were omitted for simplicity’s sake, var was left out so that CoffeeScript developers would not have to deal with variable declarations, and the get and set keywords were omitted to avoid grammatical ambiguity. CoffeeScripts omission of these JavaScript features does not affect compatibility or interoperability with JavaScript modules or libraries.