8000 format intersection type · predever/TypeScript@dfde9e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfde9e4

Browse files
committed
format intersection type
1 parent 9e0949b commit dfde9e4

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

src/services/formatting/rules.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,13 @@ namespace ts.formatting {
227227
public SpaceBetweenTagAndTemplateString: Rule;
228228
public NoSpaceBetweenTagAndTemplateString: Rule;
229229

230-
// Union type
230+
// Type operation
231231
public SpaceBeforeBar: Rule;
232232
public NoSpaceBeforeBar: Rule;
233233
public SpaceAfterBar: Rule;
234234
public NoSpaceAfterBar: Rule;
235+
public SpaceBeforeAmpersand: Rule;
236+
public SpaceAfterAmpersand: Rule;
235237

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

397-
// union type
399+
// type operation
398400
this.SpaceBeforeBar = new Rule(RuleDescriptor.create3(SyntaxKind.BarToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
399401
this.NoSpaceBeforeBar = new Rule(RuleDescriptor.create3(SyntaxKind.BarToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
400402
this.SpaceAfterBar = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.BarToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
401403
this.NoSpaceAfterBar = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.BarToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
402-
404+
this.SpaceBeforeAmpersand = new Rule(RuleDescriptor.create3(SyntaxKind.AmpersandToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
405+
this.SpaceAfterAmpersand = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.AmpersandToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
403406

404407
// These rules are higher in priority than user-configurable rules.
405408
this.HighPriorityCommonRules =
@@ -432,6 +435,7 @@ namespace ts.formatting {
432435
this.SpaceAfterTypeKeyword, this.NoSpaceAfterTypeKeyword,
433436
this.SpaceBetweenTagAndTemplateString, this.NoSpaceBetweenTagAndTemplateString,
434437
this.SpaceBeforeBar, this.NoSpaceBeforeBar, this.SpaceAfterBar, this.NoSpaceAfterBar,
438+
this.SpaceBeforeAmpersand, this.SpaceAfterAmpersand,
435439

436440
// TypeScript-specific rules
437441
this.NoSpaceAfterConstructor, this.NoSpaceAfterModuleImport,

src/services/formatting/smartIndenter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ namespace ts.formatting {
436436
case SyntaxKind.Parameter:
437437
case SyntaxKind.FunctionType:
438438
case SyntaxKind.ConstructorType:
439-
case SyntaxKind.UnionType:
440439
case SyntaxKind.ParenthesizedType:
441440
case SyntaxKind.TaggedTemplateExpression:
442441
case SyntaxKind.AwaitExpression:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////type Union = number | {}/*formatBarOperator*/
4+
/////*indent*/
5+
////|string/*autoformat*/
6+
////type Intersection = Foo & Bar;/*formatAmpersandOperator*/
7+
////type Complexed =
8+
//// Foo&
9+
//// Bar|/*unionTypeNoIndent*/
10+
//// Baz;/*intersectionTypeNoIndent*/
11+
12+
format.document();
13+
14+
goTo.marker("formatBarOperator");
15+
verify.currentLineContentIs("type Union = number | {}");
16+
goTo.marker("indent");
17+
verify.indentationIs(4);
18+
goTo.marker("autoformat");
19+
verify.currentLineContentIs(" | string");
20+
goTo.marker("formatAmpersandOperator");
21+
verify.currentLineContentIs("type Intersection = Foo & Bar;");
22+
23+
goTo.marker("unionTypeNoIndent");
24+
verify.currentLineContentIs("Bar |");
25+
goTo.marker("intersectionTypeNoIndent");
26+
verify.currentLineContentIs("Baz;");

tests/cases/fourslash/formatTypeUnion.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0