fix(linter): fix panic with unicode in unicorn/prefer_dom_node_dataset (#4166)

This commit is contained in:
Boshen 2024-07-10 15:14:45 +00:00
parent 22031430b1
commit c8f5664e0a

View file

@ -95,16 +95,16 @@ impl Rule for PreferDomNodeDataset {
match method_name {
"setAttribute" => {
ctx.diagnostic(set(span, &dataset_property_name));
ctx.diagnostic(set(span, dataset_property_name));
}
"getAttribute" => {
ctx.diagnostic(get(span, &dataset_property_name));
ctx.diagnostic(get(span, dataset_property_name));
}
"removeAttribute" => ctx.diagnostic(remove(string_lit.span, &dataset_property_name)),
"removeAttribute" => ctx.diagnostic(remove(string_lit.span, dataset_property_name)),
"hasAttribute" => {
ctx.diagnostic(has(span, &dataset_property_name));
ctx.diagnostic(has(span, dataset_property_name));
}
_ => unreachable!(),
@ -112,13 +112,8 @@ impl Rule for PreferDomNodeDataset {
}
}
fn strip_data_prefix(s: &str) -> Option<String> {
let prefix = "data-";
if s.len() >= prefix.len() && s[..prefix.len()].eq_ignore_ascii_case(prefix) {
Some(s[prefix.len()..].to_string())
} else {
None
}
fn strip_data_prefix(s: &str) -> Option<&str> {
s.strip_prefix("data-").or_else(|| s.strip_prefix("DATA-"))
}
#[test]
@ -184,6 +179,7 @@ fn test() {
r"element.getAttribute(0);",
r#"element.getAttribute("foo-unicorn");"#,
r#"element.getAttribute("data");"#,
r#"element.getAttribute("stylý");"#,
];
let fail = vec![