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

Lexical declaration cannot appear in a single-statement context #399

Open
oljbo opened this issue Jul 13, 2024 · 1 comment
Open

Lexical declaration cannot appear in a single-statement context #399

oljbo opened this issue Jul 13, 2024 · 1 comment

Comments

@oljbo
Copy link

oljbo commented Jul 13, 2024

c# code is:

var jscode = @"
function fun(item){
    if (createmode == true) {
        let a = 'a';
        var b = 'b';
        if(a && b != null ) {
            let Maker = 'c';
            let obj = {a:a,Closed:1,Closer:Maker,CloseDate:new Date()};
            let rejust= null;
        }
    }
}

var createmode = true;
fun(null); 
";
var s1 = Uglify.Js(jscode, new NUglify.JavaScript.CodeSettings() { }).Code;

ths result is:
function fun(){if(createmode==!0){let t="a";var n="b";if(t&&n!=null)let n={a:t,Closed:1,Closer:"c",CloseDate:new Date}}}var createmode=!0;fun(null)

when I run the result in brower, It's has error:
SyntaxError: Lexical declaration cannot appear in a single-statement context

@trullock
Copy link
Owner

The issue is its outputting

if(predicate)
    let x = something

which is invalid.

As a workaround you can try,

  1. use var if possible, but beware scope hoisting
  2. the above code doesnt really make sense anyway, youre declaring something but not using it. You can probably avoid this issue just by refactoring a bit

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

No branches or pull requests

2 participants