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

Add message to formatter SyntaxError #5881

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 2 deletions crates/ruff_formatter/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::error::Error;
pub enum FormatError {
/// In case a node can't be formatted because it either misses a require child element or
/// a child is present that should not (e.g. a trailing comma after a rest element).
SyntaxError,
SyntaxError { message: &'static str },
/// In case range formatting failed because the provided range was larger
/// than the formatted syntax tree
RangeError { input: TextRange, tree: TextRange },
Expand All @@ -28,7 +28,9 @@ pub enum FormatError {
impl std::fmt::Display for FormatError {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FormatError::SyntaxError => fmt.write_str("syntax error"),
FormatError::SyntaxError {message} => {
std::write!(fmt, "syntax error: {message}")
},
FormatError::RangeError { input, tree } => std::write!(
fmt,
"formatting range {input:?} is larger than syntax tree {tree:?}"
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_python_formatter/src/comments/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ impl Format<PyFormatContext<'_>> for FormatComment<'_> {
let trailing_whitespace_len = comment_text.text_len() - trimmed.text_len();

let Some(content) = trimmed.strip_prefix('#') else {
return Err(FormatError::SyntaxError);
return Err(FormatError::SyntaxError {
message: "Didn't find expected comment token `#`",
});
};

// Fast path for correctly formatted comments:
Expand Down
12 changes: 9 additions & 3 deletions crates/ruff_python_formatter/src/expression/expr_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,23 @@ pub(crate) fn find_colons(
.as_ref()
.map_or(range.start(), |lower| lower.range().end());
let first_colon =
first_non_trivia_token(after_lower, contents).ok_or(FormatError::SyntaxError)?;
first_non_trivia_token(after_lower, contents).ok_or(FormatError::SyntaxError {
message: "Din't find any token for slice first colon",
})?;
if first_colon.kind != TokenKind::Colon {
return Err(FormatError::SyntaxError);
return Err(FormatError::SyntaxError {
message: "slice first colon token was not a colon",
});
}

let after_upper = upper
.as_ref()
.map_or(first_colon.end(), |upper| upper.range().end());
// At least the closing bracket must exist, so there must be a token there
let next_token =
first_non_trivia_token(after_upper, contents).ok_or(FormatError::SyntaxError)?;
first_non_trivia_token(after_upper, contents).ok_or(FormatError::SyntaxError {
message: "Din't find any token for slice second colon",
})?;
let second_colon = if next_token.kind == TokenKind::Colon {
debug_assert!(
next_token.range.start() < range.end(),
Expand Down
8 changes: 6 additions & 2 deletions crates/ruff_python_formatter/src/expression/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ impl Format<PyFormatContext<'_>> for FormatStringContinuation<'_> {
continue;
}
Err(_) => {
return Err(FormatError::SyntaxError);
return Err(FormatError::SyntaxError {
message: "Unexpected lexer error in string formatting",
});
}
};

Expand Down Expand Up @@ -167,7 +169,9 @@ impl Format<PyFormatContext<'_>> for FormatStringPart {
let prefix = StringPrefix::parse(string_content);
let after_prefix = &string_content[usize::from(prefix.text_len())..];

let quotes = StringQuotes::parse(after_prefix).ok_or(FormatError::SyntaxError)?;
let quotes = StringQuotes::parse(after_prefix).ok_or(FormatError::SyntaxError {
message: "Didn't find string quotes after prefix",
})?;
let relative_raw_content_range = TextRange::new(
prefix.text_len() + quotes.text_len(),
string_content.text_len() - quotes.text_len(),
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_python_formatter/src/statement/stmt_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
type_comment: _,
} = item;

let (first, rest) = targets.split_first().ok_or(FormatError::SyntaxError)?;
let (first, rest) = targets.split_first().ok_or(FormatError::SyntaxError {
message: "Expected at least on assignment target",
})?;

write!(
f,
Expand Down
8 changes: 6 additions & 2 deletions crates/ruff_python_formatter/src/statement/stmt_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,18 @@ fn are_with_items_parenthesized(
with: &AnyStatementWith,
context: &PyFormatContext,
) -> FormatResult<bool> {
let first_with_item = with.items().first().ok_or(FormatError::SyntaxError)?;
let first_with_item = with.items().first().ok_or(FormatError::SyntaxError {
message: "Expected at least one with item",
})?;
let before_first_with_item = TextRange::new(with.start(), first_with_item.start());

let mut tokenizer = SimpleTokenizer::new(context.source(), before_first_with_item)
.skip_trivia()
.skip_while(|t| t.kind() == TokenKind::Async);

let with_keyword = tokenizer.next().ok_or(FormatError::SyntaxError)?;
let with_keyword = tokenizer.next().ok_or(FormatError::SyntaxError {
message: "Expected a with keyword, didn't find any token",
})?;

debug_assert_eq!(
with_keyword.kind(),
Expand Down