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

@babel/plugin-transform-block-scoping breaks when scope.crawl() is used by another plugin #11057

Open
AriPerkkio opened this issue Jan 26, 2020 · 1 comment

Comments

@AriPerkkio
Copy link

@AriPerkkio AriPerkkio commented Jan 26, 2020

Bug Report

Hello, I've found some compatibility issues between @babel/plugin-transform-block-scoping and two community plugins (babel-plugin-transform-react-remove-prop-types, babel-plugin-remove-debug). The common thing between these two plugins is that they are using scope.crawl() method. When I comment out that method from babel-plugin-transform-react-remove-prop-types the bug seems to disappear. I've read parts of the babel-handbook but didn't find documentation for the crawl method. What does it do?

I've prepared a minimal repository for reproducing this issue with custom plugin where scope.crawl() is called.

{
    "plugins": [
        "@babel/plugin-transform-block-scoping",
        "./babel-plugin-tester"
    ]
}

Note that @babel/plugin-transform-block-scoping is included in preset-env, which is used by create-react-app. In my case this created a bug into production code which was not present in development and test code. In my opinion this is quite dangerous, especially if apps are missing proper integration tests. Developers can't trust their unit tests and development mode functionality.

I'm just getting to know babel's implementation so let's leave this unchecked for now.

  • I would like to work on a fix!

Current Behavior

This code block is transformed incorrectly:

// Input
case 'B': {
    const { unicorn } = action;
    const unicorns = state.unicorns.map(_unicorn =>
        _unicorn.id === unicorn.id ? unicorn : _unicorn
    );

    return { ...state, unicorns, someCaseBAttributes: true };
}

Note how var { unicorn: _unicorn } = action; and map(_unicorn have conflicting variable names.

// Output
case 'B': {
    var { unicorn: _unicorn } = action;

    var _unicorns = state.unicorns.map(_unicorn =>
        _unicorn.id === _unicorn.id ? _unicorn : _unicorn
    );

    return { ...state, unicorns: _unicorns, someCaseBAttributes: true };
}

When used with webpack this makes terser go crazy:

case 'B':
    var { unicorn: _unicorn } = action,
        _unicorns = state.unicorns.map(
            _unicorn => (_unicorn.id, _unicorn.id, _unicorn)
        );
    return { ...state, unicorns: _unicorns, someCaseBAttributes: !0 };

Input Code

$ yarn # Install deps
$ yarn babel # Runs babel on index.js
$ yarn terser # Runs terser on babel-output.js
$ yarn test # Runs test case against original index.js and dist/terser-output.js

Expected behavior/code

Variable names should not conflict. When scope.crawl() is not called, output is as expected:

case 'B': {
    var { unicorn: _unicorn2 } = action;

    var _unicorns = state.unicorns.map(_unicorn =>
        _unicorn.id === _unicorn2.id ? _unicorn2 : _unicorn
    );

    return { ...state, unicorns: _unicorns, someCaseBAttributes: true };
}

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

Any configuration with @babel/plugin-transform-block-scoping and other plugin using scope.crawl(), such as create-react-app's babel-preset-react-app. Here are three examples:

Environment

  System:
    OS: Linux 4.19 Debian GNU/Linux 9 (stretch) 9 (stretch)
  Binaries:
    Node: 13.7.0 - /usr/bin/node
    Yarn: 1.21.1 - /usr/bin/yarn
    npm: 6.13.6 - /usr/bin/npm
  npmPackages:
    @babel/cli: ^7.8.3 => 7.8.3 
    @babel/core: ^7.8.3 => 7.8.3 
    @babel/plugin-transform-block-scoping: ^7.8.3 => 7.8.3 

Possible Solution

I'm not familiar with scope.crawl() method, but either plugins should not call it, or @babel/plugin-transform-block-scoping should not break when it's called.

Additional context/Screenshots
Original issues:

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Jan 26, 2020

Hey @AriPerkkio! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

AriPerkkio added a commit to AriPerkkio/babel that referenced this issue Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.