Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,13 @@ namespace ts.formatting {
public SpaceBetweenTagAndTemplateString: Rule;
public NoSpaceBetweenTagAndTemplateString: Rule;

// Union type
// Type operation
public SpaceBeforeBar: Rule;
public NoSpaceBeforeBar: Rule;
public SpaceAfterBar: Rule;
public NoSpaceAfterBar: Rule;
public SpaceBeforeAmpersand: Rule;
public SpaceAfterAmpersand: Rule;

constructor() {
///
Expand Down Expand Up @@ -394,12 +396,13 @@ namespace ts.formatting {
this.SpaceBetweenTagAndTemplateString = new Rule(RuleDescriptor.create3(SyntaxKind.Identifier, Shared.TokenRange.FromTokens([SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.NoSpaceBetweenTagAndTemplateString = new Rule(RuleDescriptor.create3(SyntaxKind.Identifier, Shared.TokenRange.FromTokens([SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));

// union type
// type operation
this.SpaceBeforeBar = new Rule(RuleDescriptor.create3(SyntaxKind.BarToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.NoSpaceBeforeBar = new Rule(RuleDescriptor.create3(SyntaxKind.BarToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.SpaceAfterBar = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.BarToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.NoSpaceAfterBar = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.BarToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));

this.SpaceBeforeAmpersand = new Rule(RuleDescriptor.create3(SyntaxKind.AmpersandToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.SpaceAfterAmpersand = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.AmpersandToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));

// These rules are higher in priority than user-configurable rules.
this.HighPriorityCommonRules =
Expand Down Expand Up @@ -432,6 +435,7 @@ namespace ts.formatting {
this.SpaceAfterTypeKeyword, this.NoSpaceAfterTypeKeyword,
this.SpaceBetweenTagAndTemplateString, this.NoSpaceBetweenTagAndTemplateString,
this.SpaceBeforeBar, this.NoSpaceBeforeBar, this.SpaceAfterBar, this.NoSpaceAfterBar,
this.SpaceBeforeAmpersand, this.SpaceAfterAmpersand,

// TypeScript-specific rules
this.NoSpaceAfterConstructor, this.NoSpaceAfterModuleImport,
Expand Down
1 change: 0 additions & 1 deletion src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ namespace ts.formatting {
case SyntaxKind.Parameter:
case SyntaxKind.FunctionType:
case SyntaxKind.ConstructorType:
case SyntaxKind.UnionType:
case SyntaxKind.ParenthesizedType:
Copy link
Contributor

Choose a reason for hiding this comment

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

do I understand correctly, this is removed to avoid double indentation for cases like the one below?

type a = string
    | number;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed it to prevent this:

type a = 
    string |
        number |
        boolean;

... to match normal bar operator behavior:

let x = 
    3 | 
    4 |
    5;

case SyntaxKind.TaggedTemplateExpression:
case SyntaxKind.AwaitExpression:
Expand Down
26 changes: 26 additions & 0 deletions tests/cases/fourslash/formatTypeOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference path="fourslash.ts"/>

////type Union = number | {}/*formatBarOperator*/
/////*indent*/
////|string/*autoformat*/
////type Intersection = Foo & Bar;/*formatAmpersandOperator*/
////type Complexed =
//// Foo&
//// Bar|/*unionTypeNoIndent*/
//// Baz;/*intersectionTypeNoIndent*/

format.document();

goTo.marker("formatBarOperator");
verify.currentLineContentIs("type Union = number | {}");
goTo.marker("indent");
verify.indentationIs(4);
goTo.marker("autoformat");
verify.currentLineContentIs(" | string");
goTo.marker("formatAmpersandOperator");
verify.currentLineContentIs("type Intersection = Foo & Bar;");

goTo.marker("unionTypeNoIndent");
verify.currentLineContentIs(" Bar |");
goTo.marker("intersectionTypeNoIndent");
verify.currentLineContentIs(" Baz;");
14 changes: 0 additions & 14 deletions tests/cases/fourslash/formatTypeUnion.ts

This file was deleted.