Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix: parsing logic in formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jul 4, 2023
1 parent 5db3d15 commit 8d7f73e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
45 changes: 37 additions & 8 deletions crates/rome_cli/src/execute/process_file/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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,
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
│ ^^^^^^^^^
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 │
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 │
```
Expand Down

0 comments on commit 8d7f73e

Please sign in to comment.