Emit does not preserve empty lines
Hi,
TS Version: 1.1
Given
function foo() {
var x = 10;
var y = 11;
}
We used to get
function foo() {
var x = 10;
var y = 11;
}
In the new compiler the line break is missing
function foo() {
var x = 10;
var y = 11;
}
(Both compilers removed the first empty line, but the new compiler has gone a step further.)
This can affect the experience when debugging JavaScript in the browser.
Adding some background info... The reason the new compiler removes all blank lines is that this is the only thing we can do consistently. Blank line preservation is always going to be a hit or miss thing when it comes to constructs that are subject to rewriting, e.g. class and module declarations. We face exactly the same issues with comment preservation. So, while I'm sympathetic to resolving this, it's not an easy thing to do.
@NoelAbrahams I'm curious what issues you see when debugging?
@ahejlsberg @NoelAbrahams I created a prototype emitted in the original CodePlex project that actually did a terrific job with comment and newline preservation. Even on constructs that got heavily rewritten (like arrow-functions to function expressions) it would almost always do what you intuitively expected it to do.
Line preservation mostly a function of just reusing the line breaks when preserving old code, and using relative spacing when doing transformations. i.e. if you have:
module M {
}
module N {
}
Then when you're emitting the IIFE for 'N' you say "we should keep the trivia between the module we're rewriting and the previous syntactic element".
Here is the before/after for how my emitter prototype worked that preserved comments/newlines with high levels of fidelity:
https://typescript.codeplex.com/SourceControl/latest#tests/Fidelity/emitter2/ecmascript5/Parser.ts https://typescript.codeplex.com/SourceControl/latest#tests/Fidelity/emitter2/ecmascript5/Parser.ts.expected
You can see a lot of examples here as well: https://typescript.codeplex.com/SourceControl/latest#tests/Fidelity/emitter/ecmascript5/
The only feature i didn't implement was 'alignment'. i.e. if you had code that was aligned in some way in the original (very common with parameter declarations), we'd want the emitted code to preserve that as well.
But it would have been very trivial to do.
That said, conversion of contructs from TS to JS did try to preserve indentation. You can see that here: https://typescript.codeplex.com/SourceControl/latest#tests/Fidelity/emitter/ecmascript5/ClassDeclaration/ClassDeclaration2.ts https://typescript.codeplex.com/SourceControl/latest#tests/Fidelity/emitter/ecmascript5/ClassDeclaration/ClassDeclaration2.ts.expected
Note how the statements are properly indendented (even when they spread over multiple lines) even after we convert the nested modules and class into IIFEs
@ahejlsberg, there are no significant issues when debugging in the browser. It just makes it easer to navigate the JavaScript code and locate lines for setting breakpoints when there is an exact correspondence with the actual TypeScript source code.
I could personally live without the empty lines, but since TS has gone to such lengths to preserve and emit _beautiful_ JavaScript (:smile:) it seems only natural that this is implemented.
@ahejlsberg @NoelAbrahams There is one issue that exists with debugging in the browser that is slightly related to this conversation. When using setter/getter chains (like with jquery) or chaining promises, the new line feeds are lost during translation. That being said, it is a huge pain point when working with Arrow functions.
As an example:
(<any> x).a('#test')
.b('test')
.c(() => 'foo')
.d(() => 'bar')
.e(() => 5)
.f(() => 6);
Becomes:
x.a('#test').b('test').c(function () { return 'foo'; }).d(function () { return 'bar'; }).e(function () { return 5; }).f(function () { return 6; });
Using Chrome and sourceMaps, the breakpoints are still skipped.
http://www.typescriptlang.org/Playground#src=(%3Cany%3E%20x).a('%23test')%0A%20%20%20%20.b('test')%0A%09.c(()%20%3D%3E%20'foo')%0A%09.d(()%20%3D%3E%20'bar')%0A%09.e(()%20%3D%3E%205)%0A%09.f(()%20%3D%3E%206)%3B
@mtraynham, actually I think the problem you highlight is a slightly different one.
In previous versions the body of an inline function was always emitted on new lines:
// TS
var x = () => 'foo';
// JS - old
var x = function () {
return 'foo';
};
// JS - new
var x = function () { return 'foo'; };
I too have found this to be a problem - having to occasionally go back and create a function so that I can set a breakpoint when debugging in the browser.
@NoelAbrahams Ahh yes, I have been using this exact same temp solution... I wasn't sure if this was an appropriate bug to contribute this problem to (erasure of line feeds), or should I open another?
I created #2259 to the separate issue.
As an engineering director exploring moving our javascript development community to typescript the new line capability would really help. One of the primary appeal of typescript was maintaining the readability and structure of the code created in typescript generatedJavascript.
Great to see comments are kept in the recent update and addition of the “--removeComments" command line directive.
I would also like this for a similar reason as @timjmartel - when developers see the emitted JS looks nice they are less resistant to adopt. Preserving (at least vertical) whitespace makes the code look less like it was generated by a machine and more like idomatic JS code written by a human.
If our team ever decided to abandon TS and instead continue with the transpiled JS it would be much easier to adopt the emitted JS sources if they had human-friendly whitespace.
In regard to empty lines, would it be possible - for now - to have them emitted, even if they are hit or miss? Such experimental feature could be asked with a “--keepEmptyLines" option. It would be not so much for having nice JS, but for having more readable JS.
In regard to chained function calls, would it be possible to have one call for line, to let users set breakpoints? Again, this feature could be asked with a "--oneCallForLine" option, if it is another "hit or miss" thing.
Thanks for your attention.
Actually, chained function calls could be split by a source code beautifier, therefore it doesn't make sense to embed such feature in TypeScript.
This shouldn't be that hard, shouldn't there just be an option in tsconfig.json
preserveWhitespace: true/false
?
But I am not seeing this as a compiler option: https://www.typescriptlang.org/docs/handbook/compiler-options.html
I just realized. One good reason to not keep whitespace is that it will really help prevent you from accidentally editing the .js instead of the .ts. I guess one thing to do would be to write out the .js files as readonly/execute only. So maybe this is a non-issue.
That being said, I don't think tsc writes out .js files as readonly/execute only, is there a way to configure tsc to do this?
No, not at the moment. Feel free to open up a separate issue for that though.
@DanielRosenwasser you're saying if we want to preserve whitespace that we should open a separate issue? Is this issue not enough? Nevermind LOL more than a month later I realize you were saying to open a separate issue for read/write/execution permissions on transpiled files :)
It would be nice to have this.
+1
+1
+1