Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ module ts.formatting {
public NoSpaceBeforeColon: Rule;
public NoSpaceBeforeQMark: Rule;
public SpaceAfterColon: Rule;
public SpaceAfterQMark: Rule;
// insert space after '?' only when it is used in conditional operator
public SpaceAfterQMarkInConditionalOperator: Rule;
// in other cases there should be no space between '?' and next token
public NoSpaceAfterQMark: Rule;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't abbreviate QuestionMark to QMark.

👍


public SpaceAfterSemicolon: Rule;

// Space/new line after }.
Expand Down Expand Up @@ -217,7 +221,8 @@ module ts.formatting {
this.NoSpaceBeforeColon = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.ColonToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Delete));
this.NoSpaceBeforeQMark = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.QuestionToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Delete));
this.SpaceAfterColon = new Rule(RuleDescriptor.create3(SyntaxKind.ColonToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Space));
this.SpaceAfterQMark = new Rule(RuleDescriptor.create3(SyntaxKind.QuestionToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Space));
this.SpaceAfterQMarkInConditionalOperator = new Rule(RuleDescriptor.create3(SyntaxKind.QuestionToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsConditionalOperatorContext), RuleAction.Space));
this.NoSpaceAfterQMark = new Rule(RuleDescriptor.create3(SyntaxKind.QuestionToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.SpaceAfterSemicolon = new Rule(RuleDescriptor.create3(SyntaxKind.SemicolonToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));

// Space after }.
Expand Down Expand Up @@ -341,7 +346,8 @@ module ts.formatting {
this.HighPriorityCommonRules =
[
this.IgnoreBeforeComment, this.IgnoreAfterLineComment,
this.NoSpaceBeforeColon, this.SpaceAfterColon, this.NoSpaceBeforeQMark, this.SpaceAfterQMark,
this.NoSpaceBeforeColon, this.SpaceAfterColon, this.NoSpaceBeforeQMark, this.SpaceAfterQMarkInConditionalOperator,
this.NoSpaceAfterQMark,
this.NoSpaceBeforeDot, this.NoSpaceAfterDot,
this.NoSpaceAfterUnaryPrefixOperator,
this.NoSpaceAfterUnaryPreincrementOperator, this.NoSpaceAfterUnaryPredecrementOperator,
Expand Down Expand Up @@ -475,6 +481,10 @@ module ts.formatting {
return !Rules.IsBinaryOpContext(context);
}

static IsConditionalOperatorContext(context: FormattingContext): boolean {
return context.contextNode.kind === SyntaxKind.ConditionalExpression;
}

static IsSameLineTokenOrBeforeMultilineBlockContext(context: FormattingContext): boolean {
//// This check is mainly used inside SpaceBeforeOpenBraceInControl and SpaceBeforeOpenBraceInFunction.
////
Expand Down
6 changes: 6 additions & 0 deletions tests/cases/fourslash/formattingConditionalOperator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference path='fourslash.ts'/>

////var x=true?1:2
format.document();
goTo.bof();
verify.currentLineContentIs("var x = true ? 1 : 2");;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;; not that it really matters

12 changes: 12 additions & 0 deletions tests/cases/fourslash/formattingQMark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts'/>

////interface A {
/////*1*/ foo? ();
/////*2*/ foo? <T>();
////}

format.document();
goTo.marker("1");
verify.currentLineContentIs(" foo?();");
goTo.marker("2");
verify.currentLineContentIs(" foo?<T>();");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/genericsFormatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ goTo.marker("inNewSignature");
verify.currentLineContentIs(" new <T>(a: T);");

goTo.marker("inOptionalMethodSignature");
verify.currentLineContentIs(" op? <T, M>(a: T, b: M);");
verify.currentLineContentIs(" op?<T, M>(a: T, b: M);");