Skip to content

Feature request: Align trailing comments across consecutive lines #778

@YuanTu-8

Description

@YuanTu-8

Description

It would be great to have an option to align trailing comments on consecutive lines to the same column, similar to clang-format's AlignTrailingComments.

Currently, dprint normalizes trailing comments to a single space after the code, which breaks intentional alignment:

Before formatting (desired output)

let diffZero = 0;  // mismatch count against "010101..."
let diffOne = 0;   // mismatch count against "101010..."

After formatting (current behavior)

let diffZero = 0; // mismatch count against "010101..."
let diffOne = 0; // mismatch count against "101010..."

Proposed behavior

When consecutive lines each have a trailing comment, align them to the same column. A minimum spacing (e.g., 2 spaces) should be maintained between the code and the comment.

A possible configuration might look like:

{
  "typescript": {
    "alignTrailingComments": true,         // default: false
    "alignTrailingComments.minSpaces": 2   // minimum gap before comment
  }
}

Prior art

  • clang-format has AlignTrailingComments with Kind: Always | Leave | Never and OverEmptyLines to control how many empty lines still count as "consecutive".
  • ReSharper / Rider (C#) supports aligning trailing comments as well.

This feature is common in C/C++ formatters and would be a welcome addition for TypeScript/JavaScript, especially for code with parallel declarations or flag variables where aligned comments greatly improve readability.

Workaround

Currently using // dprint-ignore to preserve manually aligned comments, which works but requires opting out of formatting for those blocks entirely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions