Merge pull request #32359 from orta/fix_14589
Don't add extra indentation for objects inside function parameters
This commit is contained in:
commit
4bb0aaea06
|
@ -490,6 +490,9 @@ namespace ts.formatting {
|
|||
else if (SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) {
|
||||
return { indentation: parentDynamicIndentation.getIndentation(), delta };
|
||||
}
|
||||
else if (SmartIndenter.argumentStartsOnSameLineAsPreviousArgument(parent, node, startLine, sourceFile)) {
|
||||
return { indentation: parentDynamicIndentation.getIndentation(), delta };
|
||||
}
|
||||
else {
|
||||
return { indentation: parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta(node), delta };
|
||||
}
|
||||
|
|
|
@ -322,6 +322,25 @@ namespace ts.formatting {
|
|||
return false;
|
||||
}
|
||||
|
||||
export function argumentStartsOnSameLineAsPreviousArgument(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFileLike): boolean {
|
||||
if (isCallOrNewExpression(parent)) {
|
||||
if (!parent.arguments) return false;
|
||||
|
||||
const currentNode = Debug.assertDefined(find(parent.arguments, arg => arg.pos === child.pos));
|
||||
const currentIndex = parent.arguments.indexOf(currentNode);
|
||||
if (currentIndex === 0) return false; // Can't look at previous node if first
|
||||
|
||||
const previousNode = parent.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);
|
||||
}
|
||||
|
|
|
@ -8,4 +8,4 @@ format.document();
|
|||
goTo.marker("1");
|
||||
verify.currentLineContentIs("}, {");
|
||||
goTo.marker("2");
|
||||
verify.currentLineContentIs(" });");
|
||||
verify.currentLineContentIs("});");
|
||||
|
|
44
tests/cases/fourslash/formatMultipleFunctionArguments.ts
Normal file
44
tests/cases/fourslash/formatMultipleFunctionArguments.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
/// <reference path="fourslash.ts"/>
|
||||
|
||||
////
|
||||
//// someRandomFunction({
|
||||
//// prop1: 1,
|
||||
//// prop2: 2
|
||||
//// }, {
|
||||
//// prop3: 3,
|
||||
//// prop4: 4
|
||||
//// }, {
|
||||
//// prop5: 5,
|
||||
//// prop6: 6
|
||||
//// });
|
||||
////
|
||||
//// someRandomFunction(
|
||||
//// { prop7: 1, prop8: 2 },
|
||||
//// { prop9: 3, prop10: 4 },
|
||||
//// {
|
||||
//// prop11: 5,
|
||||
//// prop2: 6
|
||||
//// }
|
||||
//// );
|
||||
|
||||
format.document();
|
||||
verify.currentFileContentIs(`
|
||||
someRandomFunction({
|
||||
prop1: 1,
|
||||
prop2: 2
|
||||
}, {
|
||||
prop3: 3,
|
||||
prop4: 4
|
||||
}, {
|
||||
prop5: 5,
|
||||
prop6: 6
|
||||
});
|
||||
|
||||
someRandomFunction(
|
||||
{ prop7: 1, prop8: 2 },
|
||||
{ prop9: 3, prop10: 4 },
|
||||
{
|
||||
prop11: 5,
|
||||
prop2: 6
|
||||
}
|
||||
);`);
|
Loading…
Reference in a new issue