Skip to content

Commit d951e0d

Browse files
feat(biome_js_analyze): Improve no_unused_private_class_members to handle dynamic access class members based on their ts type
1 parent d6da4d5 commit d951e0d

23 files changed

+2895
-917
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Changed [`noUnusedPrivateClassMembers`](https://biomejs.dev/linter/rules/no-unused-private-class-members/) to align more fully with meaningful reads.
6+
7+
This rule now distinguishes more carefully between writes and reads of private class members.
8+
9+
- A *meaningful read* is any access that affects program behavior.
10+
- For example, `this.#x += 1` both reads and writes `#x`, so it counts as usage.
11+
- Pure writes without a read (e.g. `this.#x = 1` with no getter) are no longer treated as usage.
12+
13+
This change ensures that private members are only considered “used” when they are actually read in a way that influences execution.
14+
15+
***Invalid examples (previously valid)***
16+
17+
```ts
18+
class UsedMember {
19+
set #x(value) {
20+
doSomething(value);
21+
}
22+
23+
foo() {
24+
// This assignment does not actually read #x, because there is no getter.
25+
// Previously, this was considered a usage, but now it’s correctly flagged.
26+
this.#x = 1;
27+
}
28+
}
29+
```
30+
31+
***Valid example (Previously invalid)***
32+
33+
```js
34+
class Foo {
35+
#usedOnlyInWriteStatement = 5;
36+
37+
method() {
38+
// This counts as a meaningful read because we both read and write the value.
39+
this.#usedOnlyInWriteStatement += 42;
40+
}
41+
}
42+
```
43+
44+
***Summary***
45+
• Only accesses that read a value are considered meaningful for the purpose of this rule.
46+
• Simple assignments to a setter without a corresponding getter no longer count as usage.
47+
• Operations like +=, method calls returning a value, or reading the property for computation are considered meaningful reads.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
**Improved detection of used private class members**
6+
7+
The analysis for private class members has been improved: now the tool only considers a private member “used” if it is actually referenced in the code.
8+
9+
- Previously, some private members might have been reported as used even if they weren’t actually accessed.
10+
- With this change, only members that are truly read or called in the code are counted as used.
11+
- Members that are never accessed will now be correctly reported as unused.
12+
13+
This makes reports about unused private members more accurate and helps you clean up truly unused code.
14+
15+
***Example (previously valid)***
16+
17+
```ts
18+
type YesNo = "yes" | "no";
19+
20+
export class SampleYesNo {
21+
private yes: () => void;
22+
private no: () => void;
23+
private dontKnow: () => void; // <- will now report as unused
24+
25+
on(action: YesNo): void {
26+
this[action]();
27+
}
28+
}
29+
30+
```

0 commit comments

Comments
 (0)