Fix various bugs found via running the test suite#11611
Fix various bugs found via running the test suite#11611dhruvmanila merged 2 commits intodhruv/parser-phase-2from
Conversation
a40012c to
c137200
Compare
c137200 to
b356439
Compare
| } | ||
| if checker.enabled(Rule::PrintfStringFormatting) { | ||
| pyupgrade::rules::printf_string_formatting(checker, format_string, right); | ||
| pyupgrade::rules::printf_string_formatting(checker, bin_op, format_string); |
There was a problem hiding this comment.
The function requires the range and the right side of the binary expression so we let's just pass the binary expression as a whole.
| let (a_range, b_range) = match (a_token.kind(), b_token.kind()) { | ||
| (TokenKind::String, TokenKind::String) => (a_token.range(), b_token.range()), | ||
| (TokenKind::String, TokenKind::FStringStart) => { | ||
| match indexer.fstring_ranges().innermost(a_token.start()) { | ||
| match indexer.fstring_ranges().innermost(b_token.start()) { | ||
| Some(b_range) => (a_token.range(), b_range), | ||
| None => continue, | ||
| } |
There was a problem hiding this comment.
Must've been a copy paste mistake.
There was a problem hiding this comment.
That must have been annoying to find.
| TokenKind::Async if matches!(self.tokens.peek(), Some((TokenKind::Def, _))) => { | ||
| LogicalLineKind::Function | ||
| continue; | ||
| } | ||
| TokenKind::Import => LogicalLineKind::Import, | ||
| TokenKind::From => LogicalLineKind::FromImport, | ||
| _ => LogicalLineKind::Other, | ||
| }; | ||
|
|
||
| first_logical_line_token = Some((logical_line_kind, range)); | ||
| is_docstring = kind == TokenKind::String; | ||
|
|
||
| let logical_line_kind = match kind { | ||
| TokenKind::Class => LogicalLineKind::Class, | ||
| TokenKind::Comment => LogicalLineKind::Comment, | ||
| TokenKind::At => LogicalLineKind::Decorator, | ||
| TokenKind::Def => LogicalLineKind::Function, | ||
| // Lookahead to distinguish `async def` from `async with`. | ||
| TokenKind::Async | ||
| if self | ||
| .tokens | ||
| .peek() | ||
| .is_some_and(|token| token.kind() == TokenKind::Def) => |
There was a problem hiding this comment.
I think this isn't a bug fix, but mostly a change from:
if matches!(self.tokens.peek(), Some((TokenKind::Def, _)))To
if self
.tokens
.peek()
.is_some_and(|token| token.kind() == TokenKind::Def)There was a problem hiding this comment.
Yeah, I think this is more a compilation error fix?
| diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( | ||
| content, | ||
| located_op.range + expr.start(), | ||
| located_op.range, | ||
| ))); |
There was a problem hiding this comment.
The reason this offset exists was because the lexer function used was lex instead of lex_starts_at.
| // Reconstruct the source code by skipping any names that are in `members`. | ||
| let mut output = String::with_capacity(import_from_stmt.range().len().to_usize()); | ||
| let mut last_pos = TextSize::default(); | ||
| let mut last_pos = import_from_stmt.start(); |
There was a problem hiding this comment.
Now, we use the locator containing the entire source code, so the start offset should be the statement start offset.
| (_, quote @ ('\'' | '"')) => self.try_single_char_prefix(first).then_some(quote), | ||
| (_, quote @ ('\'' | '"')) => self.try_single_char_prefix(first).then(|| { | ||
| self.cursor.bump(); | ||
| quote | ||
| }), |
There was a problem hiding this comment.
Oops! Forgot to bump the cursor.
| if self.cursor.eat_char2(quote, quote) { | ||
| self.current_flags = TokenFlags::TRIPLE_QUOTED_STRING; | ||
| self.current_flags |= TokenFlags::TRIPLE_QUOTED_STRING; | ||
| } |
| if self.cursor.eat_char2(quote, quote) { | ||
| self.current_flags = TokenFlags::TRIPLE_QUOTED_STRING; | ||
| self.current_flags |= TokenFlags::TRIPLE_QUOTED_STRING; | ||
| } |
There was a problem hiding this comment.
Yeah, not sure why I did this xD
| /// Returns `true` if the token is a raw string. | ||
| const fn is_raw_string(self) -> bool { | ||
| self.contains(TokenFlags::RAW_STRING) | ||
| self.intersects(TokenFlags::RAW_STRING) | ||
| } |
There was a problem hiding this comment.
Using contains is incorrect here.
For all other changes, I find intersects easier to reason about.
| let (a_range, b_range) = match (a_token.kind(), b_token.kind()) { | ||
| (TokenKind::String, TokenKind::String) => (a_token.range(), b_token.range()), | ||
| (TokenKind::String, TokenKind::FStringStart) => { | ||
| match indexer.fstring_ranges().innermost(a_token.start()) { | ||
| match indexer.fstring_ranges().innermost(b_token.start()) { | ||
| Some(b_range) => (a_token.range(), b_range), | ||
| None => continue, | ||
| } |
There was a problem hiding this comment.
That must have been annoying to find.
| TokenKind::Async if matches!(self.tokens.peek(), Some((TokenKind::Def, _))) => { | ||
| LogicalLineKind::Function | ||
| continue; | ||
| } | ||
| TokenKind::Import => LogicalLineKind::Import, | ||
| TokenKind::From => LogicalLineKind::FromImport, | ||
| _ => LogicalLineKind::Other, | ||
| }; | ||
|
|
||
| first_logical_line_token = Some((logical_line_kind, range)); | ||
| is_docstring = kind == TokenKind::String; | ||
|
|
||
| let logical_line_kind = match kind { | ||
| TokenKind::Class => LogicalLineKind::Class, | ||
| TokenKind::Comment => LogicalLineKind::Comment, | ||
| TokenKind::At => LogicalLineKind::Decorator, | ||
| TokenKind::Def => LogicalLineKind::Function, | ||
| // Lookahead to distinguish `async def` from `async with`. | ||
| TokenKind::Async | ||
| if self | ||
| .tokens | ||
| .peek() | ||
| .is_some_and(|token| token.kind() == TokenKind::Def) => |
There was a problem hiding this comment.
Yeah, I think this is more a compilation error fix?
|
|
||
| impl LogicalLinesIter<'_> { | ||
| fn new(tokens: Iter<'_, parser::Token>, verbatim_range: TextRange) -> Self { | ||
| impl<'a> LogicalLinesIter<'a> { |
There was a problem hiding this comment.
Nit: I think '_ should just work fine here but there's no need to change it.
| pub(crate) fn visit_token(&mut self, token: &Token) { | ||
| if matches!(token.kind(), TokenKind::String | TokenKind::FStringStart) { | ||
| if matches!(token.kind(), TokenKind::String | TokenKind::FStringMiddle) { | ||
| if token.is_triple_quoted_string() { | ||
| self.ranges.push(token.range()); | ||
| } |
There was a problem hiding this comment.
Is this a bug in main too or something you introduced by your refactor?
## Summary This PR fixes various bugs found when running the test suite. Note that it doesn't update the code to make it compile which is done in a separate PR. Refer to review comments for each individual bug.
## Summary This PR fixes various bugs found when running the test suite. Note that it doesn't update the code to make it compile which is done in a separate PR. Refer to review comments for each individual bug.
Summary
This PR fixes various bugs found when running the test suite. Note that it doesn't update the code to make it compile which is done in a separate PR.
Refer to review comments for each individual bug.