TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Transpiling ES6 generators into a ES5 state machine?

Open d180cf opened this issue 11 years ago • 14 comments

This must have been discussed here, but I couldn't find that discussion. So what's the reason for not supporting es6 generators when targeting es5?

In my spare time I'm writing a tsumego (it's like a mating problem in chess) solver in js which is supposed to be run on a tsumego hosting site like goproblems. This means that the solver must be es5-compatible. The solver itself is a fairly complicated variation of the depth-first search which is, obviously, recursive and can easily go 30-40 stack frames deep. Debugging such a recursive algorithm is a nightmare. I obviously needed a way to implement a special tsumego debugger that would let me use F10, F11, Shift+F11 to suspend and resume the solver at some interesting locations in the search tree, but a conventional recursive function doesn't allow to do that: it either runs to the end or just hangs due to some bug causing a tricky infinite loop 30-or-so levels deep in the search tree. So I had to rewrite the solver in a non recusrive form, which made it much less elegant. Today I've realized that what I actually needed is a recursive generator that would yield at every node in the search tree and yield* itself when it needs to go deeper.

I'm aware of transpilers like traceur that can do this transformation, but I'm afraid that this won't work with tsc well: if I write yiled* in a ts file, vs2015 will complain about the syntax, tsc will simply fail and traceur won't be able to do much due to ts-specific type annotations.

d180cf avatar Jul 22 '15 03:07 d180cf

I'm aware of transpilers like traceur that can do this transformation, but I'm afraid that this won't work with tsc well: if I write yiled* in a ts file, vs2015 will complain about the syntax, tsc will simply fail and traceur won't be able to do much due to ts-specific type annotations.

You can compile with --target es6 to strip out annotations and then pass it through to traceur / others.

So what's the reason for not supporting es6 generators when targeting es5

Just work that needs to be done. It will come with release 1.6 :https://github.com/Microsoft/TypeScript/issues/1664

basarat avatar Jul 22 '15 03:07 basarat

Will I lose the ability to debug the original .ts files via source maps? Theoretically a tool like traceur could take into account a preexisting source map and generate a composition of source maps, but I have a doubt that traceur is this advanced.

Just work that needs to be done. It will come with release 1.6

Thanks. This answers my question.

d180cf avatar Jul 22 '15 04:07 d180cf

To be clear, targeting ES6 with async will be supported in 1.6. Not downlevel support.

My understanding is that the work required is understood (#1781), but the team is still weighing the complexity/gain tradeoff.

DanielRosenwasser avatar Jul 22 '15 04:07 DanielRosenwasser

As mentioned, this is on the table, we just haven't done/committed to it yet.

danquirk avatar Jul 22 '15 18:07 danquirk

Will this be implemented? why is this issue closed?

oliverjanik avatar Feb 11 '16 01:02 oliverjanik

Please note, this is gated on https://github.com/Microsoft/TypeScript/issues/5595

mhegazy avatar Feb 11 '16 18:02 mhegazy

Any progress on this? #5595 has been resolved.

kuudihxquuwhxq avatar Sep 30 '16 05:09 kuudihxquuwhxq

Downlevel generator support has been declined due to excessive complexity and inability to achieve 100% feature parity. Notes at #10307

RyanCavanaugh avatar Sep 30 '16 21:09 RyanCavanaugh

For anyone looking for a solution, here is how I solved it.

I set target: "es6" in tsconfig compilerOptions. This is all I do for dev, it works fine for me in chrome 52, but I needed to set module: "commonjs" as well because chrome complained about import statements. Then for production I just pass the output through babel.

For example I'm using webpack so my webpack.config.js looks like this:

const typescript_loader = process.env.NODE_ENV === 'production' ?
  'babel-loader?presets[]=es2015!ts-loader' :
  'ts-loader';

// lines omitted

  module: {
    loaders: [
      // other loaders
      { test: /\.tsx?$/, loader: typescript_loader },
    ],
    preloaders: { ... },
  }

// rest omitted

kuudihxquuwhxq avatar Oct 01 '16 00:10 kuudihxquuwhxq

This was sad news for the part of react/redux community that have adopted redux-saga for side-effect where generator functions are essential.

@nhjk I think regenerator is a more lightweight work-around.

jonaskello avatar Oct 01 '16 07:10 jonaskello

Very sad, indeed...

@jonaskello Do you have an example of how you use regenerator with Webpack to support redux-saga?

kdalgaard avatar Oct 07 '16 13:10 kdalgaard

@kdalgaard Sorry, I have no examples for webpack close. However I know this starter is using regenerator with jspm. Maybe you can have a look and do the same for webpack.

jonaskello avatar Oct 07 '16 13:10 jonaskello

If async await landed in TS 2.1, and these are using generators, how come that generators are not possible to include? Thanks!

tomitrescak avatar Dec 12 '16 07:12 tomitrescak

Generator support should be available in the next release. see https://github.com/Microsoft/TypeScript/issues/1564.

mhegazy avatar Dec 12 '16 18:12 mhegazy