From 65d63042962a3015c102dd53a8cb13f705902e43 Mon Sep 17 00:00:00 2001 From: l3ops Date: Mon, 21 Nov 2022 17:23:39 +0100 Subject: [PATCH] expose the verbose flag on `PrintDiagnostic` as explicit constructors --- crates/rome_cli/src/traversal.rs | 10 ++--- crates/rome_diagnostics/examples/cli.rs | 2 +- crates/rome_diagnostics/examples/fs.rs | 2 +- crates/rome_diagnostics/examples/lint.rs | 2 +- crates/rome_diagnostics/examples/serde.rs | 2 +- crates/rome_diagnostics/src/display.rs | 41 ++++++++++++++----- crates/rome_js_analyze/src/lib.rs | 2 +- crates/rome_js_analyze/tests/spec_tests.rs | 2 +- .../rome_js_formatter/src/check_reformat.rs | 2 +- .../rome_js_formatter/tests/check_reformat.rs | 2 +- .../rome_js_formatter/tests/prettier_tests.rs | 2 +- crates/rome_js_parser/src/lib.rs | 4 +- crates/rome_js_parser/src/test_utils.rs | 2 +- crates/rome_js_parser/src/tests.rs | 4 +- .../rome_js_semantic/src/tests/assertions.rs | 12 +++--- crates/rome_wasm/src/utils.rs | 8 +++- xtask/bench/src/features/parser.rs | 2 +- xtask/coverage/src/runner.rs | 2 +- xtask/lintdoc/src/main.rs | 6 +-- 19 files changed, 67 insertions(+), 42 deletions(-) diff --git a/crates/rome_cli/src/traversal.rs b/crates/rome_cli/src/traversal.rs index d9b1237ee37..86465118969 100644 --- a/crates/rome_cli/src/traversal.rs +++ b/crates/rome_cli/src/traversal.rs @@ -419,7 +419,7 @@ fn process_messages(options: ProcessMessagesOptions) { if mode.should_report_to_terminal() { if should_print { console.error(markup! { - {PrintDiagnostic(&err, verbose)} + {if verbose { PrintDiagnostic::verbose(&err) } else { PrintDiagnostic::simple(&err) }} }); } } else { @@ -466,7 +466,7 @@ fn process_messages(options: ProcessMessagesOptions) { let diag = diag.with_file_path(&name).with_file_source_code(&content); console.error(markup! { - {PrintDiagnostic(&diag, verbose)} + {if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }} }); } } else { @@ -492,7 +492,7 @@ fn process_messages(options: ProcessMessagesOptions) { let diag = diag.with_file_path(&name).with_file_source_code(&content); console.error(markup! { - {PrintDiagnostic(&diag, verbose)} + {if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }} }); } } else { @@ -542,7 +542,7 @@ fn process_messages(options: ProcessMessagesOptions) { }; console.error(markup! { - {PrintDiagnostic(&diag, verbose)} + {if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }} }); } else { let diag = FormatDiffDiagnostic { @@ -554,7 +554,7 @@ fn process_messages(options: ProcessMessagesOptions) { }; console.error(markup! { - {PrintDiagnostic(&diag, verbose)} + {if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }} }); } } diff --git a/crates/rome_diagnostics/examples/cli.rs b/crates/rome_diagnostics/examples/cli.rs index 5a8776484ee..c41a7477cec 100644 --- a/crates/rome_diagnostics/examples/cli.rs +++ b/crates/rome_diagnostics/examples/cli.rs @@ -59,5 +59,5 @@ pub fn main() { }, }; - EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) })); + EnvConsole::default().error(markup!({ PrintDiagnostic::verbose(&diag) })); } diff --git a/crates/rome_diagnostics/examples/fs.rs b/crates/rome_diagnostics/examples/fs.rs index 2a6b51cc083..65ce55d094b 100644 --- a/crates/rome_diagnostics/examples/fs.rs +++ b/crates/rome_diagnostics/examples/fs.rs @@ -68,5 +68,5 @@ pub fn main() { }, }; - EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) })); + EnvConsole::default().error(markup!({ PrintDiagnostic::verbose(&diag) })); } diff --git a/crates/rome_diagnostics/examples/lint.rs b/crates/rome_diagnostics/examples/lint.rs index e7390c6e16e..66645b55a46 100644 --- a/crates/rome_diagnostics/examples/lint.rs +++ b/crates/rome_diagnostics/examples/lint.rs @@ -90,5 +90,5 @@ console.log(FOO);"; }, }; - EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) })); + EnvConsole::default().error(markup!({ PrintDiagnostic::verbose(&diag) })); } diff --git a/crates/rome_diagnostics/examples/serde.rs b/crates/rome_diagnostics/examples/serde.rs index 6f5649ef1da..dd0dde3d7af 100644 --- a/crates/rome_diagnostics/examples/serde.rs +++ b/crates/rome_diagnostics/examples/serde.rs @@ -53,6 +53,6 @@ fn from_str(input: &str) -> Result { pub fn main() { if let Err(err) = from_str("{\"syntax_error\"") { - EnvConsole::default().error(markup!({ PrintDiagnostic(&err, true) })); + EnvConsole::default().error(markup!({ PrintDiagnostic::verbose(&err) })); }; } diff --git a/crates/rome_diagnostics/src/display.rs b/crates/rome_diagnostics/src/display.rs index a3a1cbb1f91..f3ba3e667e1 100644 --- a/crates/rome_diagnostics/src/display.rs +++ b/crates/rome_diagnostics/src/display.rs @@ -33,11 +33,30 @@ impl<'fmt, D: AsDiagnostic + ?Sized> std::fmt::Display for PrintDescription<'fmt /// Helper struct for printing a diagnostic as markup into any formatter /// implementing [rome_console::fmt::Write]. -pub struct PrintDiagnostic<'fmt, D: ?Sized>(pub &'fmt D, pub bool); +pub struct PrintDiagnostic<'fmt, D: ?Sized> { + diag: &'fmt D, + verbose: bool, +} + +impl<'fmt, D: AsDiagnostic + ?Sized> PrintDiagnostic<'fmt, D> { + pub fn simple(diag: &'fmt D) -> Self { + Self { + diag, + verbose: false, + } + } + + pub fn verbose(diag: &'fmt D) -> Self { + Self { + diag, + verbose: true, + } + } +} impl<'fmt, D: AsDiagnostic + ?Sized> fmt::Display for PrintDiagnostic<'fmt, D> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> io::Result<()> { - let diagnostic = self.0.as_diagnostic(); + let diagnostic = self.diag.as_diagnostic(); // Print the header for the diagnostic fmt.write_markup(markup! { @@ -49,7 +68,7 @@ impl<'fmt, D: AsDiagnostic + ?Sized> fmt::Display for PrintDiagnostic<'fmt, D> { let mut fmt = IndentWriter::wrap(fmt, &mut slot, true, " "); let mut visitor = PrintAdvices(&mut fmt); - print_advices(&mut visitor, diagnostic, self.1) + print_advices(&mut visitor, diagnostic, self.verbose) } } @@ -734,7 +753,7 @@ mod tests { fn test_header() { let diag = TestDiagnostic::::with_location(); - let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); + let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned(); let expected = markup!{ "path:1:1 internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -758,7 +777,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); + let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -788,7 +807,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); + let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -813,7 +832,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); + let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -838,7 +857,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); + let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -863,7 +882,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); + let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -889,7 +908,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); + let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -913,7 +932,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); + let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" diff --git a/crates/rome_js_analyze/src/lib.rs b/crates/rome_js_analyze/src/lib.rs index 2279984cb4c..60d232e573a 100644 --- a/crates/rome_js_analyze/src/lib.rs +++ b/crates/rome_js_analyze/src/lib.rs @@ -167,7 +167,7 @@ mod tests { } let error = diag.with_file_path("ahahah").with_file_source_code(SOURCE); let text = markup_to_string(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); eprintln!("{text}"); } diff --git a/crates/rome_js_analyze/tests/spec_tests.rs b/crates/rome_js_analyze/tests/spec_tests.rs index d61296036a9..aa22e4500d6 100644 --- a/crates/rome_js_analyze/tests/spec_tests.rs +++ b/crates/rome_js_analyze/tests/spec_tests.rs @@ -171,7 +171,7 @@ fn diagnostic_to_string(name: &str, source: &str, diag: AnalyzerDiagnostic) -> S .with_file_path((name, FileId::zero())) .with_file_source_code(source); let text = markup_to_string(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); text diff --git a/crates/rome_js_formatter/src/check_reformat.rs b/crates/rome_js_formatter/src/check_reformat.rs index 501d59adc74..7af12729449 100644 --- a/crates/rome_js_formatter/src/check_reformat.rs +++ b/crates/rome_js_formatter/src/check_reformat.rs @@ -38,7 +38,7 @@ pub fn check_reformat(params: CheckReformatParams) { .with_file_source_code(text.to_string()); Formatter::new(&mut Termcolor(&mut buffer)) .write_markup(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }) .expect("failed to emit diagnostic"); } diff --git a/crates/rome_js_formatter/tests/check_reformat.rs b/crates/rome_js_formatter/tests/check_reformat.rs index fad302299bd..4e02fe09f7a 100644 --- a/crates/rome_js_formatter/tests/check_reformat.rs +++ b/crates/rome_js_formatter/tests/check_reformat.rs @@ -39,7 +39,7 @@ pub fn check_reformat(params: CheckReformatParams) { .with_file_source_code(text.to_string()); Formatter::new(&mut Termcolor(&mut buffer)) .write_markup(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }) .expect("failed to emit diagnostic"); } diff --git a/crates/rome_js_formatter/tests/prettier_tests.rs b/crates/rome_js_formatter/tests/prettier_tests.rs index a19c7a8b1fd..86691585fbd 100644 --- a/crates/rome_js_formatter/tests/prettier_tests.rs +++ b/crates/rome_js_formatter/tests/prettier_tests.rs @@ -226,7 +226,7 @@ fn test_snapshot(input: &'static str, _: &str, _: &str, _: &str) { .with_file_source_code(parse_input.clone()); Formatter::new(&mut Termcolor(&mut buffer)) .write_markup(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }) .expect("failed to emit diagnostic"); } diff --git a/crates/rome_js_parser/src/lib.rs b/crates/rome_js_parser/src/lib.rs index e3430b3d783..05df3aa2903 100644 --- a/crates/rome_js_parser/src/lib.rs +++ b/crates/rome_js_parser/src/lib.rs @@ -517,7 +517,7 @@ impl ParseDiagnostic { /// .with_file_source_code(source.to_string()); /// Formatter::new(&mut Termcolor(&mut write)) /// .write_markup(markup! { - /// {PrintDiagnostic(&error, true)} + /// {PrintDiagnostic::verbose(&error)} /// }) /// .expect("failed to emit diagnostic"); /// @@ -575,7 +575,7 @@ impl ParseDiagnostic { /// .with_file_source_code(source.to_string()); /// Formatter::new(&mut Termcolor(&mut write)) /// .write_markup(markup! { - /// {PrintDiagnostic(&error, true)} + /// {PrintDiagnostic::verbose(&error)} /// }) /// .expect("failed to emit diagnostic"); /// diff --git a/crates/rome_js_parser/src/test_utils.rs b/crates/rome_js_parser/src/test_utils.rs index 88fc43ab3e7..879c8641c9f 100644 --- a/crates/rome_js_parser/src/test_utils.rs +++ b/crates/rome_js_parser/src/test_utils.rs @@ -51,7 +51,7 @@ where .with_file_source_code(syntax.to_string()); Formatter::new(&mut Termcolor(&mut buffer)) .write_markup(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }) .unwrap(); } diff --git a/crates/rome_js_parser/src/tests.rs b/crates/rome_js_parser/src/tests.rs index f8d008e56a0..ef138e6e533 100644 --- a/crates/rome_js_parser/src/tests.rs +++ b/crates/rome_js_parser/src/tests.rs @@ -125,7 +125,7 @@ fn run_and_expect_errors(path: &str, _: &str, _: &str, _: &str) { .with_file_source_code(text.to_string()); Formatter::new(&mut Termcolor(&mut write)) .write_markup(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }) .expect("failed to emit diagnostic"); write!( @@ -378,7 +378,7 @@ fn diagnostics_print_correctly() { Formatter::new(&mut Termcolor(&mut write)) .write_markup(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }) .expect("failed to emit diagnostic"); diff --git a/crates/rome_js_semantic/src/tests/assertions.rs b/crates/rome_js_semantic/src/tests/assertions.rs index 845ba221621..27e6fcea1d6 100644 --- a/crates/rome_js_semantic/src/tests/assertions.rs +++ b/crates/rome_js_semantic/src/tests/assertions.rs @@ -114,7 +114,7 @@ pub fn assert(code: &str, test_name: &str) { .with_file_path(FileId::zero()) .with_file_source_code(code); console.log(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); } panic!("Compilation error"); @@ -754,7 +754,7 @@ fn error_assertion_not_attached_to_a_declaration( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); panic!("This assertion must be attached to a SemanticEvent::DeclarationFound."); } @@ -775,7 +775,7 @@ fn error_declaration_pointing_to_unknown_scope( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); } @@ -800,7 +800,7 @@ fn error_assertion_name_clash( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); panic!("Assertion label conflict"); @@ -823,7 +823,7 @@ fn error_scope_end_assertion_points_to_non_existing_scope_start_assertion( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); panic!("Scope start assertion not found."); } @@ -850,7 +850,7 @@ fn error_scope_end_assertion_points_to_the_wrong_scope_start( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); panic!("Wrong scope start"); } diff --git a/crates/rome_wasm/src/utils.rs b/crates/rome_wasm/src/utils.rs index 20b9dd9e183..d2397319e48 100644 --- a/crates/rome_wasm/src/utils.rs +++ b/crates/rome_wasm/src/utils.rs @@ -51,7 +51,13 @@ impl DiagnosticPrinter { let mut html = HTML(&mut self.buffer); Formatter::new(&mut html) - .write_markup(markup!({ PrintDiagnostic(&err, verbose) })) + .write_markup(markup!({ + if verbose { + PrintDiagnostic::verbose(&err) + } else { + PrintDiagnostic::simple(&err) + } + })) .map_err(into_error)?; Ok(()) diff --git a/xtask/bench/src/features/parser.rs b/xtask/bench/src/features/parser.rs index 91f6cfd22ff..c285b855aa7 100644 --- a/xtask/bench/src/features/parser.rs +++ b/xtask/bench/src/features/parser.rs @@ -106,7 +106,7 @@ impl Display for ParseMeasurement { .with_file_source_code(self.code.clone()); rome_diagnostics::console::fmt::Formatter::new(&mut Termcolor(&mut buffer)) .write_markup(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }) .unwrap(); } diff --git a/xtask/coverage/src/runner.rs b/xtask/coverage/src/runner.rs index a29fe109fe0..a4aa3bb8d77 100644 --- a/xtask/coverage/src/runner.rs +++ b/xtask/coverage/src/runner.rs @@ -150,7 +150,7 @@ impl TestCaseFiles { pub(crate) fn emit_errors(&self, errors: &[Error], buffer: &mut Buffer) { for error in errors { if let Err(err) = Formatter::new(&mut Termcolor(&mut *buffer)).write_markup(markup! { - {PrintDiagnostic(error, true)} + {PrintDiagnostic::verbose(error)} }) { eprintln!("Failed to print diagnostic: {}", err); } diff --git a/xtask/lintdoc/src/main.rs b/xtask/lintdoc/src/main.rs index 3a7285c7643..146e015c538 100644 --- a/xtask/lintdoc/src/main.rs +++ b/xtask/lintdoc/src/main.rs @@ -466,7 +466,7 @@ fn assert_lint( let mut write_diagnostic = |code: &str, diag: rome_diagnostics::Error| { let category = diag.category().map_or("", |code| code.name()); Formatter::new(&mut write).write_markup(markup! { - {PrintDiagnostic(&diag, true)} + {PrintDiagnostic::verbose(&diag)} })?; all_diagnostics.push(diag); @@ -479,7 +479,7 @@ fn assert_lint( console.print( rome_console::LogLevel::Error, markup! { - {PrintDiagnostic(diag, true)} + {PrintDiagnostic::verbose(diag)} }, ); } @@ -497,7 +497,7 @@ fn assert_lint( console.print( rome_console::LogLevel::Error, markup! { - {PrintDiagnostic(diag, true)} + {PrintDiagnostic::verbose(diag)} }, ); }