Skip to content

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

Closed

Description

@babel-bot

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

Activity

  1. babel-bot commented on Jan 8, 2016

    @babel-bot
    CollaboratorAuthor

    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

  2. babel-bot commented on Jan 8, 2016

    @babel-bot
    CollaboratorAuthor

    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

  3. babel-bot commented on Mar 7, 2016

    @babel-bot
    CollaboratorAuthor

    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.
  4. babel-bot commented on Mar 13, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @loganfsmyth

    Ended up going with #2.

    #3423

  5. loganfsmyth commented on Sep 24, 2016

    @loganfsmyth
    Member

    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.

  6. matildayipan commented on Oct 4, 2016

    @matildayipan

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

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

    still have the same error

  7. christensson commented on Oct 14, 2016

    @christensson

    @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"
          }
        ]
      ]
    }
    
  8. develar commented on Oct 19, 2016

    @develar
  9. alexeyraspopov commented on Nov 19, 2016

    @alexeyraspopov

    Any updates on this?

  10. tnunes commented on Jan 16, 2017

    @tnunes

    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.

  11. MikeKovarik commented on Feb 23, 2017

    @MikeKovarik

    +1

  12. uscengineer commented on Mar 9, 2017

    @uscengineer

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

    async visit(category) {
      await BaseClass.prototype.visit(category)
    }
    
  13. xtuc commented on Apr 26, 2017

    @xtuc
    Member
  14. julien-f commented on Jun 20, 2017

    @julien-f

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

    Thanks :)

  15. hzoo commented on Jun 20, 2017

    @hzoo
    Member

    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)

  16. julien-f commented on Jun 20, 2017

    @julien-f

    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.

  17. added a commit that references this issue on Dec 13, 2017
  18. devongovett commented on Feb 12, 2018

    @devongovett
    Contributor

    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.

  19. added
    outdated
    on May 14, 2018
  20. locked as resolved and limited conversation to collaborators on May 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has PRoutdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions