From 595f134e8bbfe5bf8ce93387a1a7ec2991ce7f0f Mon Sep 17 00:00:00 2001 From: SaschaNaz Date: Wed, 9 Dec 2015 00:02:10 +0900 Subject: [PATCH] space around arrow --- src/services/formatting/rules.ts | 14 +++++++------- .../cases/fourslash/formattingFatArrowFunctions.ts | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 12efb774dd3..de4761f57d2 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -123,6 +123,7 @@ namespace ts.formatting { public SpaceAfterModuleName: Rule; // Lambda expressions + public SpaceBeforeArrow: Rule; public SpaceAfterArrow: Rule; // Optional parameters and let args @@ -254,7 +255,7 @@ namespace ts.formatting { // No space before and after indexer this.NoSpaceBeforeOpenBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete)); - this.NoSpaceAfterCloseBracket = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext ), RuleAction.Delete)); + this.NoSpaceAfterCloseBracket = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext), RuleAction.Delete)); // Place a space before open brace in a function declaration this.FunctionOpenBraceLeftTokenRange = Shared.TokenRange.AnyIncludingMultilineComments; @@ -342,6 +343,7 @@ namespace ts.formatting { this.SpaceAfterModuleName = new Rule(RuleDescriptor.create1(SyntaxKind.StringLiteral, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsModuleDeclContext), RuleAction.Space)); // Lambda expressions + this.SpaceBeforeArrow = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.EqualsGreaterThanToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space)); this.SpaceAfterArrow = new Rule(RuleDescriptor.create3(SyntaxKind.EqualsGreaterThanToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space)); // Optional parameters and let args @@ -379,8 +381,7 @@ namespace ts.formatting { this.NoSpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete)); // These rules are higher in priority than user-configurable rules. - this.HighPriorityCommonRules = - [ + this.HighPriorityCommonRules = [ this.IgnoreBeforeComment, this.IgnoreAfterLineComment, this.NoSpaceBeforeColon, this.SpaceAfterColon, this.NoSpaceBeforeQuestionMark, this.SpaceAfterQuestionMarkInConditionalOperator, this.NoSpaceAfterQuestionMark, @@ -411,7 +412,7 @@ namespace ts.formatting { this.NoSpaceAfterConstructor, this.NoSpaceAfterModuleImport, this.SpaceAfterCertainTypeScriptKeywords, this.SpaceBeforeCertainTypeScriptKeywords, this.SpaceAfterModuleName, - this.SpaceAfterArrow, + this.SpaceBeforeArrow, this.SpaceAfterArrow, this.NoSpaceAfterEllipsis, this.NoSpaceAfterOptionalParameters, this.NoSpaceBetweenEmptyInterfaceBraceBrackets, @@ -427,8 +428,7 @@ namespace ts.formatting { ]; // These rules are lower in priority than user-configurable rules. - this.LowPriorityCommonRules = - [ + this.LowPriorityCommonRules = [ this.NoSpaceBeforeSemicolon, this.SpaceBeforeOpenBraceInControl, this.SpaceBeforeOpenBraceInFunction, this.SpaceBeforeOpenBraceInTypeScriptDeclWithBlock, this.NoSpaceBeforeComma, @@ -732,7 +732,7 @@ namespace ts.formatting { } static IsStartOfVariableDeclarationList(context: FormattingContext): boolean { - return context.currentTokenParent.kind === SyntaxKind.VariableDeclarationList && + return context.currentTokenParent.kind === SyntaxKind.VariableDeclarationList && context.currentTokenParent.getStart(context.sourceFile) === context.currentTokenSpan.pos; } diff --git a/tests/cases/fourslash/formattingFatArrowFunctions.ts b/tests/cases/fourslash/formattingFatArrowFunctions.ts index c540c2837e1..d4ce38dbd62 100644 --- a/tests/cases/fourslash/formattingFatArrowFunctions.ts +++ b/tests/cases/fourslash/formattingFatArrowFunctions.ts @@ -4,6 +4,7 @@ //// ( ) => 1 ;/*1*/ //// ( arg ) => 2 ;/*2*/ //// arg => 2 ;/*3*/ +//// arg=>2 ;/*3a*/ //// ( arg = 1 ) => 3 ;/*4*/ //// ( arg ? ) => 4 ;/*5*/ //// ( arg : number ) => 5 ;/*6*/ @@ -118,7 +119,9 @@ verify.currentLineContentIs("() => 1;"); goTo.marker("2"); verify.currentLineContentIs("(arg) => 2;"); goTo.marker("3"); -verify.currentLineContentIs("arg => 2;"); +verify.currentLineContentIs("arg => 2;"); +goTo.marker("3a"); +verify.currentLineContentIs("arg => 2;"); goTo.marker("4"); verify.currentLineContentIs("(arg = 1) => 3;"); goTo.marker("5");