Skip to content

Commit 4bb0aae

Browse files
author
Orta
authored
Merge pull request #32359 from orta/fix_14589
Don't add extra indentation for objects inside function parameters
2 parents 303297a + 1d78218 commit 4bb0aae

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

src/services/formatting/formatting.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ namespace ts.formatting {
490490
else if (SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) {
491491
return { indentation: parentDynamicIndentation.getIndentation(), delta };
492492
}
493+
else if (SmartIndenter.argumentStartsOnSameLineAsPreviousArgument(parent, node, startLine, sourceFile)) {
494+
return { indentation: parentDynamicIndentation.getIndentation(), delta };
495+
}
493496
else {
494497
return { indentation: parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta(node), delta };
495498
}

src/services/formatting/smartIndenter.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,25 @@ namespace ts.formatting {
322322
return false;
323323
}
324324

325+
export function argumentStartsOnSameLineAsPreviousArgument(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFileLike): boolean {
326+
if (isCallOrNewExpression(parent)) {
327+
if (!parent.arguments) return false;
328+
329+
const currentNode = Debug.assertDefined(find(parent.arguments, arg => arg.pos === child.pos));
330+
const currentIndex = parent.arguments.indexOf(currentNode);
331+
if (currentIndex === 0) return false; // Can't look at previous node if first
332+
333+
const previousNode = parent.arguments[currentIndex - 1];
334+
const lineOfPreviousNode = getLineAndCharacterOfPosition(sourceFile, previousNode.getEnd()).line;
335+
336+
if (childStartLine === lineOfPreviousNode) {
337+
return true;
338+
}
339+
}
340+
341+
return false;
342+
}
343+
325344
export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
326345
return node.parent && getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent, sourceFile);
327346
}

tests/cases/fourslash/consistenceOnIndentionsOfObjectsInAListAfterFormatting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ format.document();
88
goTo.marker("1");
99
verify.currentLineContentIs("}, {");
1010
goTo.marker("2");
11-
verify.currentLineContentIs(" });");
11+
verify.currentLineContentIs("});");
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////
4+
//// someRandomFunction({
5+
//// prop1: 1,
6+
//// prop2: 2
7+
//// }, {
8+
//// prop3: 3,
9+
//// prop4: 4
10+
//// }, {
11+
//// prop5: 5,
12+
//// prop6: 6
13+
//// });
14+
////
15+
//// someRandomFunction(
16+
//// { prop7: 1, prop8: 2 },
17+
//// { prop9: 3, prop10: 4 },
18+
//// {
19+
//// prop11: 5,
20+
//// prop2: 6
21+
//// }
22+
//// );
23+
24+
format.document();
25+
verify.currentFileContentIs(`
26+
someRandomFunction({
27+
prop1: 1,
28+
prop2: 2
29+
}, {
30+
prop3: 3,
31+
prop4: 4
32+
}, {
33+
prop5: 5,
34+
prop6: 6
35+
});
36+
37+
someRandomFunction(
38+
{ prop7: 1, prop8: 2 },
39+
{ prop9: 3, prop10: 4 },
40+
{
41+
prop11: 5,
42+
prop2: 6
43+
}
44+
);`);

0 commit comments

Comments
 (0)