Skip to content

Improve handling of dangling comments in ForStatement#10059

Open
thorn0 wants to merge 23 commits intoprettier:mainfrom
thorn0:for-statement-dangling-comments
Open

Improve handling of dangling comments in ForStatement#10059
thorn0 wants to merge 23 commits intoprettier:mainfrom
thorn0:for-statement-dangling-comments

Conversation

@thorn0
Copy link
Copy Markdown
Member

@thorn0 thorn0 commented Jan 15, 2021

Description

Fixes #10046

A better handling for comments like this: for (/*a*/; /*b*/; /*c*/) ...

A proof of concept of an approach that doesn't modify the AST unlike an alternative approach to a similar problem in #9857.

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@thorn0 thorn0 force-pushed the for-statement-dangling-comments branch 2 times, most recently from 4e76287 to 51774a0 Compare January 15, 2021 00:37
@fisker
Copy link
Copy Markdown
Member

fisker commented Jan 15, 2021

Seems a good solution.

@thorn0 thorn0 marked this pull request as ready for review January 15, 2021 16:25
@thorn0 thorn0 marked this pull request as draft January 15, 2021 20:05
@thorn0 thorn0 marked this pull request as ready for review January 15, 2021 22:26
@thorn0 thorn0 force-pushed the for-statement-dangling-comments branch 2 times, most recently from cde2eb0 to c34a492 Compare January 20, 2021 09:19
}

function createCommentAnchorPseudoNode(ref, marker, loc) {
return { type: "Prettier:CommentAnchor", ref, marker, range: [loc, loc] };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use object for this marker? I remeber you already use if for something in class, it should be more clear to understand what it is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a string label on a dangling comment needed to differentiate it from other dangling comments and to associate it with a specific place inside the enclosing node. Are you proposing not to use strings for this? I think strings work pretty well here: e.g. if the init part of a for statement is omitted, we might have dangling comments with the init marker there.

I remeber you already use if for something in class

For the same thing: to differentiate comments related to extends from comments related to implements, etc.

Copy link
Copy Markdown
Member Author

@thorn0 thorn0 Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking about renaming marker to anchor as it seems to convey the purpose better ("something comments are attached to"), but let's do that in another PR. Another idea was attachmentPoint. I chose anchor as a shorter synonym.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some renames, see the last commit.

Base automatically changed from master to main January 23, 2021 17:13
@thorn0 thorn0 force-pushed the for-statement-dangling-comments branch from 0a6d307 to 05d2d4b Compare January 23, 2021 20:55
@thorn0 thorn0 force-pushed the for-statement-dangling-comments branch from c29d1f7 to 2589c65 Compare January 25, 2021 14:43
Copy link
Copy Markdown
Member

@fisker fisker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I would prefer to use tokens.

@thorn0
Copy link
Copy Markdown
Member Author

thorn0 commented Jan 25, 2021

@fisker I like tokens too as they're more structured then the text-scanning hacks, but on the other hand depending only on 2 inputs (AST and original text) is better than on 3 (AST, original text, tokens). The resulting code is a bit more universal.


function createCommentAnchorPseudoNode(enclosingNode, name, loc) {
return {
type: "Prettier:CommentAnchor",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think ::CommentAnchor, like a css pseudo element. 😄

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it 👍

@thorn0 thorn0 force-pushed the for-statement-dangling-comments branch 4 times, most recently from b32ffe3 to 4f09191 Compare January 31, 2021 22:33
@thorn0 thorn0 force-pushed the for-statement-dangling-comments branch from 4f09191 to be00809 Compare February 9, 2021 09:26
@thorn0 thorn0 force-pushed the for-statement-dangling-comments branch from be00809 to c964222 Compare February 9, 2021 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve comments inside for loop

2 participants