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

Reformatting JSX Children #167

Open
justafish opened this issue Mar 4, 2015 · 1 comment
Open

Reformatting JSX Children #167

justafish opened this issue Mar 4, 2015 · 1 comment
Assignees

Comments

@justafish
Copy link

When parsing the following

<If cond={1 === 1}>
  <h1>OH HAI</h1>
</If>

The top level JSX element (If) has the following children:

0: "\n " Literal
1: "<h1>OH HAI</h1>" JSXElement
2: "\n " Literal

Is it safe to just simply throw away any instances of Literal "\n " and "\n" when visiting JSXElements, or is there a way to have the parser not pick these up in the first place?

@benjamn benjamn self-assigned this Mar 5, 2015
@benjamn
Copy link
Owner

benjamn commented Mar 5, 2015

JSX preserves leading/trailing whitespace as JSXText nodes because JSX is more whitespace-sensitive than most other kinds of JS syntax, and that whitespace needs to be passed as strings to the function calls that JSX is translated to.

I have a theory about what was happening here:

if (1 === 1) {

    ;

  <h1>OH HAI</h1>;


  ;
};

With the original <If> parent node stripped off, those whitespace-only JSXText nodes were ending up without a JSXElement parent, so the printer was treating them as ordinary ExpressionStatements.

So, yes, I think it's safe to throw away those string literals in this case, specifically because JSXText nodes should always have a JSXElement as their parent.

It might make sense, then, to change the printer case for JSXText to print nothing (or throw an exception) if path.getParentNode() is not a JSXElement.

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

No branches or pull requests

2 participants