Skip to content

Commit

Permalink
fix(semantic): jsx reference with an incorrect node id (#2546)
Browse files Browse the repository at this point in the history
The get kind from the node id should be `JSXIdentifier`, but now it's `JSXOpeningElement`.
  • Loading branch information
Dunqing authored Mar 1, 2024
1 parent 34ecdd5 commit 37de80d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 29 deletions.
8 changes: 5 additions & 3 deletions crates/oxc_linter/src/rules/import/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_ast::{ast::JSXElementName, AstKind};
use oxc_ast::AstKind;
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
Expand Down Expand Up @@ -95,8 +95,10 @@ impl Rule for Namespace {
}
}

AstKind::JSXOpeningElement(element) => {
if let JSXElementName::MemberExpression(expr) = &element.name {
AstKind::JSXMemberExpressionObject(_) => {
if let Some(AstKind::JSXMemberExpression(expr)) =
ctx.nodes().parent_kind(node.id())
{
check_binding_exported(&expr.property.name, expr.property.span);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/nextjs/inline_script_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Rule for InlineScriptId {
'references_loop: for reference in
ctx.semantic().symbol_references(specifier.local.symbol_id.get().unwrap())
{
let node = ctx.nodes().get_node(reference.node_id());
let Some(node) = ctx.nodes().parent_node(reference.node_id()) else { return };

let AstKind::JSXElementName(_) = node.kind() else { continue };
let parent_node = ctx.nodes().parent_node(node.id()).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Rule for NoScriptComponentInHead {
for reference in
ctx.semantic().symbol_references(default_import.local.symbol_id.get().unwrap())
{
let node = ctx.nodes().get_node(reference.node_id());
let Some(node) = ctx.nodes().parent_node(reference.node_id()) else { return };

let AstKind::JSXElementName(_) = node.kind() else { continue };
let parent_node = ctx.nodes().parent_node(node.id()).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Rule for NoTitleInDocumentHead {
for reference in
ctx.semantic().symbol_references(default_import.local.symbol_id.get().unwrap())
{
let node = ctx.nodes().get_node(reference.node_id());
let Some(node) = ctx.nodes().parent_node(reference.node_id()) else { return };

let AstKind::JSXElementName(_) = node.kind() else { continue };
let parent_node = ctx.nodes().parent_node(node.id()).unwrap();
Expand Down
40 changes: 17 additions & 23 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,9 @@ impl<'a> SemanticBuilder<'a> {
AstKind::IdentifierReference(ident) => {
self.reference_identifier(ident);
}
AstKind::JSXIdentifier(ident) => {
self.reference_jsx_identifier(ident);
}
AstKind::UpdateExpression(_) => {
if self.is_not_expression_statement_parent() {
self.current_reference_flag |= ReferenceFlag::Read;
Expand All @@ -1767,9 +1770,6 @@ impl<'a> SemanticBuilder<'a> {
AstKind::AssignmentTarget(_) => {
self.current_reference_flag |= ReferenceFlag::Write;
}
AstKind::JSXElementName(elem) => {
self.reference_jsx_element_name(elem);
}
AstKind::LabeledStatement(stmt) => {
self.label_builder.enter(stmt, self.current_node_id);
}
Expand Down Expand Up @@ -1872,29 +1872,23 @@ impl<'a> SemanticBuilder<'a> {
}
}

fn reference_jsx_element_name(&mut self, elem: &JSXElementName) {
if matches!(
self.nodes.parent_kind(self.current_node_id),
Some(AstKind::JSXOpeningElement(_))
) {
if let Some(ident) = match elem {
JSXElementName::Identifier(ident)
if ident.name.chars().next().is_some_and(char::is_uppercase) =>
{
Some(ident)
fn reference_jsx_identifier(&mut self, ident: &JSXIdentifier) {
match self.nodes.parent_kind(self.current_node_id) {
Some(AstKind::JSXElementName(_)) => {
if !ident.name.chars().next().is_some_and(char::is_uppercase) {
return;
}
JSXElementName::MemberExpression(expr) => Some(expr.get_object_identifier()),
_ => None,
} {
let reference = Reference::new(
ident.span,
ident.name.to_compact_string(),
self.current_node_id,
ReferenceFlag::read(),
);
self.declare_reference(reference);
}
Some(AstKind::JSXMemberExpressionObject(_)) => {}
_ => return,
}
let reference = Reference::new(
ident.span,
ident.name.to_compact_string(),
self.current_node_id,
ReferenceFlag::read(),
);
self.declare_reference(reference);
}

fn is_not_expression_statement_parent(&self) -> bool {
Expand Down

0 comments on commit 37de80d

Please sign in to comment.