From 2b6ef79ada46c00daf21186bb627e31b58fbd61a Mon Sep 17 00:00:00 2001 From: Vakhurin Sergey Date: Thu, 19 Oct 2017 02:00:05 +0300 Subject: [PATCH] Fixed incorrect insertSpaceBeforeFunctionParenthesis behavior on functions with type-arguments There was an interference between the SpaceBeforeOpenParenInFuncDecl and the NoSpaceAfterCloseAngularBracket rules. So, the NoSpaceAfterCloseAngularBracket eliminated a space which has been added by the SpaceBeforeOpenParenInFuncDecl rule. --- src/services/formatting/rules.ts | 6 +++++- tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts | 5 ++++- tests/cases/fourslash/genericsFormatting.ts | 5 ++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index d41d9dfbfd8..32c435773db 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -379,7 +379,7 @@ namespace ts.formatting { this.NoSpaceBetweenCloseParenAndAngularBracket = new Rule(RuleDescriptor.create1(SyntaxKind.CloseParenToken, SyntaxKind.LessThanToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeArgumentOrParameterOrAssertionContext), RuleAction.Delete)); this.NoSpaceAfterOpenAngularBracket = new Rule(RuleDescriptor.create3(SyntaxKind.LessThanToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeArgumentOrParameterOrAssertionContext), RuleAction.Delete)); this.NoSpaceBeforeCloseAngularBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.GreaterThanToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeArgumentOrParameterOrAssertionContext), RuleAction.Delete)); - this.NoSpaceAfterCloseAngularBracket = new Rule(RuleDescriptor.create3(SyntaxKind.GreaterThanToken, Shared.TokenRange.FromTokens([SyntaxKind.OpenParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.GreaterThanToken, SyntaxKind.CommaToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeArgumentOrParameterOrAssertionContext), RuleAction.Delete)); + this.NoSpaceAfterCloseAngularBracket = new Rule(RuleDescriptor.create3(SyntaxKind.GreaterThanToken, Shared.TokenRange.FromTokens([SyntaxKind.OpenParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.GreaterThanToken, SyntaxKind.CommaToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeArgumentOrParameterOrAssertionContext, Rules.IsNotFunctionDeclContext /*To prevent an interference with the SpaceBeforeOpenParenInFuncDecl rule*/), RuleAction.Delete)); // Remove spaces in empty interface literals. e.g.: x: {} this.NoSpaceBetweenEmptyInterfaceBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsObjectTypeContext), RuleAction.Delete)); @@ -718,6 +718,10 @@ namespace ts.formatting { return false; } + static IsNotFunctionDeclContext(context: FormattingContext): boolean { + return !Rules.IsFunctionDeclContext(context); + } + static IsFunctionDeclarationOrFunctionExpressionContext(context: FormattingContext): boolean { return context.contextNode.kind === SyntaxKind.FunctionDeclaration || context.contextNode.kind === SyntaxKind.FunctionExpression; } diff --git a/tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts b/tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts index ce85521879e..42e48ecd38d 100644 --- a/tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts +++ b/tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts @@ -4,6 +4,7 @@ /////*2*/function boo () { } /////*3*/var bar = function foo() { }; /////*4*/var foo = { bar() { } }; +/////*5*/function tmpl () { } format.setOption("InsertSpaceBeforeFunctionParenthesis", true); @@ -16,4 +17,6 @@ verify.currentLineContentIs('function boo () { }'); goTo.marker('3'); verify.currentLineContentIs('var bar = function foo () { };'); goTo.marker('4'); -verify.currentLineContentIs('var foo = { bar () { } };'); \ No newline at end of file +verify.currentLineContentIs('var foo = { bar () { } };'); +goTo.marker('5'); +verify.currentLineContentIs('function tmpl () { }'); diff --git a/tests/cases/fourslash/genericsFormatting.ts b/tests/cases/fourslash/genericsFormatting.ts index f5e44522a8e..bb2a218a440 100644 --- a/tests/cases/fourslash/genericsFormatting.ts +++ b/tests/cases/fourslash/genericsFormatting.ts @@ -15,7 +15,7 @@ ////foo()(); ////(a + b)(); //// -////function bar() { +/////*inFunctionDeclaration*/function bar () { /////*inClassExpression*/ return class < T2 > { //// } ////} @@ -42,6 +42,9 @@ verify.currentLineContentIs(" new (a: T);"); goTo.marker("inOptionalMethodSignature"); verify.currentLineContentIs(" op?(a: T, b: M);"); +goTo.marker("inFunctionDeclaration"); +verify.currentLineContentIs("function bar() {"); + goTo.marker("inClassExpression"); verify.currentLineContentIs(" return class {");