Merge pull request #14899 from KingHenne/fix/format-multi-line-object-destructuring

Fix formatting for multi-line object destructuring
This commit is contained in:
Mohamed Hegazy 2017-03-29 09:43:04 -07:00 committed by GitHub
commit 75e8ba746e
3 changed files with 41 additions and 2 deletions

View file

@ -297,8 +297,8 @@ namespace ts.formatting {
// Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}.
this.SpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Space));
this.SpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Space));
this.NoSpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Delete));
this.NoSpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Delete));
this.NoSpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
this.NoSpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
this.NoSpaceBetweenEmptyBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsObjectContext), RuleAction.Delete));
// Insert new line after { and before } in multi-line contexts.

View file

@ -0,0 +1,20 @@
/// <reference path='fourslash.ts'/>
/////*1*/const {
/////*2*/ a,
/////*3*/ b,
/////*4*/} = {a: 1, b: 2};
/////*5*/const {a: c} = {a: 1, b: 2};
format.document();
goTo.marker("1");
verify.currentLineContentIs("const {");
goTo.marker("2");
verify.currentLineContentIs(" a,");
goTo.marker("3");
verify.currentLineContentIs(" b,");
goTo.marker("4");
verify.currentLineContentIs("} = { a: 1, b: 2 };");
goTo.marker("5");
verify.currentLineContentIs("const { a: c } = { a: 1, b: 2 };");

View file

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts'/>
/////*1*/const {
/////*2*/ a,
/////*3*/ b,
/////*4*/} = { a: 1, b: 2 };
format.setOption('InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces', false);
format.document();
goTo.marker("1");
verify.currentLineContentIs("const {");
goTo.marker("2");
verify.currentLineContentIs(" a,");
goTo.marker("3");
verify.currentLineContentIs(" b,");
goTo.marker("4");
verify.currentLineContentIs("} = {a: 1, b: 2};");