CoffeeScriptRedux
CoffeeScriptRedux copied to clipboard
Target ES6
This is a work-in-progress. I'm sharing in case others are interested or have feedback. This branch creates a new option --target-es6 that tells the compiler to emit ES6 when possible.
Since many ES6 features are closely aligned with CoffeeScript, we can actually do less and emit smaller, simpler code that takes advantage of native implementations.
Why would you want this?
- because you're targeting an environment with native ES6 support
- because you want to convert an existing CoffeeScript codebase to JS without all the noise introduced by missing ES6 features.
Here's an example of what it can already do:
$ cat sample.coffee
handleAllThings = (things...) ->
myThings.map (thing) => this.handle(thing)
$ ./bin/coffee --bare --js --input sample.coffee --target-es6
// Generated by CoffeeScript 2.0.0-beta9-dev
var handleAllThings;
handleAllThings = function (...things) {
return myThings.map(thing => this.handle(thing));
};
For comparison, without --target-es6 we get a lot more code:
$ ./bin/coffee --bare --js --input sample.coffee
// Generated by CoffeeScript 2.0.0-beta9-dev
var handleAllThings;
handleAllThings = function () {
var things;
things = arguments.length > 0 ? [].slice.call(arguments, 0) : [];
return myThings.map(function (this$) {
return function (thing) {
return this$.handle(thing);
};
}(this));
};
Status:
- [x] default params
- [x] class
- [x] array destructuring
- [ ] object destructuring
- [ ] translate
for intofor of - [x] super must be called before accessing
thisin constructors - [x] class declarations inside IIFEs need to get lifted to outer scope
:heart:
Fantastic! I'm especially interested in the default parameters.
@srb- default params are working now, for both ArrowExpressions and FunctionExpresion:
$ cat sample.coffee
class Foo extends Bar
doIt: (a=1, b=2, c...)->
super("yay", a, b, c)
demo = (a, b=2, c...) => a + b + c.length
$ ./bin/coffee --bare --js --input ./sample.coffee --target-es6
// Generated by CoffeeScript 2.0.0-beta9-dev-es6
var demo;
class Foo extends Bar {
doIt(a = 1, b = 2, ...c) {
return super.doIt('yay', a, b, c);
}
}
demo = (a, b = 2, ...c) => a + b + c.length;
Very cool! Question: do you actually use this compiler in production? I was under the impression it's in a semi-abandoned state so I've been afraid to use it for real projects.
I haven't yet, but I'm planning to use it to help me port a significant production codebase from coffeescript to ES6. This is admittedly a lower bar than getting it working as an ongoing production compiler -- I can supervise the process more closely.
Overall I have been happy with the parser -- it is more conservative than the one in original coffeescript, and it forced me to disambiguate a bunch of dubious code. And some of the roughest features (like super, which exists only in a PR) are things that I've already replaced with much simpler ES6 versions.
So for my use case it works, but I can understand the hesitation. You definitely have to be willing to debug the compiler to use it at this point.
I like this PR a lot. I am willing to merge it in. @ef4: Can you rebase?
Sure, happy to rebase in anticipation of merge, as soon as I have the test suite stabilized.
Wow, I wish I'd seen this sooner. I've been working on decaffeinate which has a pretty similar goal. Did I understand you correctly, @ef4, that you're trying to move a codebase from CoffeeScript to ES6? If so, we might want to see if there's any work we can do together as that's my goal.
The main reason I didn't go with the approach in this PR is that I'd lose comments and formatting.
I have a separate tool for safely porting the comments across, using source mapping as a guide. I should get around to publishing it.
This PR is still definitely a work in progress. It's usable to if you're doing one file at a time and supervising it, but it definitely has holes. On Aug 26, 2015 8:13 PM, "Brian Donovan" [email protected] wrote:
Wow, I wish I'd seen this sooner. I've been working on decaffeinate https://github.com/eventualbuddha/decaffeinate which has a pretty similar goal. Did I understand you correctly, @ef4 https://github.com/ef4, that you're trying to move a codebase from CoffeeScript to ES6? If so, we might want to see if there's any work we can do together as that's my goal.
The main reason I didn't go with the approach in this PR is that I'd lose comments and formatting.
— Reply to this email directly or view it on GitHub https://github.com/michaelficarra/CoffeeScriptRedux/pull/344#issuecomment-135214201 .
great works. but
This branch has conflicts that must be resolved
@ef4 what's the status of this project?
EDIT: And are you looking for help to get it over the finish line?
I had to shift focus to other things, anyone is welcome to take it and run with it.
The status is the same as my last comment: it's already useful as an assistant if you're hand-porting code from coffeescript to ES2015. I wouldn't trust it to transpile a whole codebase.
Awesome, thanks @ef4 !
Do you think you'd be willing to provide advice from time to time if someone were to pick it up?
Sure.