diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 5fb6b330b42..50d6d596198 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -52,6 +52,10 @@ namespace ts.formatting { public SpaceBeforeCloseBrace: Rule; public NoSpaceBetweenEmptyBraceBrackets: Rule; + // No space after { and before } in JSX expression + public NoSpaceAfterOpenBraceInJsxExpression: Rule; + public NoSpaceBeforeCloseBraceInJsxExpression: Rule; + // Insert new line after { and before } in multi-line contexts. public NewLineAfterOpenBraceInBlockContext: Rule; @@ -276,6 +280,10 @@ namespace ts.formatting { this.SpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSingleLineBlockContext), RuleAction.Space)); this.NoSpaceBetweenEmptyBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsObjectContext), RuleAction.Delete)); + // No space after { and before } in JSX expression + this.NoSpaceAfterOpenBraceInJsxExpression = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isJsxExpressionContext), RuleAction.Delete)); + this.NoSpaceBeforeCloseBraceInJsxExpression = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isJsxExpressionContext), RuleAction.Delete)); + // Insert new line after { and before } in multi-line contexts. this.NewLineAfterOpenBraceInBlockContext = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsMultilineBlockContext), RuleAction.NewLine)); @@ -395,6 +403,7 @@ namespace ts.formatting { this.SpaceAfterSubtractWhenFollowedByUnaryMinus, this.SpaceAfterSubtractWhenFollowedByPredecrement, this.NoSpaceAfterCloseBrace, this.SpaceAfterOpenBrace, this.SpaceBeforeCloseBrace, this.NewLineBeforeCloseBraceInBlockContext, + this.NoSpaceAfterOpenBraceInJsxExpression, this.NoSpaceBeforeCloseBraceInJsxExpression, this.SpaceAfterCloseBrace, this.SpaceBetweenCloseBraceAndElse, this.SpaceBetweenCloseBraceAndWhile, this.NoSpaceBetweenEmptyBraceBrackets, this.NoSpaceBetweenFunctionKeywordAndStar, this.SpaceAfterStarInGeneratorDeclaration, this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember, @@ -727,6 +736,10 @@ namespace ts.formatting { return context.contextNode.kind !== SyntaxKind.JsxElement; } + static isJsxExpressionContext(context: FormattingContext): boolean { + return context.contextNode.kind === SyntaxKind.JsxExpression; + } + static IsNotBeforeBlockInFunctionDeclarationContext(context: FormattingContext): boolean { return !Rules.IsFunctionDeclContext(context) && !Rules.IsBeforeBlockContext(context); } diff --git a/tests/cases/fourslash/formattingJsxElements.ts b/tests/cases/fourslash/formattingJsxElements.ts index 50e9bb8ea8f..e07149960db 100644 --- a/tests/cases/fourslash/formattingJsxElements.ts +++ b/tests/cases/fourslash/formattingJsxElements.ts @@ -66,10 +66,12 @@ /////*containedClosingTagAutoformat*/ ////; //// -////
,{integer}
;/*commaInJsxElement*/ -////
, {integer}
;/*commaInJsxElement2*/ +////
,{integer}
;/*commaInJsxElement*/ +////
, {integer}
;/*commaInJsxElement2*/ ////);/*closingParenInJsxElement*/ ////) ;/*closingParenInJsxElement2*/ +////;/*jsxExpressionSpaces*/ +////;/*jsxExpressionSpaces2*/ format.document(); goTo.marker("autoformat"); @@ -139,4 +141,8 @@ verify.currentLineContentIs("
, {integer}
;"); goTo.marker("closingParenInJsxElement"); verify.currentLineContentIs(");"); goTo.marker("closingParenInJsxElement2"); -verify.currentLineContentIs(") ;"); \ No newline at end of file +verify.currentLineContentIs(") ;"); +goTo.marker("jsxExpressionSpaces"); +verify.currentLineContentIs(";"); +goTo.marker("jsxExpressionSpaces2"); +verify.currentLineContentIs(";"); \ No newline at end of file