Skip to content

Commit 75e8ba7

Browse files
authored
Merge pull request #14899 from KingHenne/fix/format-multi-line-object-destructuring
Fix formatting for multi-line object destructuring
2 parents a8aa070 + 3e81f08 commit 75e8ba7

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/services/formatting/rules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ namespace ts.formatting {
297297
// Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}.
298298
this.SpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Space));
299299
this.SpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Space));
300-
this.NoSpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Delete));
301-
this.NoSpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Delete));
300+
this.NoSpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
301+
this.NoSpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
302302
this.NoSpaceBetweenEmptyBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsObjectContext), RuleAction.Delete));
303303

304304
// Insert new line after { and before } in multi-line contexts.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
/////*1*/const {
4+
/////*2*/ a,
5+
/////*3*/ b,
6+
/////*4*/} = {a: 1, b: 2};
7+
/////*5*/const {a: c} = {a: 1, b: 2};
8+
9+
format.document();
10+
11+
goTo.marker("1");
12+
verify.currentLineContentIs("const {");
13+
goTo.marker("2");
14+
verify.currentLineContentIs(" a,");
15+
goTo.marker("3");
16+
verify.currentLineContentIs(" b,");
17+
goTo.marker("4");
18+
verify.currentLineContentIs("} = { a: 1, b: 2 };");
19+
goTo.marker("5");
20+
verify.currentLineContentIs("const { a: c } = { a: 1, b: 2 };");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
/////*1*/const {
4+
/////*2*/ a,
5+
/////*3*/ b,
6+
/////*4*/} = { a: 1, b: 2 };
7+
8+
format.setOption('InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces', false);
9+
10+
format.document();
11+
12+
goTo.marker("1");
13+
verify.currentLineContentIs("const {");
14+
goTo.marker("2");
15+
verify.currentLineContentIs(" a,");
16+
goTo.marker("3");
17+
verify.currentLineContentIs(" b,");
18+
goTo.marker("4");
19+
verify.currentLineContentIs("} = {a: 1, b: 2};");

0 commit comments

Comments
 (0)