From 23b6ad5796d39f96d5e5505164694cc9290ee91c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 29 Jun 2021 12:17:34 -0500 Subject: [PATCH] feat(parser): Ignore SHA-1+ Fixes #270 --- crates/typos/src/tokens.rs | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index 43b1ace6..1e8798b3 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -182,6 +182,7 @@ mod parser { take_many0(alt(( sep1, terminated(uuid_literal, sep1), + terminated(hash_literal, sep1), terminated(hex_literal, sep1), terminated(dec_literal, sep1), )))(input) @@ -246,6 +247,29 @@ mod parser { )))(input) } + fn hash_literal(input: T) -> IResult + where + T: nom::InputTakeAtPosition + + nom::InputTake + + nom::InputIter + + nom::InputLength + + nom::Offset + + nom::Slice> + + nom::Slice> + + Clone, + ::Item: AsChar + Copy, + ::Item: AsChar + Copy, + { + // Size considerations: + // - sha-1 is git's original hash + // - sha-256 is git's new hash + // - Git hashes can be abbreviated but we need a good abbreviation that won't be mistaken + // for a variable name + const SHA_1_MAX: usize = 40; + const SHA_256_MAX: usize = 64; + take_while_m_n(SHA_1_MAX, SHA_256_MAX, is_lower_hex_digit)(input) + } + fn take_many0(mut f: F) -> impl FnMut(I) -> IResult where I: nom::Offset + nom::InputTake + Clone + PartialEq + std::fmt::Debug, @@ -696,6 +720,21 @@ mod test { assert_eq!(expected, actual); } + #[test] + fn tokenize_ignore_hash() { + let parser = TokenizerBuilder::new().build(); + + let input = "Hello 485865fd0412e40d041e861506bb3ac11a3a91e3 World"; + let expected: Vec = vec![ + Identifier::new_unchecked("Hello", Case::None, 0), + Identifier::new_unchecked("World", Case::None, 47), + ]; + 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_leading_digits() { let parser = TokenizerBuilder::new().build();