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

`jsx_text` should ignore blank spaces

Open drybalka opened this issue 3 years ago • 1 comments

Consider the following simple code:

<div>
  <div/>
</div>

In my opinion the outer div element node should have only one (named) child besides the opening/closing tags - the inner div. And it is indeed so in the html tree-sitter parser:

fragment [0, 0] - [3, 0]
  element [0, 0] - [2, 6]
    start_tag [0, 0] - [0, 5]
      tag_name [0, 1] - [0, 4]
    element [1, 1] - [1, 7]
      self_closing_tag [1, 1] - [1, 7]
        tag_name [1, 2] - [1, 5]
    end_tag [2, 0] - [2, 6]
      tag_name [2, 2] - [2, 5]

However, the javascript parser produces also 2 extra jsx_text nodes, which span newline and indentation, but are otherwise just blank space:

program [0, 0] - [3, 0]
  expression_statement [0, 0] - [2, 6]
    jsx_element [0, 0] - [2, 6]
      open_tag: jsx_opening_element [0, 0] - [0, 5]
        name: identifier [0, 1] - [0, 4]
      jsx_text [0, 5] - [1, 1]
      jsx_self_closing_element [1, 1] - [1, 7]
        name: identifier [1, 2] - [1, 5]
      jsx_text [1, 7] - [2, 0]
      close_tag: jsx_closing_element [2, 0] - [2, 6]
        name: identifier [2, 2] - [2, 5]

This is not a problem for highlighting (cause there is nothing to highlight), but for other use-cases this is quite confusing. I would really prefer if this blank space were ignored and jsx_text was generated only for non-empty text, similar to how it is done in html parser.

drybalka avatar May 18 '22 08:05 drybalka

Good point; I agree.

maxbrunsfeld avatar May 18 '22 17:05 maxbrunsfeld