-
Notifications
You must be signed in to change notification settings - Fork 48.8k
Improved whitespace collapsing rules #459
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
Conversation
@@ -74,7 +74,7 @@ function visitReactTag(traverse, object, path, state) { | |||
move(object.name.range[1], state); | |||
|
|||
var childrenToRender = object.children.filter(function(child) { | |||
return !(child.type === Syntax.Literal && !child.value.match(/\S/)); | |||
return !(child.type === Syntax.Literal && child.value.match(/^[ \t\xA0]*[\r\n][ \t\xA0\r\n]*$/)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
80char line width plz
Instead of using browser sniffing, `getUnboundedScrollPosition` can do better and not have to depend on the `getDocumentScrollElement` module.
added explanation of the rules and annotated the regexps removed \xA0 from being treated as regular whitespace (or should it?)
Redid the example for a much more understandable view of the problem, and my solution to it. @jeffmo code has been cleaned up slightly and the rules clarified more thoroughly. |
Would you mind rebasing on top of the following commit so it's a little easier for me to patch this and play with it? |
fixed line lengths, all under 80 except one slightly longer regex cleaned up the code a bit added explanation of the rules and annotated the regexps removed \xA0 from being treated as regular whitespace (or should it?) moved helper function outside of renderXJSLiteral updated "S \n S" rule to produce "S\nS" instead
@jeffmo Commits rebased (shout if I messed it up) |
…ewline to a single space
@@ -1,27 +0,0 @@ | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, looks like something funky happened in your rebase maybe? This file was removed in 0dc011c
@jeffmo Yeah. Reject this one and I'll create a fresh pull request instead? |
I propose the following stricter and more well-defined rules for collapsing and removing whitespace, and have provided a patch to do it:
\t ABC \n
→\t ABC
ABC \n DEF \n
→ABC\nDEF \n
* The start and end of the string relates to
<tag>this plain text in the middle here{expr}
Special-cases for whitespace between text literals
Rule 2, variation A:
A \n B
→A\nB
(current)A{' '}\nB
→A B
Rule 2, variation B:
A \n B
→A B
A{'\n'}\nB
→A\nB
Rule 3, variation A:
A \t B
→A B
{' \t '}
Rule 3, variation B:
A \t B
→A \t B
(current, so no rule 3)There are a few possible variations of these as well, but they very rarely make sense. So overall I feel like the above rules are the most intuitive, consistent and quite flexible.
I've switched to using Rule 3B instead (feel free to object), it doesn't make a difference for normal rendering, but allows almost all types of markdown to be written inline, it doesn't require being explicit like Rule 3A, but it is much more flexible when it comes to special inline text uses (white-space: pre, markdown, etc), with virtually no negatives.
Example:
What JSXTransformer produces:
In my opinion, a lot of unnecessary and unintentional whitespace is produced simply because the code is arranged into multiple lines for readability. This may sometimes produce unintentional "padding" in the resulting elements.
{'A'} {'B'}
does also not produceA B
as one would expect.What my patch produces:
It produces whitespace according to well-defined and hopefully intuitive rules. It also mirrors the original code structure as closely as possible. Note that
\n
renders identically to space unless otherwise specified with CSS.