From 9eb6424b8f6244b9a74cbea47c76b209c41b2cef Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 22 Sep 2020 17:16:09 -0700 Subject: [PATCH] Fix indentation of arrow functions returning parenthesized expressions (#40677) * Fix indentation of arrow functions returning parenthesized expressions * Add more test cases --- src/harness/fourslashImpl.ts | 3 +- src/services/formatting/smartIndenter.ts | 6 ++- ...ingArrowFunctionParenthesizedExpression.ts | 47 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/formattingArrowFunctionParenthesizedExpression.ts diff --git a/src/harness/fourslashImpl.ts b/src/harness/fourslashImpl.ts index 299403d8974..a87d0a9e1da 100644 --- a/src/harness/fourslashImpl.ts +++ b/src/harness/fourslashImpl.ts @@ -2461,8 +2461,7 @@ namespace FourSlash { const { fileName } = this.activeFile; const before = this.getFileContent(fileName); this.formatDocument(); - const after = this.getFileContent(fileName); - this.assertObjectsEqual(after, before); + this.verifyFileContent(fileName, before); } public verifyTextAtCaretIs(text: string) { diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 23011643811..b8af97350f7 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -558,11 +558,15 @@ namespace ts.formatting { case SyntaxKind.FunctionDeclaration: case SyntaxKind.FunctionExpression: case SyntaxKind.MethodDeclaration: - case SyntaxKind.ArrowFunction: case SyntaxKind.Constructor: case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: return childKind !== SyntaxKind.Block; + case SyntaxKind.ArrowFunction: + if (sourceFile && childKind === SyntaxKind.ParenthesizedExpression) { + return rangeIsOnOneLine(sourceFile, child!); + } + return childKind !== SyntaxKind.Block; case SyntaxKind.ExportDeclaration: return childKind !== SyntaxKind.NamedExports; case SyntaxKind.ImportDeclaration: diff --git a/tests/cases/fourslash/formattingArrowFunctionParenthesizedExpression.ts b/tests/cases/fourslash/formattingArrowFunctionParenthesizedExpression.ts new file mode 100644 index 00000000000..de1021bf1c6 --- /dev/null +++ b/tests/cases/fourslash/formattingArrowFunctionParenthesizedExpression.ts @@ -0,0 +1,47 @@ +/// + +// @Filename: Bar.tsx +//// export const Bar = ({ +//// foo, +//// bar, +//// }: any) => ( +////
Hello world
+//// ) +//// +//// export const Bar2 = ({ +//// foo, +//// bar, +//// }) => (
Hello world
) +//// +//// export const Bar2 = ({ +//// foo, +//// bar, +//// }) =>
Hello world
+//// +//// export const Bar3 = ({ +//// foo, +//// bar, +//// }) => +//// (
Hello world
) +//// +//// export const Bar4 = ({ +//// foo, +//// bar, +//// }) => +////
Hello world
+//// +//// export const Bar5 = () => ( +////
Hello world
+//// ) +//// +//// export const Bar6 = () => (
Hello world
) +//// +//// export const Bar7 = () =>
Hello world
+//// +//// export const Bar8 = () => +//// (
Hello world
) +//// +//// export const Bar9 = () => +////
Hello world
+ +verify.formatDocumentChangesNothing();