question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Build produces incorrect JS for specific switch-case block

See original GitHub issue

Describe the bug

This is most probably a bug in one of the build tools used by react-scripts, or some misconfiguration. I recently ran into unexpected functionality in production build. https://github.com/AriPerkkio/asus-merlin-simple-vpn-client-setup/issues/1 The code works just fine on development mode but breaks on production build. I’ve traced the bug into a switch-case block which produces incorrect results after transpile process. I could debug this further but don’t have much time for this now. I’ve prepared a minimal reproduce branch for this, see below.

Did you try recovering your dependencies?

Yes, I’ve ran rm -rf node_modules/ yarn.lock.

Which terms did you search for in User Guide?

Read it many times, great documentation!

Environment

Environment Info:

System: OS: Linux 4.19 Debian GNU/Linux 9 (stretch) 9 (stretch) CPU: (4) x64 06/8e Binaries: Node: 13.6.0 - /usr/bin/node Yarn: 1.21.1 - /usr/bin/yarn npm: 6.13.4 - /usr/bin/npm Browsers: Chrome: Not Found Firefox: Not Found npmPackages: react: ^16.12.0 => 16.12.0 react-dom: ^16.12.0 => 16.12.0 react-scripts: 3.3.0 => 3.3.0 npmGlobalPackages: create-react-app: Not Found

Steps to reproduce

$ git clone https://github.com/AriPerkkio/asus-merlin-simple-vpn-client-setup.git
$ cd asus-merlin-simple-vpn-client-setup
$ git checkout transpile-bug
$ yarn # Install dependencies
$ yarn build # Builds minimal setup and runs prettier on it
# Check build/ui/static/js/main.<hash>.chunk.js

Expected behavior

Input:

case 'B': {
    const { key } = action;
    const keys = state.keys.map(_key =>
        _key.id === key.id ? key : _key
    );

    return { ...state, keys };
}

Unexpected output in build:

case 'B':
    t.key;
    var i = e.keys.map(function(e) {
        return e.id, e.id, e;
    });
    return c({}, e, { keys: i });

Rename _key

case 'B': {
    const { key } = action;
    const keys = state.keys.map(_someKey =>
        _someKey.id === key.id ? key : _someKey
    );

    return { ...state, keys };
}

Output is as expected:

case 'B':
    var i = t.key,
        u = e.keys.map(function(e) {
            return e.id === i.id ? i : e;
        });
    return c({}, e, { keys: u });

Actual behavior

See above.

Reproducible demo

See steps to reproduce.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
iansucommented, May 19, 2021

Can you replicate this with the latest release?

1reaction
AriPerkkiocommented, Jan 26, 2020

I found that scope.crawl() method used by babel-plugin-transform-react-remove-prop-types is causing this. By commenting it out the bug disappeared. I searched for other plugins using the same method and found babel-plugin-remove-debug. The issue can now be reproduced without babel-plugin-transform-react-remove-prop-types.

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

Bug opened to babel: https://github.com/babel/babel/issues/11057

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expression inside switch case statement - Stack Overflow
The solution where the switch expression is set to true works not because true is a constant but because equality with the case...
Read more >
Replacing switch statements with Object literals
In many programming languages, the switch statement exists - but should it any longer? If you're a JavaScript programmer, you're often ...
Read more >
JavaScript Switch Case – JS Switch Statement Example
Block Scope and Switch Statements​​ This example will produce an error message, because the message variable has already been declared and you ...
Read more >
How To Use the Switch Statement in JavaScript - DigitalOcean
The switch statement is closely related to a conditional statement containing many else if blocks, and they can often be used interchangeably.
Read more >
The "switch" statement - The Modern JavaScript Tutorial
It gives a more descriptive way to compare a value with multiple variants. The syntax. The switch has one or more case blocks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found