Closed brian-reichle closed 1 year ago
What happens to trivia, e.g. foo |= false; // a comment
?
When we replace a node with another node (possibly a nested node) we typically copy across the leading and trailing trivia. This means that the outer trivia is retained but inner trivia is lost.
foo |= /* stuff */ false; // a comment
-> ; // a comment
If that empty statement is dissolved into a BlockStatement, we simply delete it, so the trivia is lost.
{
// Comment A
foo |= /* stuff */ false; // Comment B
// Comment C
}
would become:
{
// Comment C
}
It's probably safe to say that // Comment B
should be removed. Whether or not // Comment A
should be removed is probably dependent on what the comment is saying. I don't think there is any approach we can take that will be correct in all cases. We want it to be as close to correct as we can get in as many cases as we can, but people should really be reviewing the changes made and correcting it as necessary.
Perhaps it would make sense to prepend the leading trivia of the removed statement onto the leading trivia of the following statement, with an elastic end-of-line between them.
except for whitespace trivia, I guess?
fixes #191
When collapsing an and/or assignment to just the LHS, we mark it as "weak". When visiting an ExpressionStatement, we can detect the weak expression and return the "discardable" empty statement. There is already logic to dissolve the discardable empty statement if it happens to land in a BlockStatement.