Skip to content

Commit e36cfbf

Browse files
authored
perf!: faster printing (compact) (#225)
1 parent 29bc27c commit e36cfbf

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

crates/lintspec-core/src/lint.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,38 +61,23 @@ pub struct ItemDiagnostics {
6161
impl ItemDiagnostics {
6262
/// Print the diagnostics for a single source item in a compact format
6363
///
64-
/// The writer `f` can be stderr or a file handle, for example. The path to the file containing this source item
65-
/// should be provided, as well as the current working directory where the command was launched from. This last
66-
/// information is used to compute the relative path when possible.
64+
/// The writer `f` can be stderr or a file handle, for example. The `source_name` should be
65+
/// the display name for the file (typically a relative path from the working directory).
6766
pub fn print_compact(
6867
&self,
6968
f: &mut impl io::Write,
70-
path: impl AsRef<Path>,
71-
root_dir: impl AsRef<Path>,
69+
source_name: &str,
7270
) -> std::result::Result<(), io::Error> {
73-
fn inner(
74-
this: &ItemDiagnostics,
75-
f: &mut impl io::Write,
76-
path: &Path,
77-
root_dir: &Path,
78-
) -> std::result::Result<(), io::Error> {
79-
let source_name = match path.strip_prefix(root_dir) {
80-
Ok(relative_path) => relative_path.to_string_lossy(),
81-
Err(_) => path.to_string_lossy(),
82-
};
83-
writeln!(f, "{source_name}:{}", this.span.start)?;
84-
if let Some(parent) = &this.parent {
85-
writeln!(f, "{} {}.{}", this.item_type, parent, this.name)?;
86-
} else {
87-
writeln!(f, "{} {}", this.item_type, this.name)?;
88-
}
89-
for diag in &this.diags {
90-
writeln!(f, " {}", diag.message)?;
91-
}
92-
writeln!(f)?;
93-
Ok(())
71+
writeln!(f, "{source_name}:{}", self.span.start)?;
72+
if let Some(parent) = &self.parent {
73+
writeln!(f, "{} {}.{}", self.item_type, parent, self.name)?;
74+
} else {
75+
writeln!(f, "{} {}", self.item_type, self.name)?;
76+
}
77+
for diag in &self.diags {
78+
writeln!(f, " {}", diag.message)?;
9479
}
95-
inner(self, f, path.as_ref(), root_dir.as_ref())
80+
writeln!(f)
9681
}
9782
}
9883

crates/lintspec-core/tests/common.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ pub fn print_reports(
7575
compact: bool,
7676
) -> Result<(), io::Error> {
7777
if compact {
78+
let source_name = match file_diags.path.strip_prefix(root_path) {
79+
Ok(relative_path) => relative_path.to_string_lossy(),
80+
Err(_) => file_diags.path.to_string_lossy(),
81+
};
7882
for item_diags in file_diags.items {
79-
item_diags.print_compact(f, &file_diags.path, root_path)?;
83+
item_diags.print_compact(f, &source_name)?;
8084
}
8185
} else {
8286
let source_name = match file_diags.path.strip_prefix(root_path) {

crates/lintspec/src/cli.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,12 @@ pub fn print_reports(
523523
compact: bool,
524524
) -> Result<(), io::Error> {
525525
if compact {
526+
let source_name = match file_diags.path.strip_prefix(root_path) {
527+
Ok(relative_path) => relative_path.to_string_lossy(),
528+
Err(_) => file_diags.path.to_string_lossy(),
529+
};
526530
for item_diags in file_diags.items {
527-
item_diags.print_compact(f, &file_diags.path, root_path)?;
531+
item_diags.print_compact(f, &source_name)?;
528532
}
529533
} else {
530534
let source_name = match file_diags.path.strip_prefix(root_path) {

0 commit comments

Comments
 (0)