mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +00:00
Avoid emitting empty logical lines (#4452)
This commit is contained in:
parent
4b05ca1198
commit
7e0d018b35
4 changed files with 45 additions and 5 deletions
|
@ -49,3 +49,18 @@ if False:
|
||||||
#:
|
#:
|
||||||
if False: #
|
if False: #
|
||||||
print()
|
print()
|
||||||
|
#:
|
||||||
|
if False:
|
||||||
|
print()
|
||||||
|
|
||||||
|
print()
|
||||||
|
#:
|
||||||
|
if False:
|
||||||
|
print()
|
||||||
|
if False:
|
||||||
|
|
||||||
|
print()
|
||||||
|
#:
|
||||||
|
if False:
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
|
@ -250,5 +250,20 @@ f()"#
|
||||||
"f()",
|
"f()",
|
||||||
];
|
];
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
|
|
||||||
|
let contents = r#"
|
||||||
|
if False:
|
||||||
|
|
||||||
|
print()
|
||||||
|
"#
|
||||||
|
.trim();
|
||||||
|
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
|
||||||
|
let locator = Locator::new(contents);
|
||||||
|
let actual: Vec<String> = LogicalLines::from_tokens(&lxr, &locator)
|
||||||
|
.into_iter()
|
||||||
|
.map(|line| line.text_trimmed().to_string())
|
||||||
|
.collect();
|
||||||
|
let expected = vec!["if False:", "print()", ""];
|
||||||
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -500,11 +500,16 @@ impl LogicalLinesBuilder {
|
||||||
fn finish_line(&mut self) {
|
fn finish_line(&mut self) {
|
||||||
let end = self.tokens.len() as u32;
|
let end = self.tokens.len() as u32;
|
||||||
if self.current_line.tokens_start < end {
|
if self.current_line.tokens_start < end {
|
||||||
|
let is_empty = self.tokens[self.current_line.tokens_start as usize..end as usize]
|
||||||
|
.iter()
|
||||||
|
.all(|token| token.kind.is_newline());
|
||||||
|
if !is_empty {
|
||||||
self.lines.push(Line {
|
self.lines.push(Line {
|
||||||
flags: self.current_line.flags,
|
flags: self.current_line.flags,
|
||||||
tokens_start: self.current_line.tokens_start,
|
tokens_start: self.current_line.tokens_start,
|
||||||
tokens_end: end,
|
tokens_end: end,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
self.current_line = CurrentLine {
|
self.current_line = CurrentLine {
|
||||||
flags: TokenFlags::default(),
|
flags: TokenFlags::default(),
|
||||||
|
|
|
@ -167,6 +167,11 @@ pub enum TokenKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokenKind {
|
impl TokenKind {
|
||||||
|
#[inline]
|
||||||
|
pub const fn is_newline(&self) -> bool {
|
||||||
|
matches!(self, TokenKind::Newline | TokenKind::NonLogicalNewline)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn is_unary(&self) -> bool {
|
pub const fn is_unary(&self) -> bool {
|
||||||
matches!(self, TokenKind::Plus | TokenKind::Minus | TokenKind::Star)
|
matches!(self, TokenKind::Plus | TokenKind::Minus | TokenKind::Star)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue