-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Conversation
@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 |
@rbuckton does this cause conflicts for your work there? |
Not presently. If we do end up introducing the whitespace-removing text writer I will address any conflicts at that point. |
There was a problem hiding this 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: () => { } }); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
b95d586
to
3abd816
Compare
3abd816
to
62bfb06
Compare
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... 🙈🤷♀ |
* Fix semicolon-omitting writer * Use writeTrailingSemicolon for do statements
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
togetTrailingSemicolonDeferringWriter
, and adds a propergetTrailingSemicolonOmittingWriter
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:
}
: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 withTextChanges
, but would be open to discussing other options.