From 2bb67ce8cd326dbaccbdec97e7578985f6009a14 Mon Sep 17 00:00:00 2001 From: Klaus Hott Vidal Date: Fri, 10 Jul 2026 03:48:01 +1200 Subject: [PATCH] Preserving comments' `placement` property (#19567) Co-authored-by: fisker --- changelog_unreleased/misc/19567.md | 3 +++ src/main/comments/attach.js | 7 +++++-- .../__tests__/__snapshots__/debug-print-comments.js.snap | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/misc/19567.md diff --git a/changelog_unreleased/misc/19567.md b/changelog_unreleased/misc/19567.md new file mode 100644 index 000000000..5ac721126 --- /dev/null +++ b/changelog_unreleased/misc/19567.md @@ -0,0 +1,3 @@ +#### Preserving comments' `placement` property (#19567 by @Janther) + +Prettier@3.9.0 deleted an undocumented property on comments, which was already used by plugins, `comment.placement` is now available again after comment attach. diff --git a/src/main/comments/attach.js b/src/main/comments/attach.js index e5c6481d4..824a4b2c2 100644 --- a/src/main/comments/attach.js +++ b/src/main/comments/attach.js @@ -174,11 +174,11 @@ function attachComments(ast, options) { } if (attachPropertiesToComment) { - comment.placement = placement; comment.enclosingNode = enclosingNode; comment.precedingNode = precedingNode; comment.followingNode = followingNode; } + comment.placement = placement; if (placement === "ownLine") { // If a comment exists on its own line, prefer a leading comment. @@ -256,7 +256,10 @@ function attachComments(ast, options) { delete comment.precedingNode; delete comment.enclosingNode; delete comment.followingNode; - delete comment.placement; + + // The `placement` property already used by plugins, can't delete + // https://github.com/prettier-solidity/prettier-plugin-solidity/blob/6986134753d72095bde41fe8e9a3c795830282ea/src/slang-nodes/IfStatement.ts#L56 + // https://github.com/dangmai/prettier-plugin-apex/blob/ff35044e53e0ba30423e1f2030161d096e99ae99/packages/prettier-plugin-apex/src/printer.ts#L251 } } } diff --git a/tests/integration/__tests__/__snapshots__/debug-print-comments.js.snap b/tests/integration/__tests__/__snapshots__/debug-print-comments.js.snap index bec3f81ed..647afcc39 100644 --- a/tests/integration/__tests__/__snapshots__/debug-print-comments.js.snap +++ b/tests/integration/__tests__/__snapshots__/debug-print-comments.js.snap @@ -12,6 +12,7 @@ exports[`prints information for debugging comment attachment with --debug-print- "start": { "line": 1, "column": 0, "index": 0 }, "end": { "line": 1, "column": 7, "index": 7 } }, + "placement": "endOfLine", "leading": true, "trailing": false, "nodeDescription": "ExpressionStatement" @@ -25,6 +26,7 @@ exports[`prints information for debugging comment attachment with --debug-print- "start": { "line": 2, "column": 16, "index": 24 }, "end": { "line": 2, "column": 23, "index": 31 } }, + "placement": "remaining", "leading": false, "trailing": true, "nodeDescription": "Identifier foo" @@ -38,6 +40,7 @@ exports[`prints information for debugging comment attachment with --debug-print- "start": { "line": 2, "column": 26, "index": 34 }, "end": { "line": 2, "column": 30, "index": 38 } }, + "placement": "remaining", "leading": false, "trailing": true, "nodeDescription": "ExpressionStatement"