@@ -35,9 +35,13 @@ module ts.formatting {
3535 // Space after keyword but not before ; or : or ?
3636 public NoSpaceBeforeSemicolon : Rule ;
3737 public NoSpaceBeforeColon : Rule ;
38- public NoSpaceBeforeQMark : Rule ;
38+ public NoSpaceBeforeQuestionMark : Rule ;
3939 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+
4145 public SpaceAfterSemicolon : Rule ;
4246
4347 // Space/new line after }.
@@ -215,9 +219,10 @@ module ts.formatting {
215219 // Space after keyword but not before ; or : or ?
216220 this . NoSpaceBeforeSemicolon = new Rule ( RuleDescriptor . create2 ( Shared . TokenRange . Any , SyntaxKind . SemicolonToken ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext ) , RuleAction . Delete ) ) ;
217221 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 ) ) ;
219223 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 ) ) ;
221226 this . SpaceAfterSemicolon = new Rule ( RuleDescriptor . create3 ( SyntaxKind . SemicolonToken , Shared . TokenRange . Any ) , RuleOperation . create2 ( new RuleOperationContext ( Rules . IsSameLineTokenContext ) , RuleAction . Space ) ) ;
222227
223228 // Space after }.
@@ -341,7 +346,8 @@ module ts.formatting {
341346 this . HighPriorityCommonRules =
342347 [
343348 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 ,
345351 this . NoSpaceBeforeDot , this . NoSpaceAfterDot ,
346352 this . NoSpaceAfterUnaryPrefixOperator ,
347353 this . NoSpaceAfterUnaryPreincrementOperator , this . NoSpaceAfterUnaryPredecrementOperator ,
@@ -475,6 +481,10 @@ module ts.formatting {
475481 return ! Rules . IsBinaryOpContext ( context ) ;
476482 }
477483
484+ static IsConditionalOperatorContext ( context : FormattingContext ) : boolean {
485+ return context . contextNode . kind === SyntaxKind . ConditionalExpression ;
486+ }
487+
478488 static IsSameLineTokenOrBeforeMultilineBlockContext ( context : FormattingContext ) : boolean {
479489 //// This check is mainly used inside SpaceBeforeOpenBraceInControl and SpaceBeforeOpenBraceInFunction.
480490 ////
0 commit comments