fix(anatomy): R5.4 field segments and code spans on the generator alphabet, wiki and liquid brackets refused in list items, linear guard

This commit is contained in:
bchanot committed 2026-10-23 13:58:14 +02:00
1 parent 1b12d90325
commit aeb5050502
10 files changed
+325 -48

No files matched your search

+9 -6
View File
@@ -101,13 +101,16 @@ pub fn split_field(field: &str) -> Result<Vec<&str>, PipelineError> {
if let Some(key) = path.iter().find(|key| key.starts_with('\0')) {
return Err(fail(format!("reserved key {key:?} in field \"{field}\"")));
}
// A control character or a backtick would reach the reports' code spans through the record.
if path
.iter()
.any(|key| key.chars().any(|c| c.is_control()) || key.contains('`'))
{
// A segment reaches the reports' code spans through the record: the alphabet of every
// settable path (`name.fr`, `view_box.min_x`, `count_range.min`), nothing a Markdown parser
// could pair with the text around the span (a `]` closed an author's `[` there).
let id_alphabet = |key: &str| {
key.chars()
.all(|c| c.is_ascii_alphanumeric() || "_-".contains(c))
};
if !path.iter().all(|key| id_alphabet(key)) {
return Err(fail(format!(
"field {field:?} holds a control character or a backtick"
"field {field:?} holds a segment outside [A-Za-z0-9_-]"
)));
}
// A JavaScript assignment through `__proto__` never wrote a key: it set a prototype,
@@ -357,9 +357,15 @@ fn intro() -> String {
/// A control character (a line break, a tab, a carriage return…) in an authored text the
/// report writes in a list item would break the item, and Prettier re-indents what follows:
/// refused (the TypeScript wrote it raw).
/// refused (the TypeScript wrote it raw). A list-item field shares its line with the other fields of the change and the generated
/// code spans: no control character, and no wiki-link or liquid bracket (`[[`, `]]`, `{%`, `%}`),
/// which Prettier pairs across the whole line (a `[[` in a rationale closed by the question's
/// `]]`, the code spans between them notwithstanding).
fn assert_single_line(text: &str) -> Result<(), PipelineError> {
if text.chars().any(char::is_control) {
let across = ["[[", "]]", "{%", "%}"]
.iter()
.any(|bracket| text.contains(bracket));
if across || text.chars().any(char::is_control) {
return Err(PipelineError::UnsupportedMarkdown(text.to_owned()));
}
assert_authored(text)
+43 -19
View File
@@ -51,12 +51,16 @@ const LEGACY_ENTITIES: [&str; 106] = [
/// HTML tag or an autolink (`<` before a letter, `/`, `!` or `?`), a character reference
/// (`&name;`, `&#n;`); a bare `&` is ruled in `admitted_char`.
fn rewritten_construct(plain: &str) -> bool {
// A `[[` padded inside with a `]]` after it, or a `]]` padded inside with a `[[` before it.
// A `[[` padded inside with a `]]` after it, or a `]]` padded inside with a `[[` before it
// (the last `]]` and the first `[[` found once: linear).
let last_close = plain.rfind("]]");
let first_open = plain.find("[[");
let padded_wiki = plain.match_indices("[[").any(|(at, _)| {
plain[at + 2..].starts_with(char::is_whitespace) && plain[at + 2..].contains("]]")
}) || plain
.match_indices("]]")
.any(|(at, _)| plain[..at].ends_with(char::is_whitespace) && plain[..at].contains("[["));
plain[at + 2..].starts_with(char::is_whitespace) && last_close.is_some_and(|c| c > at + 1)
}) || plain.match_indices("]]").any(|(at, _)| {
plain[..at].ends_with(char::is_whitespace) && first_open.is_some_and(|o| o + 1 < at)
});
let liquid = plain.contains("{%") || plain.contains("%}");
let tag = plain
.split('<')
.skip(1)
@@ -75,6 +79,7 @@ fn rewritten_construct(plain: &str) -> bool {
|| plain.contains("][")
|| plain.contains("![")
|| padded_wiki
|| liquid
|| tag
|| reference
}
@@ -111,22 +116,29 @@ fn starts_with(chars: &[char], from: usize, text: &str) -> bool {
.all(|(index, c)| chars.get(index) == Some(&c))
}
/// Whether position `i` lies in a bare URL token: the run of non-space characters around it
/// starts with a URL literal.
fn in_url_token(chars: &[char], i: usize) -> bool {
let start = chars[..i]
.iter()
.rposition(|c| c.is_whitespace())
.map_or(0, |p| p + 1);
(start..i).any(|k| URL_STARTS.iter().any(|url| starts_with(chars, k, url)))
/// For each position, whether it lies in a bare URL token (a run of non-space characters
/// holding a URL literal before it), computed in one pass.
fn url_tokens(chars: &[char]) -> Vec<bool> {
let mut inside = Vec::with_capacity(chars.len());
let mut seen = false;
for (k, c) in chars.iter().enumerate() {
if c.is_whitespace() {
seen = false;
}
inside.push(seen);
if URL_STARTS.iter().any(|url| starts_with(chars, k, url)) {
seen = true;
}
}
inside
}
/// An `&` Prettier writes as is: before a space, or a URL's query separator (`&name=`, the name
/// not starting with a legacy reference). Anything else may start a character reference.
fn admitted_ampersand(chars: &[char], i: usize) -> bool {
fn admitted_ampersand(chars: &[char], urls: &[bool], i: usize) -> bool {
match chars.get(i + 1) {
Some(' ') => true,
Some(c) if c.is_ascii_lowercase() && in_url_token(chars, i) => {
Some(c) if c.is_ascii_lowercase() && urls[i] => {
let name: String = chars[i + 1..]
.iter()
.take_while(|c| c.is_ascii_alphanumeric())
@@ -140,13 +152,13 @@ fn admitted_ampersand(chars: &[char], i: usize) -> bool {
}
}
fn admitted_char(chars: &[char], i: usize) -> bool {
fn admitted_char(chars: &[char], urls: &[bool], i: usize) -> bool {
let at = |k: usize| chars.get(k).copied();
let previous = i.checked_sub(1).and_then(at);
match chars[i] {
c if c.is_alphanumeric() => true,
' ' => at(i + 1) != Some(' '),
'&' => admitted_ampersand(chars, i),
'&' => admitted_ampersand(chars, urls, i),
'_' => {
previous.is_some_and(|c| c.is_ascii_alphanumeric())
&& at(i + 1).is_some_and(|c| c.is_ascii_alphanumeric())
@@ -165,7 +177,19 @@ fn admitted_plain(plain: &str) -> bool {
return false;
};
let chars: Vec<char> = plain.chars().collect();
(0..chars.len()).all(|i| admitted_char(&chars, i))
let urls = url_tokens(&chars);
(0..chars.len()).all(|i| admitted_char(&chars, &urls, i))
}
/// A code span as the generator writes one: ids, targets, field names, file paths and the
/// template's `pnpm import:equine` (the alphabet `[A-Za-z0-9_:/. -]`), never empty, no outer space.
fn generated_code(code: &str) -> bool {
!code.is_empty()
&& !code.starts_with(' ')
&& !code.ends_with(' ')
&& code
.chars()
.all(|c| c.is_ascii_alphanumeric() || "_:/. -".contains(c))
}
/// A text outside table delimiters: its code spans and its plain parts.
@@ -176,7 +200,7 @@ fn guard_text(text: &str) -> bool {
}
let admitted = parts.iter().enumerate().all(|(index, part)| {
if index % 2 == 1 {
!part.is_empty() && !part.starts_with(' ') && !part.ends_with(' ')
generated_code(part)
} else {
admitted_plain(part)
}