+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/five-ghosts-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#6795](https://github.com/biomejs/biome/issues/6795): `noUnassignedVariables` now correctly recognizes variables used in JSX `ref` attributes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* should not generate diagnostics */
export const Component = () => {
let value;
return <div ref={value} />
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid.jsx
---
# Input
```jsx
/* should not generate diagnostics */
export const Component = () => {
let value;
return <div ref={value} />
}

```
24 changes: 23 additions & 1 deletion crates/biome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use biome_js_syntax::{
AnyJsIdentifierUsage, JsDirective, JsLanguage, JsSyntaxKind, JsSyntaxNode, TextRange,
TsTypeParameterName, inner_string_text,
};
use biome_js_syntax::{AnyJsImportClause, AnyJsNamedImportSpecifier, AnyTsType};
use biome_js_syntax::{
AnyJsImportClause, AnyJsNamedImportSpecifier, AnyJsxAttributeName, AnyTsType, JsxAttribute,
};
use biome_rowan::TextSize;
use biome_rowan::{AstNode, SyntaxNodeOptionExt, TokenText, syntax::Preorder};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -709,6 +711,18 @@ impl SemanticEventExtractor {
}
}

fn is_inside_jsx_ref_attribute(node: &JsSyntaxNode) -> bool {
node.ancestors()
.find_map(JsxAttribute::cast)
.and_then(|attr| attr.name().ok())
.is_some_and(|attr_name| match attr_name {
AnyJsxAttributeName::JsxName(jsx_name) => jsx_name
.value_token()
.is_ok_and(|token| token.text_trimmed() == "ref"),
_ => false,
})
}

fn enter_identifier_usage(&mut self, node: AnyJsIdentifierUsage) {
let range = node.syntax().text_trimmed_range();
let Ok(name_token) = node.value_token() else {
Expand All @@ -728,6 +742,14 @@ impl SemanticEventExtractor {
);
return;
};

if parent.kind() == JS_IDENTIFIER_EXPRESSION
&& Self::is_inside_jsx_ref_attribute(&parent)
{
self.push_reference(BindingName::Value(name), Reference::Write(range));
return;
}

match parent.kind() {
JS_EXPORT_NAMED_SHORTHAND_SPECIFIER | JS_EXPORT_NAMED_SPECIFIER => {
self.push_reference(
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载