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

Inline comments can result in mis-parsed return, break, and continue statements #324

Closed
jackschu opened this issue Jun 18, 2024 · 1 comment
Labels

Comments

@jackschu
Copy link
Contributor

The following piece of code is valid but it is parsed incorrectly:

function foo() {
    return /**/ 1;
}

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABMOcAUBKRBvAUIgxAJwFMoQikB6AKhqsQEYBuXAXyA

The output of tree-sitter parse is the following:

(program [0, 0] - [3, 0]
  (function_declaration [0, 0] - [2, 1]
    name: (identifier [0, 9] - [0, 12])
    parameters: (formal_parameters [0, 12] - [0, 14])
    body: (statement_block [0, 15] - [2, 1]
      (return_statement [1, 4] - [1, 10])
      (comment [1, 11] - [1, 15])
      (expression_statement [1, 16] - [1, 18]
        (number [1, 16] - [1, 17])))))

Notice that the expression_statement which points to the text 1 is not a child of the return_statement.

This gets especially weird for the parent's bounding box as in this example the if_statement is selected but is not known to include the return value
image

This seems to also affect break and continue statements capturing their labels
ie bar here should be considered a label as a statement_identifier but isnt and instead is an expression_statement

function foo() {
	while (true) {
      if(true)
      break /**/ bar
    }
}

It seems like what's happening here is: If you have a statement in a statement block that can end early (ie it ends with optionals). Then an in-line comment can trick the parser into thinking the statement has ended early and leave the now-unaccounted-for optional suffix to be considered distinct expressions statements.

@jackschu jackschu added the bug label Jun 18, 2024
@amaanq
Copy link
Member

amaanq commented Jul 5, 2024

I believe #326 fixed this, thank you!

@amaanq amaanq closed this as completed Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants