| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
use std::fs; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const HIGH_WATER: usize = 113; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
fn count_window_globals() -> usize { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
let re = regex::Regex::new(r"window\.[A-Za-z_][A-Za-z0-9_]*\s*=[^=]").unwrap(); |
| 28 |
let static_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/static"); |
| 29 |
let mut total = 0; |
| 30 |
for entry in fs::read_dir(static_dir).expect("read static/ dir") { |
| 31 |
let path = entry.expect("dir entry").path(); |
| 32 |
if path.extension().and_then(|e| e.to_str()) == Some("js") { |
| 33 |
let src = fs::read_to_string(&path).unwrap_or_default(); |
| 34 |
total += re.find_iter(&src).count(); |
| 35 |
} |
| 36 |
} |
| 37 |
total |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn frontend_globals_do_not_grow() { |
| 42 |
let count = count_window_globals(); |
| 43 |
assert!( |
| 44 |
count <= HIGH_WATER, |
| 45 |
"window.* global assignments in static/*.js rose to {count} (HIGH_WATER {HIGH_WATER}). \ |
| 46 |
New frontend code must be a typed ES module in frontend/src registered through the \ |
| 47 |
dispatcher, not a `window.*` global. If you REMOVED globals, lower HIGH_WATER to {count}." |
| 48 |
); |
| 49 |
} |
| 50 |
|