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

+1

giuseppelt avatar Feb 04 '15 16:02 giuseppelt

It's even worse when you use the errorCallback of promise.then(...) and/or finally()

this.somethingWithAPromise()
    .then(() => {
    // more code
},
    (reason) => {
        // more code
    })
    .finally(() => {
    // more code
});

mhov avatar Feb 10 '15 18:02 mhov

Can this please be moved back to Milestone 1.5? This is a real minute-by-minute hassle.

ghost avatar Mar 20 '15 13:03 ghost

I have to agree, it is making the code look far more difficult to read and follow than it really needs to be.

Anupheaus avatar Mar 20 '15 14:03 Anupheaus

@mhegazy @vladima how much work is it to fix this? Can we get it in 1.5?

danquirk avatar Mar 20 '15 18:03 danquirk

Not to mention all the unnecessary diffs that will occur after this is fixed. So sooner the better.

NoelAbrahams avatar Mar 20 '15 18:03 NoelAbrahams

PRs are always welcomed :)

mhegazy avatar Mar 22 '15 18:03 mhegazy

Seems like a fairly exacting repair - even if someone submitted a fix, how could they or you be confident that it didn't make other formatting scenarios break? I'd be more willing to look into it if internal developers gave notes/guidance on what files/area of the project need to be considered, possibly including unit tests it needs to pass and how to run them. I guess it depends on which would be more work.

ghost avatar Mar 22 '15 20:03 ghost

A recent example of a test : https://github.com/Microsoft/TypeScript/pull/2108/files#diff-1437e63151cc8482fc0ffc4f1d0fc3ddR2

If the tests pass. It hasn't broken old stuff (hopefully) :+1:

basarat avatar Mar 22 '15 21:03 basarat

Another instance of TS 1.4 changed code format behaviour: It is removing the space after the first parameter in _.each

TS 1.3:

_.each(foo, (bar: string): void => {
    // code
});

TS 1.4

_.each(foo,(bar: string): void => {
    // code
});

cwmacdon avatar Mar 23 '15 02:03 cwmacdon

@cwmacdon perhaps (https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#formatcodeoptions) insertSpaceAfterCommaDelimiter: true?

basarat avatar Mar 23 '15 03:03 basarat

@cwmacdon this is known issue that is already fixed in master

vladima avatar Mar 23 '15 05:03 vladima

I'm not giving up but I'm out of time for now so I'll share my learning curve:

I created a new test case called tests\cases\fourslash\smartIndentFunctionAsParameter.ts as follows. I prefixed smartIndent rather than format because ... well I don't know why. Seems like formatter rules are about spaces as if there's only one line of code and smartIndent is for once linebreaks are introduced - but I probably have that wrong. Anyway, the name of the file doesn't affect the code at all and the new file gets discovered by the jake script automatically.

/// <reference path='fourslash.ts' />
/////*0*/deferred
////    /*1*/.done(data => {
////        /*2*/var a = 1;
////    /*3*/})
////    /*4*/.fail(function () {
////        /*5*/var a = 1;
////    /*6*/})
////    /*7*/.always(() =>
////        /*8*/null);
////    /*9*/.always(() => null);
/////*10*/var x = 0;
format.document();
goTo.marker("0"); debug.printCurrentFileStateWithWhitespace(); // <-- this call generates an "Invalid Invocation" error in the browser-based runtests
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(info => {");
goTo.marker("5"); verify.currentLineContentIs("        var b = 2;");
goTo.marker("6"); verify.currentLineContentIs("    })");
goTo.marker("7"); verify.currentLineContentIs("    .always(() =>");
goTo.marker("8"); verify.currentLineContentIs("        null);");
goTo.marker("9"); verify.currentLineContentIs("    .always(() => null);");
goTo.marker("10"); verify.currentLineContentIs("var x = 0;");

To test:

> jake tests
> jake runtests tests=smartIndent

I'm pretty sure the fix needs to go in src\services\formatting\smartIndenter.ts:getIndentationForNodeWorker().

My guess was that the && !parentAndChildShareLine condition on line 127 needs to be expanded to include cases where they START on the same line, but where the child spans multiple lines. I tried && (!parentAndChildShareLine || sourceFile.getLineAndCharacterOfPosition(current.end).line !== currentStart.line) but that merely broke other test cases and didn't seem to affect THIS one. Probably there are other/shorter/clearer ways to code the condition but I started there. I tried some variations on it including checking current.kind or parent.kind being an ArrowFunction or a FunctionExpression, but no luck - no effect on my formatted case and it just broke the others.

To try changed smartIndenter.ts code:

> jake local
> jake runtests tests=smartIndent

or, most specifically:

> jake runtests tests=smartIndentFunctionAsParameter

I couldn't figure out a good way to step through the code and see useful information about the state.

ghost avatar Mar 23 '15 17:03 ghost

I couldn't figure out a good way to step through the code and see useful information about the state.

I suspect console.log

basarat avatar Mar 23 '15 20:03 basarat

I tried that but messages don't appear in the shell, debug.print* calls fail in the browser, and I didn't find documentation on how to get access to the state information to print it myself.

ghost avatar Mar 23 '15 20:03 ghost

try:

jake runtests-browser tests=smartIndentFunctionAsParameter

then use F12 in IE/Chrome to debug

mhegazy avatar Mar 23 '15 21:03 mhegazy

As noted, I get an "illegal invocation" error on the debug.print*() methods in the browser. ... which are the only obvious way to supposedly see what's going on, rather than dealing with character/line offsets.

ghost avatar Mar 23 '15 21:03 ghost

The problem is that the browser tests redirect to a web server all the host interaction, and we are handling the print ones correctly now. but debugging should work, like if you put a "debugger;" in your fourslash tests, run the tests with F12 on, the debugger should break.

mhegazy avatar Mar 24 '15 16:03 mhegazy

Is there a quick way to see the full syntax tree of the snippet?

ghost avatar Mar 24 '15 17:03 ghost

I usually break in the debugger and investigate the nodes i care about. if you are talking about tree serialization to string, then no. though you can find an example on how to serialize the tree here.

mhegazy avatar Mar 24 '15 17:03 mhegazy