@@ -35,9 +35,13 @@ module ts.formatting {
35
35
// Space after keyword but not before ; or : or ?
36
36
public NoSpaceBeforeSemicolon : Rule ;
37
37
public NoSpaceBeforeColon : Rule ;
38
- public NoSpaceBeforeQMark : Rule ;
38
+ public NoSpaceBeforeQuestionMark : Rule ;
39
39
public SpaceAfterColon : Rule ;
40
- public SpaceAfterQMark : Rule ;
40
+ // insert space after '?' only when it is used in conditional operator
41
+ public SpaceAfterQuestionMarkInConditionalOperator : Rule ;
42
+ // in other cases there should be no space between '?' and next token
43
+ public NoSpaceAfterQuestionMark : Rule ;
44
+
41
45
public SpaceAfterSemicolon : Rule ;
42
46
43
47
// Space/new line after }.
@@ -215,9 +219,10 @@ module ts.formatting {
215
219
// Space after keyword but not before ; or : or ?
216
220
this . NoSpaceBeforeSemicolon = new Rule ( RuleDescriptor . create2 ( Shared . TokenRange . Any , SyntaxKind . SemicolonToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext ) , RuleAction . Delete ) ) ;
217
221
this . NoSpaceBeforeColon = new Rule ( RuleDescriptor . create2 ( Shared . TokenRange . Any , SyntaxKind . ColonToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext , Rules . IsNotBinaryOpContext ) , RuleAction . Delete ) ) ;
218
- this . NoSpaceBeforeQMark = new Rule ( RuleDescriptor . create2 ( Shared . TokenRange . Any , SyntaxKind . QuestionToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext , Rules . IsNotBinaryOpContext ) , RuleAction . Delete ) ) ;
222
+ this . NoSpaceBeforeQuestionMark = new Rule ( RuleDescriptor . create2 ( Shared . TokenRange . Any , SyntaxKind . QuestionToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext , Rules . IsNotBinaryOpContext ) , RuleAction . Delete ) ) ;
219
223
this . SpaceAfterColon = new Rule ( RuleDescriptor . create3 ( SyntaxKind . ColonToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext , Rules . IsNotBinaryOpContext ) , RuleAction . Space ) ) ;
220
- this . SpaceAfterQMark = new Rule ( RuleDescriptor . create3 ( SyntaxKind . QuestionToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext , Rules . IsNotBinaryOpContext ) , RuleAction . Space ) ) ;
224
+ this . SpaceAfterQuestionMarkInConditionalOperator = new Rule ( RuleDescriptor . create3 ( SyntaxKind . QuestionToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext , Rules . IsConditionalOperatorContext ) , RuleAction . Space ) ) ;
225
+ this . NoSpaceAfterQuestionMark = new Rule ( RuleDescriptor . create3 ( SyntaxKind . QuestionToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext ) , RuleAction . Delete ) ) ;
221
226
this . SpaceAfterSemicolon = new Rule ( RuleDescriptor . create3 ( SyntaxKind . SemicolonToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext ) , RuleAction . Space ) ) ;
222
227
223
228
// Space after }.
@@ -341,7 +346,8 @@ module ts.formatting {
341
346
this . HighPriorityCommonRules =
342
347
[
343
348
this . IgnoreBeforeComment , this . IgnoreAfterLineComment ,
344
- this . NoSpaceBeforeColon , this . SpaceAfterColon , this . NoSpaceBeforeQMark , this . SpaceAfterQMark ,
349
+ this . NoSpaceBeforeColon , this . SpaceAfterColon , this . NoSpaceBeforeQuestionMark , this . SpaceAfterQuestionMarkInConditionalOperator ,
350
+ this . NoSpaceAfterQuestionMark ,
345
351
this . NoSpaceBeforeDot , this . NoSpaceAfterDot ,
346
352
this . NoSpaceAfterUnaryPrefixOperator ,
347
353
this . NoSpaceAfterUnaryPreincrementOperator , this . NoSpaceAfterUnaryPredecrementOperator ,
@@ -475,6 +481,10 @@ module ts.formatting {
475
481
return ! Rules . IsBinaryOpContext ( context ) ;
476
482
}
477
483
484
+ static IsConditionalOperatorContext ( context : FormattingContext ) : boolean {
485
+ return context . contextNode . kind === SyntaxKind . ConditionalExpression ;
486
+ }
487
+
478
488
static IsSameLineTokenOrBeforeMultilineBlockContext ( context : FormattingContext ) : boolean {
479
489
//// This check is mainly used inside SpaceBeforeOpenBraceInControl and SpaceBeforeOpenBraceInFunction.
480
490
////
0 commit comments