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

Fix lexing of regex at the start of a if/loop #2529

Merged
merged 1 commit into from
Jul 10, 2024

Commits on Jul 10, 2024

  1. Fix lexing of regex at the start of a if/loop

    This only really appears in code (espruino/BangleApps#3498) which is minified to something like:
    ```javascript
    var b=E.getPowerUsage(),
      c=E.getBattery(),
      a=0;
    
    for(var d in b.device)
      /^(LCD|LED)/.test(d) || (a += b.device[d]);
    ```
    
    ... resulting in:
    
    ```
    Uncaught SyntaxError: Got '^' expected EOF
     at line 1 col 109 in widbattpwr.wid.js
    ...attery(),a=0;for(var d in b.device)/^(LCD|LED)/.test(d)||(a+=b.device[...
                                            ^
     at line 1 col 110 in widbattpwr.wid.js
    ...ttery(),a=0;for(var d in b.device)/^(LCD|LED)/.test(d)||(a+=b.device[d...
                                            ^
    ```
    
    So while the code runs fine normally, installing it via the proper route minifies it and leads to it being unable to run.
    
    ---
    
    This is the case for both `if` statements and loops:
    
    ```javascript
    >for(;0;)/a/;
    Uncaught SyntaxError: Got ';' expected EOF
     at line 1 col 9
    for(;0;)/a/;
            ^
     at line 1 col 12
    for(;0;)/a/;
               ^
    >for(x in [])/a/;
    Uncaught SyntaxError: Got ';' expected EOF
     at line 1 col 13
    for(x in [])/a/;
                ^
     at line 1 col 16
    for(x in [])/a/;
                   ^
    ```
    
    while & if statements:
    
    ```javascript
    >while(0)/a/;
    Uncaught SyntaxError: Got ';' expected EOF
     at line 1 col 9
    while(0)/a/;
            ^
     at line 1 col 12
    while(0)/a/;
               ^
    >if(0)/a/;
    Uncaught SyntaxError: Got '/' expected EOF
     at line 1 col 6
    if(0)/a/;
         ^
    >if(0)3;
    =undefined
    ```
    
    The issue is that we don't lex regex immediately after a close-paren, so
    the fix is quite straightforward.
    bobrippling committed Jul 10, 2024
    Configuration menu
    Copy the full SHA
    03f293d View commit details
    Browse the repository at this point in the history