tree-sitter / tree-sitter-typescript

TypeScript grammar for tree-sitter
MIT License
361 stars 108 forks source link

fix: 🐛 template-literal-type with infer type #188

Closed IWANABETHATGUY closed 2 years ago

IWANABETHATGUY commented 2 years ago

Checklist:

IWANABETHATGUY commented 2 years ago

assume we have file

type space = ' ' | '\t' | '\n'
type Trim<S extends string> = S extends `${space}${infer R}` ? Trim<R> : S extends `${infer B}${space}` ? Trim<B> : S

master:

(program [0, 0] - [2, 0]
  (type_alias_declaration [0, 0] - [0, 30]
    name: (type_identifier [0, 5] - [0, 10])
    value: (union_type [0, 13] - [0, 30]
      (union_type [0, 13] - [0, 23]
        (literal_type [0, 13] - [0, 16]
          (string [0, 13] - [0, 16]
            (string_fragment [0, 14] - [0, 15])))
        (literal_type [0, 19] - [0, 23]
          (string [0, 19] - [0, 23]
            (escape_sequence [0, 20] - [0, 22]))))
      (literal_type [0, 26] - [0, 30]
        (string [0, 26] - [0, 30]
          (escape_sequence [0, 27] - [0, 29])))))
  (type_alias_declaration [1, 0] - [1, 117]
    name: (type_identifier [1, 5] - [1, 9])
    type_parameters: (type_parameters [1, 9] - [1, 27]
      (type_parameter [1, 10] - [1, 26]
        name: (type_identifier [1, 10] - [1, 11])
        constraint: (constraint [1, 12] - [1, 26]
          (predefined_type [1, 20] - [1, 26]))))
    value: (conditional_type [1, 30] - [1, 117]
      left: (type_identifier [1, 30] - [1, 31])
      right: (template_literal_type [1, 40] - [1, 103]
        (template_type [1, 41] - [1, 49]
          (type_identifier [1, 43] - [1, 48]))
        (template_type [1, 49] - [1, 102]
          (ERROR [1, 51] - [1, 96]
            (infer_type [1, 51] - [1, 58]
              (type_identifier [1, 57] - [1, 58]))
            (template_literal_type [1, 59] - [1, 84])
            (infer_type [1, 86] - [1, 93]
              (type_identifier [1, 92] - [1, 93])))
          (type_identifier [1, 96] - [1, 101])))
      consequence: (generic_type [1, 106] - [1, 113]
        name: (type_identifier [1, 106] - [1, 110])
        type_arguments: (type_arguments [1, 110] - [1, 113]
          (type_identifier [1, 111] - [1, 112])))
      alternative: (type_identifier [1, 116] - [1, 117]))))
./test.ts       0 ms    (ERROR [1, 51] - [1, 96])

current:

(program [0, 0] - [2, 0]
  (type_alias_declaration [0, 0] - [0, 30]
    name: (type_identifier [0, 5] - [0, 10])
    value: (union_type [0, 13] - [0, 30]
      (union_type [0, 13] - [0, 23]
        (literal_type [0, 13] - [0, 16]
          (string [0, 13] - [0, 16]
            (string_fragment [0, 14] - [0, 15])))
        (literal_type [0, 19] - [0, 23]
          (string [0, 19] - [0, 23]
            (escape_sequence [0, 20] - [0, 22]))))
      (literal_type [0, 26] - [0, 30]
        (string [0, 26] - [0, 30]
          (escape_sequence [0, 27] - [0, 29])))))
  (type_alias_declaration [1, 0] - [1, 117]
    name: (type_identifier [1, 5] - [1, 9])
    type_parameters: (type_parameters [1, 9] - [1, 27]
      (type_parameter [1, 10] - [1, 26]
        name: (type_identifier [1, 10] - [1, 11])
        constraint: (constraint [1, 12] - [1, 26]
          (predefined_type [1, 20] - [1, 26]))))
    value: (conditional_type [1, 30] - [1, 117]
      left: (conditional_type [1, 30] - [1, 74]
        left: (type_identifier [1, 30] - [1, 31])
        right: (template_literal_type [1, 40] - [1, 60]
          (template_type [1, 41] - [1, 49]
            (type_identifier [1, 43] - [1, 48]))
          (template_type [1, 49] - [1, 59]
            (infer_type [1, 51] - [1, 58]
              (type_identifier [1, 57] - [1, 58]))))
        consequence: (generic_type [1, 63] - [1, 70]
          name: (type_identifier [1, 63] - [1, 67])
          type_arguments: (type_arguments [1, 67] - [1, 70]
            (type_identifier [1, 68] - [1, 69])))
        alternative: (type_identifier [1, 73] - [1, 74]))
      right: (template_literal_type [1, 83] - [1, 103]
        (template_type [1, 84] - [1, 94]
          (infer_type [1, 86] - [1, 93]
            (type_identifier [1, 92] - [1, 93])))
        (template_type [1, 94] - [1, 102]
          (type_identifier [1, 96] - [1, 101])))
      consequence: (generic_type [1, 106] - [1, 113]
        name: (type_identifier [1, 106] - [1, 110])
        type_arguments: (type_arguments [1, 110] - [1, 113]
          (type_identifier [1, 111] - [1, 112])))
      alternative: (type_identifier [1, 116] - [1, 117]))))
mjambon commented 2 years ago

thank you!