diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index c3bc8e7802d..12efb774dd3 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -214,6 +214,7 @@ namespace ts.formatting { public SpaceBetweenYieldOrYieldStarAndOperand: Rule; // Async functions + public SpaceBetweenAsyncAndOpenParen: Rule; public SpaceBetweenAsyncAndFunctionKeyword: Rule; // Template strings @@ -369,6 +370,7 @@ namespace ts.formatting { 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)); // Async-await + this.SpaceBetweenAsyncAndOpenParen = new Rule(RuleDescriptor.create1(SyntaxKind.AsyncKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsArrowFunctionContext, Rules.IsSameLineTokenContext), RuleAction.Space)); this.SpaceBetweenAsyncAndFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.AsyncKeyword, SyntaxKind.FunctionKeyword), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space)); // template string @@ -402,7 +404,7 @@ namespace ts.formatting { this.NoSpaceBeforeOpenParenInFuncCall, this.SpaceBeforeBinaryKeywordOperator, this.SpaceAfterBinaryKeywordOperator, this.SpaceAfterVoidOperator, - this.SpaceBetweenAsyncAndFunctionKeyword, + this.SpaceBetweenAsyncAndOpenParen, this.SpaceBetweenAsyncAndFunctionKeyword, this.SpaceBetweenTagAndTemplateString, this.NoSpaceAfterTemplateHeadAndMiddle, this.NoSpaceBeforeTemplateMiddleAndTail, // TypeScript-specific rules @@ -703,6 +705,10 @@ namespace ts.formatting { return context.currentTokenSpan.kind !== SyntaxKind.CommaToken; } + static IsArrowFunctionContext(context: FormattingContext): boolean { + return context.contextNode.kind === SyntaxKind.ArrowFunction; + } + static IsSameLineTokenContext(context: FormattingContext): boolean { return context.TokensAreOnSameLine(); } diff --git a/tests/cases/fourslash/formatAsyncKeyword.ts b/tests/cases/fourslash/formatAsyncKeyword.ts new file mode 100644 index 00000000000..305d4ea4244 --- /dev/null +++ b/tests/cases/fourslash/formatAsyncKeyword.ts @@ -0,0 +1,13 @@ +/// + +/////*1*/let x = async () => 1; +/////*2*/let y = async() => 1; +/////*3*/let z = async function () { return 1; }; + +format.document(); +goTo.marker("1"); +verify.currentLineContentIs("let x = async () => 1;"); +goTo.marker("2"); +verify.currentLineContentIs("let y = async () => 1;"); +goTo.marker("3"); +verify.currentLineContentIs("let z = async function() { return 1; };") \ No newline at end of file