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

super is not working inside methods marked async (T6895) #3930

Closed
babel-bot opened this Issue Dec 28, 2015 · 17 comments

Comments

Projects
None yet
@babel-bot
Copy link
Collaborator

babel-bot commented Dec 28, 2015

Issue originally made by Dmitriy Krasnikov (dmitriy.krasnikov)

Bug information

  • Babel version: 6.3.26
  • Node version: 5.3
  • npm version: 3.3.12

Options

{
  plugins:[
    "transform-async-to-generator",
  ]
}

Input code

export class BaseClass {
  async test(id){
    console.log(id);
  }
}
export class ChildClass extends BaseClass{
  async test(id){
    id=1;
    super.test(id);
  }
}

let cls = new ChildClass();
cls.test(5);

Description

Outputs:

export class BaseClass {
  test(id) {
    return _asyncToGenerator(function* () {
      console.log(id);
    })();
  }
}
export class ChildClass extends BaseClass {
  test(id) {
    return _asyncToGenerator(function* () {
      id = 1;
      super.test(id);
    })();
  }
}

let cls = new ChildClass();
cls.test(5);
@babel-bot

This comment has been minimized.

Copy link
Collaborator

babel-bot commented Jan 8, 2016

Comment originally made by Paul Sanchez (basicdays)

I ran into this as well when upgrading from Babel 5 to 6. Seems the issue is the output is creating a generator but keeping the original super syntax. Generators don't have the ability to call super unfortunately. I'm guessing there will have to be a transformer to just transpile super references within just async methods?

I have some example input/output code here: https://gist.github.com/psanchez-alertmd/5ffa9ff5e4653d9f93d8

@babel-bot

This comment has been minimized.

Copy link
Collaborator

babel-bot commented Jan 8, 2016

Comment originally made by Paul Sanchez (basicdays)

Oh, this was created with babel 6.4.0, babel-preset-es2015-node4 2.0.2, and babel-preset-stage-3 v6.3.13. Here are the plugins it was pulling in: https://github.com/jbach/babel-preset-es2015-node4/blob/master/index.js

@babel-bot

This comment has been minimized.

Copy link
Collaborator

babel-bot commented Mar 7, 2016

Comment originally made by @loganfsmyth

This is a tough one. We've got two options I see.

  1. Add handling for this case to babel-helper-remap-async-to-generator to use babel-helper-replace-supers
  2. Add general support to the internal shadow-functions.js transform so that at the same time we remap this to point at parent scopes, we do the same for super, since super and this are tied to the same function context.
@babel-bot

This comment has been minimized.

Copy link
Collaborator

babel-bot commented Mar 13, 2016

Comment originally made by @loganfsmyth

Ended up going with #2.

#3423

@loganfsmyth

This comment has been minimized.

Copy link
Member

loganfsmyth commented Sep 24, 2016

For those coming across this, my recommendation for now would be to manually enable transform-es2015-classes which obviously isn't ideal, but does the job.

@matildayipan

This comment has been minimized.

Copy link

matildayipan commented Oct 4, 2016

I have enabled transform-es2015-classes in .babelrc:

{
  "presets": ["es2015"],
  "plugins": ["transform-es2015-classes"]
}

still have the same error

@christensson

This comment has been minimized.

Copy link

christensson commented Oct 14, 2016

@matildayipan If you are accessing super from within an async method, you still need to enable the async-function transformation:

{
  "plugins": [
    [
      "transform-es2015-classes"
    ],
    [
      "transform-async-to-module-method", {
        "module": "bluebird",
        "method": "coroutine"
      }
    ]
  ]
}
@develar

This comment has been minimized.

Copy link

develar commented Oct 19, 2016

Workaround: SuperClass.prototype.method.call(this): http://madole.xyz/babel-plugin-transform-async-to-module-method-gotcha/

@alexeyraspopov

This comment has been minimized.

Copy link

alexeyraspopov commented Nov 19, 2016

Any updates on this?

@tnunes

This comment has been minimized.

Copy link

tnunes commented Jan 16, 2017

Would also like to understand the state of this issue. Did we agree on the solution path? If so and with some general pointers, I can try to send a PR to address this.

azazel75 added a commit to metapensiero/metapensiero.pj that referenced this issue Jan 21, 2017

@MikeKovarik

This comment has been minimized.

Copy link

MikeKovarik commented Feb 23, 2017

+1

@uscengineer

This comment has been minimized.

Copy link

uscengineer commented Mar 9, 2017

@rightaway for me what worked was BaseClass.prototype.myfunction. Example:

async visit(category) {
  await BaseClass.prototype.visit(category)
}
@xtuc

This comment has been minimized.

Copy link
Member

xtuc commented Apr 26, 2017

@julien-f

This comment has been minimized.

Copy link

julien-f commented Jun 20, 2017

@loganfsmyth Has it been fixed by #5677?
Any plan to release this fix soon?

Thanks :)

@hzoo

This comment has been minimized.

Copy link
Member

hzoo commented Jun 20, 2017

You'll need to upgrade everything to v7.0.0-alpha.12 (and it might not be compatible with various packages outside of this monorepo)

@julien-f

This comment has been minimized.

Copy link

julien-f commented Jun 20, 2017

TBH, I'm a bit afraid of using this in production :/

FYI, I wanted to stop using babel-preset-stage-0 in favor of specific transforms to be able to not transpile async functions in Node when not necessary (to help with debugging). (See vatesfr/xo-server#536)
Unfortunately it breaks on Node 6.

eeve added a commit to eeve/microweb that referenced this issue Dec 13, 2017

upgrade
- 新增BaseClass类,所有Controller和Service都必须直接或间接继承此类
- 实例化Controller或Service时,第一个参数永远为app实例
- App新增mountControllers和mountServices方法,以便一次性挂载多个Controller或Service
- App提供interceptor方法,以便在router之前执行某些middleware
- BasicService类构造函数第二个Model参数由必填修改为选填,Service的Model属性可以直接在子类中设置
- Service类提供callSuper方法以便子类调用Service提供的方法,详见:babel/babel#3930
- 更新Service类find方法
- Service类新增updateByPK,deleteByPK,findByPK方法
- BasicController继承BaseClass
- params middleware当程序报错时ctx.logger输出日志
@devongovett

This comment has been minimized.

Copy link
Contributor

devongovett commented Feb 12, 2018

Just wrote a simple plugin for Babel 6 to work around this issue here: https://github.com/devongovett/babel-plugin-transform-async-super. It only compiles super expressions inside async functions, so you could use it when you aren't already compiling classes to ES5 (e.g. Node 6).

If you want to make it part of preset-env as a temporary fix for users on Babel 6, I'd be happy to contribute it. It's not ideal that preset-env fails to generate working code when targeting Node 6 at the moment, so I think it would be great to either back port the fix from Babel 7, or add something like the above plugin to preset-env when compiling async functions but not classes.

4000D added a commit to Onther-Tech/tokyo-solidity-template that referenced this issue Mar 4, 2018

@lock lock bot added the outdated label May 14, 2018

@lock lock bot locked as resolved and limited conversation to collaborators May 14, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.