Replies: 3 comments
-
During comment attach, Lines 220 to 222 in 3ef82ae Lines 407 to 412 in 3ef82ae (There are some other mutations in those files) After it's printed. Line 421 in 3ef82ae You can freeze comment to see what's going to change.
It's called automatically when you call prettier/src/main/ast-to-doc.js Line 145 in 3ef82ae |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer, but I still don't really get it. From the documentation I understood that prettier will move comments from the root node in AST to the nodes to which they belong to:
This doesn't seem to be happening, and your response also only describes how the comment node itself is mutated, saying nothing about whether something else in the AST also gets changed. After parsing the following code: SELECT /*comment1*/ 1 My parser produces the following AST: {
"type": "program",
"statements": [
{
"type": "select_stmt",
"clauses": [
{
"type": "select_clause",
"selectKw": { "type": "keyword", "text": "SELECT", "name": "SELECT", "range": [0, 6] },
"options": [],
"columns": {
"type": "list_expr",
"items": [
{ "type": "number_literal", "text": "1", "value": 1, "range": [20, 21] }
],
"range": [20, 21],
},
"range": [0, 21]
}
],
"range": [0, 21]
}
],
"range": [0, 21],
"comments": [
{ "type": "block_comment", "value": "/*comment1*/", "range": [7, 19] }
]
} After prettier has processed it, and calls the {
"type": "program",
"statements": [
{
"type": "select_stmt",
"clauses": [
{
"type": "select_clause",
"selectKw": { "type": "keyword", "text": "SELECT", "name": "SELECT", "range": [0, 6] },
"options": [],
"columns": {
"type": "list_expr",
"items": [
{ "type": "number_literal", "text": "1", "value": 1, "range": [20, 21] }
],
"range": [20, 21],
},
"range": [0, 21]
}
],
"range": [0, 21]
}
],
"range": [0, 21],
"comments": [
{
"type": "block_comment",
"value": "/*comment1*/",
"range": [7, 19],
"placement": "remaining",
"leading": false,
"trailing": false,
"printed": false,
"nodeDescription": "program"
}
]
} Is that's what's supposed to happen or not? It definitely conflicts with what the documentation says (the comments property is deleted from the AST root.) It seems like Prettier associates the comment with the root node ( You said that
|
Beta Was this translation helpful? Give feedback.
-
After reading through lots of Prettier source code and running several experiments I finally figured out the culprit: I had not implemented the The documentation is not really clear on what methods one should implement to make use of the Prettier automatic comments handling. All the comments-related methods are marked as optional, which makes one think that Prettier will take care of comments automatically out of the box, but it's not really so. One definitely needs to implement If you fail to implement
But if you fail to implement
|
Beta Was this translation helpful? Give feedback.
-
I'm writing a prettier plugin and trying to understand how the prettiers comment algorithm works.
The documentation says that:
OK, but what sort of data should this
comments
field contain? I assume the following is not quite enough:It probably also needs a location info to be able to associate comments with AST nodes. So I tried something like the following (using the same
range
fields that I'm accessing inlocStart
andlocEnd
functions):Doesn't seem to work. I just get an error:
TypeError: Cannot read properties of undefined (reading 'trim')
The error gets thrown from
ensureAllCommentsPrinted()
function which tries to performcomment.value.trim()
. Aha... so it expects avalue
field. But when I change my currenttext
field tovalue
, it still doesn't manage to print the comment, instead ending up with an error:Comment "/* comment1 */" was not printed. Please report this error!
I also tried to implement the
printComment()
function. But it never gets called.I'm clearly doing something very wrong, but I have no idea what.
The docs also say:
This worries me. Especially as I have no idea what kind of mutations are going to be performed.
Beta Was this translation helpful? Give feedback.
All reactions