Skip to content

Commit c19bcde

Browse files
YTommematipicodyc3
committed
fix: remove unexpected new line when adding a readonly class property (biomejs#8075)
Co-authored-by: Emanuele Stoppa <[email protected]> Co-authored-by: Carson McManus <[email protected]>
1 parent 18c129c commit c19bcde

File tree

4 files changed

+141
-105
lines changed

4 files changed

+141
-105
lines changed

.changeset/fuzzy-dragons-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#7948](https://github.com/biomejs/biome/issues/7948): The `useReadonlyClassProperties` code fix when `checkAllProperties` is enabled will no longer insert a newline after `readonly` and the class property.

crates/biome_js_analyze/src/lint/style/use_readonly_class_properties.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl Rule for UseReadonlyClassProperties {
219219
);
220220

221221
if let Some(modified_member) =
222-
extract_property_member_name_trimmed_whitespace(member_name.clone())
222+
extract_class_member_name_trimmed_whitespace(member_name.clone())
223223
{
224224
let mut builder =
225225
make::js_property_class_member(replace_modifiers, modified_member);
@@ -353,10 +353,10 @@ fn collect_non_readonly_constructor_parameters(
353353
.collect()
354354
}
355355

356-
/// Removes leading whitespace from `#privateProperty` names. Without this, the name might include
357-
/// unwanted whitespace (e.g., "\n #privateProperty"). This ensures that when adding modifiers like
358-
/// `readonly`, they are appended correctly without being affected by the whitespace.
359-
fn extract_property_member_name_trimmed_whitespace(
356+
/// Removes leading trivia from class member names. Without this, the name might include
357+
/// unwanted trivia (e.g., "\n #privateProperty"). This ensures that when adding modifiers like
358+
/// `readonly`, they are appended correctly without being affected by the trivia.
359+
fn extract_class_member_name_trimmed_whitespace(
360360
member_name: AnyJsClassMemberName,
361361
) -> Option<AnyJsClassMemberName> {
362362
match member_name {
@@ -367,6 +367,13 @@ fn extract_property_member_name_trimmed_whitespace(
367367

368368
Some(AnyJsClassMemberName::JsPrivateClassMemberName(trimmed))
369369
}
370+
AnyJsClassMemberName::JsLiteralMemberName(name) => {
371+
let value = name.value().ok()?;
372+
let new_value = value.with_leading_trivia([]);
373+
let trimmed = name.replace_token_discard_trivia(value, new_value)?;
374+
375+
Some(AnyJsClassMemberName::JsLiteralMemberName(trimmed))
376+
}
370377
_ => Some(member_name),
371378
}
372379
}

crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/invalid_checkAllPropertiesTrue.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Example1 {
33
private prop1: number = 42;
44
protected prop2: string;
55
public prop3: string;
6+
prop4: string;
67
}
78

89
class Example2 {

0 commit comments

Comments
 (0)