TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Emit does not preserve empty lines

Open NoelAbrahams opened this issue 12 years ago • 45 comments

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.

NoelAbrahams avatar Oct 07 '14 10:10 NoelAbrahams

I would also like to see this implemented, with a few specific reasons in mind:

  • Consistency with general design philosophy per NoelAbrahams' comment above
  • Ciantic's comment about needing to deliver JS elsewhere
  • Being able to cleanly compare JS generated through TS and JS that existed before, where demonstrating a close match is important to adoption
  • Not having long lines requiring horizontal scrolling in the emitted code, where the source included line breaks to make things neat and beautiful and meeting linter/style specs (also commented on above)
  • Being able to continue working with transpiled JS if all these overhead costs of TypeScript become too much; anticipating difficulty here creates lock-in costs that raise barriers to adoption in the first place.

This PR, discussed in the 3.9 release notes, show code actions preserving newlines; why can't the emitted code do something similar?

wbt avatar Feb 16 '22 21:02 wbt

Are there any updates for almost 10 years? I would like to be able to configure preserving new lines

devope avatar Jan 03 '24 07:01 devope

I'm changing this to Won't Fix. Reasons:

  • This has obviously not been a key blocker for anyone
  • Of all the "keep trivia" problems we have, this is surely the lowest-priority among them
  • At this point, many alternative transpilers with different configurable behaviors around comments/whitespace exist; if you need truly need newline preservation, you can use one of those
  • Performance remains top-of-mind for us and anything that makes emit slower needs to be carrying a lot more value than newline preservation.

RyanCavanaugh avatar Jan 03 '24 20:01 RyanCavanaugh

At this point, many alternative transpilers with different configurable behaviors around comments/whitespace exist

Does anyone know of a good option for this?

rattrayalex avatar Jan 04 '24 01:01 rattrayalex

At this point, many alternative transpilers with different configurable behaviors around comments/whitespace exist

Does anyone know of a good option for this?

It depends on your exact needs. There are several suggestions in this issue and a list of other transpilers is as follows. Note that this list hasn't been sanitized for the specific requirement:

favna avatar Jan 04 '24 09:01 favna