Skip to content

Commit 34c5c39

Browse files
authored
[wit-parser] Don't trim leading/trailing whitespace from lines in doc comments (#1954)
* When stripping leading whitespace from lines of doc comments, find the minimum amount of whitespace common to all lines and strip that much, rather than going all-or-nothing. * Update test expectations * * Find min leading whitespace using iterators * Rename intermediate value `d` to `contents` * Manually update the blessed output for cli/wit-with-all-features * When running tests, don't remove the trailing newline from the previously-generated output * Count leading whitespace after stripping the leading comment tokens so that we actually _find_ the leading whitespace. * Change `assert_output()` so that all test outputs have a single trailing newline. Most outputs already had this, but some had none. * Re-run tests with `BLESS=1` after updating with latest from main
1 parent a43321d commit 34c5c39

34 files changed

+65
-42
lines changed

crates/wit-component/tests/interfaces/wasi-http.wat

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

crates/wit-component/tests/interfaces/wasi-http/http.wit.print

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interface types {
4949
}
5050

5151
/// These cases are inspired by the IANA HTTP Proxy Error Types:
52-
/// https://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types
52+
/// https://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types
5353
variant error-code {
5454
DNS-timeout,
5555
DNS-error(DNS-error-payload),

crates/wit-parser/src/ast/resolve.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,13 +1434,34 @@ impl<'a> Resolver<'a> {
14341434

14351435
fn docs(&mut self, doc: &super::Docs<'_>) -> Docs {
14361436
let mut docs = vec![];
1437+
14371438
for doc in doc.docs.iter() {
1438-
if let Some(doc) = doc.strip_prefix("/**") {
1439-
docs.push(doc.strip_suffix("*/").unwrap().trim());
1440-
} else {
1441-
docs.push(doc.trim_start_matches('/').trim());
1442-
}
1439+
let contents = match doc.strip_prefix("/**") {
1440+
Some(doc) => doc.strip_suffix("*/").unwrap(),
1441+
None => doc.trim_start_matches('/'),
1442+
};
1443+
1444+
docs.push(contents.trim_end());
14431445
}
1446+
1447+
// Scan the (non-empty) doc lines to find the minimum amount of leading whitespace.
1448+
// This amount of whitespace will be removed from the start of all doc lines,
1449+
// normalizing the output while retaining intentional spacing added by the original authors.
1450+
let min_leading_ws = docs
1451+
.iter()
1452+
.filter(|doc| !doc.is_empty())
1453+
.map(|doc| doc.bytes().take_while(|c| c.is_ascii_whitespace()).count())
1454+
.min()
1455+
.unwrap_or(0);
1456+
1457+
if min_leading_ws > 0 {
1458+
let leading_ws_pattern = " ".repeat(min_leading_ws);
1459+
docs = docs
1460+
.iter()
1461+
.map(|doc| doc.strip_prefix(&leading_ws_pattern).unwrap_or(doc))
1462+
.collect();
1463+
}
1464+
14441465
let contents = if docs.is_empty() {
14451466
None
14461467
} else {

crates/wit-parser/tests/ui/comments.wit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"functions": {},
1111
"docs": {
12-
"contents": "hello\nworld\nwhy, yes\nthis is a comment\n* this too */\n* is a comment */\n* this /* is /* a */ nested */ comment */"
12+
"contents": " hello\n world\n why, yes\n this is a comment\n* this too */\n* is a comment */\n* this /* is /* a */ nested */ comment */"
1313
},
1414
"package": 0
1515
}

tests/cli.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn assert_output(bless: bool, output: &[u8], path: &Path, tempdir: &TempDir) ->
203203
// sanitize the output to be consistent across platforms and handle per-test
204204
// differences such as `%tmpdir`, as well as the version number of the crate being
205205
// tested in the producers custom section.
206-
let output = String::from_utf8_lossy(output)
206+
let mut output = String::from_utf8_lossy(output)
207207
.replace(tempdir, "%tmpdir")
208208
.replace("\\", "/")
209209
.lines()
@@ -216,7 +216,14 @@ fn assert_output(bless: bool, output: &[u8], path: &Path, tempdir: &TempDir) ->
216216
}
217217
})
218218
.collect::<Vec<String>>()
219-
.join("\n");
219+
.join("\n")
220+
.trim_end()
221+
.to_string();
222+
223+
// Leave a single trailing newline on all test outputs
224+
if !output.is_empty() {
225+
output.push_str("\n");
226+
}
220227

221228
if bless {
222229
if output.is_empty() {
@@ -234,13 +241,9 @@ fn assert_output(bless: bool, output: &[u8], path: &Path, tempdir: &TempDir) ->
234241
Ok(())
235242
}
236243
} else {
237-
let mut contents = std::fs::read_to_string(path)
244+
let contents = std::fs::read_to_string(path)
238245
.with_context(|| format!("failed to read {path:?}"))?
239246
.replace("\r\n", "\n");
240-
// Drop any trailing newline, the lines iterator on output above will do the same
241-
if contents.ends_with('\n') {
242-
contents.pop();
243-
}
244247
if output != contents {
245248
bail!(
246249
"failed test: result is not as expected:{}",

tests/cli/add-metadata-merge-sections.wat.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
│ language ┆ bar [1] │
1313
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
1414
│ sdk ┆ foo [2] │
15-
╰──────────┴───────────╯
15+
╰──────────┴───────────╯

tests/cli/add-metadata-overwrite-name.wat.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
│ name ┆ foo │
77
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
88
│ range ┆ 0x0..0x15 │
9-
╰───────┴───────────╯
9+
╰───────┴───────────╯

tests/cli/add-metadata.wat.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
│ processed-by ┆ baz [1] │
1313
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
1414
│ sdk ┆ my-sdk [2] │
15-
╰──────────────┴────────────╯
15+
╰──────────────┴────────────╯

tests/cli/dump-dylink0.wat.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
0x26 | 04 06 01 01 | ImportInfo([ImportInfo { module: "a", field: "a", flags: SymbolFlags(0x0) }])
1414
| 61 01 61 00
1515
0x2e | 03 04 01 01 | ExportInfo([ExportInfo { name: "a", flags: SymbolFlags(BINDING_WEAK | BINDING_LOCAL | UNDEFINED) }])
16-
| 61 13
16+
| 61 13

tests/cli/dump/bundled.wat.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,4 @@
235235
0x25e | 01 | 1 count
236236
0x25f | 00 09 72 65 | Naming { index: 0, name: "real-wasi" }
237237
| 61 6c 2d 77
238-
| 61 73 69
238+
| 61 73 69

0 commit comments

Comments
 (0)