Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSXText node trims whitespace too often #325

Closed
jackschu opened this issue Jun 28, 2024 · 0 comments · Fixed by #328
Closed

JSXText node trims whitespace too often #325

jackschu opened this issue Jun 28, 2024 · 0 comments · Fixed by #328
Labels

Comments

@jackschu
Copy link
Contributor

jackschu commented Jun 28, 2024

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

<div> <div/></div>

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:

https://www.typescriptlang.org/play/?#code/DwEwlgbgfABKkHorAeaQ

The output of tree-sitter parse is the following:

program [0, 0] - [1, 0]
  expression_statement [0, 0] - [0, 18]
    jsx_element [0, 0] - [0, 18]
      open_tag: jsx_opening_element [0, 0] - [0, 5]
        name: identifier [0, 1] - [0, 4]
      jsx_self_closing_element [0, 6] - [0, 12]
        name: identifier [0, 7] - [0, 10]
      close_tag: jsx_closing_element [0, 12] - [0, 18]
        name: identifier [0, 14] - [0, 17]

This is wrong because there is no jsx_text node
but as seen in the ts playground link tsc will output

"use strict";
React.createElement("div", null,
    " ",
    React.createElement("div", null));

Note that " " will end up inside a dom node, so I think there should be a jsx_text node here.

This is not limited to an empty string ie

<div> foo </div>

will result in a dom node with the text " foo " , so if I query for 'is there a jsx_text node with text === ' foo ' tree-sitter-javascript will tell me 'no' but i think the answer should be 'yes'


Advice / pointers

There is a lot of un-specified nuance here. Ie the whitespace rules are not part of the JSX spec (facebook/jsx#143), but are implemented by React (facebook/jsx#40)

Basically, i think the jsx_text node should grow to encapsulate the rules around whitespace rather than trimming always.

Relevant: #227

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant