Skip to content

Commit 96174ba

Browse files
committed
adding tests
1 parent b232159 commit 96174ba

File tree

10 files changed

+167
-26
lines changed

10 files changed

+167
-26
lines changed

src/services/formatting/rules.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,15 @@ namespace ts.formatting {
213213
public NoSpaceBetweenYieldKeywordAndStar: Rule;
214214
public SpaceBetweenYieldOrYieldStarAndOperand: Rule;
215215

216-
// Await-async
217-
public SpaceAfterAwaitKeyword: Rule;
218-
public NoSpaceAfterAwaitKeyword: Rule;
216+
// Async-await
219217
public SpaceBetweenAsyncAndFunctionKeyword: Rule;
220218
public NoSpaceBetweenAsyncAndFunctionKeyword: Rule;
219+
public SpaceAfterAwaitKeyword: Rule;
220+
public NoSpaceAfterAwaitKeyword: Rule;
221+
222+
// Type alias declaration
223+
public SpaceAfterTypeKeyword: Rule;
224+
public NoSpaceAfterTypeKeyword: Rule;
221225

222226
// Tagged template string
223227
public SpaceBetweenTagAndTemplateString: Rule;
@@ -376,12 +380,16 @@ namespace ts.formatting {
376380
this.NoSpaceBetweenYieldKeywordAndStar = new Rule(RuleDescriptor.create1(SyntaxKind.YieldKeyword, SyntaxKind.AsteriskToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsYieldOrYieldStarWithOperand), RuleAction.Delete));
377381
this.SpaceBetweenYieldOrYieldStarAndOperand = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.YieldKeyword, SyntaxKind.AsteriskToken]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsYieldOrYieldStarWithOperand), RuleAction.Space));
378382

