Skip to content

Commit

Permalink
Merge branch 'main' into format-idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhuene authored Jan 14, 2025
2 parents 00a058d + 0880d3f commit d762772
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
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

0 comments on commit d762772

Please sign in to comment.