Skip to content

Commit

Permalink
Improve error reporting on full moon errors (#921)
Browse files Browse the repository at this point in the history
* Improve error reporting on full moon errors

* Add space before error

* Improve printing for verification AST error

* Remove newline
  • Loading branch information
JohnnyMorganz authored Nov 17, 2024
1 parent de63b66 commit 5972683
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,40 @@ pub enum OutputVerification {
None,
}

fn print_full_moon_error(error: &full_moon::Error) -> String {
match error {
full_moon::Error::AstError(ast_error) => format!(
"unexpected token `{}` ({}:{} to {}:{}), {}",
ast_error.token(),
ast_error.range().0.line(),
ast_error.range().0.character(),
ast_error.range().1.line(),
ast_error.range().1.character(),
ast_error.error_message()
),
full_moon::Error::TokenizerError(tokenizer_error) => tokenizer_error.to_string(),
}
}

fn print_full_moon_errors(errors: &[full_moon::Error]) -> String {
if errors.len() == 1 {
print_full_moon_error(errors.first().unwrap())
} else {
errors
.iter()
.map(|err| "\n - ".to_string() + &print_full_moon_error(err))
.collect::<String>()
}
}

/// A formatting error
#[derive(Clone, Debug, Error)]
pub enum Error {
/// The input AST has a parsing error.
#[error("error parsing: {0:?}")]
#[error("error parsing: {}", print_full_moon_errors(.0))]
ParseError(Vec<full_moon::Error>),
/// The output AST after formatting generated a parse error. This is a definite error.
#[error("INTERNAL ERROR: Output AST generated a syntax error. Please report this at https://github.com/johnnymorganz/stylua/issues\n{0:?}")]
#[error("INTERNAL ERROR: Output AST generated a syntax error. Please report this at https://github.com/johnnymorganz/stylua/issues: {}", print_full_moon_errors(.0))]
VerificationAstError(Vec<full_moon::Error>),
/// The output AST after formatting differs from the input AST.
#[error("INTERNAL WARNING: Output AST may be different to input AST. Code correctness may have changed. Please examine the formatting diff and report any issues at https://github.com/johnnymorganz/stylua/issues")]
Expand Down

0 comments on commit 5972683

Please sign in to comment.