From e63659c208a83d3e04855e0d8a97127e24ae6224 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 18 Apr 2022 09:19:42 -0500 Subject: [PATCH] fix: Ignore CSS colors Fixes #462 --- crates/typos/src/tokens.rs | 48 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index ba2b5e7..83241ae 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -189,6 +189,7 @@ mod parser { terminated(base64_literal, sep1), terminated(email_literal, sep1), terminated(url_literal, sep1), + terminated(css_color, sep1), c_escape, printf, other, @@ -283,6 +284,27 @@ mod parser { )(input) } + fn css_color(input: T) -> IResult + where + T: nom::InputTakeAtPosition + + nom::InputTake + + nom::InputIter + + nom::InputLength + + nom::Slice> + + Clone + + std::fmt::Debug, + ::Item: AsChar + Copy, + ::Item: AsChar + Copy, + { + preceded( + char('#'), + alt(( + take_while_m_n(3, 8, is_lower_hex_digit), + take_while_m_n(3, 8, is_upper_hex_digit), + )), + )(input) + } + fn uuid_literal(input: T) -> IResult where T: nom::InputTakeAtPosition @@ -620,8 +642,13 @@ mod parser { #[inline] fn is_ignore_char(i: impl AsChar + Copy) -> bool { let c = i.as_char(); - // See c_escape and printf - !unicode_xid::UnicodeXID::is_xid_continue(c) && c != '\\' && c != '%' + !unicode_xid::UnicodeXID::is_xid_continue(c) && + // See c_escape + c != '\\' && + // See printf + c != '%' && + // See css_color + c != '#' } #[inline] @@ -1248,6 +1275,23 @@ mod test { assert_eq!(expected, actual); } + #[test] + fn tokenize_color() { + let parser = TokenizerBuilder::new().build(); + + let input = "#[derive(Clone)] #aaa # #111 #AABBCC #hello #AABBCCDD World"; + let expected: Vec = vec![ + Identifier::new_unchecked("derive", Case::None, 2), + Identifier::new_unchecked("Clone", Case::None, 9), + Identifier::new_unchecked("hello", Case::None, 38), + Identifier::new_unchecked("World", Case::None, 54), + ]; + let actual: Vec<_> = parser.parse_bytes(input.as_bytes()).collect(); + assert_eq!(expected, actual); + let actual: Vec<_> = parser.parse_str(input).collect(); + assert_eq!(expected, actual); + } + #[test] fn tokenize_template() { let parser = TokenizerBuilder::new().build();