Skip to content

Commit 59d5585

Browse files
committed
Don't indent properties if an object literal follows directly from another object on the same line
1 parent dfc97db commit 59d5585

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
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.childStartsInlineWithPreviousObject(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: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,26 @@ namespace ts.formatting {
322322
return false;
323323
}
324324

325+
export function childStartsInlineWithPreviousObject(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFileLike): boolean {
326+
if (parent.kind === SyntaxKind.CallExpression) {
327+
const parentCallExpression = <CallExpression>parent;
328+
const currentNode = find(parentCallExpression.arguments, arg => arg.pos === child.pos);
329+
if (!currentNode) return false; // Shouldn't happen
330+
331+
const currentIndex = parentCallExpression.arguments.indexOf(currentNode);
332+
if (currentIndex === 0) return false; // Can't look at previous node if first
333+
334+
const previousNode = parentCallExpression.arguments[currentIndex - 1];
335+
const lineOfPreviousNode = getLineAndCharacterOfPosition(sourceFile, previousNode.getEnd()).line;
336+
337+
if (childStartLine === lineOfPreviousNode) {
338+
return true;
339+
}
340+
}
341+
342+
return false;
343+
}
344+
325345
export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
326346
return node.parent && getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent, sourceFile);
327347
}
@@ -482,6 +502,7 @@ namespace ts.formatting {
482502
case SyntaxKind.ArrayLiteralExpression:
483503
case SyntaxKind.Block:
484504
case SyntaxKind.ModuleBlock:
505+
case SyntaxKind.ObjectLiteralExpression:
485506
case SyntaxKind.TypeLiteral:
486507
case SyntaxKind.MappedType:
487508
case SyntaxKind.TupleType:
@@ -523,8 +544,6 @@ namespace ts.formatting {
523544
return rangeIsOnOneLine(sourceFile, child!);
524545
}
525546
return true;
526-
case SyntaxKind.CallExpression:
527-
return childKind !== SyntaxKind.ObjectLiteralExpression
528547
case SyntaxKind.DoStatement:
529548
case SyntaxKind.WhileStatement:
530549
case SyntaxKind.ForInStatement:

tests/cases/fourslash/formatMultipleFunctionArguments.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
//// prop5: 5,
1212
//// prop6: 6
1313
//// });
14+
////
15+
//// someRandomFunction(
16+
//// { prop7: 1, prop8: 2 },
17+
//// { prop9: 3, prop10: 4 },
18+
//// {
19+
//// prop11: 5,
20+
//// prop2: 6
21+
//// }
22+
//// );
1423

1524
format.document();
1625
verify.currentFileContentIs(`
@@ -23,4 +32,13 @@ someRandomFunction({
2332
}, {
2433
prop5: 5,
2534
prop6: 6
26-
});`);
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)