379-
// Await-async
380-
this.SpaceAfterAwaitKeyword = new Rule(RuleDescriptor.create3(SyntaxKind.AwaitKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
381-
this.NoSpaceAfterAwaitKeyword = new Rule(RuleDescriptor.create3(SyntaxKind.AwaitKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
383+
// Async-await
382384
this.SpaceBetweenAsyncAndFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.AsyncKeyword, SyntaxKind.FunctionKeyword), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
383385
this.NoSpaceBetweenAsyncAndFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.AsyncKeyword, SyntaxKind.FunctionKeyword), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
384-
386+
this.SpaceAfterAwaitKeyword = new Rule(RuleDescriptor.create3(SyntaxKind.AwaitKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
387+
this.NoSpaceAfterAwaitKeyword = new Rule(RuleDescriptor.create3(SyntaxKind.AwaitKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
388+
389+
// Type alias declaration
390+
this.SpaceAfterTypeKeyword = new Rule(RuleDescriptor.create3(SyntaxKind.TypeKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
391+
this.NoSpaceAfterTypeKeyword = new Rule(RuleDescriptor.create3(SyntaxKind.TypeKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
392+
385393
// template string
386394
this.SpaceBetweenTagAndTemplateString = new Rule(RuleDescriptor.create3(SyntaxKind.Identifier, Shared.TokenRange.FromTokens([SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
387395
this.NoSpaceBetweenTagAndTemplateString = new Rule(RuleDescriptor.create3(SyntaxKind.Identifier, Shared.TokenRange.FromTokens([SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
@@ -419,8 +427,9 @@ namespace ts.formatting {
419427
this.NoSpaceBeforeOpenParenInFuncCall,
420428
this.SpaceBeforeBinaryKeywordOperator, this.SpaceAfterBinaryKeywordOperator,
421429
this.SpaceAfterVoidOperator,
422-
this.SpaceAfterAwaitKeyword, this.NoSpaceAfterAwaitKeyword,
423430
this.SpaceBetweenAsyncAndFunctionKeyword, this.NoSpaceBetweenAsyncAndFunctionKeyword,
431+
this.SpaceAfterAwaitKeyword, this.NoSpaceAfterAwaitKeyword,
432+
this.SpaceAfterTypeKeyword, this.NoSpaceAfterTypeKeyword,
424433
this.SpaceBetweenTagAndTemplateString, this.NoSpaceBetweenTagAndTemplateString,
425434
this.SpaceBeforeBar, this.NoSpaceBeforeBar, this.SpaceAfterBar, this.NoSpaceAfterBar,
426435

src/services/formatting/smartIndenter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ namespace ts.formatting {
408408
case SyntaxKind.ClassDeclaration:
409409
case SyntaxKind.InterfaceDeclaration:
410410
case SyntaxKind.EnumDeclaration:
411+
case SyntaxKind.TypeAliasDeclaration:
411412
case SyntaxKind.ArrayLiteralExpression:
412413
case SyntaxKind.Block:
413414
case SyntaxKind.ModuleBlock:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////async function asyncFunction() {/*asyncKeyword*/
4+
//// await
5+
/////*awaitExpressionIndent*/
6+
//// Promise.resolve("await");/*awaitExpressionAutoformat*/
7+
//// return await Promise.resolve("completed");/*awaitKeyword*/
8+
////}
9+
10+
format.document();
11+
12+
goTo.marker("asyncKeyword");
13+
verify.currentLineContentIs("async function asyncFunction() {");
14+
goTo.marker("awaitExpressionIndent");
15+
verify.indentationIs(8);
16+
goTo.marker("awaitExpressionAutoformat");
17+
verify.currentLineContentIs(' Promise.resolve("await");');
18+
goTo.marker("awaitKeyword");
19+
verify.currentLineContentIs(' return await Promise.resolve("completed");');
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function renderElement(
4+
//// element: Element,
5+
//// renderNode:
6+
////(/*funcAutoformat*/
7+
//// node: Node/*funcParamAutoformat*/
8+
/////*funcIndent*/
9+
//// ) => void,
10+
////newNode:
11+
////new(/*constrAutoformat*/
12+
//// name: string/*constrParamAutoformat*/
13+
/////*constrIndent*/
14+
////) => Node
15+
////): void {
16+
////}
17+
18+
format.document();
19+
20+
goTo.marker("funcAutoformat");
21+
verify.currentLineContentIs(" (");
22+
goTo.marker("funcParamAutoformat");
23+
verify.currentLineContentIs(" node: Node");
24+
goTo.marker("funcIndent");
25+
verify.indentationIs(12);
26+
27+
goTo.marker("constrAutoformat");
28+
verify.currentLineContentIs(" new (");
29+
goTo.marker("constrParamAutoformat");
30+
verify.currentLineContentIs(" name: string");
31+
goTo.marker("constrIndent");
32+
verify.indentationIs(12);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function foo(
4+
//// first:
5+
//// number,/*first*/
6+
//// second: (
7+
//// string/*second*/
8+
//// ),
9+
//// third:
10+
//// (
11+
//// boolean/*third*/
12+
//// )
13+
////) {
14+
////}
15+
16+
format.document();
17+
18+
goTo.marker("first");
19+
verify.currentLineContentIs(" number,");
20+
goTo.marker("second");
21+
verify.currentLineContentIs(" string");
22+
goTo.marker("third");
23+
verify.currentLineContentIs(" boolean");
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////type Foo = {
4+
//// (
5+
//// call: any/*callAutoformat*/
6+
/////*callIndent*/
7+
//// ): void;
8+
//// new (
9+
//// constr: any/*constrAutoformat*/
10+
/////*constrIndent*/
11+
//// ): void;
12+
//// method(
13+
//// whatever: any/*methodAutoformat*/
14+
/////*methodIndent*/
15+
//// ): void;
16+
////};
17+
18+
format.document();
19+
20+
goTo.marker("callAutoformat");
21+
verify.currentLineContentIs(" call: any");
22+
goTo.marker("callIndent");
23+
verify.indentationIs(8);
24+
goTo.marker("constrAutoformat");
25+
verify.currentLineContentIs(" constr: any");
26+
goTo.marker("constrIndent");
27+
verify.indentationIs(8);
28+
goTo.marker("methodAutoformat");
29+
verify.currentLineContentIs(" whatever: any");
30+
goTo.marker("methodIndent");
31+
verify.indentationIs(8);

tests/cases/fourslash/formatTemplateLiteral.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
////var x = `sadasdasdasdasfegsfd
33
/////*1*/rasdesgeryt35t35y35 e4 ergt er 35t 3535 `;
44
////var y = `1${2}/*2*/3`;
5+
////let z= `foo`/*3*/
6+
////let w= `bar${3}`/*4*/
7+
////String.raw
8+
//// `template`/*5*/
59

610

711
goTo.marker("1");
@@ -10,4 +14,15 @@ edit.insert("\r\n"); // edit will trigger formatting - should succeeed
1014
goTo.marker("2");
1115
edit.insert("\r\n");
1216
verify.indentationIs(0);
13-
verify.currentLineContentIs("3`;")
17+
verify.currentLineContentIs("3`;")
18+
19+
goTo.marker("3");
20+
edit.insert(";");
21+
verify.currentLineContentIs("let z = `foo`;");
22+
goTo.marker("4");
23+
edit.insert(";");
24+
verify.currentLineContentIs("let w = `bar${3}`;");
25+
26+
goTo.marker("5");
27+
edit.insert(";");
28+
verify.currentLineContentIs(" `template`;");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////type Alias = /*typeKeyword*/
4+
/////*indent*/
5+
////number;/*autoformat*/
6+
7+
format.document();
8+
9+
goTo.marker("typeKeyword");
10+
verify.currentLineContentIs("type Alias =");
11+
goTo.marker("indent");
12+
verify.indentationIs(4);
13+
goTo.marker("autoformat");
14+
verify.currentLineContentIs(" number;");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////type Union = number | {}/*formatOperator*/
4+
/////*indent*/
5+
////|string/*autoformat*/
6+
7+
format.document();
8+
9+
goTo.marker("formatOperator");
10+
verify.currentLineContentIs("type Union = number | {}");
11+
goTo.marker("indent");
12+
verify.indentationIs(4);
13+
goTo.marker("autoformat");
14+
verify.currentLineContentIs(" | string");

tests/cases/fourslash/formattingFunctionType.ts

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

0 commit comments

Comments
 (0)