diff --git a/.changeset/use_readonly_class_properties_nested_assignment_fix.md b/.changeset/use_readonly_class_properties_nested_assignment_fix.md new file mode 100644 index 000000000000..bfcd2a114be3 --- /dev/null +++ b/.changeset/use_readonly_class_properties_nested_assignment_fix.md @@ -0,0 +1,18 @@ +--- +"@biomejs/biome": patch +--- + +Fixed [#7310](https://github.com/biomejs/biome/issues/7310): [`useReadonlyClassProperties`](https://biomejs.dev/linter/rules/use-readonly-class-properties/) correctly handles nested assignments, avoiding false positives when a class property is assigned within another assignment expression. + +Example of code that previously triggered a false positive but is now correctly ignored: + +```ts +class test { + private thing: number = 0; // incorrectly flagged + + public incrementThing(): void { + const temp = {x: 0}; + temp.x = this.thing++; + } +} +``` diff --git a/crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/valid.ts b/crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/valid.ts index d69704fe6294..21f280965fd3 100644 --- a/crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/valid.ts +++ b/crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/valid.ts @@ -664,3 +664,24 @@ export class ToastService { this.activeToasts.push({id, message, type, autoClose}); } } + +class TestIncrementInAssignment { + private thing: number = 0; + + public incrementThing(): void { + const temp = { x: 0 }; + temp.x = this.thing++; + } +} + +class TesAssignmentInNestedCallback { + private thing: any; + + public doStuff(): void { + ui.showText("example", { + callback: () => { + this.thing = x; + }, + }); + } +} diff --git a/crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/valid.ts.snap b/crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/valid.ts.snap index 4fb750b12381..b0498464026c 100644 --- a/crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/valid.ts.snap +++ b/crates/biome_js_analyze/tests/specs/style/useReadonlyClassProperties/valid.ts.snap @@ -1,6 +1,6 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs -assertion_line: 146 +assertion_line: 152 expression: valid.ts --- # Input @@ -672,4 +672,25 @@ export class ToastService { } } +class TestIncrementInAssignment { + private thing: number = 0; + + public incrementThing(): void { + const temp = { x: 0 }; + temp.x = this.thing++; + } +} + +class TesAssignmentInNestedCallback { + private thing: any; + + public doStuff(): void { + ui.showText("example", { + callback: () => { + this.thing = x; + }, + }); + } +} + ```