From 7139f37201a7e0a1a64774d733a077e60a9ddcd7 Mon Sep 17 00:00:00 2001 From: Jm Date: Fri, 13 Aug 2021 05:01:52 +0800 Subject: [PATCH] fix: nodeWillIndentChild judge for BinaryExpression with JsxElement child (#44695) --- src/services/formatting/smartIndenter.ts | 5 +++ .../formatSelectionJsxWithBinaryExpression.ts | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/cases/fourslash/formatSelectionJsxWithBinaryExpression.ts diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 8da3368742ca9..de22396383207 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -606,6 +606,11 @@ namespace ts.formatting { if (!settings.indentMultiLineObjectLiteralBeginningOnBlankLine && sourceFile && childKind === SyntaxKind.ObjectLiteralExpression) { // TODO: GH#18217 return rangeIsOnOneLine(sourceFile, child!); } + if (parent.kind === SyntaxKind.BinaryExpression && sourceFile && child && childKind === SyntaxKind.JsxElement) { + const parentStartLine = sourceFile.getLineAndCharacterOfPosition(skipTrivia(sourceFile.text, parent.pos)).line; + const childStartLine = sourceFile.getLineAndCharacterOfPosition(skipTrivia(sourceFile.text, child.pos)).line; + return parentStartLine !== childStartLine; + } if (parent.kind !== SyntaxKind.BinaryExpression) { return true; } diff --git a/tests/cases/fourslash/formatSelectionJsxWithBinaryExpression.ts b/tests/cases/fourslash/formatSelectionJsxWithBinaryExpression.ts new file mode 100644 index 0000000000000..f9510e5af78b9 --- /dev/null +++ b/tests/cases/fourslash/formatSelectionJsxWithBinaryExpression.ts @@ -0,0 +1,36 @@ +/// + +//@Filename: file.tsx +//// function TestWidget() { +//// const test = true; +//// return ( +////
+//// {test && +////
+//// /*1*/
some text
/*2*/ +////
some text
+////
some text
+////
+//// } +////
some text
+////
+//// ); +//// } + +format.selection("1", "2"); +verify.currentFileContentIs( + `function TestWidget() { + const test = true; + return ( +
+ {test && +
+
some text
+
some text
+
some text
+
+ } +
some text
+
+ ); +}`)