Skip to content

Formatter adds a blank line and over-indents a union used as a template argument #11009

@oha-4

Description

@oha-4

Describe the bug

When a union is passed directly as a template argument (e.g. PickProperties<T, "a" | "b" | ...>) and the template argument list is long enough to wrap to multiple lines, tsp format inserts a blank line after the preceding argument and over-indents the union variants. The result looks unbalanced/broken compared to the surrounding code.

Note: a short union that fits on one line does not reproduce this — the template arguments must exceed the print width and wrap.

Reproduction

model Sample {
  alpha: string;
  bravo: string;
  charlie: string;
  delta: string;
  echo: string;
  foxtrot: string;
  golf: string;
  hotel: string;
}
model Picked is PickProperties<Sample, "alpha" | "bravo" | "charlie" | "delta" | "echo" | "foxtrot" | "golf" | "hotel">;

Run tsp format on the above.

Actual

model Picked
  is PickProperties<
    Sample,

      | "alpha"
      | "bravo"
      | "charlie"
      | "delta"
      | "echo"
      | "foxtrot"
      | "golf"
      | "hotel"
  >;

Note the empty line after Sample, and the extra indentation applied to the leading-| union variants (they are indented one level deeper than the Sample argument).

Expected

No blank line, and the union variants aligned to the same indentation as the other template arguments, e.g.

model Picked is PickProperties<
  Sample,
  | "alpha"
  | "bravo"
  | "charlie"
  | "delta"
  | "echo"
  | "foxtrot"
  | "golf"
  | "hotel"
>;

(exact wrapping is up to the formatter, but there should be no inserted blank line and no extra indent level for the union)

Checklist

  • Reproduced with @typespec/compiler 1.13.0
  • The issue appears to come from the combination of template-argument line breaking and the union's leading-| printing in packages/compiler/src/formatter/print/printer.ts.

Workaround

Extracting the union into an alias avoids the broken formatting, since the template argument becomes a single reference:

alias PickedKeys =
  | "alpha"
  | "bravo"
  | "charlie"
  | "delta"
  | "echo"
  | "foxtrot"
  | "golf"
  | "hotel";
model Picked is PickProperties<Sample, PickedKeys>;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions