diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index fc65d3581bd..78b8a95890e 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -56,9 +56,9 @@ namespace ts.formatting { // for block indentation, we should look for a line which contains something that's not // whitespace. const currentToken = getTokenAtPosition(sourceFile, position); - // for object literal, we want to the indentation work like block - // if { starts in any position (can be in the middle of line) - // the following indentation should treat { as starting of that line (including leading whitespace) + // For object literals, we want indentation to work just like with blocks. + // If the `{` starts in any position (even in the middle of a line), then + // the following indentation should treat `{` as the start of that line (including leading whitespace). // ``` // const a: { x: undefined, y: undefined } = {} // leading 4 whitespaces and { starts in the middle of line // -> @@ -76,7 +76,8 @@ namespace ts.formatting { // y: undefined, // } // ``` - if (options.indentStyle === IndentStyle.Block || currentToken.kind === SyntaxKind.OpenBraceToken) { + const isObjectLiteral = currentToken.kind === SyntaxKind.OpenBraceToken && currentToken.parent.kind === SyntaxKind.ObjectLiteralExpression; + if (options.indentStyle === IndentStyle.Block || isObjectLiteral) { return getBlockIndent(sourceFile, position, options); } @@ -91,7 +92,9 @@ namespace ts.formatting { const containerList = getListByPosition(position, precedingToken.parent, sourceFile); // use list position if the preceding token is before any list items if (containerList && !rangeContainsRange(containerList, precedingToken)) { - return getActualIndentationForListStartLine(containerList, sourceFile, options) + options.indentSize!; // TODO: GH#18217 + const useTheSameBaseIndentation = [SyntaxKind.FunctionExpression, SyntaxKind.ArrowFunction].indexOf(currentToken.parent.kind) !== -1; + const indentSize = useTheSameBaseIndentation ? 0 : options.indentSize!; + return getActualIndentationForListStartLine(containerList, sourceFile, options) + indentSize; // TODO: GH#18217 } return getSmartIndent(sourceFile, position, precedingToken, lineAtPosition, assumeNewLineBeforeCloseBrace, options); diff --git a/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToAnon_FnArgument.ts b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToAnon_FnArgument.ts index c07a741f1ab..28ad1354991 100644 --- a/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToAnon_FnArgument.ts +++ b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToAnon_FnArgument.ts @@ -10,6 +10,6 @@ edit.applyRefactor({ actionDescription: "Convert to anonymous function", newContent: `function doSomething(a){} doSomething(function() { - return 1 + 1; - });`, + return 1 + 1; +});`, }); diff --git a/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToArrow_FnArgIndent.ts b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToArrow_FnArgIndent.ts new file mode 100644 index 00000000000..3c9fe02b6a2 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToArrow_FnArgIndent.ts @@ -0,0 +1,19 @@ +/// + +////console.log(function /*a*/()/*b*/ { +//// console.log(1); +//// console.log(2); +//// return 3; +////}) + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Convert arrow function or function expression", + actionName: "Convert to arrow function", + actionDescription: "Convert to arrow function", + newContent: `console.log(() => { + console.log(1); + console.log(2); + return 3; +})`, +}); diff --git a/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToArrow_FunctionFnArgIndent.ts b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToArrow_FunctionFnArgIndent.ts new file mode 100644 index 00000000000..3108527c7d7 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToArrow_FunctionFnArgIndent.ts @@ -0,0 +1,28 @@ +/// + +////function foo() { +//// const a = { +//// b: builder.create(function /*a*/()/*b*/ { +//// console.log(1); +//// console.log(2); +//// return 3; +//// }) +//// } +////} + + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Convert arrow function or function expression", + actionName: "Convert to arrow function", + actionDescription: "Convert to arrow function", + newContent: `function foo() { + const a = { + b: builder.create(() => { + console.log(1); + console.log(2); + return 3; + }) + } +}`, +});