Skip to content

[css] comments #3905

Open

Description

@alexander-akait

Prettier 1.10.2
Playground link

Input:

a {
    /* comment 1 */
    /* comment 2 */ padding /* comment 3 */ : /* comment 4 */ 10px /* comment 5 */ 10px /* comment 6 */; /* comment 7 */
    /* comment 8 */
    transform: translate(/* comment 9 */ 10px /* comment 10 */);
    color: /* comment 11 */ red /* comment 12 */ !important /* comment 13 */ ; /* comment 14 */
    /* comment 15 */
}
/* comment 4 */
/* comment 5 */ .field /* comment 6 */
/* comment 7 */
/* comment 8 */ { /* comment 9 */
    /* comment 10 */
    background: green;
    /* comment 11 */
/* comment 12 */ } /* comment 13 */
/* comment 14 */

Output:

a {
  /* comment 1 */
  /* comment 2 */
  padding/* comment 3 */ : /* comment 4 */ 10px /* comment 5 */ 10px
    /* comment 6 */; /* comment 7 */
  /* comment 8 */
  transform: translate(/* comment 9 */ 10px /* comment 10 */);
  color: /* comment 11 */ red /* comment 12 */ !important /* comment 13 */ ; /* comment 14 */
  /* comment 15 */
}
/* comment 4 */
/* comment 5 */
.field /* comment 6 */
/* comment 7 */
/* comment 8 */ {
  /* comment 9 */
  /* comment 10 */
  background: green;
  /* comment 11 */
  /* comment 12 */
} /* comment 13 */
/* comment 14 */

Expected behavior: Wait feedback from contributors and community.

Don't work in main playground because this fix only in master and was not released.

Activity

  1. added
    area:comments
    lang:css/scss/less
    on Feb 6, 2018
  2. changed the title [-][css] comments around declaration[/-] [+][css] comments[/+] on Feb 6, 2018
  3. alexander-akait commented on May 14, 2018

    @alexander-akait
    MemberAuthor

    Ugly output:

    @import
      // Variables
      'mixins',
      'variables',
      // Base
      'reset',
      'scaffolding',
      'type',
      // Components
      'bar',
      'tabs';
  4. alexander-akait commented on May 29, 2018

    @alexander-akait
    MemberAuthor

    @vjeux @azz @ikatyang @duailibe @lydell, sorry if i forget ping someone.

    Somebody can me help with implement attach/detach comments (right now they are very ugly 😞 )? Maybe examples or just implement PoC and I'll finish it myself? Any ideas how we can solve this?

  5. vjeux commented on May 30, 2018

    @vjeux
    Contributor

    Sorry for the delay. The whole idea with comments is to figure out from the position in the AST how it should be attached (before/after/inside) of which node.

    Those are all the special cases we're doing for js. You probably want to have a similar approach for CSS.
    https://github.com/prettier/prettier/blob/master/src/language-js/comments.js

  6. alexander-akait commented on May 31, 2018

    @alexander-akait
    MemberAuthor

    @vjeux problem what comment nodes inside nodes arrray not in comment property as in babylon

  7. vjeux commented on May 31, 2018

    @vjeux
    Contributor

    I don't understand what you just said. Can you give an example maybe?

  8. vjeux commented on May 31, 2018

    @vjeux
    Contributor

    Okay, now I see.

    What I would do is to traverse the tree and gather all the comments into a single list. Then pass them to prettier algorithm that does the binary search to attach them to nodes.

    This way you can add all the heuristics you want.

  9. alexander-akait commented on May 31, 2018

    @alexander-akait
    MemberAuthor

    @vjeux maybe i can look on PoC somewhere?

  10. vjeux commented on May 31, 2018

    @vjeux
    Contributor

    I'm sorry but don't have the time to build one :(

  11. suchipi commented on May 31, 2018

    @suchipi
    Member

    Untested, but here's the idea:

    const ast = {
      /* ... */
    };
    const comments = [];
    
    function grabComments(node) {
      if (node.comments) {
        comments.push(...node.comments);
      }
      for (let property in node) {
        const child = node[property];
        if (
          typeof child === "object" &&
          child != null &&
          typeof child.type === "string"
        ) {
          grabComments(child);
        }
      }
    }
    grabComments(ast);
    
    ast.comments = comments;
  12. suchipi commented on May 31, 2018

    @suchipi
    Member

    You'd have to handle arrays of nodes, too. I forgot about that when writing that example.

  13. alexander-akait commented on May 31, 2018

    @alexander-akait
    MemberAuthor

    @suchipi we have problem - some parser don't handle // comment, only /* */, looks we should write parser 😞

  14. suchipi commented on May 31, 2018

    @suchipi
    Member

    Writing a parser which extracts comments only would hopefully be easier than writing an entire CSS parser, at least

  15. alexander-akait commented on May 31, 2018

    @alexander-akait
    MemberAuthor

    @suchipi try do this in next month, i will be grateful for any help

  16. suchipi commented on May 31, 2018

    @suchipi
    Member

    Hopefully we could leverage https://pegjs.org/ to make one relatively easily

  17. j-f1 commented on May 31, 2018

    @j-f1
    Member

    You could use a tokenizer since it will capture the comments properly but it won’t try to generate a complete syntax tree, which isn’t something you need in this case.

  18. added theissue type on Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:commentslang:css/scss/less

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions