From 9dfcb4419ca52efce7321ef08e88a1c4c0491c93 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 25 Jun 2016 19:23:46 +0900 Subject: [PATCH] do not format comma/closeparen in jsxelement --- src/services/formatting/rules.ts | 10 ++++--- .../cases/fourslash/formattingJsxElements.ts | 26 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index b4916025af2..5fb6b330b42 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -316,7 +316,7 @@ namespace ts.formatting { // Add a space between statements. All keywords except (do,else,case) has open/close parens after them. // So, we have a rule to add a space for [),Any], [do,Any], [else,Any], and [case,Any] - this.SpaceBetweenStatements = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.DoKeyword, SyntaxKind.ElseKeyword, SyntaxKind.CaseKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsNotForContext), RuleAction.Space)); + this.SpaceBetweenStatements = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.DoKeyword, SyntaxKind.ElseKeyword, SyntaxKind.CaseKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isNonJsxElementContext, Rules.IsNotForContext), RuleAction.Space)); // This low-pri rule takes care of "try {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter. this.SpaceAfterTryFinally = new Rule(RuleDescriptor.create2(Shared.TokenRange.FromTokens([SyntaxKind.TryKeyword, SyntaxKind.FinallyKeyword]), SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Space)); @@ -444,8 +444,8 @@ namespace ts.formatting { /// // Insert space after comma delimiter - this.SpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsNextTokenNotCloseBracket), RuleAction.Space)); - this.NoSpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete)); + this.SpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isNonJsxElementContext, Rules.IsNextTokenNotCloseBracket), RuleAction.Space)); + this.NoSpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.isNonJsxElementContext), RuleAction.Delete)); // Insert space before and after binary operators this.SpaceBeforeBinaryOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.BinaryOperators), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space)); @@ -723,6 +723,10 @@ namespace ts.formatting { return context.TokensAreOnSameLine() && context.contextNode.kind !== SyntaxKind.JsxText; } + static isNonJsxElementContext(context: FormattingContext): boolean { + return context.contextNode.kind !== SyntaxKind.JsxElement; + } + static IsNotBeforeBlockInFunctionDeclarationContext(context: FormattingContext): boolean { return !Rules.IsFunctionDeclContext(context) && !Rules.IsBeforeBlockContext(context); } diff --git a/tests/cases/fourslash/formattingJsxElements.ts b/tests/cases/fourslash/formattingJsxElements.ts index fbe4bb5d29e..50e9bb8ea8f 100644 --- a/tests/cases/fourslash/formattingJsxElements.ts +++ b/tests/cases/fourslash/formattingJsxElements.ts @@ -9,7 +9,7 @@ //// //// ) ////} -//// +//// ////function foo1() { //// return ( ////
@@ -45,8 +45,8 @@ //// class3= {/*5*/ //// }/>/*6*/ //// ) -////} -//// +////} +//// ////(function () { //// return
/*grandchildJsxElementAutoformat*/ /////*containedClosingTagAutoformat*/ -//// +////; +//// +////
,{integer}
;/*commaInJsxElement*/ +////
, {integer}
;/*commaInJsxElement2*/ +////);/*closingParenInJsxElement*/ +////) ;/*closingParenInJsxElement2*/ format.document(); goTo.marker("autoformat"); @@ -114,7 +119,7 @@ verify.indentationIs(12); goTo.marker("danglingBracketAutoformat") // TODO: verify.currentLineContentIs(" >"); -verify.currentLineContentIs(" >"); +verify.currentLineContentIs(" >"); goTo.marker("closingTagAutoformat"); verify.currentLineContentIs("
"); @@ -125,4 +130,13 @@ verify.indentationIs(8); goTo.marker("grandchildJsxElementAutoformat"); verify.currentLineContentIs(" "); goTo.marker("containedClosingTagAutoformat"); -verify.currentLineContentIs(" "); \ No newline at end of file +verify.currentLineContentIs(" "); + +goTo.marker("commaInJsxElement"); +verify.currentLineContentIs("
,{integer}
;"); +goTo.marker("commaInJsxElement2"); +verify.currentLineContentIs("
, {integer}
;"); +goTo.marker("closingParenInJsxElement"); +verify.currentLineContentIs(");"); +goTo.marker("closingParenInJsxElement2"); +verify.currentLineContentIs(") ;"); \ No newline at end of file