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

Improve error reporting on full moon errors #921

Merged
merged 4 commits into from
Nov 17, 2024
Merged
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
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 @@
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(),

Check warning on line 450 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L439-L450

Added lines #L439 - L450 were not covered by tests
}
}

Check warning on line 452 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L452

Added line #L452 was not covered by tests

fn print_full_moon_errors(errors: &[full_moon::Error]) -> String {
if errors.len() == 1 {
print_full_moon_error(errors.first().unwrap())

Check warning on line 456 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L454-L456

Added lines #L454 - L456 were not covered by tests
} else {
errors
.iter()
.map(|err| "\n - ".to_string() + &print_full_moon_error(err))
.collect::<String>()

Check warning on line 461 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L458-L461

Added lines #L458 - L461 were not covered by tests
}
}

Check warning on line 463 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L463

Added line #L463 was not covered by tests

/// 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
Loading