@@ -225,6 +225,12 @@ namespace ts.formatting {
225
225
public NoSpaceBeforeTemplateMiddleAndTail : Rule ;
226
226
public SpaceBeforeTemplateMiddleAndTail : Rule ;
227
227
228
+ // No space after { and before } in JSX expression
229
+ public NoSpaceAfterOpenBraceInJsxExpression : Rule ;
230
+ public SpaceAfterOpenBraceInJsxExpression : Rule ;
231
+ public NoSpaceBeforeCloseBraceInJsxExpression : Rule ;
232
+ public SpaceBeforeCloseBraceInJsxExpression : Rule ;
233
+
228
234
constructor ( ) {
229
235
///
230
236
/// Common Rules
@@ -316,7 +322,7 @@ namespace ts.formatting {
316
322
317
323
// Add a space between statements. All keywords except (do,else,case) has open/close parens after them.
318
324
// 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 ) ) ;
325
+ 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 ) ) ;
320
326
321
327
// This low-pri rule takes care of "try {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter.
322
328
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 +450,8 @@ namespace ts.formatting {
444
450
///
445
451
446
452
// 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 ) ) ;
453
+ this . SpaceAfterComma = new Rule ( RuleDescriptor . create3 ( SyntaxKind . CommaToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext , Rules . isNonJsxElementContext , Rules . IsNextTokenNotCloseBracket ) , RuleAction . Space ) ) ;
454
+ this . NoSpaceAfterComma = new Rule ( RuleDescriptor . create3 ( SyntaxKind . CommaToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext , Rules . isNonJsxElementContext ) , RuleAction . Delete ) ) ;
449
455
450
456
// Insert space before and after binary operators
451
457
this . SpaceBeforeBinaryOperator = new Rule ( RuleDescriptor . create4 ( Shared . TokenRange . Any , Shared . TokenRange . BinaryOperators ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext , Rules . IsBinaryOpContext ) , RuleAction . Space ) ) ;
@@ -491,6 +497,12 @@ namespace ts.formatting {
491
497
this . NoSpaceBeforeTemplateMiddleAndTail = new Rule ( RuleDescriptor . create4 ( Shared . TokenRange . Any , Shared . TokenRange . FromTokens ( [ SyntaxKind . TemplateMiddle , SyntaxKind . TemplateTail ] ) ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext ) , RuleAction . Delete ) ) ;
492
498
this . SpaceBeforeTemplateMiddleAndTail = new Rule ( RuleDescriptor . create4 ( Shared . TokenRange . Any , Shared . TokenRange . FromTokens ( [ SyntaxKind . TemplateMiddle , SyntaxKind . TemplateTail ] ) ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext ) , RuleAction . Space ) ) ;
493
499
500
+ // No space after { and before } in JSX expression
501
+ this . NoSpaceAfterOpenBraceInJsxExpression = new Rule ( RuleDescriptor . create3 ( SyntaxKind . OpenBraceToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext , Rules . isJsxExpressionContext ) , RuleAction . Delete ) ) ;
502
+ this . SpaceAfterOpenBraceInJsxExpression = new Rule ( RuleDescriptor . create3 ( SyntaxKind . OpenBraceToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext , Rules . isJsxExpressionContext ) , RuleAction . Space ) ) ;
503
+ this . NoSpaceBeforeCloseBraceInJsxExpression = new Rule ( RuleDescriptor . create2 ( Shared . TokenRange . Any , SyntaxKind . CloseBraceToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext , Rules . isJsxExpressionContext ) , RuleAction . Delete ) ) ;
504
+ this . SpaceBeforeCloseBraceInJsxExpression = new Rule ( RuleDescriptor . create2 ( Shared . TokenRange . Any , SyntaxKind . CloseBraceToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsNonJsxSameLineTokenContext , Rules . isJsxExpressionContext ) , RuleAction . Space ) ) ;
505
+
494
506
// Insert space after function keyword for anonymous functions
495
507
this . SpaceAfterAnonymousFunctionKeyword = new Rule ( RuleDescriptor . create1 ( SyntaxKind . FunctionKeyword , SyntaxKind . OpenParenToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsFunctionDeclContext ) , RuleAction . Space ) ) ;
496
508
this . NoSpaceAfterAnonymousFunctionKeyword = new Rule ( RuleDescriptor . create1 ( SyntaxKind . FunctionKeyword , SyntaxKind . OpenParenToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsFunctionDeclContext ) , RuleAction . Delete ) ) ;
@@ -729,6 +741,14 @@ namespace ts.formatting {
729
741
return context . TokensAreOnSameLine ( ) && context . contextNode . kind !== SyntaxKind . JsxText ;
730
742
}
731
743
744
+ static isNonJsxElementContext ( context : FormattingContext ) : boolean {
745
+ return context . contextNode . kind !== SyntaxKind . JsxElement ;
746
+ }
747
+
748
+ static isJsxExpressionContext ( context : FormattingContext ) : boolean {
749
+ return context . contextNode . kind === SyntaxKind . JsxExpression ;
750
+ }
751
+
732
752
static IsNotBeforeBlockInFunctionDeclarationContext ( context : FormattingContext ) : boolean {
733
753
return ! Rules . IsFunctionDeclContext ( context ) && ! Rules . IsBeforeBlockContext ( context ) ;
734
754
}
0 commit comments