#[derive(Debug, Default, PartialEq, Eq)]
pub(super) struct CodeLiteralClasses {
pub tokens: Vec<String>,
pub prefixes: Vec<String>,
}
const MAX_STATEMENT_HEAD: usize = 240;
const MIN_PREFIX_STEM: usize = 3;
pub(super) fn scan_code_literal_classes(source: &str) -> CodeLiteralClasses {
let bytes = source.as_bytes();
let mut out = CodeLiteralClasses::default();
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'/' if bytes.get(i + 1) == Some(&b'/') => {
i = source[i..].find('\n').map_or(bytes.len(), |n| i + n);
}
b'/' if bytes.get(i + 1) == Some(&b'*') => {
i = source[i + 2..]
.find("*/")
.map_or(bytes.len(), |n| i + n + 4);
}
quote @ (b'\'' | b'"') => {
let end = string_end(bytes, i + 1, quote);
let tokens = source
.get(i + 1..end)
.map(literal_tokens)
.unwrap_or_default();
if !tokens.is_empty() && names_a_class(bytes, i) {
out.tokens.extend(tokens.into_iter().map(str::to_owned));
}
i = end + 1;
}
b'`' => {
let (head_end, end) = template_bounds(bytes, i + 1);
let head = source.get(i + 1..head_end).unwrap_or_default();
let (tokens, prefix) = if head_end == end {
(literal_tokens(head), None)
} else {
template_head(head)
};
if (!tokens.is_empty() || prefix.is_some()) && names_a_class(bytes, i) {
out.tokens.extend(tokens.into_iter().map(str::to_owned));
out.prefixes.extend(prefix.map(str::to_owned));
}
i = end + 1;
}
_ => i += 1,
}
}
out
}
fn names_a_class(bytes: &[u8], quote: usize) -> bool {
let floor = quote.saturating_sub(MAX_STATEMENT_HEAD);
let mut end = quote;
while end > floor {
let boundary = bytes[floor..end]
.iter()
.rposition(|b| matches!(b, b';' | b'{' | b'}' | b'\n' | b'\'' | b'"' | b'`'))
.map(|pos| floor + pos);
let start = boundary.map_or(floor, |pos| pos + 1);
let head = &bytes[start..end];
if head
.windows("class".len())
.any(|window| window.eq_ignore_ascii_case(b"class"))
{
return true;
}
if !head.iter().all(|b| b.is_ascii_whitespace() || *b == b',') {
return false;
}
end = match boundary.map(|pos| (pos, bytes[pos])) {
Some((pos, b'\n')) => pos,
Some((pos, quote @ (b'\'' | b'"' | b'`'))) => {
match bytes[floor..pos].iter().rposition(|b| *b == quote) {
Some(open) => floor + open,
None => return false,
}
}
_ => return false,
};
}
false
}
fn string_end(bytes: &[u8], start: usize, quote: u8) -> usize {
let mut j = start;
while j < bytes.len() && bytes[j] != quote && bytes[j] != b'\n' {
j += if bytes[j] == b'\\' { 2 } else { 1 };
}
j.min(bytes.len())
}
fn template_bounds(bytes: &[u8], start: usize) -> (usize, usize) {
let mut head_end = None;
let mut depth = 0usize;
let mut j = start;
while j < bytes.len() {
match bytes[j] {
b'\\' => j += 1,
b'$' if depth == 0 && bytes.get(j + 1) == Some(&b'{') => {
head_end.get_or_insert(j);
depth = 1;
j += 1;
}
b'{' if depth > 0 => depth += 1,
b'}' if depth > 0 => depth -= 1,
b'`' if depth == 0 => break,
_ => {}
}
j += 1;
}
let end = j.min(bytes.len());
(head_end.unwrap_or(end), end)
}
fn literal_tokens(content: &str) -> Vec<&str> {
let parts: Vec<&str> = content.split_ascii_whitespace().collect();
if parts.is_empty() || !parts.iter().all(|part| is_class_token(part)) {
return Vec::new();
}
parts.into_iter().filter(|part| is_compound(part)).collect()
}
fn template_head(head: &str) -> (Vec<&str>, Option<&str>) {
if head.ends_with(|c: char| c.is_ascii_whitespace()) {
return (literal_tokens(head), None);
}
let mut parts: Vec<&str> = head.split_ascii_whitespace().collect();
let Some(prefix) = parts.pop() else {
return (Vec::new(), None);
};
let stem = prefix.trim_end_matches(['-', '_']);
if !parts.iter().all(|part| is_class_token(part))
|| stem.len() == prefix.len()
|| stem.len() < MIN_PREFIX_STEM
|| !is_class_token(stem)
{
return (Vec::new(), None);
}
parts.retain(|part| is_compound(part));
(parts, Some(prefix))
}
fn is_class_token(token: &str) -> bool {
let bytes = token.as_bytes();
bytes
.first()
.is_some_and(|b| b.is_ascii_alphabetic() || *b == b'_')
&& bytes
.iter()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_'))
&& !token.ends_with('-')
}
fn is_compound(token: &str) -> bool {
token.contains(['-', '_'])
}
#[cfg(test)]
mod tests {
use super::*;
fn scan(source: &str) -> (Vec<String>, Vec<String>) {
let out = scan_code_literal_classes(source);
(out.tokens, out.prefixes)
}
#[test]
fn class_constants_and_pushed_literals_credit_compound_tokens() {
let (tokens, _) = scan(
"export const ROW_MARKER_CLASS = \"table-row-drag-marker\";\nif (top) {\n classes.push('selectedCell-border-top');\n}\nconst CLASSES = [\n 'drop-marker hidden',\n];\n",
);
assert_eq!(
tokens,
[
"table-row-drag-marker",
"selectedCell-border-top",
"drop-marker"
]
);
}
#[test]
fn literals_outside_a_class_statement_credit_nothing() {
let (tokens, prefixes) = scan(
"export const helper = 'helper-only';\ndocument.getElementById('app-root');\nconst key = `user-${id}`;\n// reuse the class below\nconst id = 'user-card';\nconst cls = classNames(a)\nconst other = 'row-item';\n<div className=\"card\" data-testid=\"user-card\" aria-label=\"close-dialog\">\nconst shortClass = `ab-${b}`;\n",
);
assert!(tokens.is_empty(), "{tokens:?}");
assert!(prefixes.is_empty(), "{prefixes:?}");
}
#[test]
fn plain_words_prose_and_paths_credit_nothing() {
let (tokens, prefixes) = scan(
"const aClass = 'hidden';\nconst bClass = \"Could not load the user-profile.\";\nconst cClass = '/api/user-list';\nconst dClass = 'trailing-';\n",
);
assert!(tokens.is_empty(), "{tokens:?}");
assert!(prefixes.is_empty(), "{prefixes:?}");
}
#[test]
fn template_heads_credit_a_prefix() {
let (tokens, prefixes) = scan(
"const aClass = `cell-edge-${side}`;\nconst bClass = `grid-wrap col_${n} x`;\nconst cClass = `/users-${id}`;\nconst dClass = `${base} is-open`;\nconst eClass = `plain-class`;\n",
);
assert_eq!(tokens, ["grid-wrap", "plain-class"]);
assert_eq!(prefixes, ["cell-edge-", "col_"]);
}
#[test]
fn comments_and_nested_templates_do_not_desync_the_scan() {
let (tokens, prefixes) = scan(
"// don't read 'comment-class'\n/* it's 'block-class' */\nconst rowClass = `row-${cond ? `inner-${x}` : 'b'}`;\nconst kClass = 'after-nested';\n",
);
assert_eq!(tokens, ["after-nested"]);
assert_eq!(prefixes, ["row-"]);
}
}