Skip to content

Commit

Permalink
Show line/column numbers for syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Apr 17, 2023
1 parent 52562da commit c477216
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
8 changes: 2 additions & 6 deletions crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::checkers::physical_lines::check_physical_lines;
use crate::checkers::tokens::check_tokens;
use crate::directives::Directives;
use crate::doc_lines::{doc_lines_from_ast, doc_lines_from_tokens};
use crate::logging::DisplayParseError;
use crate::message::Message;
use crate::noqa::add_noqa;
use crate::registry::{AsRule, Rule};
Expand Down Expand Up @@ -296,12 +297,7 @@ pub fn add_noqa_to_path(path: &Path, package: Option<&Path>, settings: &Settings

// Log any parse errors.
if let Some(err) = error {
error!(
"{}{}{} {err:?}",
"Failed to parse ".bold(),
fs::relativize_path(path).bold(),
":".bold()
);
error!("{}", DisplayParseError::new(err, locator.to_source_code()));
}

// Add any missing `# noqa` pragmas.
Expand Down
33 changes: 33 additions & 0 deletions crates/ruff/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::fmt::{Display, Formatter};
use std::path::Path;
use std::sync::Mutex;

use crate::fs;
use anyhow::Result;
use colored::Colorize;
use fern;
use log::Level;
use once_cell::sync::Lazy;
use ruff_python_ast::source_code::SourceCode;
use rustpython_parser::ParseError;

pub(crate) static WARNINGS: Lazy<Mutex<Vec<&'static str>>> = Lazy::new(Mutex::default);

Expand Down Expand Up @@ -127,6 +132,34 @@ pub fn set_up_logging(level: &LogLevel) -> Result<()> {
Ok(())
}

pub struct DisplayParseError<'a> {
error: ParseError,
source_code: SourceCode<'a, 'a>,
}

impl<'a> DisplayParseError<'a> {
pub fn new(error: ParseError, source_code: SourceCode<'a, 'a>) -> Self {
Self { error, source_code }
}
}

impl Display for DisplayParseError<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let source_location = self.source_code.source_location(self.error.location);

write!(
f,
"{header} {path}{colon}{row}{colon}{column}{colon} {inner}",
header = "Failed to parse ".bold(),
path = fs::relativize_path(Path::new(&self.error.source_path)).bold(),
row = source_location.row,
column = source_location.column,
colon = ":".cyan(),
inner = &self.error.error
)
}
}

#[cfg(test)]
mod tests {
use crate::logging::LogLevel;
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ use similar::TextDiff;
use ruff::fs;
use ruff::jupyter::{is_jupyter_notebook, JupyterIndex, JupyterNotebook};
use ruff::linter::{lint_fix, lint_only, FixTable, FixerResult, LinterResult};
use ruff::logging::DisplayParseError;
use ruff::message::Message;
use ruff::settings::{flags, AllSettings, Settings};
use ruff_python_ast::imports::ImportMap;
use ruff_python_ast::source_code::SourceFileBuilder;
use ruff_python_ast::source_code::{LineIndex, SourceCode, SourceFileBuilder};

use crate::cache;

Expand Down Expand Up @@ -200,13 +201,12 @@ pub fn lint_path(
let imports = imports.unwrap_or_default();

if let Some(err) = parse_error {
// FIXME micha manually print parse errors to get line and column numbers
// Notify the user of any parse errors.
error!(
"{}{}{} {err}",
"Failed to parse ".bold(),
fs::relativize_path(path).bold(),
":".bold()
"{}",
DisplayParseError::new(
err,
SourceCode::new(&contents, &LineIndex::from_source_text(&contents))
)
);

// Purge the cache.
Expand Down

0 comments on commit c477216

Please sign in to comment.