Skip to content

don't apply rule SpaceBetweenStatements for JsxText #9376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.IsNotForContext, Rules.IsCurrentParentNotJsxText, Rules.IsNextParentNotJsxExpression), 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));
Expand Down Expand Up @@ -723,6 +723,14 @@ namespace ts.formatting {
return context.TokensAreOnSameLine() && context.contextNode.kind !== SyntaxKind.JsxText;
}

static IsCurrentParentNotJsxText(context: FormattingContext): boolean {
return context.currentTokenParent.kind !== SyntaxKind.JsxText;
}

static IsNextParentNotJsxExpression(context: FormattingContext): boolean {
return context.nextTokenParent.kind !== SyntaxKind.JsxExpression;
}

static IsNotBeforeBlockInFunctionDeclarationContext(context: FormattingContext): boolean {
return !Rules.IsFunctionDeclContext(context) && !Rules.IsBeforeBlockContext(context);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/fourslash/formattingJsxElements2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />

//@Filename: file.tsx
////function test1(param) {
//// return (
//// <span>({param})/*1*/</span>
//// );
////}
////function test2(param) {
//// return (
//// <Route routes={getRoutes()/*2*/} id={("")} other={ "" } />
//// );
////}

format.document();
goTo.marker("1");
verify.currentLineContentIs(" <span>({param})</span>");
goTo.marker("2");
verify.currentLineContentIs(` <Route routes={getRoutes()} id={("")} other={ "" } />`);