Fix missing comments in assignment pattern#704
Conversation
| " | ||
| `; | ||
|
|
||
| exports[`test dangling_array.js 1`] = ` |
There was a problem hiding this comment.
Oops.. will fix that!
src/comments.js
Outdated
| } else { | ||
| if (handleIfStatementComments(enclosingNode, followingNode, comment)) { | ||
| // We're good | ||
| } else if (handleAssignmentPatternComments(enclosingNode, comment)) { |
There was a problem hiding this comment.
can you put
if (
handleIfStatement... ||
handleAssignment... ||
)like we do above?
src/comments.js
Outdated
| return false; | ||
| } | ||
|
|
||
| function handleAssignmentPatternComments(enclosingNode, comment) { |
There was a problem hiding this comment.
I don't think that this is the right fix. Can you figure out where the comment is attached without this logic and find out why this node didn't print its comment?
There was a problem hiding this comment.
I did investigate a bit. For the following case, this line is involved https://github.com/jlongster/prettier/blob/master/src/comments.js#L192. Then addCommentHelper enforcescomment.printed = false which strips the comment. If set to true then this is working again:
const { c = 1 /* comment */ } = d;I don't think you want to alter such a primitive function, that's why I used this handleAssignmentPatternComments function.
There was a problem hiding this comment.
But, funny thing, switching to comment.printed = true doesn't break any tests 😮! Then, I just need to figure out how to print the comment for:
const {a /* comment */ = 1 } = bThere was a problem hiding this comment.
comment.printed shouldn't have any affect on the output; it's used to do a sanity check afterwards to make sure we printed all the comments. I don't know why it would affect the output.
There was a problem hiding this comment.
Good to know, thanks! But this definitely has an impact... Your thoughts on the handleAssignmentPatternComments function, is that a wrong approach?
There was a problem hiding this comment.
It's not the wrong approach if we have to do it, but I'd also like to know why we have to. If like you say it triggers that branch and attaches it as a trailing comment to the identifier, it should be printed.
Search the code for comment.printed; it's only ever set the true and false. Nothing ever branched on it, so can you look into why something is changed and why the comment isn't printed in the first place?
There was a problem hiding this comment.
Yeah, I saw it. I'm going to investigate, thanks!
There was a problem hiding this comment.
@jlongster + @vjeux I think I've identified what's causing the issue. For example, with something like:
const {a /* comment */ = 1 } = bThe comment is stripped and we get the error message for non-printed comments. This is coming from https://github.com/jlongster/prettier/blob/master/src/comments.js#L487 where the right-hand side of the boolean (types.getFieldValue(value, "comments")) is always null or undefined. As a consequence, on line 490, printed is returned and the comment is sadly omitted...
There was a problem hiding this comment.
Oh, and I forgot to mention that in this case there is a trailing comment (I can trace it here https://github.com/jlongster/prettier/blob/master/src/comments.js#L193).
|
I discovered the problem. The problem is that the The problem is that the comment is attached to the identifier in the Not sure what the best solution is yet, but that's the problem. |
|
@jlongster I tried to implement a different logic, i.e. solving the root issue by handling these comments attached to the key node. Not sure if this is a better way to do it, and by the way, this currently only solves the non-printed comment warning (still need to print those comments in the end). |
@@ -179,7 +179,8 @@ function attach(comments, ast, text) {
addDanglingComment(ast, comment);
}
} else {
- if (handleIfStatementComments(enclosingNode, followingNode, comment)) {
+ if (handleIfStatementComments(enclosingNode, followingNode, comment) ||
+ handleObjectProperty(enclosingNode, precedingNode, comment)) {
// We're good
} else if (precedingNode && followingNode) {
// Otherwise, text exists both before and after the comment on
@@ -395,6 +396,17 @@ function handleConditionalExpressionComments(
return false;
}
+function handleObjectProperty(enclosingNode, precedingNode, comment) {
+ if (enclosingNode.type === "ObjectProperty" &&
+ enclosingNode.shorthand &&
+ enclosingNode.key === precedingNode &&
+ enclosingNode.value.type === "AssignmentPattern") {
+ addTrailingComment(enclosingNode.value.left, comment);
+ return true;
+ }
+ return false;
+}
+What do you think about relocating the comment the same way we move a bunch of others? const {/* missing */ a /* missing comment */ = 1} = boutputs const { /* missing */ a /* missing comment */ = 1 } = b; |
|
This looks good to me! |
src/comments.js
Outdated
|
|
||
| if ( | ||
| locStart(child) - locStart(comment) <= 0 && | ||
| locEnd(comment) - locEnd(child) <= 0 |
There was a problem hiding this comment.
Can you revert all the changes you've triggered by running prettier on that file? :)
| return path.needsParens() ? concat(["(", lines, ")"]) : lines; | ||
| } | ||
|
|
||
| function shouldPrintComma(options, level) { |
There was a problem hiding this comment.
I think you messed up your rebase :x
There was a problem hiding this comment.
Oops, let me clean that!
…99-fix-missing-comments-assignment-pattern
|
Yay, thanks! |
|
@vjeux Now this should be cleaned up and all the tests should pass! |
|
@vjeux Wow, you're fast! Thanks 👍 |
|
I'm pretty excited about this PR :) |
This PR should fix #699.