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

Transpiling ES6 generators into a ES5 state machine? #3975

Closed
d180cf opened this issue Jul 22, 2015 · 14 comments
Closed

Transpiling ES6 generators into a ES5 state machine? #3975

d180cf opened this issue Jul 22, 2015 · 14 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@d180cf
Copy link

d180cf commented Jul 22, 2015

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.

@basarat
Copy link
Contributor

basarat commented Jul 22, 2015

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 :#1664

@d180cf
Copy link
Author

d180cf commented Jul 22, 2015

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.

@DanielRosenwasser
Copy link
Member

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.

@danquirk danquirk added the Question An issue which isn't directly actionable in code label Jul 22, 2015
@danquirk
Copy link
Member

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

@oliverjanik
Copy link

Will this be implemented? why is this issue closed?

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Question An issue which isn't directly actionable in code labels Feb 11, 2016
@mhegazy mhegazy reopened this Feb 11, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Feb 11, 2016

Please note, this is gated on #5595

@kuudihxquuwhxq
Copy link

kuudihxquuwhxq commented Sep 30, 2016

Any progress on this? #5595 has been resolved.

@RyanCavanaugh RyanCavanaugh added Too Complex An issue which adding support for may be too complex for the value it adds and removed In Discussion Not yet reached consensus labels Sep 30, 2016
@RyanCavanaugh
Copy link
Member

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

@kuudihxquuwhxq
Copy link

kuudihxquuwhxq commented Oct 1, 2016

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

@jonaskello
Copy link

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.

@kdalgaard
Copy link

kdalgaard commented Oct 7, 2016

Very sad, indeed...

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

@jonaskello
Copy link

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

@tomitrescak
Copy link

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

@mhegazy
Copy link
Contributor

mhegazy commented Dec 12, 2016

Generator support should be available in the next release. see #1564.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests