question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to Enable or Disable Trailing or Leading Comments

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
platinumazurecommented, Feb 12, 2018

@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?

0reactions
kaicataldocommented, Feb 13, 2018

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found