Skip to content

Commit 378c6b5

Browse files
committed
format jsx expression
1 parent 9dfcb44 commit 378c6b5

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/services/formatting/rules.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ namespace ts.formatting {
5252
public SpaceBeforeCloseBrace: Rule;
5353
public NoSpaceBetweenEmptyBraceBrackets: Rule;
5454

55+
// No space after { and before } in JSX expression
56+
public NoSpaceAfterOpenBraceInJsxExpression: Rule;
57+
public NoSpaceBeforeCloseBraceInJsxExpression: Rule;
58+
5559
// Insert new line after { and before } in multi-line contexts.
5660
public NewLineAfterOpenBraceInBlockContext: Rule;
5761

@@ -276,6 +280,10 @@ namespace ts.formatting {
276280
this.SpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSingleLineBlockContext), RuleAction.Space));
277281
this.NoSpaceBetweenEmptyBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsObjectContext), RuleAction.Delete));
278282

283+
// No space after { and before } in JSX expression
284+
this.NoSpaceAfterOpenBraceInJsxExpression = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isJsxExpressionContext), RuleAction.Delete));
285+
this.NoSpaceBeforeCloseBraceInJsxExpression = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isJsxExpressionContext), RuleAction.Delete));
286+
279287
// Insert new line after { and before } in multi-line contexts.
280288
this.NewLineAfterOpenBraceInBlockContext = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsMultilineBlockContext), RuleAction.NewLine));
281289

@@ -395,6 +403,7 @@ namespace ts.formatting {
395403
this.SpaceAfterSubtractWhenFollowedByUnaryMinus, this.SpaceAfterSubtractWhenFollowedByPredecrement,
396404
this.NoSpaceAfterCloseBrace,
397405
this.SpaceAfterOpenBrace, this.SpaceBeforeCloseBrace, this.NewLineBeforeCloseBraceInBlockContext,
406+
this.NoSpaceAfterOpenBraceInJsxExpression, this.NoSpaceBeforeCloseBraceInJsxExpression,
398407
this.SpaceAfterCloseBrace, this.SpaceBetweenCloseBraceAndElse, this.SpaceBetweenCloseBraceAndWhile, this.NoSpaceBetweenEmptyBraceBrackets,
399408
this.NoSpaceBetweenFunctionKeywordAndStar, this.SpaceAfterStarInGeneratorDeclaration,
400409
this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember,
@@ -727,6 +736,10 @@ namespace ts.formatting {
727736
return context.contextNode.kind !== SyntaxKind.JsxElement;
728737
}
729738

739+
static isJsxExpressionContext(context: FormattingContext): boolean {
740+
return context.contextNode.kind === SyntaxKind.JsxExpression;
741+
}
742+
730743
static IsNotBeforeBlockInFunctionDeclarationContext(context: FormattingContext): boolean {
731744
return !Rules.IsFunctionDeclContext(context) && !Rules.IsBeforeBlockContext(context);
732745
}

tests/cases/fourslash/formattingJsxElements.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@
6666
////</span>/*containedClosingTagAutoformat*/
6767
////</h5>;
6868
////
69-
////<div>,{integer}</div>;/*commaInJsxElement*/
70-
////<div>, {integer}</div>;/*commaInJsxElement2*/
69+
////<div>,{integer}</div>;/*commaInJsxElement*/
70+
////<div>, {integer}</div>;/*commaInJsxElement2*/
7171
////<span>)</span>;/*closingParenInJsxElement*/
7272
////<span>) </span>;/*closingParenInJsxElement2*/
73+
////<Router routes={ 3 } />;/*jsxExpressionSpaces*/
74+
////<Router routes={ (3) } />;/*jsxExpressionSpaces2*/
7375

7476
format.document();
7577
goTo.marker("autoformat");
@@ -139,4 +141,8 @@ verify.currentLineContentIs("<div>, {integer}</div>;");
139141
goTo.marker("closingParenInJsxElement");
140142
verify.currentLineContentIs("<span>)</span>;");
141143
goTo.marker("closingParenInJsxElement2");
142-
verify.currentLineContentIs("<span>) </span>;");
144+
verify.currentLineContentIs("<span>) </span>;");
145+
goTo.marker("jsxExpressionSpaces");
146+
verify.currentLineContentIs("<Router routes={3} />;");
147+
goTo.marker("jsxExpressionSpaces2");
148+
verify.currentLineContentIs("<Router routes={(3)} />;");

0 commit comments

Comments
 (0)