Skip to content

Commit

Permalink
Stop indentation checking at EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
zsol committed Jan 17, 2022
1 parent 332710d commit 3bb68a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions native/libcst/src/tokenizer/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,13 @@ impl<'t> TokState<'t> {
}
}

// Lines with only whitespace and/or comments and/or a line continuation character shouldn't
// affect the indentation and are not passed to the parser as NEWLINE tokens.
self.blank_line = matches!(self.text_pos.peek(), Some('#') | Some('\n') | Some('\\'));
// Lines with only whitespace and/or comments and/or a line continuation
// character shouldn't affect the indentation and are not passed to the parser
// as NEWLINE tokens.
self.blank_line = matches!(
self.text_pos.peek(),
Some('#') | Some('\n') | Some('\\') | None
);

if self.blank_line || !self.paren_stack.is_empty() {
return Ok(());
Expand Down
17 changes: 17 additions & 0 deletions native/libcst/src/tokenizer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,20 @@ fn test_add_dedents_for_dangling_indent_with_comment() {
])
);
}

#[test]
fn test_inconsistent_indentation_at_eof() {
assert_eq!(
tokenize_all("if 1:\n pass\n ", &default_config()),
Ok(vec![
(TokType::Name, "if"),
(TokType::Number, "1"),
(TokType::Op, ":"),
(TokType::Newline, "\n"),
(TokType::Indent, ""),
(TokType::Name, "pass"),
(TokType::Newline, "\n"),
(TokType::Dedent, ""),
])
)
}

0 comments on commit 3bb68a8

Please sign in to comment.