Skip to content

Commit c35419e

Browse files
committed
add rule to insert space between async keyword and open paren
1 parent 1b5dc0d commit c35419e

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/services/formatting/rules.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ namespace ts.formatting {
214214
public SpaceBetweenYieldOrYieldStarAndOperand: Rule;
215215

216216
// Async functions
217+
public SpaceBetweenAsyncAndOpenParen: Rule;
217218
public SpaceBetweenAsyncAndFunctionKeyword: Rule;
218219

219220
// Template strings
@@ -369,6 +370,7 @@ namespace ts.formatting {
369370
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));
370371

371372
// Async-await
373+
this.SpaceBetweenAsyncAndOpenParen = new Rule(RuleDescriptor.create1(SyntaxKind.AsyncKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsArrowFunctionContext, Rules.IsSameLineTokenContext), RuleAction.Space));
372374
this.SpaceBetweenAsyncAndFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.AsyncKeyword, SyntaxKind.FunctionKeyword), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
373375

374376
// template string
@@ -402,7 +404,7 @@ namespace ts.formatting {
402404
this.NoSpaceBeforeOpenParenInFuncCall,
403405
this.SpaceBeforeBinaryKeywordOperator, this.SpaceAfterBinaryKeywordOperator,
404406
this.SpaceAfterVoidOperator,
405-
this.SpaceBetweenAsyncAndFunctionKeyword,
407+
this.SpaceBetweenAsyncAndOpenParen, this.SpaceBetweenAsyncAndFunctionKeyword,
406408
this.SpaceBetweenTagAndTemplateString, this.NoSpaceAfterTemplateHeadAndMiddle, this.NoSpaceBeforeTemplateMiddleAndTail,
407409

408410
// TypeScript-specific rules
@@ -703,6 +705,10 @@ namespace ts.formatting {
703705
return context.currentTokenSpan.kind !== SyntaxKind.CommaToken;
704706
}
705707

708+
static IsArrowFunctionContext(context: FormattingContext): boolean {
709+
return context.contextNode.kind === SyntaxKind.ArrowFunction;
710+
}
711+
706712
static IsSameLineTokenContext(context: FormattingContext): boolean {
707713
return context.TokensAreOnSameLine();
708714
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
/////*1*/let x = async () => 1;
4+
/////*2*/let y = async() => 1;
5+
/////*3*/let z = async function () { return 1; };
6+
7+
format.document();
8+
goTo.marker("1");
9+
verify.currentLineContentIs("let x = async () => 1;");
10+
goTo.marker("2");
11+
verify.currentLineContentIs("let y = async () => 1;");
12+
goTo.marker("3");
13+
verify.currentLineContentIs("let z = async function() { return 1; };")

0 commit comments

Comments
 (0)