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

fix(rome_cli): correctly compute can_handle in CI mode #3500

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions crates/rome_cli/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,14 @@ impl<'ctx, 'app> TraversalOptions<'ctx, 'app> {
feature: FeatureName::Lint,
})
}

fn miss_handler_err(&self, err: RomeError, rome_path: &RomePath) {
self.push_diagnostic(
StdError::from(err)
.with_category(category!("files/missingHandler"))
.with_file_path(rome_path.file_id()),
);
}
}

impl<'ctx, 'app> TraversalContext for TraversalOptions<'ctx, 'app> {
Expand All @@ -543,23 +551,32 @@ impl<'ctx, 'app> TraversalContext for TraversalOptions<'ctx, 'app> {
}

fn can_handle(&self, rome_path: &RomePath) -> bool {
let result = match self.execution.traversal_mode() {
TraversalMode::Check { .. } => self.can_lint(rome_path),
TraversalMode::CI { .. } => self
.can_lint(rome_path)
.and_then(|_| self.can_format(rome_path)),
TraversalMode::Format { .. } => self.can_format(rome_path),
};
match result {
Ok(result) => result.reason.is_none(),
Err(err) => {
self.push_diagnostic(
StdError::from(err)
.with_category(category!("files/missingHandler"))
.with_file_path(rome_path.file_id()),
);
false
}
let can_lint = self.can_lint(rome_path);
let can_format = self.can_format(rome_path);

match self.execution.traversal_mode() {
TraversalMode::Check { .. } => can_lint
.map(|result| result.reason.is_none())
.unwrap_or_else(|err| {
self.miss_handler_err(err, rome_path);
false
}),
TraversalMode::CI { .. } => match (can_format, can_lint) {
// the result of the error is the same, rome can't handle the file
(Err(err), _) | (_, Err(err)) => {
self.miss_handler_err(err, rome_path);
false
}
(Ok(can_format), Ok(can_lint)) => {
can_lint.reason.is_none() || can_format.reason.is_none()
}
},
TraversalMode::Format { .. } => can_format
.map(|result| result.reason.is_none())
.unwrap_or_else(|err| {
self.miss_handler_err(err, rome_path);
false
}),
}
}

Expand Down
39 changes: 39 additions & 0 deletions crates/rome_cli/tests/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use rome_service::DynRef;
use std::ffi::OsString;
use std::path::Path;

const INCORRECT_CODE: &str = "let a = !b || !c";

#[test]
fn ok() {
let mut fs = MemoryFileSystem::default();
Expand Down Expand Up @@ -242,3 +244,40 @@ fn file_too_large() {
result,
));
}

#[test]
fn ci_runs_linter_not_formatter_issue_3495() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let file_path = Path::new("rome.json");
fs.insert(file_path.into(), CONFIG_DISABLED_FORMATTER.as_bytes());

let file_path = Path::new("file.js");
fs.insert(file_path.into(), INCORRECT_CODE.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![OsString::from("ci"), file_path.as_os_str().into()]),
);

assert!(result.is_err(), "run_cli returned {result:?}");

let mut file = fs
.open(file_path)
.expect("ci target file was removed by the CLI");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");

drop(file);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"ci_runs_linter_not_formatter_issue_3495",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is correct because if the formatter was enabled, it would have added ; at the end of the statement

source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `rome.json`

```json
{
"formatter": {
"enabled": false
}
}

```

## `file.js`

```js
let a = !b || !c
```

# Termination Message

```block
errors where emitted while running checks
```

# Emitted Messages

```block
file.js:1:9 lint/correctness/useSimplifiedLogicExpression FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Logical expression contains unnecessary complexity.

> 1 │ let a = !b || !c
│ ^^^^^^^^

i Suggested fix: Reduce the complexity of the logical expression.

- let·a·=·!b·||·!c
+ let·a·=·!(b·&&·c)


```


点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载