Skip to content

Commit 0cbd02f

Browse files
committed
fix(fontmatter): Specialize the extra frontmatter close for dashes
For comparison, rustc's message is: > error: frontmatter close does not match the opening > --> $DIR/mismatch-1.rs:1:1 > | > LL | ---cargo > | ^-- > | | > | _the opening here has 3 dashes... > | | > LL | | > LL | | ---- > | |_---^ > | | > | ...while the close has 4 dashes
1 parent 1b25322 commit 0cbd02f

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/cargo/util/frontmatter.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,26 @@ impl<'s> ScriptSource<'s> {
128128
.unwrap_or_else(|| input.eof_offset()),
129129
);
130130
let content_start = input.current_token_start();
131-
let after_closing_fence = after_closing_fence.trim_matches(is_whitespace);
132-
if !after_closing_fence.is_empty() {
133-
// extra characters beyond the original fence pattern, even if they are extra `-`
131+
let extra_dashes = after_closing_fence
132+
.chars()
133+
.take_while(|b| *b == FENCE_CHAR)
134+
.count();
135+
if 0 < extra_dashes {
136+
let extra_start = close_end;
137+
let extra_end = extra_start + extra_dashes;
134138
return Err(FrontmatterError::new(
135-
format!("unexpected characters after frontmatter close"),
136-
close_end..content_start,
139+
format!("closing code fence has {extra_dashes} more `-` than the opening fence"),
140+
extra_start..extra_end,
137141
));
142+
} else {
143+
let after_closing_fence = after_closing_fence.trim_matches(is_whitespace);
144+
if !after_closing_fence.is_empty() {
145+
// extra characters beyond the original fence pattern
146+
return Err(FrontmatterError::new(
147+
format!("unexpected characters after frontmatter close"),
148+
close_end..content_start,
149+
));
150+
}
138151
}
139152

140153
source.content = content_start..content_end;
@@ -584,7 +597,7 @@ content: "\nfn main() {}\n"
584597
fn main() {}
585598
"#,
586599
),
587-
str!["unexpected characters after frontmatter close"],
600+
str!["closing code fence has 2 more `-` than the opening fence"],
588601
);
589602
}
590603

@@ -621,7 +634,7 @@ time="0.1.25"
621634
fn main() {}
622635
"#,
623636
),
624-
str!["unexpected characters after frontmatter close"],
637+
str!["closing code fence has 1 more `-` than the opening fence"],
625638
);
626639
}
627640

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
[ERROR] unexpected characters after frontmatter close
1+
[ERROR] closing code fence has 1 more `-` than the opening fence
22
--> script:3:4
33
|
4-
3 | ----
5-
| ____^
6-
4 | |
7-
| |_^
4+
3 | ----
5+
| ^

0 commit comments

Comments
 (0)