Skip to content

Regexp/division after ]} not properly disambiguated #22

Open

Description

@nicolo-ribaudo

Consider these two inputs:

void {
    x: []
} / "/ //" /*

/*/

module.exports.foo = 2;

// */
{
    x: []
} / "/ //" /*

/*/

module.exports.foo = 2;

// */

In the first one it's an object divided by the string "/ //", followed by a comment and by the expression module.exports.foo = 2;.

In the second one it's a block containing a labeled statement (x: []), then there is a regular expression (/ "/), and inline comment and a block comment.

However, it returns exports: [ 'foo' ] for both the examples.

(Sorry for those /*//// */ patterns, but I need them to write "dual mode" code to toggle between code and comments 😅)

Activity

  1. guybedford commented on Oct 29, 2020

    @guybedford
    Collaborator

    Thanks for finding this one! Fix added in #23.

  2. guybedford commented on Oct 29, 2020

    @guybedford
    Collaborator

    Looking at this more closely, it is effectively a class of ASI cases where we aren't detecting the } as a block when it should be.

    Some other variations:

    {}
    
    /'/.test(myString);
    
    module.exports.foo = 2;
    var p = 5
    
    {}
    
    /'/.test(myString);
    
    module.exports.foo = 2;
    var p = class {}
    
    {}
    
    /'/.test(myString);
    
    module.exports.foo = 2;

    When it comes down to it, these are all cases that require deeper parsing context to properly manage. That is, you have found the places where the heuristic approach breaks down!

    I get the feeling these are the limits of a small lexer though unfortunately.

    If specific cases come up with regards to real world code we can fix them individually though.

    The opt out at least is not getting any detection of course.

  3. added
    bugSomething isn't working
    and removed on Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions