Skip to content

Fixed wrong trailing comma position after comment in scss #16617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Oct 10, 2024

Conversation

Ma-hawaj
Copy link
Contributor

@Ma-hawaj Ma-hawaj commented Aug 28, 2024

Description

Fixes #16599
Fixes #6920

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing
    guidelines
    .

Try the playground for this PR

@Ma-hawaj
Copy link
Contributor Author

Thanks for the fast response, appreciate it.

Copy link
Member

@sosukesuzuki sosukesuzuki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh full tests fail on this change. Please fix it.

You can run full tests by FULL_TEST=1 yarn test

@Ma-hawaj Ma-hawaj force-pushed the trailing-comma-fix branch from 94f7e52 to db40272 Compare October 7, 2024 06:29
@Ma-hawaj
Copy link
Contributor Author

Ma-hawaj commented Oct 7, 2024

@sosukesuzuki Can you assist me with this ?
what is the difference between normal yarn test and the one you mentioned.
when specifying FULL_TEST=1 the output of the test becomes different and I don't know the reason.
It prints the comment in a new line when in normal test it doesn't and I can't figure out why it does this

This is the output Doc

{
    "type": "group",
    "contents": {
        "type": "indent",
        "contents": {
            "type": "fill",
            "parts": [
                [
                    [
                        [
                            [
                                [
                                    "",
                                    "overlay"
                                ],
                                {
                                    "type": "group",
                                    "contents": [
                                        ":",
                                        {
                                            "type": "line"
                                        }
                                    ],
                                    "break": false
                                }
                            ],
                            [
                                "1202",
                                ""
                            ]
                        ],
                        {
                            "type": "line-suffix",
                            "contents": [
                                " ",
                                "// The comma shoud be printed before the comment when trailing-comma = es5"
                            ]
                        }
                    ],
                    {
                        "type": "break-parent"
                    }
                ]
            ]
        }
    },
    "break": false
}

image

@sosukesuzuki
Copy link
Member

@Ma-hawaj
Copy link
Contributor Author

Ma-hawaj commented Oct 8, 2024

Seems like there is a bug with the parser it self since if there is a trailing comma it doesn't consider the comment as part of the comma separated group while when there is not it counts it as a part of the comma separated group.
So basically the parser parse it differently before and after the formatting
Do we have access to the parser since I saw that it is imported as a node module.

@fisker
Copy link
Member

fisker commented Oct 8, 2024

Can be related to #16583

@Ma-hawaj
Copy link
Contributor Author

Ma-hawaj commented Oct 9, 2024

It is now working functionally but the parsing tree a bit different when reparsing so the full test fails.

@fisker fisker self-assigned this Oct 10, 2024
@Ma-hawaj
Copy link
Contributor Author

Currently it has this weird behavior in the main release code.
Some times it print the comment in a new line and some times it print it in the same line such as the example below:
Input:

.simplification {
  foo: (
    calc(), // It is a comment
  );
}

.simplification {
  foo: (
    calc() // not a comment anymore
  );
}

$z-indexes: (
  header: 1035,
  header: 1035,
  overlay: 1202, // TODO
  header: 1035,
  header: 1035,
);

Output:

.simplification {
  foo: (calc(), // It is a comment);
}

.simplification {
  foo: (
    calc() // not a comment anymore
  );
}

$z-indexes: (
  header: 1035,
  header: 1035,
  overlay: 1202,
  // TODO
  header: 1035,
  header: 1035,
);

which also breaks the code for the first example.

@fisker
Copy link
Member

fisker commented Oct 10, 2024

I'm working on it.

@fisker fisker force-pushed the trailing-comma-fix branch from 955a537 to 244cf81 Compare October 10, 2024 07:10
Comment on lines +310 to +314
$my-map: (
"foo": 1,
// Comment
"bar": 2, // Comment
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually wrong, the first comment should not print on separate line, but it's a different issue, we can deal with it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I thought it is the default behavior since it was like this in the released version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's bug, but we don't have a comment attach logic in CSS printer, it's not easy to fix(I guess).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong, if we always print comments with other elements instead of a separate element, it should be fixed? Don't know.

@fisker
Copy link
Member

fisker commented Oct 10, 2024

@Ma-hawaj This should be good enough to merge, but some other cases need to be improved. Eg #16617 (comment), #16743 , and the TODO comment in code.

@Ma-hawaj
Copy link
Contributor Author

@fisker Thanks for your support.
It was a pleasure contributing to this project.
I hope I could do more of contributions in the future.

@fisker fisker merged commit 43feccb into prettier:main Oct 10, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong trailing comma position after comment in scss SCSS: extra comma added to map, before last comment
4 participants