Skip to content

Commit 65e4d60

Browse files
authored
Fix formatter determination of initial indentation of list items after the first (#42352)
* Add failing test * Fix determination of initial indentation of list items after the first
1 parent 2730cb2 commit 65e4d60

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,25 @@ namespace ts.formatting {
182182

183183
if (useActualIndentation) {
184184
// check if current node is a list item - if yes, take indentation from it
185-
let actualIndentation = getActualIndentationForListItem(current, sourceFile, options, !parentAndChildShareLine);
185+
const firstListChild = getContainingList(current, sourceFile)?.[0];
186+
// A list indents its children if the children begin on a later line than the list itself:
187+
//
188+
// f1( L0 - List start
189+
// { L1 - First child start: indented, along with all other children
190+
// prop: 0
191+
// },
192+
// {
193+
// prop: 1
194+
// }
195+
// )
196+
//
197+
// f2({ L0 - List start and first child start: children are not indented.
198+
// prop: 0 Object properties are indented only one level, because the list
199+
// }, { itself contributes nothing.
200+
// prop: 1 L3 - The indentation of the second object literal is best understood by
201+
// }) looking at the relationship between the list and *first* list item.
202+
const listIndentsChild = !!firstListChild && getStartLineAndCharacterForNode(firstListChild, sourceFile).line > containingListOrParentStart.line;
203+
let actualIndentation = getActualIndentationForListItem(current, sourceFile, options, listIndentsChild);
186204
if (actualIndentation !== Value.Unknown) {
187205
return actualIndentation + indentationDelta;
188206
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// console.log({
4+
//// }, {
5+
//// /*1*/ a: 1,
6+
//// /*2*/ b: 2
7+
//// })
8+
9+
format.selection("1", "2");
10+
verify.currentFileContentIs(
11+
`console.log({
12+
}, {
13+
a: 1,
14+
b: 2
15+
})`);

0 commit comments

Comments
 (0)