Skip to main content

mago_analyzer/plugin/hook/
declaration.rs

1//! Declaration hooks for class and function analysis events.
2
3use mago_codex::metadata::class_like::ClassLikeMetadata;
4use mago_codex::metadata::function_like::FunctionLikeMetadata;
5use mago_syntax::cst::Class;
6use mago_syntax::cst::Enum;
7use mago_syntax::cst::Function;
8use mago_syntax::cst::Interface;
9use mago_syntax::cst::Trait;
10
11use crate::plugin::context::HookContext;
12use crate::plugin::hook::HookResult;
13use crate::plugin::provider::Provider;
14
15/// Hook trait for intercepting class declaration analysis.
16///
17/// This hook receives the real AST class node, full class metadata,
18/// and mutable context, allowing hooks to inspect classes, report issues,
19/// and modify analysis state.
20pub trait ClassDeclarationHook: Provider {
21    /// Called when entering a class declaration.
22    ///
23    /// # Errors
24    ///
25    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
26    fn on_enter_class(
27        &self,
28        _class: &Class<'_>,
29        _metadata: &ClassLikeMetadata,
30        _context: &mut HookContext<'_, '_>,
31    ) -> HookResult<()> {
32        Ok(())
33    }
34
35    /// Called when leaving a class declaration.
36    ///
37    /// # Errors
38    ///
39    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
40    fn on_leave_class(
41        &self,
42        _class: &Class<'_>,
43        _metadata: &ClassLikeMetadata,
44        _context: &mut HookContext<'_, '_>,
45    ) -> HookResult<()> {
46        Ok(())
47    }
48}
49
50/// Hook trait for intercepting interface declaration analysis.
51pub trait InterfaceDeclarationHook: Provider {
52    /// Called when entering an interface declaration.
53    ///
54    /// # Errors
55    ///
56    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
57    fn on_enter_interface(
58        &self,
59        _interface: &Interface<'_>,
60        _metadata: &ClassLikeMetadata,
61        _context: &mut HookContext<'_, '_>,
62    ) -> HookResult<()> {
63        Ok(())
64    }
65
66    /// Called when leaving an interface declaration.
67    ///
68    /// # Errors
69    ///
70    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
71    fn on_leave_interface(
72        &self,
73        _interface: &Interface<'_>,
74        _metadata: &ClassLikeMetadata,
75        _context: &mut HookContext<'_, '_>,
76    ) -> HookResult<()> {
77        Ok(())
78    }
79}
80
81/// Hook trait for intercepting trait declaration analysis.
82pub trait TraitDeclarationHook: Provider {
83    /// Called when entering a trait declaration.
84    ///
85    /// # Errors
86    ///
87    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
88    fn on_enter_trait(
89        &self,
90        _trait_: &Trait<'_>,
91        _metadata: &ClassLikeMetadata,
92        _context: &mut HookContext<'_, '_>,
93    ) -> HookResult<()> {
94        Ok(())
95    }
96
97    /// Called when leaving a trait declaration.
98    ///
99    /// # Errors
100    ///
101    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
102    fn on_leave_trait(
103        &self,
104        _trait_: &Trait<'_>,
105        _metadata: &ClassLikeMetadata,
106        _context: &mut HookContext<'_, '_>,
107    ) -> HookResult<()> {
108        Ok(())
109    }
110}
111
112/// Hook trait for intercepting enum declaration analysis.
113pub trait EnumDeclarationHook: Provider {
114    /// Called when entering an enum declaration.
115    ///
116    /// # Errors
117    ///
118    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
119    fn on_enter_enum(
120        &self,
121        _enum_: &Enum<'_>,
122        _metadata: &ClassLikeMetadata,
123        _context: &mut HookContext<'_, '_>,
124    ) -> HookResult<()> {
125        Ok(())
126    }
127
128    /// Called when leaving an enum declaration.
129    ///
130    /// # Errors
131    ///
132    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
133    fn on_leave_enum(
134        &self,
135        _enum_: &Enum<'_>,
136        _metadata: &ClassLikeMetadata,
137        _context: &mut HookContext<'_, '_>,
138    ) -> HookResult<()> {
139        Ok(())
140    }
141}
142
143/// Hook trait for intercepting function declaration analysis.
144///
145/// This hook receives the real AST function node, full function metadata,
146/// and mutable context, allowing hooks to inspect functions, report issues,
147/// and modify analysis state.
148pub trait FunctionDeclarationHook: Provider {
149    /// Called when entering a function declaration.
150    ///
151    /// # Errors
152    ///
153    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
154    fn on_enter_function(
155        &self,
156        _function: &Function<'_>,
157        _metadata: &FunctionLikeMetadata,
158        _context: &mut HookContext<'_, '_>,
159    ) -> HookResult<()> {
160        Ok(())
161    }
162
163    /// Called when leaving a function declaration.
164    ///
165    /// # Errors
166    ///
167    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
168    fn on_leave_function(
169        &self,
170        _function: &Function<'_>,
171        _metadata: &FunctionLikeMetadata,
172        _context: &mut HookContext<'_, '_>,
173    ) -> HookResult<()> {
174        Ok(())
175    }
176}