profile
viewpoint

Ask questionsWrong Highlight When a Union Type Inside a Generic Starts With null

Problem

so I've found this bug in the highlighting image as you can see, if I start the generic with a null then this error happens, a good question also is if null should be highlighted as a reserved keyword, as a type, or as a value (in my color scheme types are blue , values are purple and reserved keywords are pink)

tree-sitter/tree-sitter-typescript

Answer questions mjambon

Fascinating! Thanks for reporting this.

Case 1 (wrong):

$ tree-sitter parse <(echo 'const [a, b] = c<null|string>(null);')
(program [0, 0] - [1, 0]
  (lexical_declaration [0, 0] - [0, 36]
    (variable_declarator [0, 6] - [0, 35]
      name: (array_pattern [0, 6] - [0, 12]
        (identifier [0, 7] - [0, 8])
        (identifier [0, 10] - [0, 11]))
      value: (binary_expression [0, 15] - [0, 35]
        left: (binary_expression [0, 15] - [0, 21]
          left: (identifier [0, 15] - [0, 16])
          right: (null [0, 17] - [0, 21]))
        right: (binary_expression [0, 22] - [0, 35]
          left: (identifier [0, 22] - [0, 28])
          right: (parenthesized_expression [0, 29] - [0, 35]
            (null [0, 30] - [0, 34])))))))

Case 2 (correct):

$ tree-sitter parse <(echo 'const [a, b] = c<string|null>(null);')
(program [0, 0] - [1, 0]
  (lexical_declaration [0, 0] - [0, 36]
    (variable_declarator [0, 6] - [0, 35]
      name: (array_pattern [0, 6] - [0, 12]
        (identifier [0, 7] - [0, 8])
        (identifier [0, 10] - [0, 11]))
      value: (call_expression [0, 15] - [0, 35]
        function: (identifier [0, 15] - [0, 16])
        type_arguments: (type_arguments [0, 16] - [0, 29]
          (union_type [0, 17] - [0, 28]
            (predefined_type [0, 17] - [0, 23])
            (type_identifier [0, 24] - [0, 28])))
        arguments: (arguments [0, 29] - [0, 35]
          (null [0, 30] - [0, 34]))))))

Case 1b - With further simplication, we get a parsing error (null | d):

$ tree-sitter parse <(echo 'const [a, b] = c<null | d>()')
(program [0, 0] - [1, 0]
  (ERROR [0, 0] - [0, 28]
    (array_pattern [0, 6] - [0, 12]
      (identifier [0, 7] - [0, 8])
      (identifier [0, 10] - [0, 11]))
    (binary_expression [0, 15] - [0, 21]
      left: (identifier [0, 15] - [0, 16])
      right: (null [0, 17] - [0, 21]))
    (identifier [0, 24] - [0, 25])
    (formal_parameters [0, 26] - [0, 28])))
/dev/fd/63	0 ms	(ERROR [0, 0] - [0, 28])

Case 2b - With d | null instead of null | d, the result is still correct (like in Case 2):

$ tree-sitter parse <(echo 'const [a, b] = c<d | null>()')
(program [0, 0] - [1, 0]
  (lexical_declaration [0, 0] - [0, 28]
    (variable_declarator [0, 6] - [0, 28]
      name: (array_pattern [0, 6] - [0, 12]
        (identifier [0, 7] - [0, 8])
        (identifier [0, 10] - [0, 11]))
      value: (call_expression [0, 15] - [0, 28]
        function: (identifier [0, 15] - [0, 16])
        type_arguments: (type_arguments [0, 16] - [0, 26]
          (union_type [0, 17] - [0, 25]
            (type_identifier [0, 17] - [0, 18])
            (type_identifier [0, 21] - [0, 25])))
        arguments: (arguments [0, 26] - [0, 28])))))
useful!

Related questions

No questions were found.
source:https://uonfu.com/
Github User Rank List