From 83e25ad17f47543d361f3812de765d1adc582db1 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Fri, 18 Aug 2023 10:10:04 -0400 Subject: [PATCH] Detect files with really long lines as generated (#59) Files with absurdly long lines are most likely generated, after going through a minifier or a code generator that produces unformatted code. --- gengo/src/generated.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/gengo/src/generated.rs b/gengo/src/generated.rs index ba912551..1c238871 100644 --- a/gengo/src/generated.rs +++ b/gengo/src/generated.rs @@ -20,8 +20,16 @@ impl Generated { self.globs.iter().any(|g| g.matches_path(filepath.as_ref())) } - fn is_generated_with_read>(&self, _filepath: P, _contents: &[u8]) -> bool { - false + fn is_generated_with_read>(&self, _filepath: P, contents: &[u8]) -> bool { + self.likely_minified(contents) + } + + fn likely_minified(&self, contents: &[u8]) -> bool { + // NOTE If the first 10 lines are really long, it's probably minified. + contents + .split(|&b| b == b'\n') + .take(10) + .any(|line| line.len() > 250) } fn globs() -> Vec { @@ -52,4 +60,13 @@ mod tests { let generated = Generated::new(); assert_eq!(generated.is_generated_no_read(filepath), expected); } + + #[test] + fn test_likely_minified() { + let generated = Generated::new(); + let header: Vec = b"/*!\n * This is my license etc etc\n */".to_vec(); + let contents = b"console.log('hello, world!');".repeat(50); + let contents = [header, contents].concat(); + assert!(generated.likely_minified(&contents)); + } }