Skip to content

Commit 0df66a5

Browse files
committed
format space before single-line blocks
1 parent 4c40c42 commit 0df66a5

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

src/services/formatting/rules.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,15 @@ namespace ts.formatting {
290290

291291
// Place a space before open brace in a function declaration
292292
this.FunctionOpenBraceLeftTokenRange = Shared.TokenRange.AnyIncludingMultilineComments;
293-
this.SpaceBeforeOpenBraceInFunction = new Rule(RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsBeforeBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
293+
this.SpaceBeforeOpenBraceInFunction = new Rule(RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsBeforeBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
294294

295295
// Place a space before open brace in a TypeScript declaration that has braces as children (class, module, enum, etc)
296296
this.TypeScriptOpenBraceLeftTokenRange = Shared.TokenRange.FromTokens([SyntaxKind.Identifier, SyntaxKind.MultiLineCommentTrivia, SyntaxKind.ClassKeyword, SyntaxKind.ExportKeyword, SyntaxKind.ImportKeyword]);
297-
this.SpaceBeforeOpenBraceInTypeScriptDeclWithBlock = new Rule(RuleDescriptor.create2(this.TypeScriptOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsTypeScriptDeclWithBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
297+
this.SpaceBeforeOpenBraceInTypeScriptDeclWithBlock = new Rule(RuleDescriptor.create2(this.TypeScriptOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsTypeScriptDeclWithBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
298298

299299
// Place a space before open brace in a control flow construct
300300
this.ControlOpenBraceLeftTokenRange = Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.MultiLineCommentTrivia, SyntaxKind.DoKeyword, SyntaxKind.TryKeyword, SyntaxKind.FinallyKeyword, SyntaxKind.ElseKeyword]);
301-
this.SpaceBeforeOpenBraceInControl = new Rule(RuleDescriptor.create2(this.ControlOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsControlDeclContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
301+
this.SpaceBeforeOpenBraceInControl = new Rule(RuleDescriptor.create2(this.ControlOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsControlDeclContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
302302

303303
// Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}.
304304
this.SpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces"), Rules.IsBraceWrappedContext), RuleAction.Space));
@@ -644,25 +644,8 @@ namespace ts.formatting {
644644
return context.contextNode.kind === SyntaxKind.ConditionalExpression;
645645
}
646646

647-
static IsSameLineTokenOrBeforeMultilineBlockContext(context: FormattingContext): boolean {
648-
//// This check is mainly used inside SpaceBeforeOpenBraceInControl and SpaceBeforeOpenBraceInFunction.
649-
////
650-
//// Ex:
651-
//// if (1) { ....
652-
//// * ) and { are on the same line so apply the rule. Here we don't care whether it's same or multi block context
653-
////
654-
//// Ex:
655-
//// if (1)
656-
//// { ... }
657-
//// * ) and { are on different lines. We only need to format if the block is multiline context. So in this case we don't format.
658-
////
659-
//// Ex:
660-
//// if (1)
661-
//// { ...
662-
//// }
663-
//// * ) and { are on different lines. We only need to format if the block is multiline context. So in this case we format.
664-
665-
return context.TokensAreOnSameLine() || Rules.IsBeforeMultilineBlockContext(context);
647+
static IsSameLineTokenOrBeforeBlockContext(context: FormattingContext): boolean {
648+
return context.TokensAreOnSameLine() || Rules.IsBeforeBlockContext(context);
666649
}
667650

668651
static IsBraceWrappedContext(context: FormattingContext): boolean {

0 commit comments

Comments
 (0)