CoffeeScriptRedux icon indicating copy to clipboard operation
CoffeeScriptRedux copied to clipboard

Target ES6

Open ef4 opened this issue 10 years ago • 14 comments

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 in to for of
  • [x] super must be called before accessing this in constructors
  • [x] class declarations inside IIFEs need to get lifted to outer scope

ef4 avatar May 26 '15 20:05 ef4

:heart:

ghempton avatar May 26 '15 21:05 ghempton

Fantastic! I'm especially interested in the default parameters.

srb- avatar May 27 '15 13:05 srb-

@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;

ef4 avatar May 27 '15 15:05 ef4

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.

srb- avatar May 27 '15 15:05 srb-

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.

ef4 avatar May 27 '15 15:05 ef4

I like this PR a lot. I am willing to merge it in. @ef4: Can you rebase?

michaelficarra avatar May 29 '15 00:05 michaelficarra

Sure, happy to rebase in anticipation of merge, as soon as I have the test suite stabilized.

ef4 avatar May 29 '15 00:05 ef4

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.

eventualbuddha avatar Aug 27 '15 00:08 eventualbuddha

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 .

ef4 avatar Aug 27 '15 00:08 ef4

great works. but

This branch has conflicts that must be resolved

auvipy avatar Nov 24 '15 10:11 auvipy

@ef4 what's the status of this project?

EDIT: And are you looking for help to get it over the finish line?

rattrayalex avatar Aug 07 '16 14:08 rattrayalex

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.

ef4 avatar Aug 08 '16 16:08 ef4

Awesome, thanks @ef4 !

Do you think you'd be willing to provide advice from time to time if someone were to pick it up?

rattrayalex avatar Aug 09 '16 04:08 rattrayalex

Sure.

ef4 avatar Aug 09 '16 08:08 ef4