1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-07 22:58:24 -05:00
This commit is contained in:
Bartek Iwańczuk 2024-11-06 18:13:56 +01:00
parent 207ec468c1
commit 9520620289
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750

View file

@ -269,10 +269,11 @@ fn apply_lint_fixes(
pub fn is_minified_file(code: &str) -> bool { pub fn is_minified_file(code: &str) -> bool {
const LONG_LINE_THRESHOLD: usize = 250; const LONG_LINE_THRESHOLD: usize = 250;
const WHITESPACE_THRESHOLD: f64 = 0.01; const WHITESPACE_THRESHOLD: f64 = 0.1;
let mut whitespace_count = 0; let mut whitespace_count = 0;
let mut total_len = 0; let mut total_len = 0;
let mut line_count = 0;
let is_possibly_minified = code.lines().any(|line| { let is_possibly_minified = code.lines().any(|line| {
// Line length over the threshold suggests a minified file. // Line length over the threshold suggests a minified file.
@ -287,6 +288,7 @@ pub fn is_minified_file(code: &str) -> bool {
return true; return true;
} }
line_count += 1;
// Last ditch effort, keep track of whitespace count. // Last ditch effort, keep track of whitespace count.
if line_len > 0 { if line_len > 0 {
whitespace_count += whitespace_count +=
@ -297,13 +299,17 @@ pub fn is_minified_file(code: &str) -> bool {
false false
}); });
if is_possibly_minified {
return true;
}
eprintln!(
"whitespace count {} total_len {}",
whitespace_count, total_len
);
let whitespace_ratio = whitespace_count as f64 / total_len as f64; let whitespace_ratio = whitespace_count as f64 / total_len as f64;
eprintln!("whitespace_ration {}", whitespace_ratio); eprintln!("whitespace_ration {}", whitespace_ratio);
if whitespace_ratio < WHITESPACE_THRESHOLD { whitespace_ratio < WHITESPACE_THRESHOLD
true
} else {
is_possibly_minified
}
} }
// Example test module // Example test module
@ -325,6 +331,15 @@ mod tests {
assert!(is_minified_file(minified)); assert!(is_minified_file(minified));
} }
#[test]
fn test_minified_file_sourcemap() {
let minified = r#"function f(a, b) { return a.concat(b) }
var x = function(n) { return n + 1; };
//# sourceMappingURL=sourcefile.map.js"
"#;
assert!(is_minified_file(minified));
}
#[test] #[test]
fn test_normal_file() { fn test_normal_file() {
let normal = r#" let normal = r#"
@ -339,7 +354,7 @@ const incrementNumber = function(number) {
} }
#[test] #[test]
fn test_minified_file_source_map() { fn test_normal_file_source_map() {
let normal = r#" let normal = r#"
function concatenateArrays(array1, array2) { function concatenateArrays(array1, array2) {
return array1.concat(array2); return array1.concat(array2);
@ -349,6 +364,6 @@ const incrementNumber = function(number) {
return number + 1; return number + 1;
}; };
//# sourceMappingURL=sourcefile.map.js"#; //# sourceMappingURL=sourcefile.map.js"#;
assert!(is_minified_file(normal)); assert!(!is_minified_file(normal));
} }
} }