dprint / dprint-plugin-typescript

TypeScript and JavaScript code formatting plugin for dprint.
https://dprint.dev/plugins/typescript
MIT License
248 stars 50 forks source link

Interpretation of trailing comments with ASI formatting #609

Open cjpearson opened 5 months ago

cjpearson commented 5 months ago

Describe the bug

dprint-plugin-typescript version: 0.88.10

When using the "asi" option for semicolons, formatters will sometimes have to insert a semicolon at the beginning of a line. An example with dprint:

Input

let line1 = 'hello';

// Comment
[].filter(x => x)

Formatted

let line1 = "hello"

// Comment
;[].filter(x => x)

However, if the code is already formatted, dprint will attach the comment to the first statement. Although technically the comment may be contained within the first statement, developers would typically understand the comment to belong to the latter statement and it would nice if dprint could handle this case.

Input Code

let line1 = "hello"

// Comment
;[].filter(x => x)

Expected Output

let line1 = "hello"

// Comment
;[].filter(x => x)

Actual Output

let line1 = "hello" // Comment
;[].filter(x => x)