-
Notifications
You must be signed in to change notification settings - Fork 75
Comment positioning issues #230
Comments
I discovered that this is a problem with @babel/generator and not this library. Completely removing the call to traverse(), comments are still moved around unfavorably. 🙁 Perhaps switching to something like recast would be the best way to move forward. |
I thought I did a better job of handling fixing trailing comments but I didn't consider the case where there were multiple comments between statements. 😞 I also didn't realize that you could pass |
I'd rather not maintain complicated comment code if it can be avoid. Hopefully |
I gave
became
My hunch is that We can't "fix" the |
@jtbandes I wasn't able to reproduce this using a smaller chunk of code with multiple comments in between lines, see playground example. Can you submit a minimal reproducible example as a playground link? |
Here's what I was testing (just modified one of your test cases): For a larger test case you can also see the file I shared above: https://gist.github.com/jtbandes/17606101e1c17d4f8b979bee01a44bb0/revisions |
Thanks. There are two issues:
It looks like the latter issue is happening certain statements, but not all statements. This should be easy to fix, we just need to call The issue with blanks lines not being preserved is more difficult. Previously I had vendored That being said I think the current approach could be improved to better handle
|
I think that's true, but there might be some cases where a totally separate comment block in the middle is surrounded by blank lines:
Right now all these comments get squashed into one block. |
I think there's probably a way to maintain the blank lines without having to vendor |
@jtbandes I've fixed some of the issues with #233. Please give https://deploy-preview-233--flow-to-ts.netlify.app a try and let me know if you're seeing any issues not pertaining to the removal of blank lines (I haven't got to that issue yet). |
Thanks! This does look a lot better so far. I am noticing that some comments are still being moved between adjacent class members, although maybe this is partly because of the blank lines issue: |
|
I wasn't tracking comments in class methods/properties. I've updated the PR to handle those now and have also enabled private class methods/properties. |
I figured out kind of a hacky way to maintain blank lines between single line comments. Adding a |
I'll try it out soon. Regarding recast, did you see their note in the readme that you need to use |
flow-to-ts now seems to be adding blank lines where there weren't any: |
Comment still moving around in some cases: const a = 1;
// foo
export default {}; const a = 1;
// foo
class X {}; Is there any way to ensure that your trackComments function is being run for every type of AST node? |
Same-line comment gets moved to next line: const next = {
foo, // a
bar,
}; Note that there is also some surprising behavior with inner comments, like: const next = {
foo /*hi*/,
bar,
}; But the code I'm working with almost never uses inner comments. |
Blank line added before comment: if (true) {
}
// foo
let x; |
Next-line comment gets moved to same line if there is no next sibling node: function foo() {
let x;
// foo
} |
I tried an older version of the playground and the same blank lines are being inserted there too.
I believe that
Not sure if we'll be able to do anything about this without vendoring
I can fix this. The right only processed comments that appear in both |
I was skimming the README for how to pretty print an AST. I missed the important part:
I also didn't realize that you can specify the parser for |
I also try parsing and pretty print some code without running any transforms on it and it added an extra blank line. Here's the code I used:
and here's the console output:
|
Here's another test case, no transforms:
|
Yeah, it seems like comment positioning is simply a weak point of the Babel AST. Recast may have better workarounds but I think it still needs workarounds. :( It claims to be "nondestructive" so maybe it is worth filing an issue against recast. |
I looked at the following example:
There isn't much we can do about this without patching I think we're going to have to bring back a patched version of I'm going to focus on conversion bugs instead of formatting bugs for the next while. Once more of the conversion issues are addressed I'll return to formatting issues. |
Now that we're consuming the babel source (instead of vendored built files) I can start work on re-adding the blank-line handling that we had a while go. |
Hi! I recently used flow-to-ts and ran into some issues with comment positioning. It looks like you've been working on it recently in #212 and #216, but I found that even running with the latest master, there are some issues where whitespace between near-adjacent comments is not respected, and comments are moved onto preceding lines.
An example of changes produced by flow-to-ts is here: https://gist.github.com/jtbandes/17606101e1c17d4f8b979bee01a44bb0/revisions
Some examples of really surprising behavior:
Comments are moved up to be on the same line as a preceding
}
, even though there was previously a blank line in between. The blank line is now after the comment.Roughly the same thing happens for variable declarations inside a class, meaning that these now appear to be same-line comments on the wrong variable:
I'd be open to helping fix this if you have some pointers to where the problem might be arising from.
I should add that I think the ideal behavior is to leave comments as close to their original positions as possible, and not try to be smart about which comments apply to declarations on the next/previous line.
The text was updated successfully, but these errors were encountered: