Skip to content

Commit 9dfcb44

Browse files
committed
do not format comma/closeparen in jsxelement
1 parent 6e415cc commit 9dfcb44

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

src/services/formatting/rules.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ namespace ts.formatting {
316316

317317
// Add a space between statements. All keywords except (do,else,case) has open/close parens after them.
318318
// So, we have a rule to add a space for [),Any], [do,Any], [else,Any], and [case,Any]
319-
this.SpaceBetweenStatements = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.DoKeyword, SyntaxKind.ElseKeyword, SyntaxKind.CaseKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsNotForContext), RuleAction.Space));
319+
this.SpaceBetweenStatements = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.DoKeyword, SyntaxKind.ElseKeyword, SyntaxKind.CaseKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isNonJsxElementContext, Rules.IsNotForContext), RuleAction.Space));
320320

321321
// This low-pri rule takes care of "try {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter.
322322
this.SpaceAfterTryFinally = new Rule(RuleDescriptor.create2(Shared.TokenRange.FromTokens([SyntaxKind.TryKeyword, SyntaxKind.FinallyKeyword]), SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Space));
@@ -444,8 +444,8 @@ namespace ts.formatting {
444444
///
445445

446446
// Insert space after comma delimiter
447-
this.SpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsNextTokenNotCloseBracket), RuleAction.Space));
448-
this.NoSpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
447+
this.SpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isNonJsxElementContext, Rules.IsNextTokenNotCloseBracket), RuleAction.Space));
448+
this.NoSpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isNonJsxElementContext), RuleAction.Delete));
449449

450450
// Insert space before and after binary operators
451451
this.SpaceBeforeBinaryOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.BinaryOperators), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
@@ -723,6 +723,10 @@ namespace ts.formatting {
723723
return context.TokensAreOnSameLine() && context.contextNode.kind !== SyntaxKind.JsxText;
724724
}
725725

726+
static isNonJsxElementContext(context: FormattingContext): boolean {
727+
return context.contextNode.kind !== SyntaxKind.JsxElement;
728+
}
729+
726730
static IsNotBeforeBlockInFunctionDeclarationContext(context: FormattingContext): boolean {
727731
return !Rules.IsFunctionDeclContext(context) && !Rules.IsBeforeBlockContext(context);
728732
}

tests/cases/fourslash/formattingJsxElements.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//// </div>
1010
//// )
1111
////}
12-
////
12+
////
1313
////function foo1() {
1414
//// return (
1515
//// <div className="commentBox" data-id="test">
@@ -45,8 +45,8 @@
4545
//// class3= {/*5*/
4646
//// }/>/*6*/
4747
//// )
48-
////}
49-
////
48+
////}
49+
////
5050
////(function () {
5151
//// return <div
5252
////className=""/*attrAutoformat*/
@@ -64,7 +64,12 @@
6464
/////*childJsxElementIndent*/
6565
////<span></span>/*grandchildJsxElementAutoformat*/
6666
////</span>/*containedClosingTagAutoformat*/
67-
////</h5>
67+
////</h5>;
68+
////
69+
////<div>,{integer}</div>;/*commaInJsxElement*/
70+
////<div>, {integer}</div>;/*commaInJsxElement2*/
71+
////<span>)</span>;/*closingParenInJsxElement*/
72+
////<span>) </span>;/*closingParenInJsxElement2*/
6873

6974
format.document();
7075
goTo.marker("autoformat");
@@ -114,7 +119,7 @@ verify.indentationIs(12);
114119

115120
goTo.marker("danglingBracketAutoformat")
116121
// TODO: verify.currentLineContentIs(" >");
117-
verify.currentLineContentIs(" >");
122+
verify.currentLineContentIs(" >");
118123
goTo.marker("closingTagAutoformat");
119124
verify.currentLineContentIs(" </div>");
120125

@@ -125,4 +130,13 @@ verify.indentationIs(8);
125130
goTo.marker("grandchildJsxElementAutoformat");
126131
verify.currentLineContentIs(" <span></span>");
127132
goTo.marker("containedClosingTagAutoformat");
128-
verify.currentLineContentIs(" </span>");
133+
verify.currentLineContentIs(" </span>");
134+
135+
goTo.marker("commaInJsxElement");
136+
verify.currentLineContentIs("<div>,{integer}</div>;");
137+
goTo.marker("commaInJsxElement2");
138+
verify.currentLineContentIs("<div>, {integer}</div>;");
139+
goTo.marker("closingParenInJsxElement");
140+
verify.currentLineContentIs("<span>)</span>;");
141+
goTo.marker("closingParenInJsxElement2");
142+
verify.currentLineContentIs("<span>) </span>;");

0 commit comments

Comments
 (0)