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

Proposal: different compilation targets es6, es7, es8 and so on #5198

Closed
aleksandrlat opened this issue Apr 11, 2019 · 7 comments
Closed

Proposal: different compilation targets es6, es7, es8 and so on #5198

aleksandrlat opened this issue Apr 11, 2019 · 7 comments
Labels

Comments

@aleksandrlat
Copy link

aleksandrlat commented Apr 11, 2019

Choose one: is this a bug report or feature request?
Feature request

Expected Behavior

Compile CS to different JS standards or at least to latest standard instead of just es6.

Current Behavior

Compilation target is es6 only now.

Possible Solution

Probably nodes.coffee can be updated or multiple nodes.coffee can be created for each compilation targets.

Context

It makes sense for me to compile to different targets to be used by different environments. If some environment supports new features we just need to provide JS without additional changes.
Also if needed we can let other tools do the rest.

But I'm not sure about the effort to do it :)

Environment

  • CoffeeScript version: N/A
  • Node.js version: N/A
@GeoffreyBooth
Copy link
Collaborator

You can use the transpile option to configure Babel to output whatever target you want. By default (without using transpile), CoffeeScript 2 outputs modern, latest-version JavaScript; transpile attaches Babel as a second step to transpile the output to whatever compatibility level you desire.

@aleksandrlat
Copy link
Author

aleksandrlat commented Apr 12, 2019

Thank you @GeoffreyBooth!

Sorry maybe I was not clear but I mean coffeescript compiler does not compile to lates modern JS now.

And I cannot upgrade JS level with babel. I can only downgrade. I.e. if I have es6 I cannot make it es7 with babel. I can only make it es5 and lower.

So my problem is I want to have output JS like this:

class Component extends React.Component {
  instanceField = 'value'

  method = () => {
    // ...
  }
}

instead of

class Component extends React.Component {
  constructor(props) {
    super(props)
    this.method = this.method.bind(this);
  }

  method() {
    // ...
  }
}

Component.prototype.instanceField = 'value';

Do I miss something?

@GeoffreyBooth
Copy link
Collaborator

When new features are added to JavaScript, CoffeeScript lags behind and adds support for them after they're standardized. You're referring to class fields: #4552

It happens that CoffeeScript already supports arbitrary code in a class body, not just assignments, which makes it look like CoffeeScript supports class fields.

@aleksandrlat
Copy link
Author

Ok. Thank you @GeoffreyBooth!
I just realized that field in

class Test
  field: 5

is on prototype level and shared between instances.
It is not obvious and it is very confusing because in many other languages it is instance level field.

But what about methods?
Because these look almost equivalently for me except problems with inheritance:

// JS current
class Test {
  constructor() {
    this.getAll =this.getAll.bind(this) 
  }

  getAll() {
    return []
  }
}

// JS desired
class Test {
  getAll = () => {
    return []
  }
}

@GeoffreyBooth
Copy link
Collaborator

This isn’t currently valid JavaScript:

class Test {
  getAll = () => {
    return []
  }
}

You’re using experimental features here. See Babel REPL.

This is because you’re defining the method via = instead of :. Try this:

class Test
  getAll: ->
    []

There’s also no benefit to using => over -> here, as far as I’m aware, as the value of this is the class instance in both cases.

My suggested CoffeeScript outputs as:

var Test;

Test = class Test {
  getAll() {
    return [];
  }

};

Which I think should function identically to your desired output.

@aleksandrlat
Copy link
Author

Thank you @GeoffreyBooth!

This was bad example without this inside method. But of course I meant cases when we use this inside method.

I know I need babel to transpile class properties. But this is my feature request to target different ES versions and let other tool to do the rest or in some cases we need to use latest node or chrome and all these features are supported by them.

@GeoffreyBooth
Copy link
Collaborator

CoffeeScript supports targeting various ES versions through the transpile option and configuring Babel. Babel has presets for the various ES versions, and does a good job of converting CoffeeScript’s output to whichever minimum version(s) you need to support. See https://coffeescript.org/#transpilation

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

No branches or pull requests

2 participants