Don't indent properties if an object literal follows directly from another object on the same line

This commit is contained in:
Orta Therox 2019-07-12 15:21:57 -04:00
parent dfc97db323
commit 59d5585814
3 changed files with 43 additions and 3 deletions

View file

@ -490,6 +490,9 @@ namespace ts.formatting {
else if (SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) {
return { indentation: parentDynamicIndentation.getIndentation(), delta };
}
else if (SmartIndenter.childStartsInlineWithPreviousObject(parent, node, startLine, sourceFile)) {
return { indentation: parentDynamicIndentation.getIndentation(), delta };
}
else {
return { indentation: parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta(node), delta };
}

View file

@ -322,6 +322,26 @@ namespace ts.formatting {
return false;
}
export function childStartsInlineWithPreviousObject(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFileLike): boolean {
if (parent.kind === SyntaxKind.CallExpression) {
const parentCallExpression = <CallExpression>parent;
const currentNode = find(parentCallExpression.arguments, arg => arg.pos === child.pos);
if (!currentNode) return false; // Shouldn't happen
const currentIndex = parentCallExpression.arguments.indexOf(currentNode);
if (currentIndex === 0) return false; // Can't look at previous node if first
const previousNode = parentCallExpression.arguments[currentIndex - 1];
const lineOfPreviousNode = getLineAndCharacterOfPosition(sourceFile, previousNode.getEnd()).line;
if (childStartLine === lineOfPreviousNode) {
return true;
}
}
return false;
}
export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
return node.parent && getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent, sourceFile);
}
@ -482,6 +502,7 @@ namespace ts.formatting {
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.Block:
case SyntaxKind.ModuleBlock:
case SyntaxKind.ObjectLiteralExpression:
case SyntaxKind.TypeLiteral:
case SyntaxKind.MappedType:
case SyntaxKind.TupleType:
@ -523,8 +544,6 @@ namespace ts.formatting {
return rangeIsOnOneLine(sourceFile, child!);
}
return true;
case SyntaxKind.CallExpression:
return childKind !== SyntaxKind.ObjectLiteralExpression
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.ForInStatement:

View file

@ -11,6 +11,15 @@
//// prop5: 5,
//// prop6: 6
//// });
////
//// someRandomFunction(
//// { prop7: 1, prop8: 2 },
//// { prop9: 3, prop10: 4 },
//// {
//// prop11: 5,
//// prop2: 6
//// }
//// );
format.document();
verify.currentFileContentIs(`
@ -23,4 +32,13 @@ someRandomFunction({
}, {
prop5: 5,
prop6: 6
});`);
});
someRandomFunction(
{ prop7: 1, prop8: 2 },
{ prop9: 3, prop10: 4 },
{
prop11: 5,
prop2: 6
}
);`);