Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DisplayParseError for stdin parser errors #9409

Merged
merged 1 commit into from
Jan 6, 2024
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
6 changes: 3 additions & 3 deletions crates/ruff_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,10 @@ pub(crate) fn lint_stdin(

let imports = imports.unwrap_or_default();

if let Some(err) = parse_error {
if let Some(error) = parse_error {
error!(
"Failed to parse {}: {err}",
path.map_or_else(|| "-".into(), fs::relativize_path).bold()
"{}",
DisplayParseError::from_source_kind(error, path.map(Path::to_path_buf), &transformed)
);
}

Expand Down
17 changes: 17 additions & 0 deletions crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,22 @@ fn stdin_format_jupyter() {
"###);
}

#[test]
fn stdin_parse_error() {
let mut cmd = RuffCheck::default().build();
assert_cmd_snapshot!(cmd
.pass_stdin("from foo import =\n"), @r###"
success: false
exit_code: 1
----- stdout -----
-:1:17: E999 SyntaxError: Unexpected token '='
Found 1 error.

----- stderr -----
error: Failed to parse at 1:17: Unexpected token '='
"###);
}

#[test]
fn show_source() {
let mut cmd = RuffCheck::default().args(["--show-source"]).build();
Expand All @@ -750,6 +766,7 @@ fn show_source() {
fn explain_status_codes_f401() {
assert_cmd_snapshot!(ruff_cmd().args(["--explain", "F401"]));
}

#[test]
fn explain_status_codes_ruf404() {
assert_cmd_snapshot!(ruff_cmd().args(["--explain", "RUF404"]), @r###"
Expand Down
7 changes: 1 addition & 6 deletions crates/ruff_linter/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,7 @@ impl Display for DisplayParseError {
colon = ":".cyan(),
)?;
} else {
write!(
f,
"{header}{colon}",
header = "Failed to parse".bold(),
colon = ":".cyan(),
)?;
write!(f, "{header}", header = "Failed to parse at ".bold())?;
}
match &self.location {
ErrorLocation::File(location) => {
Expand Down
Loading