TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

TypeScript 1.4 indentation issue on chained function calls

Open basarat opened this issue 11 years ago • 23 comments

Before we would have:

var doSomethingThatReturnsAPromise: any;
this.doSomethingThatReturnsAPromise()
    .then(() => {
        // more code
    })
    .then(() => {
        // even more code
    });

Now we have

var doSomethingThatReturnsAPromise: any;
this.doSomethingThatReturnsAPromise()
    .then(() => {
    // more code
})
    .then(() => {
    // even more code
});

Raised from SO

basarat avatar Feb 03 '15 22:02 basarat

OK. Now I realize the name of the test should be formattingFunctionInParameterList.ts and with these corrections:

/// <reference path='fourslash.ts' />

////{
////    deferred/*0*/
////        .done(data => {/*1*/
////            var a = 1;/*2*/
////        })/*3*/
////        .fail(function() {/*4*/
////            var b = 2;/*5*/
////        })/*6*/
////        .always(() =>/*7*/
////            3)/*8*/
////        .then(() => 4);/*9*/
////}
////var x = 0;/*10*/

//debugger;
format.document();
//goTo.marker("0"); debug.printCurrentFileStateWithWhitespace();
goTo.marker("0");  verify.currentLineContentIs("    deferred");
goTo.marker("1");  verify.currentLineContentIs("        .done(data => {");
goTo.marker("2");  verify.currentLineContentIs("            var a = 1;");
goTo.marker("3");  verify.currentLineContentIs("        })");
goTo.marker("4");  verify.currentLineContentIs("        .fail(function() {"); // missing space after function??
goTo.marker("5");  verify.currentLineContentIs("            var b = 2;");
goTo.marker("6");  verify.currentLineContentIs("        })");
goTo.marker("7");  verify.currentLineContentIs("        .always(() =>");
goTo.marker("8");  verify.currentLineContentIs("            3)");
goTo.marker("9");  verify.currentLineContentIs("        .then(() => 4);");
goTo.marker("10"); verify.currentLineContentIs("var x = 0;");

The fix perhaps is instead in formatting.ts computeIndentation().

@@ -406,7 +406,8 @@ module ts.formatting {

             var delta = SmartIndenter.shouldIndentChildNode(node.kind, SyntaxKind.Unknown) ? options.IndentSize : 0;

-            if (effectiveParentStartLine === startLine) {
+            if (effectiveParentStartLine === startLine &&
+                !((node.kind === SyntaxKind.ArrowFunction || node.kind === SyntaxKind.FunctionExpression) && parent.kind === SyntaxKind.CallExpression)) {
                 // if node is located on the same line with the parent
                 // - inherit indentation from the parent
                 // - push children if either parent of node itself has non-zero delta

But this still makes 6 other fourslash tests fail, so obviously I'm not close.

ghost avatar Mar 24 '15 21:03 ghost

Oh, also, this single command line makes retesting just that much less tedious:

jake local && jake tests && jake runtests-browser browser=chrome tests=formattingFunctionInParameterList

ghost avatar Mar 25 '15 13:03 ghost

You shouldn't need to do jake local and jake tests like that. jake runtests (and runtests-browser) have those tasks as dependencies. You should be able to simple say jake runtests-browser b=chrome t=<yourtest> and the compiler and harness will be rebuilt if necessary.

danquirk avatar Mar 25 '15 19:03 danquirk