-
-
Notifications
You must be signed in to change notification settings - Fork 75
Discussion: Attach Comments Implementation/Outstanding Issues #34
Conversation
// TODO: Discuss - this approach doesn't seem right... | ||
// In the test this Identifier should not have trailingComments so naively delete | ||
// - Just because it doesn't in this test, doesn't mean it couldn't have them? | ||
var functionId = convertChild(node.name); |
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'm general, nodes don't have trailingComments
or leadingComments
if they are empty. If that's a pain, we could try leaving them for 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.
Just to clarify, what do you mean by an "empty" node here?
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.
If trailingComments
is an array of zero length, then we just removed trailingComments
from the node. Same for leadingComments
.
Nothing looks to hacky to me, nice job so far. The switch fall though tests are definitely important because they affect the ESLint no-fallthrough rule. |
Ok, cool. As I mentioned, I am moving flat this weekend so I will not be able to pick up on this again for a while. If you have any further thoughts please let me know, otherwise I will just continue as and when I can. |
👍 Good luck moving. I may or may not try to pick this up in the meantime, depending on how I'm feeling. |
FYI: There were some bugs in the Espree comment attachment code that just got fixed. You may want to re-port that logic and the associated tests to double-check. |
Looping back to this at last! Merging in the espree changes you mentioned has indeed changed things - it has upped the failing test count from 3 to 5 :/ I feel like I am back up to speed on this now, and will find time to properly attack it later in the week. |
Great, thanks! |
LGTM |
Updated the PR to include the latest changes from master, working on remaining issues now |
I have done quite a lot of digging today, and am going to update my original description on this PR to note what I have found. Unfortunately, it strongly seems like we will need to reimplement attach-comment.js specifically for this parser, as we cannot replicate how it is done in Espree (down to fundamental differences between TypeScript Compiler and Acorn). |
LGTM |
I am not 100% familiar with the code, but I read the writeup I think that's probably a good approach. @mhegazy might have some thoughts too. If you didn't want to hold each of the comments all at once before the ESTree tree is constructed, you would also be able to scan and re-populate comments starting from each node's full start, though I don't know what sorts of savings you'd see if any. |
In the interest of moving this forward, why don't we simplify and just make sure that that |
That's a great suggestion on how to unblock this most quickly, thanks @nzakas. I believe we are just about there with fulfilling the mandate of a correct |
Closing in favour of #49 |
Description Updated: Saturday, 16 July
Original Idea
Initial roadblock:
Acorn.onComment
to allow us to collect comment tokens as we parse the rest of the ASTWhen first digging through the TypeScript documentation, the solution would appear to be to use the TypeScript helpers
ts.getLeadingCommentRanges()
andts.getTrailingCommentRanges()
on each node as it is parsed and converted.However, it turns out that will not work for some of our more "interesting" test case sources, such as:
surrounding-while-loop-comments.src.js
Main Issue:
TypeScript has a fundamentally different approach to trivia ownership.
From the TypeScript Wiki:
So, in the surrounding-while-loop-comments.src.js example, the fact that there is no newline before
/* infinite */
means it is not classed as aleadingComment
for theWhileStatement
.surrounding-while-loop-comments.src.js - NO NEWLINE
Nothing returned from
ts.getLeadingCommentRanges()
forWhileStatement
nodesurrounding-while-loop-comments.src.js - NEWLINE
/* infinite */
returned fromts.getLeadingCommentRanges()
forWhileStatement
nodeAlternative Approach
createScanner()
method, withskipTrivia
set to false, so that we can identify comment tokens.extra.leadingComments
andextra.trailingComments
arrays in a very similar way to Espree.Using this method to parse and convert, combined with Espree's original attach-comment logic, is where we are currently at with this PR.
We have 5 failing tests:
The theme that runs through them is having too many comments attributed to a node in particular cases.
I believe this is down to the fundamental difference in how we are collecting and parsing comment tokens.
To reiterate, we are using the same attachment logic, but for token parsing:
Proposed Solution
It seems like the only way to resolve these 5 remaining test cases is to have a dedicated implementation for attaching comments for this parser.