tree-sitter / tree-sitter-javascript

Javascript grammar for tree-sitter
MIT License
314 stars 108 forks source link

Newline inside class body causes incorrect parsing #262

Closed helixbass closed 8 months ago

helixbass commented 9 months ago

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

class A {
  static get
  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/MYGwhgzhAECC0G8BQ1oQC5nQS2NA5gKborQBmA9hQBQCUi0ATsQK6MB20AjANzQC+SfkA

The output of tree-sitter parse is the following:

(program [0, 0] - [4, 0]
  (class_declaration [0, 0] - [3, 1]
    name: (identifier [0, 6] - [0, 7])
    body: (class_body [0, 8] - [3, 1]
      member: (field_definition [1, 2] - [1, 12]
        property: (property_identifier [1, 9] - [1, 12]))
      member: (method_definition [2, 2] - [2, 21]
        name: (property_identifier [2, 2] - [2, 5])
        parameters: (formal_parameters [2, 5] - [2, 7])
        body: (statement_block [2, 8] - [2, 21]
          (return_statement [2, 10] - [2, 19]
            (number [2, 17] - [2, 18])))))))

It looks like tree-sitter is parsing the class body as a field definition and a separate method definition, whereas it should be parsing it as a single (static getter) method definition

I don't know much about the semicolon-insertion rules in JS or whatever but it would seem likely that this is related to that?

helixbass commented 8 months ago

I see this parsing correctly now, thanks