Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Conaclos
Copy link
Member

@Conaclos Conaclos commented Sep 7, 2025

Summary

This PR removes some allocations by using iterators.
I also replaced the FxHashSet with a Vec because it is not possible to collect twice the same node.

Test Plan

Everything should be green.

Docs

No behavioral changes/

@changeset-bot
Copy link

changeset-bot bot commented Sep 7, 2025

⚠️ No Changeset found

Latest commit: a9756e8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 7, 2025

Walkthrough

Refactors the no-unused-private-class-members rule to replace FxHashSet-based aggregation with iterator- and Vec-based flows. get_all_declared_private_members and get_constructor_params now return iterators; traverse_members_usage accepts and returns Vec. run collects private members into a Vec, handles the empty case with Box::default(), and returns results via into_boxed_slice(). Private member discovery chains class members and constructor params using iterators. FxHashSet imports and collections are removed. Public function signatures are updated accordingly.

Suggested labels

A-Linter, L-JavaScript

Suggested reviewers

  • ematipico
  • dyc3
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch conaclos/noUnusedPrivateClassMembers-opti

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Sep 7, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs (3)

123-147: Pre-size results and prefer an explicit empty box

Minor allocation/readability nits: allocate results with the exact capacity after unused_members is known, and use Box::new([]) for clarity over Box::default().

-        let private_members: Vec<AnyMember> = get_all_declared_private_members(node).collect();
-        if private_members.is_empty() {
-            Box::default()
-        } else {
-            let mut results = Vec::new();
-            let unused_members = traverse_members_usage(node.syntax(), private_members);
+        let private_members: Vec<AnyMember> = get_all_declared_private_members(node).collect();
+        if private_members.is_empty() {
+            Box::new([])
+        } else {
+            let unused_members = traverse_members_usage(node.syntax(), private_members);
+            let mut results = Vec::with_capacity(unused_members.len());

248-303: Vec-based flow: confirm uniqueness and update the stale comment

Switching from a set to a Vec is fine here, but please confirm we never feed duplicates; a quick debug-only assert guards against accidental regressions. Also, the doc still says “hashmap”.

 fn traverse_members_usage(
     syntax: &JsSyntaxNode,
     mut private_members: Vec<AnyMember>,
 ) -> Vec<AnyMember> {
+    #[cfg(debug_assertions)]
+    {
+        use std::collections::HashSet;
+        let mut seen = HashSet::new();
+        assert!(
+            private_members
+                .iter()
+                .all(|m| seen.insert(m.syntax().text_range())),
+            "duplicate private members passed to traverse_members_usage"
+        );
+    }

Doc tweak (outside the selected range, near Lines 245–246):

-/// if the member usage is found, we remove it from the hashmap
+/// if the member usage is found, we remove it from the working list

352-375: Slightly flatter iterator chain for readability

Equivalent, but a tad easier to scan by grouping map(...).into_iter().flatten() at the end.

-fn get_constructor_params(
-    class_declaration: &JsClassDeclaration,
-) -> impl Iterator<Item = AnyMember> {
-    class_declaration
-        .members()
-        .iter()
-        .find_map(|member| match member {
-            AnyJsClassMember::JsConstructorClassMember(member) => Some(member),
-            _ => None,
-        })
-        .and_then(|constructor_member| constructor_member.parameters().ok())
-        .into_iter()
-        .flat_map(|constructor_params| {
-            constructor_params
-                .parameters()
-                .iter()
-                .filter_map(|param| match param.ok()? {
-                    biome_js_syntax::AnyJsConstructorParameter::TsPropertyParameter(
-                        ts_property,
-                    ) => Some(ts_property.into()),
-                    _ => None,
-                })
-        })
-}
+fn get_constructor_params(
+    class_declaration: &JsClassDeclaration,
+) -> impl Iterator<Item = AnyMember> {
+    class_declaration
+        .members()
+        .iter()
+        .find_map(|member| match member {
+            AnyJsClassMember::JsConstructorClassMember(member) => Some(member),
+            _ => None,
+        })
+        .and_then(|c| c.parameters().ok())
+        .map(|params| {
+            params
+                .parameters()
+                .iter()
+                .filter_map(|param| match param.ok()? {
+                    biome_js_syntax::AnyJsConstructorParameter::TsPropertyParameter(p) => {
+                        Some(p.into())
+                    }
+                    _ => None,
+                })
+        })
+        .into_iter()
+        .flatten()
+}
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 527493a and a9756e8.

📒 Files selected for processing (1)
  • crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs (5 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{rs,toml}

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Format Rust and TOML files before committing (use just f/just format).

Files:

  • crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs
crates/biome_*_{syntax,parser,formatter,analyze,factory,semantic}/**

📄 CodeRabbit inference engine (CLAUDE.md)

Maintain the per-language crate structure: biome_{lang}_{syntax,parser,formatter,analyze,factory,semantic}

Files:

  • crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs
crates/biome_*/**

📄 CodeRabbit inference engine (CLAUDE.md)

Place core crates under /crates/biome_*/

Files:

  • crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (24)
  • GitHub Check: Bench (biome_graphql_formatter)
  • GitHub Check: Documentation
  • GitHub Check: Bench (biome_package)
  • GitHub Check: Bench (biome_html_formatter)
  • GitHub Check: Bench (biome_configuration)
  • GitHub Check: Lint project (depot-windows-2022)
  • GitHub Check: Bench (biome_module_graph)
  • GitHub Check: Test Node.js API
  • GitHub Check: Bench (biome_html_parser)
  • GitHub Check: Check Dependencies
  • GitHub Check: Bench (biome_graphql_parser)
  • GitHub Check: Bench (biome_json_parser)
  • GitHub Check: Lint project (depot-ubuntu-24.04-arm-16)
  • GitHub Check: Bench (biome_css_analyze)
  • GitHub Check: Bench (biome_json_formatter)
  • GitHub Check: Bench (biome_json_analyze)
  • GitHub Check: Bench (biome_js_parser)
  • GitHub Check: Bench (biome_css_parser)
  • GitHub Check: Bench (biome_js_analyze)
  • GitHub Check: Bench (biome_css_formatter)
  • GitHub Check: Test (depot-windows-2022-16)
  • GitHub Check: Bench (biome_js_formatter)
  • GitHub Check: Test (depot-ubuntu-24.04-arm-16)
  • GitHub Check: autofix
🔇 Additional comments (1)
crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs (1)

343-350: Iterator-based discovery LGTM

Chaining members with constructor params and filtering late is clean and allocation-free up to collection. Nice touch.

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 7, 2025

CodSpeed Performance Report

Merging #7424 will not alter performance

Comparing conaclos/noUnusedPrivateClassMembers-opti (a9756e8) with main (527493a)

Summary

✅ 133 untouched benchmarks

@Conaclos Conaclos changed the title fix(noUnusedPrivateClassMembers): avoid some allocations refactor(noUnusedPrivateClassMembers): avoid some allocations Sep 7, 2025
@Conaclos Conaclos merged commit e3bdb5d into main Sep 7, 2025
31 checks passed
@Conaclos Conaclos deleted the conaclos/noUnusedPrivateClassMembers-opti branch September 7, 2025 17:34
kedevked pushed a commit to kedevked/biome that referenced this pull request Sep 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Linter Area: linter L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants