Skip to content

Commit 25aecd4

Browse files
Minh QuyDanielRosenwasser
andauthored
fix(48109) - 'Convert to arrow function' refactoring adds extra indent (#48440)
* fix(48109) - Remove extra indent when converting to arrow function * fix(48109) - Only treat curly brace in object literal as block * Apply suggestions from code review Co-authored-by: Daniel Rosenwasser <[email protected]>
1 parent 2767197 commit 25aecd4

4 files changed

+57
-7
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ namespace ts.formatting {
5656
// for block indentation, we should look for a line which contains something that's not
5757
// whitespace.
5858
const currentToken = getTokenAtPosition(sourceFile, position);
59-
// for object literal, we want to the indentation work like block
60-
// if { starts in any position (can be in the middle of line)
61-
// the following indentation should treat { as starting of that line (including leading whitespace)
59+
// For object literals, we want indentation to work just like with blocks.
60+
// If the `{` starts in any position (even in the middle of a line), then
61+
// the following indentation should treat `{` as the start of that line (including leading whitespace).
6262
// ```
6363
// const a: { x: undefined, y: undefined } = {} // leading 4 whitespaces and { starts in the middle of line
6464
// ->
@@ -76,7 +76,8 @@ namespace ts.formatting {
7676
// y: undefined,
7777
// }
7878
// ```
79-
if (options.indentStyle === IndentStyle.Block || currentToken.kind === SyntaxKind.OpenBraceToken) {
79+
const isObjectLiteral = currentToken.kind === SyntaxKind.OpenBraceToken && currentToken.parent.kind === SyntaxKind.ObjectLiteralExpression;
80+
if (options.indentStyle === IndentStyle.Block || isObjectLiteral) {
8081
return getBlockIndent(sourceFile, position, options);
8182
}
8283

@@ -91,7 +92,9 @@ namespace ts.formatting {
9192
const containerList = getListByPosition(position, precedingToken.parent, sourceFile);
9293
// use list position if the preceding token is before any list items
9394
if (containerList && !rangeContainsRange(containerList, precedingToken)) {
94-
return getActualIndentationForListStartLine(containerList, sourceFile, options) + options.indentSize!; // TODO: GH#18217
95+
const useTheSameBaseIndentation = [SyntaxKind.FunctionExpression, SyntaxKind.ArrowFunction].indexOf(currentToken.parent.kind) !== -1;
96+
const indentSize = useTheSameBaseIndentation ? 0 : options.indentSize!;
97+
return getActualIndentationForListStartLine(containerList, sourceFile, options) + indentSize; // TODO: GH#18217
9598
}
9699

97100
return getSmartIndent(sourceFile, position, precedingToken, lineAtPosition, assumeNewLineBeforeCloseBrace, options);

tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_ToAnon_FnArgument.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ edit.applyRefactor({
1010
actionDescription: "Convert to anonymous function",
1111
newContent: `function doSomething(a){}
1212
doSomething(function() {
13-
return 1 + 1;
14-
});`,
13+
return 1 + 1;
14+
});`,
1515
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////console.log(function /*a*/()/*b*/ {
4+
//// console.log(1);
5+
//// console.log(2);
6+
//// return 3;
7+
////})
8+
9+
goTo.select("a", "b");
10+
edit.applyRefactor({
11+
refactorName: "Convert arrow function or function expression",
12+
actionName: "Convert to arrow function",
13+
actionDescription: "Convert to arrow function",
14+
newContent: `console.log(() => {
15+
console.log(1);
16+
console.log(2);
17+
return 3;
18+
})`,
19+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function foo() {
4+
//// const a = {
5+
//// b: builder.create(function /*a*/()/*b*/ {
6+
//// console.log(1);
7+
//// console.log(2);
8+
//// return 3;
9+
//// })
10+
//// }
11+
////}
12+
13+
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Convert arrow function or function expression",
17+
actionName: "Convert to arrow function",
18+
actionDescription: "Convert to arrow function",
19+
newContent: `function foo() {
20+
const a = {
21+
b: builder.create(() => {
22+
console.log(1);
23+
console.log(2);
24+
return 3;
25+
})
26+
}
27+
}`,
28+
});

0 commit comments

Comments
 (0)