Skip to content

.before() modifies raws.before after insertion. #2017

Closed

Description

@pamelalozano16

Given a loud comment with new lines:

@mixin x {
    margin: 10px;
}

/*
* loud
* comment
*
*/

.a {
    @include x;
}

If I wanted to insert a new comment, e.g.:

const node = postcss.comment({
    text: 'New Comment',
    raws: {'inline': true, 'right': '', 'left': ' ', before:''},
});
console.log(node);

The log has the correct raws:

{"raws":{"inline":true,"right":"","left":" ","before":""},"text":"New Comment","type":"comment","inputs":[]}

But after insertion, the raws.before property is cloned from the original comment:

comment.before(node);
const previousComment = comment.prev() as postcss.Comment;
console.log(previousComment)

So the result is the following:

{
"raws": {
    "inline": true,
    "right": "",
    "left": " ",
    "before": "\n\n" // Should be ""
},
"text": "New comment",
"type": "comment",
"inputs": []
}

Activity

  1. ai commented on Feb 18, 2025

    @ai
    Member

    Can you help me find with debugger who change before to see the current logic?

    I found this block, but it should be applied only if raws.before is missing https://github.com/postcss/postcss/blob/main/lib/container.js#L219C37-L223

  2. ai commented on Jul 13, 2026

    @ai
    Member

    The fix by @MahinAnowar was released in 8.5.19.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions