From 00e2a8504ecf594e511fe02adb73ff266dc24ac5 Mon Sep 17 00:00:00 2001 From: l3ops Date: Fri, 18 Nov 2022 18:02:49 +0100 Subject: [PATCH 1/4] expose the verbose flag to the CLI --- crates/rome_cli/src/commands/help.rs | 7 ++- crates/rome_cli/src/traversal.rs | 16 ++++-- crates/rome_cli/tests/commands/check.rs | 32 +++++++++++ crates/rome_cli/tests/commands/ci.rs | 32 +++++++++++ crates/rome_cli/tests/commands/format.rs | 29 ++++++++++ .../main_commands_check/print_verbose.snap | 56 +++++++++++++++++++ .../main_commands_ci/print_verbose.snap | 56 +++++++++++++++++++ .../main_commands_format/print_verbose.snap | 25 +++++++++ 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 | 20 +++---- 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 | 4 +- xtask/bench/src/features/parser.rs | 2 +- xtask/coverage/src/runner.rs | 2 +- xtask/lintdoc/src/main.rs | 6 +- 26 files changed, 283 insertions(+), 44 deletions(-) create mode 100644 crates/rome_cli/tests/snapshots/main_commands_check/print_verbose.snap create mode 100644 crates/rome_cli/tests/snapshots/main_commands_ci/print_verbose.snap create mode 100644 crates/rome_cli/tests/snapshots/main_commands_format/print_verbose.snap diff --git a/crates/rome_cli/src/commands/help.rs b/crates/rome_cli/src/commands/help.rs index 9d4ea3c706a..a4f015407d9 100644 --- a/crates/rome_cli/src/commands/help.rs +++ b/crates/rome_cli/src/commands/help.rs @@ -37,6 +37,7 @@ const CHECK: Markup = markup! { ""--apply"" Apply safe fixes ""--apply-suggested"" Apply safe and suggested fixes ""--max-diagnostics"" Cap the amount of diagnostics displayed (default: 20) + ""--verbose"" Print additional verbose advices on diagnostics " }; @@ -63,7 +64,8 @@ const CI: Markup = markup! { ""OPTIONS:"" ""--formatter-enabled"" Allow to enable or disable the formatter check. (default: true) ""--linter-enabled"" Allow to enable or disable the linter check. (default: true) - ""--max-diagnostics"" Cap the amount of diagnostics displayed (default: 50)" + ""--max-diagnostics"" Cap the amount of diagnostics displayed (default: 50) + ""--verbose"" Print additional verbose advices on diagnostics" {FORMAT_OPTIONS} }; @@ -78,7 +80,8 @@ const FORMAT: Markup = markup! { ""OPTIONS:"" ""--write"" Edit the files in place (beware!) instead of printing the diff to the console ""--skip-errors"" Skip over files containing syntax errors instead of emitting an error diagnostic. - ""--max-diagnostics"" Cap the amount of diagnostics displayed (default: 50)" + ""--max-diagnostics"" Cap the amount of diagnostics displayed (default: 50) + ""--verbose"" Print additional verbose advices on diagnostics" {FORMAT_OPTIONS} """--stdin-file-path "" A file name with its extension to pass when reading from standard in, e.g. echo 'let a;' | rome format --stdin-file-path file.js " diff --git a/crates/rome_cli/src/traversal.rs b/crates/rome_cli/src/traversal.rs index fb89d8b4151..a2b33381075 100644 --- a/crates/rome_cli/src/traversal.rs +++ b/crates/rome_cli/src/traversal.rs @@ -57,6 +57,8 @@ impl fmt::Display for CheckResult { pub(crate) fn traverse(execution: Execution, mut session: CliSession) -> Result<(), Termination> { init_thread_pool(); + let verbose = session.args.contains("--verbose"); + // Check that at least one input file / directory was specified in the command line let mut inputs = vec![]; @@ -116,6 +118,7 @@ pub(crate) fn traverse(execution: Execution, mut session: CliSession) -> Result< remaining_diagnostics: &remaining_diagnostics, errors: &mut errors, report: &mut report, + verbose, }); }) .expect("failed to spawn console thread"); @@ -262,6 +265,8 @@ struct ProcessMessagesOptions<'ctx> { /// Mutable handle to a [Report] instance the console thread should write /// stats into report: &'ctx mut Report, + /// Whether the console thread should print diagnostics in verbose mode + verbose: bool, } #[derive(Debug, Diagnostic)] @@ -328,6 +333,7 @@ fn process_messages(options: ProcessMessagesOptions) { remaining_diagnostics, errors, report, + verbose, } = options; let mut paths = HashMap::new(); @@ -412,7 +418,7 @@ fn process_messages(options: ProcessMessagesOptions) { if mode.should_report_to_terminal() { if should_print { console.error(markup! { - {PrintDiagnostic(&err)} + {PrintDiagnostic(&err, verbose)} }); } } else { @@ -454,7 +460,7 @@ fn process_messages(options: ProcessMessagesOptions) { let diag = diag.with_file_path(&name).with_file_source_code(&content); console.error(markup! { - {PrintDiagnostic(&diag)} + {PrintDiagnostic(&diag, verbose)} }); } } else { @@ -480,7 +486,7 @@ fn process_messages(options: ProcessMessagesOptions) { let diag = diag.with_file_path(&name).with_file_source_code(&content); console.error(markup! { - {PrintDiagnostic(&diag)} + {PrintDiagnostic(&diag, verbose)} }); } } else { @@ -530,7 +536,7 @@ fn process_messages(options: ProcessMessagesOptions) { }; console.error(markup! { - {PrintDiagnostic(&diag)} + {PrintDiagnostic(&diag, verbose)} }); } else { let diag = FormatDiffDiagnostic { @@ -542,7 +548,7 @@ fn process_messages(options: ProcessMessagesOptions) { }; console.error(markup! { - {PrintDiagnostic(&diag)} + {PrintDiagnostic(&diag, verbose)} }); } } diff --git a/crates/rome_cli/tests/commands/check.rs b/crates/rome_cli/tests/commands/check.rs index 709823f9942..65490b21a44 100644 --- a/crates/rome_cli/tests/commands/check.rs +++ b/crates/rome_cli/tests/commands/check.rs @@ -1086,3 +1086,35 @@ a == b;", result, )); } + +#[test] +fn print_verbose() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let file_path = Path::new("check.js"); + fs.insert(file_path.into(), LINT_ERROR.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + DynRef::Borrowed(&mut console), + Arguments::from_vec(vec![ + OsString::from("check"), + OsString::from("--verbose"), + file_path.as_os_str().into(), + ]), + ); + + match result { + Err(Termination::CheckError) => {} + _ => panic!("run_cli returned {result:?} for a failed CI check, expected an error"), + } + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "print_verbose", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/commands/ci.rs b/crates/rome_cli/tests/commands/ci.rs index 7a4e0fcd476..c8ff340d4a4 100644 --- a/crates/rome_cli/tests/commands/ci.rs +++ b/crates/rome_cli/tests/commands/ci.rs @@ -633,3 +633,35 @@ fn max_diagnostics() { assert_eq!(diagnostic_count, 10); } + +#[test] +fn print_verbose() { + let mut fs = MemoryFileSystem::default(); + + let file_path = Path::new("ci.js"); + fs.insert(file_path.into(), LINT_ERROR.as_bytes()); + + let mut console = BufferConsole::default(); + let result = run_cli( + DynRef::Borrowed(&mut fs), + DynRef::Borrowed(&mut console), + Arguments::from_vec(vec![ + OsString::from("ci"), + OsString::from("--verbose"), + file_path.as_os_str().into(), + ]), + ); + + match &result { + Err(Termination::CheckError) => {} + _ => panic!("run_cli returned {result:?} for a failed CI check, expected an error"), + } + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "print_verbose", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/commands/format.rs b/crates/rome_cli/tests/commands/format.rs index ec88da15417..6de7a461787 100644 --- a/crates/rome_cli/tests/commands/format.rs +++ b/crates/rome_cli/tests/commands/format.rs @@ -1414,3 +1414,32 @@ fn no_supported_file_found() { result, )); } + +#[test] +fn print_verbose() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let file_path = Path::new("format.js"); + fs.insert(file_path.into(), UNFORMATTED.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + DynRef::Borrowed(&mut console), + Arguments::from_vec(vec![ + OsString::from("format"), + OsString::from("--verbose"), + file_path.as_os_str().into(), + ]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "print_verbose", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/print_verbose.snap b/crates/rome_cli/tests/snapshots/main_commands_check/print_verbose.snap new file mode 100644 index 00000000000..038f2f42a74 --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_commands_check/print_verbose.snap @@ -0,0 +1,56 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `check.js` + +```js +for(;true;); + +``` + +# Termination Message + +```block +some errors were emitted while running checks +``` + +# Emitted Messages + +```block +check.js:1:1 lint/correctness/useWhile FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Use while loops instead of for loops. + + > 1 │ for(;true;); + │ ^^^^^^^^^^^ + 2 │ + + i Suggested fix: Use a while loop + + 1 │ - for(;true;); + 1 │ + while·(true); + 2 2 │ + + +``` + +```block +check.js:1:1 lint/style/useBlockStatements FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Block statements are preferred in this position. + + > 1 │ for(;true;); + │ ^^^^^^^^^^^^ + 2 │ + + i Suggested fix: Wrap the statement with a `JsBlockStatement` + + 1 │ - for(;true;); + 1 │ + for(;true;)·{} + 2 2 │ + + +``` + + diff --git a/crates/rome_cli/tests/snapshots/main_commands_ci/print_verbose.snap b/crates/rome_cli/tests/snapshots/main_commands_ci/print_verbose.snap new file mode 100644 index 00000000000..2c559500041 --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_commands_ci/print_verbose.snap @@ -0,0 +1,56 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `ci.js` + +```js +for(;true;); + +``` + +# Termination Message + +```block +some errors were emitted while running checks +``` + +# Emitted Messages + +```block +ci.js:1:1 lint/correctness/useWhile FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Use while loops instead of for loops. + + > 1 │ for(;true;); + │ ^^^^^^^^^^^ + 2 │ + + i Suggested fix: Use a while loop + + 1 │ - for(;true;); + 1 │ + while·(true); + 2 2 │ + + +``` + +```block +ci.js:1:1 lint/style/useBlockStatements FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Block statements are preferred in this position. + + > 1 │ for(;true;); + │ ^^^^^^^^^^^^ + 2 │ + + i Suggested fix: Wrap the statement with a `JsBlockStatement` + + 1 │ - for(;true;); + 1 │ + for(;true;)·{} + 2 2 │ + + +``` + + diff --git a/crates/rome_cli/tests/snapshots/main_commands_format/print_verbose.snap b/crates/rome_cli/tests/snapshots/main_commands_format/print_verbose.snap new file mode 100644 index 00000000000..3136b73e098 --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_commands_format/print_verbose.snap @@ -0,0 +1,25 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `format.js` + +```js + statement( ) +``` + +# Emitted Messages + +```block +format.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + i Formatter would have printed the following content: + + 1 │ - ··statement(··)·· + 1 │ + statement(); + 2 │ + + + +``` + + diff --git a/crates/rome_diagnostics/examples/cli.rs b/crates/rome_diagnostics/examples/cli.rs index 18014358c88..5a8776484ee 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) })); + EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) })); } diff --git a/crates/rome_diagnostics/examples/fs.rs b/crates/rome_diagnostics/examples/fs.rs index 5c8cf4d0209..2f68e894fd1 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) })); + EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) })); } diff --git a/crates/rome_diagnostics/examples/lint.rs b/crates/rome_diagnostics/examples/lint.rs index 6de97cc8a7e..90fe901e05d 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) })); + EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) })); } diff --git a/crates/rome_diagnostics/examples/serde.rs b/crates/rome_diagnostics/examples/serde.rs index 428bc17226c..6f5649ef1da 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) })); + EnvConsole::default().error(markup!({ PrintDiagnostic(&err, true) })); }; } diff --git a/crates/rome_diagnostics/src/display.rs b/crates/rome_diagnostics/src/display.rs index a6e6c030e52..59ae4434f54 100644 --- a/crates/rome_diagnostics/src/display.rs +++ b/crates/rome_diagnostics/src/display.rs @@ -33,7 +33,7 @@ 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 struct PrintDiagnostic<'fmt, D: ?Sized>(pub &'fmt D, pub bool); impl<'fmt, D: AsDiagnostic + ?Sized> fmt::Display for PrintDiagnostic<'fmt, D> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> io::Result<()> { @@ -49,7 +49,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, true) + print_advices(&mut visitor, diagnostic, self.1) } } @@ -729,7 +729,7 @@ mod tests { fn test_header() { let diag = TestDiagnostic::::with_location(); - let diag = markup!({ PrintDiagnostic(&diag) }).to_owned(); + let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); let expected = markup!{ "path:1:1 internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -753,7 +753,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag) }).to_owned(); + let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -783,7 +783,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag) }).to_owned(); + let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -808,7 +808,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag) }).to_owned(); + let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -833,7 +833,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag) }).to_owned(); + let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -858,7 +858,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag) }).to_owned(); + let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -884,7 +884,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag) }).to_owned(); + let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned(); let expected = markup!{ "internalError/io "" FIXABLE "" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" @@ -908,7 +908,7 @@ mod tests { ..TestDiagnostic::empty() }; - let diag = markup!({ PrintDiagnostic(&diag) }).to_owned(); + let diag = markup!({ PrintDiagnostic(&diag, true) }).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 e4dd5954bb7..9d1774914d9 100644 --- a/crates/rome_js_analyze/src/lib.rs +++ b/crates/rome_js_analyze/src/lib.rs @@ -178,7 +178,7 @@ mod tests { error_ranges.push(diag.location().span.unwrap()); let error = diag.with_file_path("ahahah").with_file_source_code(SOURCE); let text = markup_to_string(markup! { - {PrintDiagnostic(&error)} + {PrintDiagnostic(&error, true)} }); eprintln!("{text}"); } diff --git a/crates/rome_js_analyze/tests/spec_tests.rs b/crates/rome_js_analyze/tests/spec_tests.rs index e89208c4f1a..bbb8067881f 100644 --- a/crates/rome_js_analyze/tests/spec_tests.rs +++ b/crates/rome_js_analyze/tests/spec_tests.rs @@ -174,7 +174,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)} + {PrintDiagnostic(&error, true)} }); text diff --git a/crates/rome_js_formatter/src/check_reformat.rs b/crates/rome_js_formatter/src/check_reformat.rs index 2533f0a82c3..501d59adc74 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)} + {PrintDiagnostic(&error, true)} }) .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 0098a080500..fad302299bd 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)} + {PrintDiagnostic(&error, true)} }) .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 a4f2698bb7a..a19c7a8b1fd 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)} + {PrintDiagnostic(&error, true)} }) .expect("failed to emit diagnostic"); } diff --git a/crates/rome_js_parser/src/lib.rs b/crates/rome_js_parser/src/lib.rs index c07da067118..54b28ef6efb 100644 --- a/crates/rome_js_parser/src/lib.rs +++ b/crates/rome_js_parser/src/lib.rs @@ -516,7 +516,7 @@ impl ParseDiagnostic { /// .with_file_source_code(source.to_string()); /// Formatter::new(&mut Termcolor(&mut write)) /// .write_markup(markup! { - /// {PrintDiagnostic(&error)} + /// {PrintDiagnostic(&error, true)} /// }) /// .expect("failed to emit diagnostic"); /// @@ -574,7 +574,7 @@ impl ParseDiagnostic { /// .with_file_source_code(source.to_string()); /// Formatter::new(&mut Termcolor(&mut write)) /// .write_markup(markup! { - /// {PrintDiagnostic(&error)} + /// {PrintDiagnostic(&error, true)} /// }) /// .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 5cf3f2993f3..88fc43ab3e7 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)} + {PrintDiagnostic(&error, true)} }) .unwrap(); } diff --git a/crates/rome_js_parser/src/tests.rs b/crates/rome_js_parser/src/tests.rs index 40c47f0ae18..f8d008e56a0 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)} + {PrintDiagnostic(&error, true)} }) .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)} + {PrintDiagnostic(&error, true)} }) .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 110f29a7be2..aea9b340f87 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)} + {PrintDiagnostic(&error, true)} }); } panic!("Compilation error"); @@ -752,7 +752,7 @@ fn error_assertion_not_attached_to_a_declaration( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error)} + {PrintDiagnostic(&error, true)} }); panic!("This assertion must be attached to a SemanticEvent::DeclarationFound."); } @@ -773,7 +773,7 @@ fn error_declaration_pointing_to_unknown_scope( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error)} + {PrintDiagnostic(&error, true)} }); } @@ -798,7 +798,7 @@ fn error_assertion_name_clash( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error)} + {PrintDiagnostic(&error, true)} }); panic!("Assertion label conflict"); @@ -821,7 +821,7 @@ fn error_scope_end_assertion_points_to_non_existing_scope_start_assertion( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error)} + {PrintDiagnostic(&error, true)} }); panic!("Scope start assertion not found."); } @@ -848,7 +848,7 @@ fn error_scope_end_assertion_points_to_the_wrong_scope_start( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error)} + {PrintDiagnostic(&error, true)} }); panic!("Wrong scope start"); } diff --git a/crates/rome_wasm/src/utils.rs b/crates/rome_wasm/src/utils.rs index 99aaabd2d6d..20b9dd9e183 100644 --- a/crates/rome_wasm/src/utils.rs +++ b/crates/rome_wasm/src/utils.rs @@ -43,7 +43,7 @@ impl DiagnosticPrinter { } } - pub fn print(&mut self, diagnostic: IDiagnostic) -> Result<(), Error> { + pub fn print(&mut self, diagnostic: IDiagnostic, verbose: bool) -> Result<(), Error> { let diag: Diagnostic = diagnostic.into_serde().map_err(into_error)?; let err = diag .with_file_path(&self.file_name) @@ -51,7 +51,7 @@ impl DiagnosticPrinter { let mut html = HTML(&mut self.buffer); Formatter::new(&mut html) - .write_markup(markup!({ PrintDiagnostic(&err) })) + .write_markup(markup!({ PrintDiagnostic(&err, verbose) })) .map_err(into_error)?; Ok(()) diff --git a/xtask/bench/src/features/parser.rs b/xtask/bench/src/features/parser.rs index aefc4d6c354..91f6cfd22ff 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)} + {PrintDiagnostic(&error, true)} }) .unwrap(); } diff --git a/xtask/coverage/src/runner.rs b/xtask/coverage/src/runner.rs index 019db627b1a..a29fe109fe0 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)} + {PrintDiagnostic(error, true)} }) { eprintln!("Failed to print diagnostic: {}", err); } diff --git a/xtask/lintdoc/src/main.rs b/xtask/lintdoc/src/main.rs index 0153feffe6f..b912160335c 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)} + {PrintDiagnostic(&diag, true)} })?; all_diagnostics.push(diag); @@ -479,7 +479,7 @@ fn assert_lint( console.print( rome_console::LogLevel::Error, markup! { - {PrintDiagnostic(diag)} + {PrintDiagnostic(diag, true)} }, ); } @@ -497,7 +497,7 @@ fn assert_lint( console.print( rome_console::LogLevel::Error, markup! { - {PrintDiagnostic(diag)} + {PrintDiagnostic(diag, true)} }, ); } From f9d3120d75df6d6665f46618eaa0023e33f84d1f Mon Sep 17 00:00:00 2001 From: l3ops Date: Mon, 21 Nov 2022 17:23:39 +0100 Subject: [PATCH 2/4] 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 a2b33381075..fd9bbe20611 100644 --- a/crates/rome_cli/src/traversal.rs +++ b/crates/rome_cli/src/traversal.rs @@ -418,7 +418,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 { @@ -460,7 +460,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 { @@ -486,7 +486,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 { @@ -536,7 +536,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 { @@ -548,7 +548,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 2f68e894fd1..0115abaf644 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 90fe901e05d..b1305fb5a95 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 59ae4434f54..8248eebeebd 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) } } @@ -729,7 +748,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" @@ -753,7 +772,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" @@ -783,7 +802,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" @@ -808,7 +827,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" @@ -833,7 +852,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" @@ -858,7 +877,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" @@ -884,7 +903,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" @@ -908,7 +927,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 9d1774914d9..0936deb7155 100644 --- a/crates/rome_js_analyze/src/lib.rs +++ b/crates/rome_js_analyze/src/lib.rs @@ -178,7 +178,7 @@ mod tests { error_ranges.push(diag.location().span.unwrap()); 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 bbb8067881f..627fdcbe1ab 100644 --- a/crates/rome_js_analyze/tests/spec_tests.rs +++ b/crates/rome_js_analyze/tests/spec_tests.rs @@ -174,7 +174,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 54b28ef6efb..d93e22cb362 100644 --- a/crates/rome_js_parser/src/lib.rs +++ b/crates/rome_js_parser/src/lib.rs @@ -516,7 +516,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"); /// @@ -574,7 +574,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 aea9b340f87..1366e8470d9 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"); @@ -752,7 +752,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."); } @@ -773,7 +773,7 @@ fn error_declaration_pointing_to_unknown_scope( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); } @@ -798,7 +798,7 @@ fn error_assertion_name_clash( let mut console = EnvConsole::default(); console.log(markup! { - {PrintDiagnostic(&error, true)} + {PrintDiagnostic::verbose(&error)} }); panic!("Assertion label conflict"); @@ -821,7 +821,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."); } @@ -848,7 +848,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 b912160335c..c22ab68a42b 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)} }, ); } From fabbe8228e5ec3f237d8e3024ef59604658400d7 Mon Sep 17 00:00:00 2001 From: l3ops Date: Wed, 23 Nov 2022 15:01:35 +0100 Subject: [PATCH 3/4] fis the method signatures on DiagnosticPrinter --- crates/rome_wasm/src/utils.rs | 22 ++++++++++++------- .../tests/wasm/diagnosticPrinter.test.mjs | 4 ++-- website/src/playground/workers/romeWorker.ts | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/crates/rome_wasm/src/utils.rs b/crates/rome_wasm/src/utils.rs index d2397319e48..aec9c9e3720 100644 --- a/crates/rome_wasm/src/utils.rs +++ b/crates/rome_wasm/src/utils.rs @@ -43,7 +43,19 @@ impl DiagnosticPrinter { } } - pub fn print(&mut self, diagnostic: IDiagnostic, verbose: bool) -> Result<(), Error> { + pub fn print_simple(&mut self, diagnostic: IDiagnostic) -> Result<(), Error> { + self.print(diagnostic, |err| PrintDiagnostic::simple(err)) + } + + pub fn print_verbose(&mut self, diagnostic: IDiagnostic) -> Result<(), Error> { + self.print(diagnostic, |err| PrintDiagnostic::verbose(err)) + } + + fn print( + &mut self, + diagnostic: IDiagnostic, + printer: fn(&rome_diagnostics::Error) -> PrintDiagnostic, + ) -> Result<(), Error> { let diag: Diagnostic = diagnostic.into_serde().map_err(into_error)?; let err = diag .with_file_path(&self.file_name) @@ -51,13 +63,7 @@ impl DiagnosticPrinter { let mut html = HTML(&mut self.buffer); Formatter::new(&mut html) - .write_markup(markup!({ - if verbose { - PrintDiagnostic::verbose(&err) - } else { - PrintDiagnostic::simple(&err) - } - })) + .write_markup(markup!({ printer(&err) })) .map_err(into_error)?; Ok(()) diff --git a/npm/js-api/tests/wasm/diagnosticPrinter.test.mjs b/npm/js-api/tests/wasm/diagnosticPrinter.test.mjs index ab324853253..618a0330fdf 100644 --- a/npm/js-api/tests/wasm/diagnosticPrinter.test.mjs +++ b/npm/js-api/tests/wasm/diagnosticPrinter.test.mjs @@ -10,7 +10,7 @@ if(expr()) { }`; const printer = new DiagnosticPrinter("file.js", SOURCE_CODE); - printer.print({ + printer.print_verbose({ advices: { advices: [], }, @@ -42,7 +42,7 @@ if(expr()) { }, }); - printer.print({ + printer.print_verbose({ advices: { advices: [], }, diff --git a/website/src/playground/workers/romeWorker.ts b/website/src/playground/workers/romeWorker.ts index 0f05dbc0c61..0cf1793f535 100644 --- a/website/src/playground/workers/romeWorker.ts +++ b/website/src/playground/workers/romeWorker.ts @@ -193,7 +193,7 @@ self.addEventListener("message", async (e) => { const printer = new DiagnosticPrinter(path.path, code); for (const diag of diagnosticsResult.diagnostics) { - printer.print(diag); + printer.print_verbose(diag); } const printed = workspace.formatFile({ From 12f40b3d2a31634eb3ce9cd0768901cf282720fc Mon Sep 17 00:00:00 2001 From: l3ops Date: Wed, 23 Nov 2022 15:08:09 +0100 Subject: [PATCH 4/4] fix rebase issues --- crates/rome_json_parser/src/lib.rs | 4 ++-- crates/rome_json_parser/tests/spec_test.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/rome_json_parser/src/lib.rs b/crates/rome_json_parser/src/lib.rs index eaae2b5903a..164cf9ab513 100644 --- a/crates/rome_json_parser/src/lib.rs +++ b/crates/rome_json_parser/src/lib.rs @@ -123,7 +123,7 @@ impl ParseDiagnostic { /// .with_file_source_code(source.to_string()); /// Formatter::new(&mut Termcolor(&mut write)) /// .write_markup(markup! { - /// {PrintDiagnostic(&error)} + /// {PrintDiagnostic::verbose(&error)} /// }) /// .expect("failed to emit diagnostic"); /// @@ -181,7 +181,7 @@ impl ParseDiagnostic { /// .with_file_source_code(source.to_string()); /// Formatter::new(&mut Termcolor(&mut write)) /// .write_markup(markup! { - /// {PrintDiagnostic(&error)} + /// {PrintDiagnostic::verbose(&error)} /// }) /// .expect("failed to emit diagnostic"); /// diff --git a/crates/rome_json_parser/tests/spec_test.rs b/crates/rome_json_parser/tests/spec_test.rs index b065077e99a..3f858c0e3cf 100644 --- a/crates/rome_json_parser/tests/spec_test.rs +++ b/crates/rome_json_parser/tests/spec_test.rs @@ -57,7 +57,7 @@ pub fn run(test_case: &str, _snapshot_name: &str, test_directory: &str, outcome: formatter .write_markup(markup! { - {PrintDiagnostic(&error)} + {PrintDiagnostic::verbose(&error)} }) .expect("failed to emit diagnostic"); }