tree-sitter-typescript icon indicating copy to clipboard operation
tree-sitter-typescript copied to clipboard

Confuses method signature and definition

Open sguillia opened this issue 3 years ago • 3 comments

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

class Vec {
	constructor()
	// comment
	{ }
}

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript: https://www.typescriptlang.org/play?#code/MYGwhgzhAEBqCmxoG8BQBIYB7AdhALgE4Cuw+WhAFAJQYD0d02Ats-DvhstAL6p9A

The output of tree-sitter parse is the following (on commit 11f8f151327e99361c1ff6764599daeef8633615):

(program [0, 0] - [5, 0]
  (class_declaration [0, 0] - [4, 1]
    name: (type_identifier [0, 6] - [0, 9])
    body: (class_body [0, 10] - [4, 1]
      (method_definition [1, 1] - [3, 4]
        name: (property_identifier [1, 1] - [1, 12])
        parameters: (formal_parameters [1, 12] - [1, 14])
        (comment [2, 1] - [2, 11])
        body: (statement_block [3, 1] - [3, 4])))))

I ran tree-sitter parse with the following versions (tree-sitter --version):

tree-sitter 0.19.0 (889dcef50b11919e9d2b27b708503c29cfa46e12)
tree-sitter 0.20.0 (e85a279cf29da1b08648e27214dda20a841e57c8)

Capture du 2021-10-28 11-01-57

I am not sure why tree-sitter parse does not show ERROR, but the tree-sitter playground and my application do find an error node

I tried with both node and wasm bindings, both give error nodes.

sguillia avatar Oct 28 '21 12:10 sguillia

thanks for the report!

mjambon avatar Oct 29 '21 02:10 mjambon

Just found out the error has nothing to do with the comment

It seems to confuse method signatures and definitions.

class Vec {
	method() { } // METHOD_DEFINITION
}

class Vec {
	method()     // METHOD_SIGNATURE
	{ }          // ERROR
}

I tried adding optional($._automatic_semicolon) after this line, but I don't know how to resolve the induced conflict

Running prettier on those files fixes the issue, except if there is a comment between the method name and body.

sguillia avatar Nov 06 '21 14:11 sguillia

This was fixed in https://github.com/tree-sitter/tree-sitter-typescript/issues/140 and the tests are still there

https://github.com/tree-sitter/tree-sitter-typescript/blob/54931b39ae7b197e20c7b06680ceaf576b18ec69/common/corpus/declarations.txt#L1002-L1004

Plus the examples work in the playground (https://tree-sitter.github.io/tree-sitter/playground).

Are you sure this isn't coming from an outdated version of tree-sitter? Perhaps the playground wasn't updated yet when you first tried.

resolritter avatar Jan 08 '22 05:01 resolritter