Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target ES6 #344

Open
wants to merge 69 commits into
base: master
Choose a base branch
from
Open

Target ES6 #344

wants to merge 69 commits into from

Conversation

ef4
Copy link
Contributor

@ef4 ef4 commented May 26, 2015

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:

  • default params
  • class
  • array destructuring
  • object destructuring
  • translate for in to for of
  • super must be called before accessing this in constructors
  • class declarations inside IIFEs need to get lifted to outer scope

@ghempton
Copy link
Contributor

❤️

@srb-
Copy link

srb- commented May 27, 2015

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

@ef4 ef4 mentioned this pull request May 27, 2015
This makes defaults & rest work in constructors
@ef4
Copy link
Contributor Author

ef4 commented May 27, 2015

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

@srb-
Copy link

srb- commented May 27, 2015

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.

@ef4
Copy link
Contributor Author

ef4 commented May 27, 2015

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.

@eventualbuddha
Copy link
Contributor

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.

@ef4
Copy link
Contributor Author

ef4 commented Aug 27, 2015

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
#344 (comment)
.

@auvipy
Copy link

auvipy commented Nov 24, 2015

great works. but

This branch has conflicts that must be resolved

@rattrayalex
Copy link

rattrayalex commented Aug 7, 2016

@ef4 what's the status of this project?

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

@ef4
Copy link
Contributor Author

ef4 commented Aug 8, 2016

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.

@rattrayalex
Copy link

Awesome, thanks @ef4 !

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

@ef4
Copy link
Contributor Author

ef4 commented Aug 9, 2016

Sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants