Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix writing multi-line text without semicolons #32903

Merged
merged 2 commits into from
Aug 28, 2019

Conversation

andrewbranch
Copy link
Member

Fixes #18780

I have just learned that my fix in #31801, in the cases that it does work, works entirely by accident. The writer returned by getTrailingSemicolonOmittingWriter was never intended to be a writer that actually drops semicolons; it simply doesn’t write them until you start writing something else. The places where it was used to omit a semicolon were simply single-statement (or even less than a single statement) writes, like printing a signature for quick info. If the writer had continued on to print another statement, the semicolon would then be added.

The reason #31801 worked at all is that the emitter ultimately ended up with a semicolon-omitting writer wrapped inside a semicolon-omitting writer, and as a result would never write a trailing semicolon, even where it’s necessary by the rules of ASI. Technically the current state is pretty wrong, but I think that the bad effects of being so wrong will never actually manifest, because I think the emitter always emits list elements on separate lines.

This fix renames getTrailingSemicolonOmittingWriter to getTrailingSemicolonDeferringWriter, and adds a proper getTrailingSemicolonOmittingWriter which will commit trailing semicolons only if they are not followed by a newline.

It should be noted that, because the writer is generally not syntax-aware and can’t perform lookahead, it fails to omit semicolons where it technically could in two scenarios:

  1. Closing parens of do/while statements:
do {} while (false) console.log()
//                 ^ don’t need one here but we print it anyway
  1. Where the offending token is }:
if (true) {
  1
  2
  3 }
// ^ don’t need one here but we print it anyway

I think these are ok.

Additional note: I didn’t change the behavior of PrinterOptions['omitTrailingSemicolons'] because it’s public API. Everything I changed is internal and applies only to changes made with TextChanges, but would be open to discussing other options.

@andrewbranch andrewbranch requested a review from rbuckton August 14, 2019 23:49
@rbuckton
Copy link
Member

@andrewbranch I should point out that I investigated a more thorough semicolon-eliding writer when I was looking into removing all insignificant whitespace during emit in #25492, specifically here: https://github.com/microsoft/TypeScript/pull/25492/files#diff-942ae3cd2a8bbd85ed86a60cd7c43307R3248

@andrewbranch
Copy link
Member Author

@rbuckton does this cause conflicts for your work there?

@rbuckton
Copy link
Member

Not presently. If we do end up introducing the whitespace-removing text writer I will address any conflicts at that point.

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good? I'm not an expert here so if there is one, they should sign off too.

@@ -15,7 +15,7 @@ verify.codeFix({
constructor() {
}
foo() {
({ bar: () => { } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about our emit with respect to formatting. Did we detect that the input didn't have semicolons, so the output shouldn't either?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. That change was in #31801, but this was a case that didn’t work until now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this only applies to edits made with TextChanges—JS emit to file always uses semicolons.

@andrewbranch
Copy link
Member Author

This is a prerequisite for some of my other 3.7 work, so I’m gonna go ahead and merge. If an expert comes along and finds I’ve done something horrible here... 🙈🤷‍♀

@andrewbranch andrewbranch merged commit cd371da into microsoft:master Aug 28, 2019
timsuchanek pushed a commit to timsuchanek/TypeScript that referenced this pull request Sep 11, 2019
* Fix semicolon-omitting writer

* Use writeTrailingSemicolon for do statements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make adding semicolon configurable for "Extract function"
3 participants