diff --git a/crates/oxc_query/src/edges.rs b/crates/oxc_query/src/edges.rs index fb1e7e492..46ae0d1d2 100644 --- a/crates/oxc_query/src/edges.rs +++ b/crates/oxc_query/src/edges.rs @@ -3504,6 +3504,7 @@ pub(super) fn resolve_var_ref_edge<'a, 'b: 'a>( ) -> ContextOutcomeIterator<'a, Vertex<'b>, VertexIterator<'a, Vertex<'b>>> { match edge_name { "span" => var_ref::span(contexts, resolve_info), + "declaration" => var_ref::declaration(contexts, resolve_info, adapter), "ancestor" => ancestors(contexts, adapter), "parent" => parents(contexts, adapter), "strip_parens" => strip_parens(contexts, parameters), @@ -3515,11 +3516,35 @@ pub(super) fn resolve_var_ref_edge<'a, 'b: 'a>( mod var_ref { use trustfall::provider::{ - ContextIterator, ContextOutcomeIterator, ResolveEdgeInfo, VertexIterator, + resolve_neighbors_with, ContextIterator, ContextOutcomeIterator, ResolveEdgeInfo, + VertexIterator, }; + use crate::Adapter; + use super::{super::vertex::Vertex, get_span}; + pub(super) fn declaration<'a, 'b: 'a>( + contexts: ContextIterator<'a, Vertex<'b>>, + _resolve_info: &ResolveEdgeInfo, + adapter: &'a Adapter<'b>, + ) -> ContextOutcomeIterator<'a, Vertex<'b>, VertexIterator<'a, Vertex<'b>>> { + resolve_neighbors_with(contexts, |v| { + let var_ref = v + .as_var_ref() + .unwrap_or_else(|| panic!("expected to have a varref vertex, instead have: {v:#?}")) + .identifier_reference; + let v = var_ref + .reference_id + .get() + .and_then(|x| adapter.semantic.symbols().references.get(x)) + .and_then(oxc_semantic::Reference::symbol_id) + .map(|x| adapter.semantic.symbol_declaration(x)); + + Box::new(v.into_iter().map(|x| (*x).into())) + }) + } + pub(super) fn span<'a, 'b: 'a>( contexts: ContextIterator<'a, Vertex<'b>>, _resolve_info: &ResolveEdgeInfo, diff --git a/crates/oxc_query/src/schema.graphql b/crates/oxc_query/src/schema.graphql index f94a55d03..e8edd8f24 100644 --- a/crates/oxc_query/src/schema.graphql +++ b/crates/oxc_query/src/schema.graphql @@ -823,6 +823,7 @@ type FnDeclarationAST implements Function & ASTNode & FnDeclaration & HasSpan & interface VarRef implements HasSpan & Expression { name: String! + declaration: ASTNode # Expression as_constant_string: String """ @@ -835,6 +836,7 @@ interface VarRef implements HasSpan & Expression { type VarRefAST implements HasSpan & Expression & VarRef & ASTNode { name: String! + declaration: ASTNode # ASTNode parent: ASTNode ancestor: [ASTNode!]!