Option to Enable or Disable Trailing or Leading Comments
See original GitHub issueI am experiencing duplicate comments when transforming ast to code.
I want to propose a way to enable or disable trailing and leading comments.
Rather than doing attachComment: true
, maybe we can do something like
{
attachComment: {
leading: true,
trailing: false
}
}
but the old way of attaching comment should still be supported so as not to break things. If you do
{
attachComment: true
}
then it will still attach leading and trailing comments.
this is espree 2.2.5, by the way.
Here are some issues I found related to this that might also help other users if implemented
https://github.com/eslint/espree/issues/184 https://github.com/estools/escodegen/issues/152
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Enable and Disable Comments | WordPress.com Support
You can enable or disable comments on all future posts by going to Settings → Discussion. Under “Default Article Settings,” toggle the option...
Read more >Rules — yamllint 1.28.0 documentation
A configuration file can be used to enable or disable these rules, ... Use this rule to control the position and formatting of...
Read more >Options - Prettier
This effectively allows using the json5 parser for “JSON with comments and trailing commas”. JSX Quotes. Use single quotes instead of double quotes...
Read more >no-trailing-spaces - ESLint - Pluggable JavaScript Linter
This rule disallows trailing whitespace (spaces, tabs, and other Unicode whitespace ... false (default) disallows trailing whitespace in comment blocks ...
Read more >Reply with inline comments within the original message text
Turn on inline comments · Click the File > Options. · In the left pane, click Mail. · In the right pane, under...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kaicataldo We (you) created a new comment attachment strategy in ESLint some time ago. Were any corresponding changes made to espree that would fix the issue described here?
@platinumazure No changes were made to Espree. We just don’t use the comment attachment option when parsing.
As @nzakas has said, this isn’t a bug, but a feature request. When Espree parses, there is no way to know whether a comment is a trailing comment for the previous node or a leading comment for the next node. Given a node, you should be able to check if it has a comment before it or after it. It’s up to the consumer to decide what they do with this information.
It feels to me like thinking of these as duplicated comments is wrong. The intended use case here is to be able to easily access comments around nodes. Attached comments are not nodes in the tree (this wouldn’t make sense) - they are just references to the comment tokens that exist in the comments array.
I don’t think adding an option to only include leading or trailing comments makes sense either, because some comments would be missed (at the beginning or end of the file, for instance).
As @nzakas mentioned, it’s on the consumer to use this information correctly. My naive take is that it would either involve using attached comments and deduping them or using the comments array generated by Espree using the
comment: true
option and inserting comments in the correct place (this is more work, but I think it would be more accurate).For reference, Babel does the first of the two options above. Babylon has a very similar comment attachment strategy and Babel does the work to dedupe comments when it generates code: https://github.com/babel/babel/blob/master/packages/babel-generator/src/printer.js#L528