Skip to content

Clarify JSX/React WhiteSpace rules #40

Closed

Description

@basarat

Based on the following test cases:

var p = 0;
// Emit "   "
<div>   </div>;
// Emit "  ", p, "   "
<div>  {p}    </div>;
// Emit only p
<div>  
      {p}    
      </div>;

// Emit only p
<div>
  {p}
    </div>;

// Emit "  3"
<div>  3  
  </div>;

// Emit "  3  "
<div>  3  </div>;

// Emit "3"
<div>   
   3    
   </div>;

// Emit no args
<div>   
   </div>;

// Emit "foo" + ' ' + "bar"
<div>  

   foo

 bar   

  </div>;

// Emit "foo  " + "bar" + "  baz"
<div>foo  <span>bar</span>  baz</div>

// Emit "foo  bar"
<div>foo        bar</div>;

For JSXText (https://facebook.github.io/jsx/) I've drawn the following set of rules:

  1. Trims whitespace in the following cases (mentioned node might be a parent or a sibling):
    • leading whitespace if this is the first node on the same line
    • trailing whitespace if this is the last node on the same line
  2. Inserts a single whitespace if the next sibling is also JSXText and not on the same line
  3. Any whitespace tabs (alt09) is replaced with a single space demo

Am I missing something ... and is this already documented elsewhere (specifically 2 wasn't mentioned explicitly anywhere I could find). Need this information to help with TypeScript/TSX integration 🌹

I know this is specific to React Emit for JSX but this seemed like the more appropriate repo 😃

PS : I've seen http://facebook.github.io/react/blog/2014/02/20/react-v0.9.html#jsx-whitespace

Activity

  1. syranide commented on Aug 20, 2015

    @syranide
    Contributor

    react/react#480 (comment), there is also the full technical reasoning react/react#480, but it may be a bit hard to digest.

    The idea being that indentation is not intended for output and trailing whitespace is a bad idea all-around. 2 is added because that is almost universally what you want and not having it is really really cumbersome and mistake-prone.

  2. basarat commented on Aug 21, 2015

    @basarat
    Author

    @syranide reading that I can only see that there is one more rule regarding tab handling. I've updated the original question with point 3, and also see demo

    Cheers 🌹

  3. RReverser commented on Aug 21, 2015

    @RReverser
    Contributor

    This question is being raised rather often, I'm thinking maybe we should add some kind of react.md in this repo?

  4. basarat commented on Aug 24, 2015

    @basarat
    Author

    I'm thinking maybe we should add some kind of react.md in this repo?

    I would prefer whitespace.md. Even though the thing I am discussing feels react emit specific, but I feel that not having whitespace rules specified in jsx decreases its portability.

    Consider:

    <div> 
    test
    </div>

    Would be weird if something things see it as test (like react) and other's see it as \ntest\n. But react.md is fine as well :)

  5. syranide commented on Aug 24, 2015

    @syranide
    Contributor

    @basarat "other's see it as \ntest\n", which ones are that?

  6. basarat commented on Aug 24, 2015

    @basarat
    Author

    "other's see it as \ntest\n", which ones are that?

    Other implementations that might use JSX for an HTML emit

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ Proxy