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

Allow trailing whitespace without newline at EOF #611

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
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
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, ""),
])
)
}