Repository navigation
Open
Labels
area:commentsIssues with how Prettier prints commentslang:css/scss/lessIssues affecting CSS, Less or SCSS
Description
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.
Reacted by David Calhoun and Jonny Gerig Meyer
Metadata
Metadata
Assignees
Labels
area:commentsIssues with how Prettier prints commentslang:css/scss/lessIssues affecting CSS, Less or SCSS
Activity
[-][css] comments around declaration[/-][+][css] comments[/+]alexander-akait commentedon May 14, 2018
Ugly output:
alexander-akait commentedon May 29, 2018
@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?
vjeux commentedon May 30, 2018
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
alexander-akait commentedon May 31, 2018
@vjeux problem what comment nodes inside
nodesarrray not incommentproperty as inbabylonvjeux commentedon May 31, 2018
I don't understand what you just said. Can you give an example maybe?
alexander-akait commentedon May 31, 2018
@vjeux https://astexplorer.net/#/gist/90d3dac1fefbbbe16655df080b25b4cc/cd2771e6c0299be7b1f6c01e6971fe909f35c92f comment is part of
nodes, it is possible solve using logic as https://github.com/prettier/prettier/blob/master/src/language-js/comments.js, maybe we need write parser this?vjeux commentedon May 31, 2018
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.
alexander-akait commentedon May 31, 2018
@vjeux maybe i can look on PoC somewhere?
vjeux commentedon May 31, 2018
I'm sorry but don't have the time to build one :(
suchipi commentedon May 31, 2018
Untested, but here's the idea:
suchipi commentedon May 31, 2018
You'd have to handle arrays of nodes, too. I forgot about that when writing that example.
alexander-akait commentedon May 31, 2018
@suchipi we have problem - some parser don't handle
//comment, only/* */, looks we should write parser 😞suchipi commentedon May 31, 2018
Writing a parser which extracts comments only would hopefully be easier than writing an entire CSS parser, at least
alexander-akait commentedon May 31, 2018
@suchipi try do this in next month, i will be grateful for any help
suchipi commentedon May 31, 2018
Hopefully we could leverage https://pegjs.org/ to make one relatively easily
j-f1 commentedon May 31, 2018
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.