Skip to content

Commit be5557a

Browse files
committed
Formatting for generators
1 parent 7f5a89a commit be5557a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/services/formatting/rules.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,16 @@ module ts.formatting {
193193
// Insert space after function keyword for anonymous functions
194194
public SpaceAfterAnonymousFunctionKeyword: Rule;
195195
public NoSpaceAfterAnonymousFunctionKeyword: Rule;
196-
196+
197197
// Insert space after @ in decorator
198198
public SpaceBeforeAt: Rule;
199199
public NoSpaceAfterAt: Rule;
200200
public SpaceAfterDecorator: Rule;
201201

202+
// Generator: function*
203+
public NoSpaceBetweenFunctionKeywordAndStar: Rule;
204+
public SpaceAfterStarInGenerator: Rule;
205+
202206
constructor() {
203207
///
204208
/// Common Rules
@@ -340,6 +344,9 @@ module ts.formatting {
340344
this.NoSpaceAfterAt = new Rule(RuleDescriptor.create3(SyntaxKind.AtToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
341345
this.SpaceAfterDecorator = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.Identifier, SyntaxKind.ExportKeyword, SyntaxKind.DefaultKeyword, SyntaxKind.ClassKeyword, SyntaxKind.StaticKeyword, SyntaxKind.PublicKeyword, SyntaxKind.PrivateKeyword, SyntaxKind.ProtectedKeyword, SyntaxKind.GetKeyword, SyntaxKind.SetKeyword, SyntaxKind.OpenBracketToken, SyntaxKind.AsteriskToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsEndOfDecoratorContextOnSameLine), RuleAction.Space));
342346

347+
this.NoSpaceBetweenFunctionKeywordAndStar = new Rule(RuleDescriptor.create1(SyntaxKind.FunctionKeyword, SyntaxKind.AsteriskToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), RuleAction.Delete));
348+
this.SpaceAfterStarInGenerator = new Rule(RuleDescriptor.create3(SyntaxKind.AsteriskToken, Shared.TokenRange.FromTokens([SyntaxKind.Identifier, SyntaxKind.OpenParenToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), RuleAction.Space));
349+
343350
// These rules are higher in priority than user-configurable rules.
344351
this.HighPriorityCommonRules =
345352
[
@@ -357,6 +364,7 @@ module ts.formatting {
357364
this.NoSpaceAfterCloseBrace,
358365
this.SpaceAfterOpenBrace, this.SpaceBeforeCloseBrace, this.NewLineBeforeCloseBraceInBlockContext,
359366
this.SpaceAfterCloseBrace, this.SpaceBetweenCloseBraceAndElse, this.SpaceBetweenCloseBraceAndWhile, this.NoSpaceBetweenEmptyBraceBrackets,
367+
this.NoSpaceBetweenFunctionKeywordAndStar, this.SpaceAfterStarInGenerator,
360368
this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember,
361369
this.NoSpaceBetweenReturnAndSemicolon,
362370
this.SpaceAfterCertainKeywords,
@@ -574,6 +582,10 @@ module ts.formatting {
574582
return false;
575583
}
576584

585+
static IsFunctionDeclarationOrFunctionExpressionContext(context: FormattingContext): boolean {
586+
return context.contextNode.kind === SyntaxKind.FunctionDeclaration || context.contextNode.kind === SyntaxKind.FunctionExpression;
587+
}
588+
577589
static IsTypeScriptDeclWithBlockContext(context: FormattingContext): boolean {
578590
return Rules.NodeIsTypeScriptDeclWithBlockContext(context.contextNode);
579591
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
//// function *g() { }/*1*/
4+
//// var v = function *() { };/*2*/
5+
6+
format.document();
7+
goTo.marker('1');
8+
verify.currentLineContentIs("function* g() { }");
9+
goTo.marker('2');
10+
verify.currentLineContentIs("var v = function* () { };");

0 commit comments

Comments
 (0)