Skip to content

Commit

Permalink
Merge pull request #7 from mizar/rust2021
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Aug 5, 2023
2 parents 5ccdec9 + 516041d commit 09a62b8
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions rustminify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,15 @@ pub fn minify_tokens(tokens: TokenStream) -> String {
st = State::None;
}
TokenTree::Ident(ident) => {
match mem::replace(&mut st, State::AlnumUnderscoreQuote) {
State::AlnumUnderscoreQuote => *acc += " ",
match mem::replace(
&mut st,
if ident.to_string().contains('#') {
State::PoundIdent
} else {
State::AlnumUnderscoreQuote
},
) {
State::AlnumUnderscoreQuote | State::PoundIdent => *acc += " ",
State::PunctChars(puncts, _, _) => *acc += &puncts,
_ => {}
}
Expand All @@ -131,7 +138,7 @@ pub fn minify_tokens(tokens: TokenStream) -> String {
(&*literal, State::AlnumUnderscoreQuote)
};
match mem::replace(&mut st, next) {
State::AlnumUnderscoreQuote => *acc += " ",
State::AlnumUnderscoreQuote | State::PoundIdent => *acc += " ",
State::PunctChars(puncts, _, _) => *acc += &puncts,
_ => {}
}
Expand Down Expand Up @@ -185,6 +192,14 @@ pub fn minify_tokens(tokens: TokenStream) -> String {
*spacing = punct.spacing();
}
} else {
match &st {

Check failure on line 195 in rustminify/src/lib.rs

View workflow job for this annotation

GitHub Actions / stable-x86_64-pc-windows-msvc

you seem to be trying to use `match` for an equality check. Consider using `if`

Check failure on line 195 in rustminify/src/lib.rs

View workflow job for this annotation

GitHub Actions / stable-x86_64-unknown-linux-gnu

you seem to be trying to use `match` for an equality check. Consider using `if`
State::AlnumUnderscoreQuote => {
if "#\"'".contains(punct.as_char()) {
*acc += " ";
}
}
_ => {}
}
st = State::PunctChars(
punct.as_char().to_string(),
cur_pos,
Expand All @@ -206,6 +221,7 @@ pub fn minify_tokens(tokens: TokenStream) -> String {
enum State {
None,
AlnumUnderscoreQuote,
PoundIdent,
PunctChars(String, LineColumn, Spacing),
}
}
Expand Down Expand Up @@ -338,6 +354,7 @@ pub fn remove_docs(mut file: File) -> File {

#[cfg(test)]
mod tests {
use core::str::FromStr;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens as _};
use syn::{parse_quote, File};
Expand All @@ -354,6 +371,32 @@ mod tests {
#[test_case(quote!(x | || ()) => "x| ||()" ; "zero_arg_closure" )]
#[test_case(quote!(println!("{}", 2 * 2 + 1)) => r#"println!("{}",2*2+1)"# ; "println" )]
#[test_case(quote!(macro_rules! m { ($($_:tt)*) => {}; }) => "macro_rules!m{($($_:tt)*)=>{};}"; "macro_rules" )]
// https://doc.rust-lang.org/reference/tokens.html#reserved-prefixes
#[test_case(
quote!(fn x(a: &'a u8[]) -> impl 'a + Clone {}) =>
"fn x(a:&'a u8[])->impl 'a+Clone{}";
"impl_lifetime"
)]
#[test_case(
quote!(match "a" { _ => false}) =>
r#"match "a"{_=>false}"#;
"match_str_literal"
)]
#[test_case(
quote!('s: loop { loop { break 's; }}) =>
"'s:loop{loop{break 's;}}";
"break_label"
)]
#[test_case(
TokenStream::from_str("macro_rules! m { ($($_:tt)*) => {} } m!{ a # foo }").unwrap() =>
"macro_rules!m{($($_:tt)*)=>{}}m!{a #foo}";
"macro_punct_pound"
)]
#[test_case(
TokenStream::from_str("macro_rules! m { ($($_:tt)*) => {} } m!{ r#let # foo }").unwrap() =>
"macro_rules!m{($($_:tt)*)=>{}}m!{r#let#foo}";
"macro_rawident_punct_pound"
)]
fn minify_tokens(tokens: TokenStream) -> String {
crate::minify_tokens(tokens)
}
Expand Down

0 comments on commit 09a62b8

Please sign in to comment.