-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathmod.rs
101 lines (82 loc) · 3.08 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
pub mod detector;
pub(crate) mod experimental;
pub mod helpers;
pub mod high;
pub mod low;
#[cfg(test)]
pub mod test_utils;
#[macro_export]
macro_rules! capture {
($self:ident, $context:ident, $item:expr) => {
if let Some(id) = $context.get_node_id_of_capturable(&$item.clone().into()) {
$self
.found_instances
.insert($context.get_node_sort_key_from_capturable(&$item.clone().into()), id);
} else {
$self
.found_instances
.insert($context.get_node_sort_key_from_capturable(&$item.clone().into()), 0);
}
};
($self:ident, $context:ident, $item:expr, $hint:tt) => {
$self
.hints
.insert($context.get_node_sort_key_from_capturable(&$item.clone().into()), $hint);
capture!($self, $context, $item);
};
}
#[macro_export]
macro_rules! issue_detector {
(
$detector_struct:ident;
severity: $detector_severity:ident,
title: $detector_title:tt,
desc: $detector_desc:tt,
name: $detector_name:ident,
|$context: ident| $e:expr
) => {
#[derive(Default)]
pub struct $detector_struct {
found_instances: std::collections::BTreeMap<(String, usize, String), $crate::ast::NodeID>,
}
impl $crate::detect::detector::IssueDetector for $detector_struct {
fn detect(&mut self, context: &$crate::context::workspace_context::WorkspaceContext) -> Result<bool, Box<dyn std::error::Error>> {
let $context = context;
macro_rules! grab {
($item:expr) => {
if let Some(id) = context.get_node_id_of_capturable(&$item.clone().into()) {
self.found_instances.insert(
$context.get_node_sort_key_from_capturable(&$item.clone().into()),
id,
);
} else {
self.found_instances.insert(
$context.get_node_sort_key_from_capturable(&$item.clone().into()),
0,
);
}
};
}
$e
Ok(!self.found_instances.is_empty())
}
fn severity(&self) -> $crate::detect::detector::IssueSeverity {
$crate::detect::detector::IssueSeverity::$detector_severity
}
fn title(&self) -> String {
String::from($detector_title)
}
fn description(&self) -> String {
String::from($detector_desc)
}
fn instances(&self) -> std::collections::BTreeMap<(String, usize, String), $crate::ast::NodeID> {
self.found_instances.clone()
}
fn name(&self) -> String {
$crate::detect::detector::IssueDetectorNamePool::$detector_name.to_string()
}
}
};
}
pub use capture;
pub use issue_detector;