From fc954bc70db99743933896ee5d2c6ceb1f8486f2 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 17:03:05 +0100 Subject: [PATCH 1/4] Improve error reporting on full moon errors --- src/lib.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b9671827..e7d24d5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -436,11 +436,37 @@ 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::() + } +} + /// 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), /// 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:?}")] From 6189bc691fb282023e3e3a1f34179191542521db Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 17:06:16 +0100 Subject: [PATCH 2/4] Add space before error --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e7d24d5d..aa8dec93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -457,7 +457,7 @@ fn print_full_moon_errors(errors: &[full_moon::Error]) -> String { } else { errors .iter() - .map(|err| "\n- ".to_string() + &print_full_moon_error(err)) + .map(|err| "\n - ".to_string() + &print_full_moon_error(err)) .collect::() } } From 8f6f7be4d29e853fa6e199e7865f90ce9d56dc4f Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 17:09:21 +0100 Subject: [PATCH 3/4] Improve printing for verification AST error --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index aa8dec93..7c4263e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -469,7 +469,7 @@ pub enum Error { #[error("error parsing: {}", print_full_moon_errors(.0))] ParseError(Vec), /// 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\n{}", print_full_moon_errors(.0))] VerificationAstError(Vec), /// 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")] From 743a3f2dd11d107b14948f1aa19648ef85ddc460 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 17:11:19 +0100 Subject: [PATCH 4/4] Remove newline --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 7c4263e9..c72f003f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -469,7 +469,7 @@ pub enum Error { #[error("error parsing: {}", print_full_moon_errors(.0))] ParseError(Vec), /// 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{}", print_full_moon_errors(.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), /// 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")]