From 45ed0186a158bdefe8c1e7d0d8dd64c28cab1486 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 3 Apr 2019 15:54:40 -0700 Subject: [PATCH] Make anonymous function formatting apply to anonymous generators too --- src/services/formatting/rules.ts | 6 +++--- .../fourslash/formattingSpaceBeforeFunctionParen.ts | 10 ++++++++-- .../cases/fourslash/generatorDeclarationFormatting.ts | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 8d710f87a4e..38792d448e4 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -98,7 +98,7 @@ namespace ts.formatting { rule("SpaceAfterConditionalClosingParen", SyntaxKind.CloseParenToken, SyntaxKind.OpenBracketToken, [isControlDeclContext], RuleAction.Space), rule("NoSpaceBetweenFunctionKeywordAndStar", SyntaxKind.FunctionKeyword, SyntaxKind.AsteriskToken, [isFunctionDeclarationOrFunctionExpressionContext], RuleAction.Delete), - rule("SpaceAfterStarInGeneratorDeclaration", SyntaxKind.AsteriskToken, [SyntaxKind.Identifier, SyntaxKind.OpenParenToken], [isFunctionDeclarationOrFunctionExpressionContext], RuleAction.Space), + rule("SpaceAfterStarInGeneratorDeclaration", SyntaxKind.AsteriskToken, SyntaxKind.Identifier, [isFunctionDeclarationOrFunctionExpressionContext], RuleAction.Space), rule("SpaceAfterFunctionInFuncDecl", SyntaxKind.FunctionKeyword, anyToken, [isFunctionDeclContext], RuleAction.Space), // Insert new line after { and before } in multi-line contexts. @@ -241,8 +241,8 @@ namespace ts.formatting { rule("NoSpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext], RuleAction.Delete), // Insert space after function keyword for anonymous functions - rule("SpaceAfterAnonymousFunctionKeyword", SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken, [isOptionEnabled("insertSpaceAfterFunctionKeywordForAnonymousFunctions"), isFunctionDeclContext], RuleAction.Space), - rule("NoSpaceAfterAnonymousFunctionKeyword", SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken, [isOptionDisabledOrUndefined("insertSpaceAfterFunctionKeywordForAnonymousFunctions"), isFunctionDeclContext], RuleAction.Delete), + rule("SpaceAfterAnonymousFunctionKeyword", [SyntaxKind.FunctionKeyword, SyntaxKind.AsteriskToken], SyntaxKind.OpenParenToken, [isOptionEnabled("insertSpaceAfterFunctionKeywordForAnonymousFunctions"), isFunctionDeclContext], RuleAction.Space), + rule("NoSpaceAfterAnonymousFunctionKeyword", [SyntaxKind.FunctionKeyword, SyntaxKind.AsteriskToken], SyntaxKind.OpenParenToken, [isOptionDisabledOrUndefined("insertSpaceAfterFunctionKeywordForAnonymousFunctions"), isFunctionDeclContext], RuleAction.Delete), // Insert space after keywords in control flow statements rule("SpaceAfterKeywordInControl", keywords, SyntaxKind.OpenParenToken, [isOptionEnabled("insertSpaceAfterKeywordsInControlFlowStatements"), isControlDeclContext], RuleAction.Space), diff --git a/tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts b/tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts index 42e48ecd38d..86388a7a8d3 100644 --- a/tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts +++ b/tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts @@ -5,9 +5,11 @@ /////*3*/var bar = function foo() { }; /////*4*/var foo = { bar() { } }; /////*5*/function tmpl () { } +/////*6*/var f = function*() { }; +/////*7*/function* g () { } -format.setOption("InsertSpaceBeforeFunctionParenthesis", true); - +format.setOption("insertSpaceBeforeFunctionParenthesis", true); +format.setOption("insertSpaceAfterFunctionKeywordForAnonymousFunctions", false); format.document(); goTo.marker('1'); @@ -20,3 +22,7 @@ goTo.marker('4'); verify.currentLineContentIs('var foo = { bar () { } };'); goTo.marker('5'); verify.currentLineContentIs('function tmpl () { }'); +goTo.marker('6'); +verify.currentLineContentIs('var f = function*() { };'); +goTo.marker('7'); +verify.currentLineContentIs('function* g () { }'); diff --git a/tests/cases/fourslash/generatorDeclarationFormatting.ts b/tests/cases/fourslash/generatorDeclarationFormatting.ts index 007380fd0de..4b7ae4157ae 100644 --- a/tests/cases/fourslash/generatorDeclarationFormatting.ts +++ b/tests/cases/fourslash/generatorDeclarationFormatting.ts @@ -7,4 +7,4 @@ format.document(); goTo.marker('1'); verify.currentLineContentIs("function* g() { }"); goTo.marker('2'); -verify.currentLineContentIs("var v = function* () { };"); \ No newline at end of file +verify.currentLineContentIs("var v = function*() { };"); \ No newline at end of file