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

Same variables in switch/case is corrupts code #12148

Closed
mixalbl4-127 opened this issue Oct 6, 2020 · 3 comments · Fixed by #12530
Closed

Same variables in switch/case is corrupts code #12148

mixalbl4-127 opened this issue Oct 6, 2020 · 3 comments · Fixed by #12530
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@mixalbl4-127
Copy link

BEFORE YOU SUBMIT please read the following:

I'm submitting a bug report

Webpack Version:
4.44.2 - latest

Babel Core Version:
7.11.6 - latest

Babel Loader Version:
8.1.0 - latest

Please tell us about your environment:
Linux Mint 18.3

Current behavior:
index.js

window.test = () => {
    const e = "TEST";
    switch (e) {
        case "TEST":
            const e = [];
            e.push('111');
            alert(e);
            break;
        default: alert('666'); break;
    }
};

converts to:
dist/out.js

    window.test = function () {
        switch (e) {
            case"TEST":
                var e = [];
                e.push("111"), alert(e);
                break;
            default:
                alert("666")
        }
    }

const e = "TEST"; was removed!

Expected/desired behavior:
since this variable is used in the switch, it should not be removed

webpack.config.js

const path = require("path");

module.exports = {
    entry: { main: './index.js' },
    module: {
        rules: [
            {
                oneOf: [
                    {
                        test: /\.js$/,
                        loader: require.resolve("babel-loader"),
                        options: {
                            presets: ['@babel/preset-env']
                        }
                    }
                ]
            }
        ]
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'out.js'
    },
};
@nicolo-ribaudo
Copy link
Member

I get this output: it's different, but still broken (it should be switch (e)):

window.test = function () {
  var e = "TEST";

  switch (_e) {
    case "TEST":
      var _e = [];

      _e.push('111');

      alert(_e);
      break;

    default:
      alert('666');
      break;
  }
};

@nicolo-ribaudo nicolo-ribaudo transferred this issue from babel/babel-loader Oct 6, 2020
@babel-bot
Copy link
Collaborator

Hey @mixalbl4-127! 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."

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Mar 23, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants