tree-sitter / tree-sitter-typescript

TypeScript grammar for tree-sitter
MIT License
335 stars 104 forks source link

Parse error on function parameter type signature within member signature in interface #236

Closed patrickt closed 5 months ago

patrickt commented 1 year ago

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

export interface EntityOptions {
    orderBy: ((object: any) => String);
}

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

The output of tree-sitter parse is the following:

(program [0, 0] - [3, 0]
  (export_statement [0, 0] - [2, 1]
    declaration: (interface_declaration [0, 7] - [2, 1]
      name: (type_identifier [0, 17] - [0, 30])
      body: (object_type [0, 31] - [2, 1]
        (property_signature [1, 4] - [1, 38]
          name: (property_identifier [1, 4] - [1, 11])
          type: (type_annotation [1, 11] - [1, 38]
            (parenthesized_type [1, 13] - [1, 38]
              (parenthesized_type [1, 14] - [1, 27]
                (predefined_type [1, 15] - [1, 21])
                (ERROR [1, 21] - [1, 26]))
              (ERROR [1, 28] - [1, 37]
                (identifier [1, 31] - [1, 37])))))))))
/tmp/go.ts      0 ms    (ERROR [1, 21] - [1, 26])

Removing the object: any type annotation fixes it.

casr commented 1 year ago

Looks like the parser is recognising the object part as predefined_type before matching it as a required_parameter. If you rename object to something else like obj it will start to work.

Looking at the TypeScript parser, it seems it is okay with using object as a parameter name.

(btw, you probably don't want to use things like String as a type in this context)