Skip to content

fix: preserve comments between node and comma in separated lists (#684)#791

Open
todor-a wants to merge 1 commit into
dprint:mainfrom
todor-a:fix-comment-before-comma
Open

fix: preserve comments between node and comma in separated lists (#684)#791
todor-a wants to merge 1 commit into
dprint:mainfrom
todor-a:fix-comment-before-comma

Conversation

@todor-a

@todor-a todor-a commented May 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #684.


Transparency note: This PR was prepared with AI assistance (Claude).


Problem

const clientCardTitle = useMemo(() =>
  <>{cardTitle ?? title}</>
  // eslint-disable-next-line react-hooks/exhaustive-deps
  , [myInfo]);

formats to (comment gone):

const clientCardTitle = useMemo(() =>
  <>{cardTitle ?? title}</>, [myInfo]);

Root cause

gen_node_with_separator (src/generation/generate.rs:8040) wraps each list element with its separator (comma) via an inner_gen closure. gen_node_with_inner_gen then runs the regular trailing-comments path:

items.extend(gen_comments_as_trailing(&node_range, trailing_comments, context));

gen_comments_as_trailing calls get_trailing_comments_same_line, which filters to comments whose start line is <= node_end_line. Anything beyond that — i.e. comments that live between the node and the upcoming comma on subsequent lines — is silently dropped. The next sibling's leading_comments_with_previous doesn't reach back across the comma, so those comments never reappear.

Fix

Before gen_node_with_inner_gen runs, collect the comma token's leading comments (the ones that sit after the node end and after node_end_line), mark them handled via gen_comment_collection, and emit them after the separator inside the same inner_gen invocation:

let between_node_and_comma = if let Some(comma_token) = comma_token {
  gen_comments_between_node_and_comma(value, comma_token, context)
} else {
  PrintItems::new()
};
…
items.push_optional_path(generated_separator);
items.push_optional_path(between_node_and_comma);

Output for issue case:

const clientCardTitle = useMemo(
  () => <>{cardTitle ?? title}</>,
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [myInfo],
);

The fix lives in the shared gen_node_with_separator so it also covers comments before commas in:

  • call arguments
  • array literals
  • object literals
  • function parameters
  • any other site routed through this helper

Tests

tests/specs/issues/issue0684.txt covers the issue regression plus mixed line/block comments, array/object/parameter cases. Verified against prettier's comment fixtures (before-comma.js, in-list/dangling-comment-in-list.js, first-argument/first-argument.js).

cargo test --release passes.

…int#684)

`gen_node_with_separator` emitted the auto-inserted comma immediately
after the node, then ran the regular trailing-comments path which only
emits same-line comments. Multi-line comments living between the node
end and the comma — e.g.

    foo(
      a
      // comment
      , b);

— were therefore captured by `trailing_comments_with_previous` but
filtered out by `get_trailing_comments_same_line` and lost.

Collect the comma token's leading comments that sit on lines after the
node end, mark them handled via `gen_comment_collection`, and emit them
after the separator. Result:

    foo(
      a,
      // comment
      b,
    );

Applies to anything routed through `gen_node_with_separator`: call
arguments, array literals, object literals, function parameters, etc.
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.

Commented code/suppression are wrongly removed by Dprint

1 participant