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

With no transforms enabled, top-level arrow functions get transformed (T7419) #4337

Closed
babel-bot opened this issue Jun 9, 2016 · 6 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@babel-bot
Copy link
Collaborator

Issue originally made by @spicyj

Bug information

  • Babel version: 6.7.7
  • Node version: 6
  • npm version: 3.8.6

Options

babel.transform(src, {plugins: []}).code

Input code

var x = () => this;
(function() { var y = () => this; });

Description

If you have no transforms enabled, top-level arrow functions still get transformed by the shadow-functions internal plugin. For the input code above, you get this output:

var _this = this;

var x = () => _this;
(function () {
  var y = () => this;
});

Note that the top-level arrow function x is transformed (bad) but y is not (good).

Repros in the repl at http://babeljs.io/repl/#?evaluate=false&lineWrap=false&presets=stage-3&experimental=true&loose=false&spec=false&playground=false&code=var%20x%20%3D%20()%20%3D%3E%20this%3B%0A(function()%20%7B%20var%20x%20%3D%20()%20%3D%3E%20this%3B%20%7D)%3B.

@babel-bot
Copy link
Collaborator Author

Comment originally made by @spicyj

Actually, I can reproduce the problem I'm seeing in a less contrived example too, using passPerPreset.

Run this in the REPL in the babel dir:

console.log(require('./packages/babel-core').transform('class C { x = () => this; }', {
  presets: {
    plugins: [
      './packages/babel-plugin-transform-class-properties',
      './packages/babel-plugin-transform-es2015-classes',
      './packages/babel-plugin-transform-es2015-arrow-functions'
    ]
  },
  passPerPreset: true
}).code)

You get:

var _this = this;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

let C = function C() {
  _classCallCheck(this, C);

  this.x = function () {
    return _this;
  };
};

Note that the _this binding is incorrect in the transformed arrow function. If you disable passPerPreset, you get this, which is correct:

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

let C = function C() {
  var _this = this;

  _classCallCheck(this, C);

  this.x = function () {
    return _this;
  };
};

This happens because the first plugins pass has no plugins to operate on so it only uses the INTERNAL_PLUGINS list.

@fab1an
Copy link

fab1an commented Sep 10, 2016

I'm running into this problem as well and it breaks my scope in es6-classes. I'm running a plugin that adds class-properties before they get transformed.

Simple test, copied from #4230:

.babelrc:

{
    plugins: [
        'babel-plugin-syntax-class-properties'
    ]
}

Input:

class Test {
    onPress = () => {
        console.log(this.id)
    }
}

Output:

var _this = this;

class Test {
    onPress = () => {
        console.log(_this.id);
    };
}

@motiz88
Copy link
Contributor

motiz88 commented Sep 11, 2016

Regarding this and #4230 - Would a PR special-casing class property initializers in shadow-functions be welcome? @spicyj / @hzoo? (Update - Here it is #4502 😄)

motiz88 added a commit to motiz88/babel-plugin-transform-hoist-nested-functions that referenced this issue Sep 11, 2016
…ue`)

See README.md for documentation.

This feature is blocked on the following Babel PRs/issues:
* babel/babel#4500
* babel/babylon#121
* babel/babel#4337
* babel/babel#4230 (partially)
@hzoo
Copy link
Member

hzoo commented Oct 22, 2016

Was closed by #4502

@hzoo hzoo closed this as completed Oct 22, 2016
@sophiebits
Copy link
Contributor

Neat, happy to have a workaround. I would argue that the current code is still wrong though since Babel shouldn't change your code at all if you have no transforms enabled, and it still does get rid of arrows in other contexts.

@hzoo
Copy link
Member

hzoo commented Oct 23, 2016

Yeah I guess it's because that shadow-function transform is always on as an internal transform - should look into that.

motiz88 added a commit to motiz88/babel-plugin-transform-hoist-nested-functions that referenced this issue Apr 2, 2017
…ue`)

See README.md for documentation.

This feature is blocked on the following Babel PRs/issues:
* babel/babel#4500
* babel/babylon#121
* babel/babel#4337
* babel/babel#4230 (partially)
motiz88 added a commit to motiz88/babel-plugin-transform-hoist-nested-functions that referenced this issue Apr 2, 2017
…ue`)

See README.md for documentation.

This feature is blocked on the following Babel PRs/issues:
* babel/babel#4500
* babel/babylon#121
* babel/babel#4337
* babel/babel#4230 (partially)
motiz88 added a commit to motiz88/babel-plugin-transform-hoist-nested-functions that referenced this issue Apr 2, 2017
…ue`)

See README.md for documentation.

This feature is blocked on the following Babel PRs/issues:
* babel/babel#4500
* babel/babylon#121
* babel/babel#4337
* babel/babel#4230 (partially)
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

5 participants