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.
This commit is contained in:
Vakhurin Sergey 2017-10-19 02:00:05 +03:00
parent e5f8287e5c
commit 2b6ef79ada
3 changed files with 13 additions and 3 deletions

View file

@ -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;
}

View file

@ -4,6 +4,7 @@
/////*2*/function boo () { }
/////*3*/var bar = function foo() { };
/////*4*/var foo = { bar() { } };
/////*5*/function tmpl <T> () { }
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 () { } };');
verify.currentLineContentIs('var foo = { bar () { } };');
goTo.marker('5');
verify.currentLineContentIs('function tmpl<T> () { }');

View file

@ -15,7 +15,7 @@
////foo()<number, string, T >();
////(a + b)<number, string, T >();
////
////function bar<T>() {
/////*inFunctionDeclaration*/function bar <T> () {
/////*inClassExpression*/ return class < T2 > {
//// }
////}
@ -42,6 +42,9 @@ verify.currentLineContentIs(" new <T>(a: T);");
goTo.marker("inOptionalMethodSignature");
verify.currentLineContentIs(" op?<T, M>(a: T, b: M);");
goTo.marker("inFunctionDeclaration");
verify.currentLineContentIs("function bar<T>() {");
goTo.marker("inClassExpression");
verify.currentLineContentIs(" return class <T2> {");