From 77139d73794eed7d95ab23cffe1d7baf72f5818c Mon Sep 17 00:00:00 2001 From: ematipico Date: Mon, 5 Dec 2022 09:01:11 +0000 Subject: [PATCH] chore: code suggestions --- crates/rome_service/src/diagnostics.rs | 21 +++++++++++++++---- crates/rome_service/src/file_handlers/mod.rs | 9 ++------ .../src/snapshots/cant_read_directory.snap | 2 +- .../src/snapshots/formatter_syntax_error.snap | 10 +++++++++ 4 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 crates/rome_service/src/snapshots/formatter_syntax_error.snap diff --git a/crates/rome_service/src/diagnostics.rs b/crates/rome_service/src/diagnostics.rs index b1c78b753ca..08b330f8ab4 100644 --- a/crates/rome_service/src/diagnostics.rs +++ b/crates/rome_service/src/diagnostics.rs @@ -167,7 +167,12 @@ impl Diagnostic for RomeError { ) } } - RomeError::FormatError(err) => err.message(f), + RomeError::FormatError(err) => { + f.write_markup(markup! { + "the formatter encountered an error while formatting the file: " + })?; + err.message(f) + }, RomeError::FormatWithErrorsDisabled => f.write_markup( markup!{ "The file could not be formatted since it has syntax errors and ""formatWithErrors"" is disabled" } ), @@ -195,10 +200,9 @@ impl Diagnostic for RomeError { write!(f, "The file {} was ignored", path) }, RomeError::FileTooLarge { path, size, limit } => { - let path = format!("{}", path.display()); f.write_markup( markup!{ - "Size of "{{path}}" is "{{Bytes(*size)}}" which exceeds configured maximum of "{{Bytes(*limit)}}" for this project. The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't." + "Size of "{{path.display().to_string()}}" is "{{Bytes(*size)}}" which exceeds configured maximum of "{{Bytes(*limit)}}" for this project. The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't." } ) }, @@ -433,6 +437,7 @@ mod test { use crate::file_handlers::Language; use crate::{RomeError, TransportError}; use rome_diagnostics::{print_diagnostic_to_string, DiagnosticExt, Error, FileId}; + use rome_formatter::FormatError; use rome_fs::RomePath; use std::path::PathBuf; @@ -464,7 +469,7 @@ mod test { fn cant_read_directory() { snap_diagnostic( "cant_read_directory", - RomeError::CantReadDirectory("example/".to_string()).with_file_path("example.js"), + RomeError::CantReadDirectory("example/".to_string()).with_file_path("example/"), ) } @@ -538,4 +543,12 @@ mod test { .into(), ) } + + #[test] + fn formatter_syntax_error() { + snap_diagnostic( + "formatter_syntax_error", + RomeError::FormatError(FormatError::SyntaxError).with_file_path("example.js"), + ) + } } diff --git a/crates/rome_service/src/file_handlers/mod.rs b/crates/rome_service/src/file_handlers/mod.rs index 37aeeb055b9..193223742f2 100644 --- a/crates/rome_service/src/file_handlers/mod.rs +++ b/crates/rome_service/src/file_handlers/mod.rs @@ -133,13 +133,8 @@ impl std::fmt::Display for Mime { } impl rome_console::fmt::Display for Mime { - fn fmt(&self, f: &mut rome_console::fmt::Formatter<'_>) -> std::io::Result<()> { - match self { - Mime::Css => f.write_markup(markup! { "text/css"}), - Mime::Json => f.write_markup(markup! { "application/json"}), - Mime::Javascript => f.write_markup(markup! { "application/javascript"}), - Mime::Text => f.write_markup(markup! { "text/plain"}), - } + fn fmt(&self, f: &mut Formatter<'_>) -> std::io::Result<()> { + write!(f, "{self}") } } diff --git a/crates/rome_service/src/snapshots/cant_read_directory.snap b/crates/rome_service/src/snapshots/cant_read_directory.snap index 26b1589a1f7..667a2ed3c47 100644 --- a/crates/rome_service/src/snapshots/cant_read_directory.snap +++ b/crates/rome_service/src/snapshots/cant_read_directory.snap @@ -2,7 +2,7 @@ source: crates/rome_service/src/diagnostics.rs expression: content --- -example.js internalError/fs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +example/ internalError/fs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ × Rome couldn't read the following directory, maybe for permissions reasons or it doesn't exists: example/ diff --git a/crates/rome_service/src/snapshots/formatter_syntax_error.snap b/crates/rome_service/src/snapshots/formatter_syntax_error.snap new file mode 100644 index 00000000000..52f5d92148a --- /dev/null +++ b/crates/rome_service/src/snapshots/formatter_syntax_error.snap @@ -0,0 +1,10 @@ +--- +source: crates/rome_service/src/diagnostics.rs +expression: content +--- +example.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × the formatter encountered an error while formatting the file: Syntax error. + + +