Skip to content

CSS: Comment gets eaten #2127

Closed
Closed

Description

@fortes

Input:

@font-face {
  font-family: "Prettier";
  src: local("Prettier"), /* Local */
    url("http://prettier.com/font.woff") /* Network*/
}

Expected:

@font-face {
  font-family: "Prettier";
  src: local("Prettier") /* Local */, url("http://prettier.com/font.woff"); /* Network*/
}

Output (the /* Local */) comment is dropped:

@font-face {
  font-family: "Prettier";
  src: local("Prettier"), url("http://prettier.com/font.woff"); /* Network*/
}

Repro: https://prettier.github.io/prettier/#%7B%22content%22%3A%22%40font-face%20%7B%5Cn%20%20font-family%3A%20%5C%22Prettier%5C%22%3B%5Cn%20%20src%3A%20local(%5C%22Prettier%5C%22)%2C%20%2F*%20Installed%20*%2F%5Cn%20%20%20%20url(%5C%22http%3A%2F%2Fprettier.com%2Ffont.woff%5C%22)%20%2F*%20Network*%2F%5Cn%7D%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3A%22all%22%2C%22bracketSpacing%22%3Atrue%2C%22jsxBracketSameLine%22%3Afalse%2C%22parser%22%3A%22postcss%22%2C%22semi%22%3Atrue%2C%22useTabs%22%3Afalse%2C%22doc%22%3Afalse%7D%7D

Activity

  1. added
    lang:css/scss/less
    on Jun 17, 2017
  2. added
    priority:high
    on Jun 17, 2017
  3. josephfrazier commented on Jul 5, 2017

    @josephfrazier
    Collaborator

    It looks like we need to make attachComments/ensureAllCommentsPrinted work with CSS comments. That way, prettier would throw an error instead of silently dropping comments.

  4. vjeux commented on Jul 5, 2017

    @vjeux
    Contributor

    Agreed, that would be much better. Are you interested in working on it?

  5. vjeux commented on Jul 5, 2017

    @vjeux
    Contributor

    The issue is that the value and media parsers do not give us comment tokens. The postcss parser makes comment nodes.

    I think the best solution is to build a small tokenizer that knows how to extract comments so that we can let prettier attach and print them.

    Maybe postcss can even do that?

    This would also let us remove the hack we have in place for prettier-ignore in CSS and instead use the generic solution.

  6. josephfrazier commented on Jul 5, 2017

    @josephfrazier
    Collaborator

    Hmm, with the following change, we can get the /* Local */ comment in the code sample posted above:

    diff --git a/src/parser-postcss.js b/src/parser-postcss.js
    index f4b2981d..4d9e972c 100644
    --- a/src/parser-postcss.js
    +++ b/src/parser-postcss.js
    @@ -171,7 +171,12 @@ function parseNestedCSS(node) {
         }
         if (node.type && typeof node.value === "string") {
           try {
    -        node.value = parseValue(node.value);
    +        if (node.raws && node.raws.value && node.raws.value.raw) {
    +          node.value = parseValue(node.raws.value.raw);
    +          console.log(JSON.stringify(node.value, null, 2))
    +        } else {
    +          node.value = parseValue(node.value);
    +        }
           } catch (e) {
             throw createError(
               "(postcss-values-parser) " + e.toString(),

    It needs more work to actually print it out, though.

  7. vjeux commented on Jul 6, 2017

    @vjeux
    Contributor

    I tried that as well but if the comment is right after the : it doesn't get picked up. I think that my solution would work all the time and solve it for real.

  8. added
    area:comments
    on Jul 15, 2017
  9. TrySound commented on Aug 8, 2017

    @TrySound
    Contributor

    My postcss value parser has comment tokens. Not sure about fork.

  10. added
    locked-due-to-inactivity
    on Jul 5, 2018
  11. locked as resolved and limited conversation to collaborators on Jul 5, 2018
  12. 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/lesslocked-due-to-inactivitypriority:high

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions