Skip to content

Commit fbb9086

Browse files
committed
Merge pull request #5988 from SaschaNaz/formatArrows
Format spaces before arrows
2 parents 6468139 + 595f134 commit fbb9086

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/services/formatting/rules.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ namespace ts.formatting {
123123
public SpaceAfterModuleName: Rule;
124124

125125
// Lambda expressions
126+
public SpaceBeforeArrow: Rule;
126127
public SpaceAfterArrow: Rule;
127128

128129
// Optional parameters and let args
@@ -254,7 +255,7 @@ namespace ts.formatting {
254255

255256
// No space before and after indexer
256257
this.NoSpaceBeforeOpenBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
257-
this.NoSpaceAfterCloseBracket = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext ), RuleAction.Delete));
258+
this.NoSpaceAfterCloseBracket = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext), RuleAction.Delete));
258259

259260
// Place a space before open brace in a function declaration
260261
this.FunctionOpenBraceLeftTokenRange = Shared.TokenRange.AnyIncludingMultilineComments;
@@ -342,6 +343,7 @@ namespace ts.formatting {
342343
this.SpaceAfterModuleName = new Rule(RuleDescriptor.create1(SyntaxKind.StringLiteral, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsModuleDeclContext), RuleAction.Space));
343344

344345
// Lambda expressions
346+
this.SpaceBeforeArrow = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.EqualsGreaterThanToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
345347
this.SpaceAfterArrow = new Rule(RuleDescriptor.create3(SyntaxKind.EqualsGreaterThanToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
346348

347349
// Optional parameters and let args
@@ -379,8 +381,7 @@ namespace ts.formatting {
379381
this.NoSpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
380382

381383
// These rules are higher in priority than user-configurable rules.
382-
this.HighPriorityCommonRules =
383-
[
384+
this.HighPriorityCommonRules = [
384385
this.IgnoreBeforeComment, this.IgnoreAfterLineComment,
385386
this.NoSpaceBeforeColon, this.SpaceAfterColon, this.NoSpaceBeforeQuestionMark, this.SpaceAfterQuestionMarkInConditionalOperator,
386387
this.NoSpaceAfterQuestionMark,
@@ -411,7 +412,7 @@ namespace ts.formatting {
411412
this.NoSpaceAfterConstructor, this.NoSpaceAfterModuleImport,
412413
this.SpaceAfterCertainTypeScriptKeywords, this.SpaceBeforeCertainTypeScriptKeywords,
413414
this.SpaceAfterModuleName,
414-
this.SpaceAfterArrow,
415+
this.SpaceBeforeArrow, this.SpaceAfterArrow,
415416
this.NoSpaceAfterEllipsis,
416417
this.NoSpaceAfterOptionalParameters,
417418
this.NoSpaceBetweenEmptyInterfaceBraceBrackets,
@@ -427,8 +428,7 @@ namespace ts.formatting {
427428
];
428429

429430
// These rules are lower in priority than user-configurable rules.
430-
this.LowPriorityCommonRules =
431-
[
431+
this.LowPriorityCommonRules = [
432432
this.NoSpaceBeforeSemicolon,
433433
this.SpaceBeforeOpenBraceInControl, this.SpaceBeforeOpenBraceInFunction, this.SpaceBeforeOpenBraceInTypeScriptDeclWithBlock,
434434
this.NoSpaceBeforeComma,
@@ -732,7 +732,7 @@ namespace ts.formatting {
732732
}
733733

734734
static IsStartOfVariableDeclarationList(context: FormattingContext): boolean {
735-
return context.currentTokenParent.kind === SyntaxKind.VariableDeclarationList &&
735+
return context.currentTokenParent.kind === SyntaxKind.VariableDeclarationList &&
736736
context.currentTokenParent.getStart(context.sourceFile) === context.currentTokenSpan.pos;
737737
}
738738

tests/cases/fourslash/formattingFatArrowFunctions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//// ( ) => 1 ;/*1*/
55
//// ( arg ) => 2 ;/*2*/
66
//// arg => 2 ;/*3*/
7+
//// arg=>2 ;/*3a*/
78
//// ( arg = 1 ) => 3 ;/*4*/
89
//// ( arg ? ) => 4 ;/*5*/
910
//// ( arg : number ) => 5 ;/*6*/
@@ -118,7 +119,9 @@ verify.currentLineContentIs("() => 1;");
118119
goTo.marker("2");
119120
verify.currentLineContentIs("(arg) => 2;");
120121
goTo.marker("3");
121-
verify.currentLineContentIs("arg => 2;");
122+
verify.currentLineContentIs("arg => 2;");
123+
goTo.marker("3a");
124+
verify.currentLineContentIs("arg => 2;");
122125
goTo.marker("4");
123126
verify.currentLineContentIs("(arg = 1) => 3;");
124127
goTo.marker("5");

0 commit comments

Comments
 (0)