Skip to content

Commit b247a7b

Browse files
feat: add rule stats
1 parent 02b8904 commit b247a7b

6 files changed

Lines changed: 35 additions & 28 deletions

File tree

crates/cli/src/config.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::lang::{CustomLang, LanguageGlobs, SerializableInjection, SgLang};
2-
use crate::utils::ErrorContext as EC;
2+
use crate::utils::{ErrorContext as EC, RuleStats};
33

44
use anyhow::{Context, Result};
55
use ast_grep_config::{
@@ -57,7 +57,7 @@ pub struct AstGrepConfig {
5757
pub fn find_rules(
5858
config_path: Option<PathBuf>,
5959
rule_filter: Option<&Regex>,
60-
) -> Result<RuleCollection<SgLang>> {
60+
) -> Result<(RuleCollection<SgLang>, RuleStats)> {
6161
let config_path =
6262
find_config_path_with_default(config_path, None).context(EC::ReadConfiguration)?;
6363
let config_str = read_to_string(&config_path).context(EC::ReadConfiguration)?;
@@ -132,7 +132,7 @@ fn read_directory_yaml(
132132
rule_dirs: Vec<PathBuf>,
133133
global_rules: GlobalRules<SgLang>,
134134
rule_filter: Option<&Regex>,
135-
) -> Result<RuleCollection<SgLang>> {
135+
) -> Result<(RuleCollection<SgLang>, RuleStats)> {
136136
let mut configs = vec![];
137137
for dir in rule_dirs {
138138
let dir_path = base_dir.join(dir);
@@ -154,14 +154,20 @@ fn read_directory_yaml(
154154
configs.extend(new_configs);
155155
}
156156
}
157+
let total_rule_count = configs.len();
157158

158159
let configs = if let Some(filter) = rule_filter {
159160
filter_rule_by_regex(configs, filter)?
160161
} else {
161162
configs
162163
};
163-
164-
RuleCollection::try_new(configs).context(EC::GlobPattern)
164+
let collection = RuleCollection::try_new(configs).context(EC::GlobPattern)?;
165+
let effective_rule_count = collection.total_rule_count();
166+
let stats = RuleStats {
167+
effective_rule_count,
168+
skipped_rule_count: total_rule_count - effective_rule_count,
169+
};
170+
Ok((collection, stats))
165171
}
166172

167173
fn filter_rule_by_regex(

crates/cli/src/lsp.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ async fn run_language_server_impl(arg: LspArg) -> Result<()> {
2525
let stdout = tokio::io::stdout();
2626
let config_base = find_config_base(arg.config.clone())?;
2727
let config_result = find_rules(arg.config, None);
28-
let config_result_std: std::result::Result<_, String> = config_result.map_err(|e| {
29-
// convert anyhow::Error to String with chain of causes
30-
e.chain()
31-
.map(|e| e.to_string())
32-
.collect::<Vec<_>>()
33-
.join(". ")
34-
});
28+
let config_result_std: std::result::Result<_, String> = config_result
29+
.map_err(|e| {
30+
// convert anyhow::Error to String with chain of causes
31+
e.chain()
32+
.map(|e| e.to_string())
33+
.collect::<Vec<_>>()
34+
.join(". ")
35+
})
36+
.map(|r| r.0);
3537
let (service, socket) =
3638
LspService::build(|client| Backend::new(client, config_base, config_result_std)).finish();
3739
Server::new(stdin, stdout, socket).serve(service).await;

crates/cli/src/scan.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::print::{
1717
};
1818
use crate::utils::ErrorContext as EC;
1919
use crate::utils::{filter_file_interactive, InputArgs, OutputArgs};
20-
use crate::utils::{FileStats, ScanStats};
20+
use crate::utils::{FileStats, RuleStats, ScanStats};
2121
use crate::utils::{Items, PathWorker, StdInWorker, Worker};
2222

2323
type AstGrep = ast_grep_core::AstGrep<StrDoc<SgLang>>;
@@ -106,6 +106,7 @@ struct ScanWithConfig<Printer> {
106106
}
107107
impl<P: Printer> ScanWithConfig<P> {
108108
fn try_new(mut arg: ScanArg, printer: P) -> Result<Self> {
109+
let mut rule_stats = RuleStats::default();
109110
let configs = if let Some(path) = &arg.rule {
110111
let rules = read_rule_file(path, None)?;
111112
RuleCollection::try_new(rules).context(EC::GlobPattern)?
@@ -114,13 +115,18 @@ impl<P: Printer> ScanWithConfig<P> {
114115
.with_context(|| EC::ParseRule("INLINE_RULES".into()))?;
115116
RuleCollection::try_new(rules).context(EC::GlobPattern)?
116117
} else {
117-
find_rules(arg.config.take(), arg.filter.as_ref())?
118+
let (configs, r_stats) = find_rules(arg.config.take(), arg.filter.as_ref())?;
119+
rule_stats = r_stats;
120+
configs
118121
};
119122
Ok(Self {
120123
arg,
121124
printer,
122125
configs,
123-
stats: ScanStats::default(),
126+
stats: ScanStats {
127+
file_stats: FileStats::default(),
128+
inner: rule_stats,
129+
},
124130
})
125131
}
126132
}
@@ -157,6 +163,7 @@ impl<P: Printer> Worker for ScanWithConfig<P> {
157163
}
158164
}
159165
self.printer.after_print()?;
166+
eprintln!("{:?}", self.stats);
160167
if error_count > 0 {
161168
Err(anyhow::anyhow!(EC::DiagnosticError(error_count)))
162169
} else {

crates/cli/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod worker;
77
pub use args::{InputArgs, OutputArgs};
88
pub use debug_query::DebugFormat;
99
pub use error_context::{exit_with_error, ErrorContext};
10-
pub use tracing::{FileStats, RunStats, ScanStats};
10+
pub use tracing::{FileStats, RuleStats, RunStats, ScanStats};
1111
pub use worker::{Items, PathWorker, StdInWorker, Worker};
1212

1313
use crate::lang::SgLang;

crates/cli/src/utils/tracing.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub enum Tracing {
3333

3434
// total = scanned + skipped
3535
// = (matched + unmatched) + skipped
36-
#[derive(Default)]
36+
#[derive(Default, Debug)]
3737
pub struct FileStats {
3838
files_scanned: AtomicUsize,
3939
files_skipped: AtomicUsize,
@@ -54,26 +54,18 @@ impl FileStats {
5454
}
5555
}
5656

57+
#[derive(Debug)]
5758
pub struct SummaryStats<T> {
5859
pub file_stats: FileStats,
5960
pub inner: T,
6061
}
6162

62-
#[derive(Default)]
63+
#[derive(Default, Debug)]
6364
pub struct RuleStats {
6465
pub effective_rule_count: usize,
6566
pub skipped_rule_count: usize,
6667
}
6768

68-
impl RuleStats {
69-
pub fn add_effective_rule(&mut self) {
70-
self.effective_rule_count += 1;
71-
}
72-
pub fn add_skipped_rule(&mut self) {
73-
self.skipped_rule_count += 1;
74-
}
75-
}
76-
7769
pub type RunStats = SummaryStats<()>;
7870
pub type ScanStats = SummaryStats<RuleStats>;
7971

crates/cli/src/verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ where
5555
}
5656

5757
fn run_test_rule_impl<R: Reporter + Send>(arg: TestArg, reporter: R) -> Result<()> {
58-
let collections = &find_rules(arg.config.clone(), None)?;
58+
let collections = &find_rules(arg.config.clone(), None)?.0;
5959
let TestHarness {
6060
test_cases,
6161
snapshots,

0 commit comments

Comments
 (0)