Skip to content

Commit

Permalink
fix: conditionally strip chars from first line (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz authored Dec 18, 2024
1 parent f36e0fd commit 8681548
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions wdl-ast/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
strings and provided unescaping of escape sequences ([#265](https://github.com/stjude-rust-labs/wdl/pull/265)).

### Fixed

* 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)).

## 0.9.0 - 10-22-2024

Expand Down
42 changes: 39 additions & 3 deletions wdl-ast/src/v1/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,10 +2218,12 @@ impl LiteralString {
}

// Trim the first line
let mut whole_first_line_trimmed = false;
if let Some(StrippedStringPart::Text(text)) = result.first_mut() {
let end = text.find('\n').map(|p| p + 1).unwrap_or(text.len());
let line = &text[..end];
let end_of_first_line = text.find('\n').map(|p| p + 1).unwrap_or(text.len());
let line = &text[..end_of_first_line];
let len = line.len() - line.trim_start().len();
whole_first_line_trimmed = len == line.len();
text.replace_range(..len, "");
}

Expand Down Expand Up @@ -2285,7 +2287,7 @@ impl LiteralString {
// Finally, strip the leading whitespace on each line
// This is done in place using the `replace_range` method; the method will
// internally do moves without allocations
let mut strip_leading_whitespace = true;
let mut strip_leading_whitespace = whole_first_line_trimmed;
for part in &mut result {
match part {
StrippedStringPart::Text(text) => {
Expand Down Expand Up @@ -7837,4 +7839,38 @@ task test {
_ => panic!("expected text part"),
}
}

#[test]
fn strip_whitespace_with_content_on_first_line() {
let (document, diagnostics) = Document::parse(
r#"
version 1.2
task test {
String hw = <<< hello world
my name is Jeff.
>>>
}"#,
);

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 world\n my name is Jeff.")
}
_ => panic!("expected text part"),
}
}
}

0 comments on commit 8681548

Please sign in to comment.