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

Declaring same variable inside switch causes outer one to drop #5787

Closed
harai opened this issue Feb 12, 2023 · 2 comments · Fixed by #5826
Closed

Declaring same variable inside switch causes outer one to drop #5787

harai opened this issue Feb 12, 2023 · 2 comments · Fixed by #5826
Labels

Comments

@harai
Copy link

harai commented Feb 12, 2023

Declaring the same variable name inside switch causes the outer one to drop when uglified with unused=true option.

Uglify version (uglifyjs -V)

uglify-js 3.17.4

JavaScript input

(function() {
const value = 'x';
switch (value) {
  case 'x':
    const value = 'dummy';
    console.log('value is x.');
    break;
  default:
    console.log(`value is not x.`);
}
})();

The uglifyjs CLI command executed or minify() options used.

Disable most of features and set unused=true:

./uglifyjs sample.js --compress annotations=false,arguments=false,arrows=false,assignments=false,awaits=false,booleans=false,collapse_vars=false,comparisons=false,conditionals=false,dead_code=false,default_values=false,directives=false,evaluate=false,functions=false,hoist_exports=false,hoist_props=false,if_return=false,imports=false,inline=false,join_vars=false,loops=false,merge_vars=false,negate_iife=false,objects=false,properties=false,pure_getters=false,reduce_funcs=false,reduce_vars=false,rests=false,sequences=false,side_effects=false,spreads=false,strings=false,switches=false,templates=false,typeofs=false,unused=true,varify=false,yields=false --keep-fnames --keep-fargs -b --output sample.unused-true.min.js

JavaScript output or error produced.

$ cat sample.unused-true.min.js 
(function() {
    switch (value) {
      case "x":
        const value = "dummy";
        console.log("value is x.");
        break;

      default:
        console.log(`value is not x.`);
    }
})();

$ node sample.unused-true.min.js
/home/jharai/Sandbox/minifyjs/sample.unused-true.min.js:2
    switch (value) {
            ^

ReferenceError: value is not defined
    at /home/jharai/Sandbox/minifyjs/sample.unused-true.min.js:2:13
    at Object.<anonymous> (/home/jharai/Sandbox/minifyjs/sample.unused-true.min.js:11:3)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
@alexlamsl alexlamsl added the bug label Jun 3, 2024
@alexlamsl
Copy link
Collaborator

Thanks for the detailed report − investigating.

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Jun 5, 2024
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Jun 5, 2024
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Jun 5, 2024
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Jun 5, 2024
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Jun 5, 2024
alexlamsl added a commit that referenced this issue Jun 5, 2024
@alexlamsl
Copy link
Collaborator

Patch released in uglify-js@3.18.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants