diff options
author | Valentin Anger <syrupthinker@gryphno.de> | 2020-06-29 14:17:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-29 14:17:37 +0200 |
commit | db36857288609858ada259444509a31637980ce3 (patch) | |
tree | 9fc83f32408941bdf75d803b4b0dbffe297c10f2 /cli/diagnostics.rs | |
parent | 0374eadcf7ecb054dae1b1587843a2006fdf4c2d (diff) |
refactor: util functions take slices instead of heap values (#6547)
Diffstat (limited to 'cli/diagnostics.rs')
-rw-r--r-- | cli/diagnostics.rs | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/cli/diagnostics.rs b/cli/diagnostics.rs index ac0405acd..00707c498 100644 --- a/cli/diagnostics.rs +++ b/cli/diagnostics.rs @@ -96,7 +96,7 @@ fn format_category_and_code( _ => "".to_string(), }; - let code = colors::bold(format!("TS{}", code.to_string())).to_string(); + let code = colors::bold(&format!("TS{}", code.to_string())).to_string(); format!("{} [{}]", code, category) } @@ -120,7 +120,7 @@ fn format_message( /// Formats optional source, line and column numbers into a single string. fn format_maybe_frame( - file_name: Option<String>, + file_name: Option<&str>, line_number: Option<i64>, column_number: Option<i64>, ) -> String { @@ -134,8 +134,8 @@ fn format_maybe_frame( let line_number = line_number.unwrap(); let column_number = column_number.unwrap(); let file_name_c = colors::cyan(file_name.unwrap()); - let line_c = colors::yellow(line_number.to_string()); - let column_c = colors::yellow(column_number.to_string()); + let line_c = colors::yellow(&line_number.to_string()); + let column_c = colors::yellow(&column_number.to_string()); format!("{}:{}:{}", file_name_c, line_c, column_c) } @@ -156,13 +156,13 @@ fn format_maybe_related_information( DiagnosticCategory::Error => true, _ => false, }, - format_message(&rd.message_chain, &rd.message, 0), - rd.source_line.clone(), + &format_message(&rd.message_chain, &rd.message, 0), + rd.source_line.as_deref(), rd.start_column, rd.end_column, // Formatter expects 1-based line and column numbers, but ours are 0-based. &[format_maybe_frame( - rd.script_resource_name.clone(), + rd.script_resource_name.as_deref(), rd.line_number.map(|n| n + 1), rd.start_column.map(|n| n + 1), )], @@ -184,17 +184,17 @@ impl fmt::Display for DiagnosticItem { DiagnosticCategory::Error => true, _ => false, }, - format!( + &format!( "{}: {}", format_category_and_code(&self.category, self.code), format_message(&self.message_chain, &self.message, 0) ), - self.source_line.clone(), + self.source_line.as_deref(), self.start_column, self.end_column, // Formatter expects 1-based line and column numbers, but ours are 0-based. &[format_maybe_frame( - self.script_resource_name.clone(), + self.script_resource_name.as_deref(), self.line_number.map(|n| n + 1), self.start_column.map(|n| n + 1) )], @@ -453,11 +453,8 @@ mod tests { #[test] fn test_format_some_frame() { - let actual = format_maybe_frame( - Some("file://foo/bar.ts".to_string()), - Some(1), - Some(2), - ); + let actual = + format_maybe_frame(Some("file://foo/bar.ts"), Some(1), Some(2)); assert_eq!(strip_ansi_codes(&actual), "file://foo/bar.ts:1:2"); } } |