From 8d7f73e99a9684546d695f934edf5a39a11e362a Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 4 Jul 2023 10:58:52 +0100 Subject: [PATCH] fix: parsing logic in formatter --- .../src/execute/process_file/format.rs | 45 +++++++++++++++---- .../apply_bogus_argument.snap | 7 ++- .../main_commands_check/parse_error.snap | 14 +++++- .../main_commands_ci/ci_parse_error.snap | 14 +++++- 4 files changed, 66 insertions(+), 14 deletions(-) diff --git a/crates/rome_cli/src/execute/process_file/format.rs b/crates/rome_cli/src/execute/process_file/format.rs index fe44b37c307..45ff699ab4b 100644 --- a/crates/rome_cli/src/execute/process_file/format.rs +++ b/crates/rome_cli/src/execute/process_file/format.rs @@ -5,8 +5,10 @@ use crate::execute::process_file::{ }; use crate::execute::TraversalMode; use crate::FormatterReportFileDetail; -use rome_diagnostics::{category, DiagnosticExt}; +use rome_diagnostics::{category, DiagnosticExt, Error}; +use rome_service::workspace::RuleCategories; use std::path::Path; +use std::sync::atomic::Ordering; pub(crate) fn format<'ctx>(ctx: &'ctx SharedTraversalOptions<'ctx, '_>, path: &Path) -> FileResult { let mut workspace_file = WorkspaceFile::new(ctx, path)?; @@ -17,15 +19,14 @@ pub(crate) fn format_with_guard<'ctx>( ctx: &'ctx SharedTraversalOptions<'ctx, '_>, workspace_file: &mut WorkspaceFile, ) -> FileResult { - let printed = workspace_file + let max_diagnostics = ctx.remaining_diagnostics.load(Ordering::Relaxed); + let diagnostics_result = workspace_file .guard() - .format_file() + .pull_diagnostics(RuleCategories::SYNTAX, max_diagnostics.into()) .with_file_path_and_code( workspace_file.path.display().to_string(), category!("format"), )?; - - let output = printed.into_code(); let (should_write, ignore_errors) = match ctx.execution.traversal_mode { TraversalMode::Format { write, @@ -39,10 +40,38 @@ pub(crate) fn format_with_guard<'ctx>( ), }; + if diagnostics_result.errors > 0 { + return Err(if ignore_errors { + Message::from( + SkippedDiagnostic.with_file_path(workspace_file.path.display().to_string()), + ) + } else { + Message::Diagnostics { + name: workspace_file.path.display().to_string(), + content: workspace_file.input().to_string(), + diagnostics: diagnostics_result + .diagnostics + .into_iter() + .map(Error::from) + .collect(), + skipped_diagnostics: diagnostics_result.skipped_diagnostics, + } + }); + } + + let printed = workspace_file + .guard() + .format_file() + .with_file_path_and_code( + workspace_file.path.display().to_string(), + category!("format"), + )?; + + let output = printed.into_code(); + + // NOTE: ignoring the if ignore_errors { - return Err(Message::from( - SkippedDiagnostic.with_file_path(workspace_file.path.display().to_string()), - )); + return Ok(FileStatus::Ignored); } if output != workspace_file.input() { diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/apply_bogus_argument.snap b/crates/rome_cli/tests/snapshots/main_commands_check/apply_bogus_argument.snap index 7204cf791bb..bc6466d35f2 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_check/apply_bogus_argument.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_check/apply_bogus_argument.snap @@ -33,9 +33,12 @@ fix.js:1:22 parse ━━━━━━━━━━━━━━━━━━━━ ``` ```block -fix.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +fix.js:1:22 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - × Format with errors is disabled. + × Illegal use of `arguments` as an identifier in strict mode + + > 1 │ function _13_1_3_fun(arguments) { } + │ ^^^^^^^^^ ``` diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/parse_error.snap b/crates/rome_cli/tests/snapshots/main_commands_check/parse_error.snap index bfe46b14454..a0025ac7786 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_check/parse_error.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_check/parse_error.snap @@ -41,9 +41,19 @@ check.js:2:1 parse ━━━━━━━━━━━━━━━━━━━━ ``` ```block -check.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +check.js:2:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - × Format with errors is disabled. + × expected `(` but instead the file ends + + 1 │ if + > 2 │ + │ + + i the file ends here + + 1 │ if + > 2 │ + │ ``` diff --git a/crates/rome_cli/tests/snapshots/main_commands_ci/ci_parse_error.snap b/crates/rome_cli/tests/snapshots/main_commands_ci/ci_parse_error.snap index 21ed20f3c26..6e2246df6f4 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_ci/ci_parse_error.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_ci/ci_parse_error.snap @@ -41,9 +41,19 @@ ci.js:2:1 parse ━━━━━━━━━━━━━━━━━━━━━ ``` ```block -ci.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +ci.js:2:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - × Format with errors is disabled. + × expected `(` but instead the file ends + + 1 │ if + > 2 │ + │ + + i the file ends here + + 1 │ if + > 2 │ + │ ```