Skip to content

Conversation

@siketyan
Copy link
Member

Summary

Fixes #6029

The Automatic Semicolon Insertion (ASI) has some exceptions where we need to put a semicolon anyway to disambiguate the syntax. When we prefer to use ASI and omit semicolons if possible, we put a required semicolon just before the next statement.

In this case, the text range of the previous statement ends at the semicolon we put. Then the distance of two statements will be zero, which leads the intended newline to be removed incorrectly.

To fix this issue, the formatter of JsExpressionStatement now measures the distance between the semicolon and the previous token of the semicolon and writes a newline if the distance is greater than one line.

Test Plan

Added a snapshot test.

@siketyan siketyan requested review from a team May 17, 2025 08:49
@siketyan siketyan self-assigned this May 17, 2025
@github-actions github-actions bot added A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages labels May 17, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented May 17, 2025

CodSpeed Performance Report

Merging #6048 will not alter performance

Comparing siketyan:fix/GH-6029 (46defdb) with main (6908606)

Summary

✅ 95 untouched benchmarks

@siketyan siketyan changed the title fix: keep newline before semicolon in the previous statement fix(formatter): keep newline before semicolon in the previous statement May 17, 2025
Copy link
Member

@Conaclos Conaclos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

Copy link
Member

@chansuke chansuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@siketyan siketyan merged commit aecdbc7 into biomejs:main May 18, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JS formatter: asNeeded semicolons results in lost whitespace

3 participants