TypeScript 1.4 indentation issue on chained function calls
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
+1
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
});
Can this please be moved back to Milestone 1.5? This is a real minute-by-minute hassle.
I have to agree, it is making the code look far more difficult to read and follow than it really needs to be.
@mhegazy @vladima how much work is it to fix this? Can we get it in 1.5?
Not to mention all the unnecessary diffs that will occur after this is fixed. So sooner the better.
PRs are always welcomed :)
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.
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:
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 perhaps (https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#formatcodeoptions) insertSpaceAfterCommaDelimiter: true?
@cwmacdon this is known issue that is already fixed in master
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.
I couldn't figure out a good way to step through the code and see useful information about the state.
I suspect console.log
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.
try:
jake runtests-browser tests=smartIndentFunctionAsParameter
then use F12 in IE/Chrome to debug
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.
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.
Is there a quick way to see the full syntax tree of the snippet?
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.