Skip to content

Functions with shadowed this bindings insert this reference before super which can cause runtime or build errors (T7191) #4162

Closed

Description

@babel-bot

Issue originally made by @taion

Bug information

  • Babel version: 6.6.5
  • Node version: 5.4.0
  • npm version: 3.8.0

Options

{
  "passPerPreset": true,
  "presets": ["es2015", "stage-0"]
}

Input code

async function foo(value) {
  return value;
}

class Base {}

class Test extends Base {
  bar = async () => {
    return await foo(this.value);
  };
}

Description

EDIT: See @loganfsmyth's comment below for a simpler example

The error I get is: SyntaxError: test.js: 'this' is not allowed before super() (This is an error on an internal node. Probably an internal error).

This goes away if I specify both es2015 and stage-0 as a single pass.

I only ran into this in the first place because I was following the Relay example, which currently specifies those two as separate passes.

This could just be a configuration issue with the Relay example, but the repro seemed sufficiently niche-y that it seemed like it might be interesting.

Activity

  1. babel-bot commented on Mar 8, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @taion

    On closer inspection, I actually can't neatly work around this – the Relay plugin stops working if I try to specify es2015, stage-0, and react as a single pass.

  2. babel-bot commented on Mar 8, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @loganfsmyth

    Since pass-per-preset does just what it says, a simplified reproduction case to see the reason for this is:

    {presets: ['stage-0']}
    

    with

    class Test extends (function(){}) {
      bar = async () => {
        return await foo(this.value);
      };
    }
    

    which results in

    var Test = class Test extends function () {} {
      constructor(...args) {
        var _temp,
            _this = this;
    
        return _temp = super(...args), this.bar = _asyncToGenerator(function* () {
          return yield foo(_this.value);
        }), _temp;
      }
    
    };
    

    That _this = this line is before super(...args), hence the error.

    This is an issue with or without passPerPreset though, since this will still happen if someone were using a preset like es2015-node5 along with stage-0. They won't get a Babel error, but the native class support will error out for the same reason Babel does.

  3. babel-bot commented on Mar 8, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @taion

    Is a workaround possible? My actual .babelrc currently looks like:

    {
      "passPerPreset": true,
      "presets": [
        { "plugins": ["./tools/babelRelayPlugin"] },
        "react",
        "es2015",
        "stage-0"
      ],
    }
    

    I tried using

    {
      "passPerPreset": true,
      "presets": [
        { "plugins": ["./tools/babelRelayPlugin"] },
        { "presets": ["es2015", "stage-0", "react"] }
      ]
    }
    

    But this didn't work (the Relay plugin didn't get applied).

  4. babel-bot commented on Mar 13, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @loganfsmyth

    #3422

  5. added
    outdated
    on May 7, 2018
  6. locked as resolved and limited conversation to collaborators on May 7, 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

    outdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions