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::ast::Class;
6use mago_syntax::ast::Enum;
7use mago_syntax::ast::Function;
8use mago_syntax::ast::Interface;
9use mago_syntax::ast::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    fn on_enter_class(
23        &self,
24        _class: &Class<'_>,
25        _metadata: &ClassLikeMetadata,
26        _context: &mut HookContext<'_, '_>,
27    ) -> HookResult<()> {
28        Ok(())
29    }
30
31    /// Called when leaving a class declaration.
32    fn on_leave_class(
33        &self,
34        _class: &Class<'_>,
35        _metadata: &ClassLikeMetadata,
36        _context: &mut HookContext<'_, '_>,
37    ) -> HookResult<()> {
38        Ok(())
39    }
40}
41
42/// Hook trait for intercepting interface declaration analysis.
43pub trait InterfaceDeclarationHook: Provider {
44    /// Called when entering an interface declaration.
45    fn on_enter_interface(
46        &self,
47        _interface: &Interface<'_>,
48        _metadata: &ClassLikeMetadata,
49        _context: &mut HookContext<'_, '_>,
50    ) -> HookResult<()> {
51        Ok(())
52    }
53
54    /// Called when leaving an interface declaration.
55    fn on_leave_interface(
56        &self,
57        _interface: &Interface<'_>,
58        _metadata: &ClassLikeMetadata,
59        _context: &mut HookContext<'_, '_>,
60    ) -> HookResult<()> {
61        Ok(())
62    }
63}
64
65/// Hook trait for intercepting trait declaration analysis.
66pub trait TraitDeclarationHook: Provider {
67    /// Called when entering a trait declaration.
68    fn on_enter_trait(
69        &self,
70        _trait_: &Trait<'_>,
71        _metadata: &ClassLikeMetadata,
72        _context: &mut HookContext<'_, '_>,
73    ) -> HookResult<()> {
74        Ok(())
75    }
76
77    /// Called when leaving a trait declaration.
78    fn on_leave_trait(
79        &self,
80        _trait_: &Trait<'_>,
81        _metadata: &ClassLikeMetadata,
82        _context: &mut HookContext<'_, '_>,
83    ) -> HookResult<()> {
84        Ok(())
85    }
86}
87
88/// Hook trait for intercepting enum declaration analysis.
89pub trait EnumDeclarationHook: Provider {
90    /// Called when entering an enum declaration.
91    fn on_enter_enum(
92        &self,
93        _enum_: &Enum<'_>,
94        _metadata: &ClassLikeMetadata,
95        _context: &mut HookContext<'_, '_>,
96    ) -> HookResult<()> {
97        Ok(())
98    }
99
100    /// Called when leaving an enum declaration.
101    fn on_leave_enum(
102        &self,
103        _enum_: &Enum<'_>,
104        _metadata: &ClassLikeMetadata,
105        _context: &mut HookContext<'_, '_>,
106    ) -> HookResult<()> {
107        Ok(())
108    }
109}
110
111/// Hook trait for intercepting function declaration analysis.
112///
113/// This hook receives the real AST function node, full function metadata,
114/// and mutable context, allowing hooks to inspect functions, report issues,
115/// and modify analysis state.
116pub trait FunctionDeclarationHook: Provider {
117    /// Called when entering a function declaration.
118    fn on_enter_function(
119        &self,
120        _function: &Function<'_>,
121        _metadata: &FunctionLikeMetadata,
122        _context: &mut HookContext<'_, '_>,
123    ) -> HookResult<()> {
124        Ok(())
125    }
126
127    /// Called when leaving a function declaration.
128    fn on_leave_function(
129        &self,
130        _function: &Function<'_>,
131        _metadata: &FunctionLikeMetadata,
132        _context: &mut HookContext<'_, '_>,
133    ) -> HookResult<()> {
134        Ok(())
135    }
136}