Skip to content

feat: add alignAssignments option - #737

Open
xseman wants to merge 1 commit into
dprint:mainfrom
xseman:align-assignments
Open

xseman wants to merge 1 commit into
dprint:mainfrom
xseman:align-assignments

Conversation

@xseman

@xseman xseman commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor

Closes #483

Adds an opt-in alignAssignments option (default false) that aligns values the way gofmt does:

const foo    = 12;
const bar    = 21;
const foobar = 22;

const obj = {
  bar:    123,
  barbaz: 123,
};

It applies to variable statements, class properties, enum members, object properties, and interface and type literal members. A member without a value is aligned on its type (declare const X: T;).

Behavior

The grouping follows gofmt (go/printer):

  • A group is the members on consecutive lines. A blank line, a comment on its own line, or a member on multiple lines ends it.
  • In an object, a key much longer or shorter than the ones before it starts a new group (gofmt's 40 characters and 2.5 ratio).
  • Assignment statements (a = 1;) aren't aligned.

The one difference: dprint has a line width, so the alignment only adds spaces and never moves a line break. A member that would go over the line width isn't aligned.

Implementation

The widths are measured from the file text, so format_text and format_parsed_source format without the alignment first and then align that output. It only happens when the option is on and costs about 2x the formatting time. I also tried measuring at print time in one pass, but it left files unstable and was 2.6x slower.

Validation

On 59,021 TypeScript files (DefinitelyTyped, vscode, bun, deno std, …) with the option on: 0 panics, 0 outputs that don't parse, 0 files that change when formatted again, and 0 files where anything other than spaces differs from the default output. With the option off, the output is byte-identical to main.

The option name is open to change, and I can add per-node options if you'd prefer them.

@xseman xseman changed the title feat: add alignment options for variable statements and object properties feat: add alignment options Sep 23, 2025
Comment thread src/configuration/resolve_config.rs Resolved
@xseman

xseman commented Dec 29, 2025

Copy link
Copy Markdown
Contributor Author

@dsherret I know you may be busy. This PR has been open for a while. Any feedback would help. Thanks

@xseman
xseman force-pushed the align-assignments branch 5 times, most recently from 8c801a9 to 5f27bc2 Compare November 11, 2026 05:44
@xseman xseman changed the title feat: add alignment options feat: add alignAssignments option Nov 11, 2026
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.

Align assignments

1 participant