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

fix: remove trailing carriage return when stripping whitespace. #291

Merged
merged 1 commit into from
Jan 14, 2025
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
2 changes: 2 additions & 0 deletions wdl-ast/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* Fixed a bug in `strip_whitespace` that left a trailing carriage return at the
end of commands and multiline strings when using Windows line endings ([#291](https://github.com/stjude-rust-labs/wdl/pull/291)).
* Fixed bug in `strip_whitespace()` that erroneously stripped characters from the first line when it had content.
Closed [issue #268](https://github.com/stjude-rust-labs/wdl/issues/268) ([#271](https://github.com/stjude-rust-labs/wdl/pull/271)).
* Fixed same #268 bug in mutliline strings as well as command sections ([#272](https://github.com/stjude-rust-labs/wdl/pull/272)).
Expand Down
31 changes: 31 additions & 0 deletions wdl-ast/src/v1/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,10 @@ impl LiteralString {
if text.ends_with('\n') {
text.pop();
}

if text.ends_with('\r') {
text.pop();
}
}

// Now that the string has been unescaped and the first and last lines trimmed,
Expand Down Expand Up @@ -7873,4 +7877,31 @@ task test {
_ => panic!("expected text part"),
}
}

#[test]
fn whitespace_stripping_on_windows() {
let (document, diagnostics) = Document::parse(
"version 1.2\r\ntask test {\r\n String s = <<<\r\n hello\r\n >>>\r\n}\r\n",
);

assert!(diagnostics.is_empty());
let ast = document.ast();
let ast = ast.as_v1().expect("should be a V1 AST");

let tasks: Vec<_> = ast.tasks().collect();
assert_eq!(tasks.len(), 1);

let decls: Vec<_> = tasks[0].declarations().collect();
assert_eq!(decls.len(), 1);

let expr = decls[0].expr().unwrap_literal().unwrap_string();
let stripped = expr.strip_whitespace().unwrap();
assert_eq!(stripped.len(), 1);
match &stripped[0] {
StrippedStringPart::Text(text) => {
assert_eq!(text.as_str(), "hello")
}
_ => panic!("expected text part"),
}
}
}
27 changes: 27 additions & 0 deletions wdl-ast/src/v1/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,10 @@ impl CommandSection {
if text.ends_with('\n') {
text.pop();
}

if text.ends_with('\r') {
text.pop();
}
}

// Check for no indentation or all whitespace, in which case we're done
Expand Down Expand Up @@ -2510,4 +2514,27 @@ task test {
};
assert_eq!(text, "");
}

#[test]
fn whitespace_stripping_on_windows() {
let (document, diagnostics) = Document::parse(
"version 1.2\r\ntask test {\r\n command <<<\r\n echo \"hello\"\r\n \
>>>\r\n}\r\n",
);

assert!(diagnostics.is_empty());
let ast = document.ast();
let ast = ast.as_v1().expect("should be a V1 AST");
let tasks: Vec<_> = ast.tasks().collect();
assert_eq!(tasks.len(), 1);

let command = tasks[0].command().expect("should have a command section");
let stripped = command.strip_whitespace().unwrap();
assert_eq!(stripped.len(), 1);
let text = match &stripped[0] {
StrippedCommandPart::Text(text) => text,
_ => panic!("expected text"),
};
assert_eq!(text, "echo \"hello\"");
}
}
6 changes: 3 additions & 3 deletions wdl-lint/src/rules/preamble_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ impl Visitor for PreambleFormattingRule {
if s.chars().filter(|&c| c == '\n').count() == 2 {
for (line, start, end) in lines_with_offset(s) {
if !line.is_empty() {
let end_offset = if s.ends_with('\n') {
1
} else if s.ends_with("\r\n") {
let end_offset = if s.ends_with("\r\n") {
2
} else if s.ends_with('\n') {
1
} else {
0
};
Expand Down
6 changes: 3 additions & 3 deletions wdl-lint/src/rules/version_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ impl Visitor for VersionFormattingRule {
if ws.chars().filter(|&c| c == '\n').count() == 2 {
for (line, start, end) in lines_with_offset(ws) {
if !line.is_empty() {
let end_offset = if ws.ends_with('\n') {
1
} else if ws.ends_with("\r\n") {
let end_offset = if ws.ends_with("\r\n") {
2
} else if ws.ends_with('\n') {
1
} else {
0
};
Expand Down
Loading