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

Don't swallow trailing whitespace #976

Merged
merged 2 commits into from
Jul 18, 2023
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
16 changes: 16 additions & 0 deletions native/libcst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ mod test {
parse_module("def g(a, b): ...", None).expect("parse error");
}

#[test]
fn test_single_statement_with_no_newline() {
for src in &[
"(\n \\\n)",
"(\n \\\n)",
"(\n '''\n''')",
"del _",
"if _:\n '''\n)'''",
"if _:\n ('''\n''')",
"if _:\n '''\n '''",
"if _:\n '''\n ''' ",
] {
parse_module(src, None).unwrap_or_else(|e| panic!("'{}' doesn't parse: {}", src, e));
}
}

#[test]
fn bol_offset_first_line() {
assert_eq!(0, bol_offset("hello", 1));
Expand Down
8 changes: 2 additions & 6 deletions native/libcst/src/nodes/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,8 @@ impl<'r, 'a> Inflate<'a> for DeflatedModule<'r, 'a> {
}
}
if let Some(num) = last_indented {
if num + 1 == footer.len() {
footer = vec![];
} else {
let (_, rest) = footer.split_at(num + 1);
footer = rest.to_vec();
}
let (_, rest) = footer.split_at(num);
footer = rest.to_vec();
}
} else {
swap(&mut header, &mut footer);
Expand Down
5 changes: 1 addition & 4 deletions native/libcst/src/tokenizer/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,7 @@ impl<'t> TokState<'t> {
return match self.text_pos.peek() {
// Check for EOF now
None => {
if self.missing_nl_before_eof
&& self.text_pos.byte_column_number() != self.bol_width
&& !self.blank_line
{
if self.missing_nl_before_eof && !self.blank_line {
self.at_bol = true;
self.missing_nl_before_eof = false;
Ok(TokType::Newline)
Expand Down
13 changes: 13 additions & 0 deletions native/libcst/src/tokenizer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,19 @@ fn test_fake_newline() {
);
}

#[test]
fn test_fake_newline_when_at_bol() {
assert_eq!(
tokenize_with_end_marker("(\n \\\n)", &default_config()),
Ok(vec![
(TokType::Op, "("),
(TokType::Op, ")"),
(TokType::Newline, ""),
(TokType::EndMarker, "")
])
)
}

#[test]
fn test_no_fake_newline_for_empty_input() {
assert_eq!(
Expand Down
5 changes: 5 additions & 0 deletions native/libcst/tests/fixtures/trailing_whitespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


x = 42
print(x)

Loading