Skip to content

Commit

Permalink
feat(tokens): Ignore JWTs
Browse files Browse the repository at this point in the history
Fixes #1057
  • Loading branch information
epage committed Jul 10, 2024
1 parent 5eab324 commit 6047fba
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions crates/typos/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ mod parser {
terminated(email_literal, peek(sep1)),
terminated(url_literal, peek(sep1)),
terminated(css_color, peek(sep1)),
terminated(jwt, peek(sep1)),
c_escape,
printf,
other,
Expand Down Expand Up @@ -297,6 +298,41 @@ mod parser {
.parse_next(input)
}

fn jwt<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Compare<char>,
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
<T as Stream>::Token: AsChar + Copy,
{
trace(
"jwt",
(
'e',
'y',
take_while(20.., is_jwt_token),
'.',
'e',
'y',
take_while(20.., is_jwt_token),
'.',
take_while(20.., is_jwt_token),
)
.recognize(),
)
.parse_next(input)
}

#[inline]
fn is_jwt_token(i: impl AsChar + Copy) -> bool {
let c = i.as_char();
c.is_ascii_lowercase()
|| c.is_ascii_uppercase()
|| c.is_ascii_digit()
|| c == '_'
|| c == '-'
}

fn uuid_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Compare<char>,
Expand Down Expand Up @@ -1582,26 +1618,6 @@ mod test {
case: None,
offset: 23,
},
Identifier {
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
case: None,
offset: 30,
},
Identifier {
token: "eyJpc3MiOiJzdXBhYmFzZSIsInJvbGUiOiJhbm9uIiwiaWF0IjoxNjQ1MTkyODI0LCJleHAiOjE5NjA3Njg4MjR9",
case: None,
offset: 67,
},
Identifier {
token: "M9jrxyvPLkUxWgOYSf5dNdJ8v_eRrq810ShFRT8N",
case: None,
offset: 156,
},
Identifier {
token: "6M",
case: None,
offset: 197,
},
]
"#]]
Expand All @@ -1626,26 +1642,6 @@ mod test {
case: None,
offset: 23,
},
Identifier {
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
case: None,
offset: 30,
},
Identifier {
token: "eyJpc3MiOiJzdXBhYmFzZSIsInJvbGUiOiJhbm9uIiwiaWF0IjoxNjQ1MTkyODI0LCJleHAiOjE5NjA3Njg4MjR9",
case: None,
offset: 67,
},
Identifier {
token: "M9jrxyvPLkUxWgOYSf5dNdJ8v_eRrq810ShFRT8N",
case: None,
offset: 156,
},
Identifier {
token: "6M",
case: None,
offset: 197,
},
]
"#]]
Expand Down

0 comments on commit 6047fba

Please sign in to comment.