dprint-plugin-typescript icon indicating copy to clipboard operation
dprint-plugin-typescript copied to clipboard

union types: fix trailing comment causing multiline when preferSingleLine is true

Open declanvong opened this issue 2 years ago • 0 comments

has_any_node_comment_on_different_line doesn't currently consider the case where the final node has a trailing line comment (which is therefore on the same line).

This causes issues such as the following, where we see union types randomly get multi-lined when we add a trailing comment:

Input:

function f(
    arg: '1' | '2',
    otherArg: string,
) { }

add a // todo: fix me after arg: '1' | '2',

Output:

function f(
  arg:
    | '1'
    | '2', // todo: fix me
  otherArg: string,
) { }

Expected:

function f(
    arg: '1' | '2', // todo: fix me
    otherArg: string,
) { }

This PR fixes that by allowing a trailing line comment for the last node in the node list.

I've added tests for this case (union / intersection types), and these are the the most obvious cases I've seen so far, but it's probable that this affects many other collections as well.

declanvong avatar Feb 22 '23 11:02 declanvong