tree-sitter / tree-sitter-typescript

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

Unable to parse <T extends abstract new [...]> #173

Closed DontBreakAlex closed 2 years ago

DontBreakAlex commented 3 years ago

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

type ConstructorParameters<T extends abstract new (...args: any) => any> = T extends abstract new (...args: infer P) => any ? P : never;

TS Playground URL

The output of tree-sitter parse is the following:

(program [0, 0] - [1, 0]
  (type_alias_declaration [0, 0] - [0, 136]
    name: (type_identifier [0, 5] - [0, 26])
    type_parameters: (type_parameters [0, 26] - [0, 72]
      (type_parameter [0, 27] - [0, 71]
        (type_identifier [0, 27] - [0, 28])
        (constraint [0, 29] - [0, 71]
          (ERROR [0, 37] - [0, 45]
            (identifier [0, 37] - [0, 45]))
          (constructor_type [0, 46] - [0, 71]
            (formal_parameters [0, 50] - [0, 64]
              (required_parameter [0, 51] - [0, 63]
                (rest_pattern [0, 51] - [0, 58]
                  (identifier [0, 54] - [0, 58]))
                (type_annotation [0, 58] - [0, 63]
                  (predefined_type [0, 60] - [0, 63]))))
            (predefined_type [0, 68] - [0, 71])))))
    value: (conditional_type [0, 75] - [0, 135]
      left: (type_identifier [0, 75] - [0, 76])
      (ERROR [0, 85] - [0, 93]
        (identifier [0, 85] - [0, 93]))
      right: (constructor_type [0, 94] - [0, 123]
        (formal_parameters [0, 98] - [0, 116]
          (required_parameter [0, 99] - [0, 115]
            (rest_pattern [0, 99] - [0, 106]
              (identifier [0, 102] - [0, 106]))
            (type_annotation [0, 106] - [0, 115]
              (infer_type [0, 108] - [0, 115]
                (type_identifier [0, 114] - [0, 115])))))
        (predefined_type [0, 120] - [0, 123]))
      consequence: (type_identifier [0, 126] - [0, 127])
      alternative: (type_identifier [0, 130] - [0, 135]))))
file.d.ts   0 ms    (ERROR [0, 37] - [0, 45])

I'm trying to parse typescript's lib type definitions. The pattern T extends abstract new seems to be unrecognized.

mjambon commented 2 years ago

Thanks for the report. I'm unclear about how well we support declarations in .d.ts files. My own questions is: Is there any special syntax in the .d.ts files that's not part of regular .ts typescript?

We use tree-sitter-typescript in semgrep and we tend to get excellent parsing rates (99.87% lines not affected by ERROR nodes) on .ts files - which is what matters the most to us - and much lower rates on .d.ts files (not sure how much but less than 80%, possibly much closer to 0%).