-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fixing JSX/TSX closing tag/attribute/expression formatting #4398
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
Changes from 4 commits
67878c9
b8cbfcb
2677b3f
eae30fd
060828c
56fc7ec
92e3b3b
c49c68d
74ef1b2
8bf79a2
e387f69
593503f
9e3ee5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,7 +360,9 @@ namespace ts.formatting { | |
range: TextRange, | ||
inheritedIndentation: number): number { | ||
|
||
if (rangeOverlapsWithStartEnd(range, startPos, endPos)) { | ||
if (rangeOverlapsWithStartEnd(range, startPos, endPos) || | ||
rangeContainsStartEnd(range, startPos, endPos) /* Not to miss zero-range nodes e.g. JsxText */) { | ||
|
||
if (inheritedIndentation !== Constants.Unknown) { | ||
return inheritedIndentation; | ||
} | ||
|
@@ -387,7 +389,10 @@ namespace ts.formatting { | |
|
||
let indentation = inheritedIndentation; | ||
if (indentation === Constants.Unknown) { | ||
if (isSomeBlock(node.kind)) { | ||
if (isIndentPreventedChildNode(parent.kind, node.kind)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would inline the body of isIndentPreventedChildNode, if (parent.kind === SyntaxKind.JsxElement && child.kind === SyntaxKind.JsxClosingElement) it is more readable this way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is similar to closing braces and parens, maybe we should add it to get indentationForToken. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possibly move getIndentationForToken to a new helper, call it in getIndentationForToken, and from here and add support for jsx elements in it as well.. possibly give the new helper a better name as getIndentationForNode. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mhegazy I currently have no idea so I just inlined it. I agree that the closing node is similar to closing tokens but I think nodes and tokens are being processed with some differences by different functions so a merged helper may not really simplify the code. |
||
indentation = parentDynamicIndentation.getIndentation(); | ||
} | ||
else if (isSomeBlock(node.kind)) { | ||
// blocks should be indented in | ||
// - other blocks | ||
// - source file | ||
|
@@ -1016,6 +1021,14 @@ namespace ts.formatting { | |
return false; | ||
} | ||
|
||
function isIndentPreventedChildNode(parent: SyntaxKind, child: SyntaxKind) { | ||
switch (parent) { | ||
case SyntaxKind.JsxElement: { | ||
return child === SyntaxKind.JsxClosingElement; | ||
} | ||
} | ||
} | ||
|
||
function getOpenTokenForList(node: Node, list: Node[]) { | ||
switch (node.kind) { | ||
case SyntaxKind.Constructor: | ||
|
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.
text can not be zero-length, is there an example that would trigger this?
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.
@mhegazy I've found that this code blocks autoformatting:
<span>
won't be indented here because by some reason a zero-length JsxText node right before JsxElement node incorrectly sets indentation value to zero. An additionalrangeContainsStartEnd
check prevents this bug.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.
looks like a parser bug then.
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.
Maybe related to #4332?
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.
#4596 didn't fix this zero-length problem. CC: @vladima