| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
use std::fs; |
| 37 |
use std::path::{Path, PathBuf}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
const HIGH_WATER: usize = 124; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
fn sources() -> Vec<PathBuf> { |
| 54 |
let root = Path::new(env!("CARGO_MANIFEST_DIR")); |
| 55 |
let mut files = Vec::new(); |
| 56 |
|
| 57 |
for entry in fs::read_dir(root.join("static")).expect("read static/ dir") { |
| 58 |
let path = entry.expect("dir entry").path(); |
| 59 |
let Some(name) = path.file_name().and_then(|n| n.to_str()) else { |
| 60 |
continue; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
let is_js = path.extension().and_then(|e| e.to_str()) == Some("js"); |
| 68 |
if name.ends_with(".min.js") || !is_js { |
| 69 |
continue; |
| 70 |
} |
| 71 |
files.push(path); |
| 72 |
} |
| 73 |
collect_ts(&root.join("frontend/src"), &mut files); |
| 74 |
|
| 75 |
files |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
fn collect_ts(dir: &Path, out: &mut Vec<PathBuf>) { |
| 82 |
let Ok(entries) = fs::read_dir(dir) else { |
| 83 |
return; |
| 84 |
}; |
| 85 |
for entry in entries { |
| 86 |
let path = entry.expect("dir entry").path(); |
| 87 |
if path.is_dir() { |
| 88 |
collect_ts(&path, out); |
| 89 |
continue; |
| 90 |
} |
| 91 |
let Some(name) = path.file_name().and_then(|n| n.to_str()) else { |
| 92 |
continue; |
| 93 |
}; |
| 94 |
if name.ends_with(".d.ts") || name.contains(".test.") { |
| 95 |
continue; |
| 96 |
} |
| 97 |
if matches!(path.extension().and_then(|e| e.to_str()), Some("ts" | "js")) { |
| 98 |
out.push(path); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
fn count_script_globals(src: &str) -> usize { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
regex::Regex::new(r"(?m)^(?:async )?function [A-Za-z_][A-Za-z0-9_]*\s*\(") |
| 120 |
.unwrap() |
| 121 |
.find_iter(src) |
| 122 |
.count() |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
fn count_globals(src: &str) -> usize { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
let direct = regex::Regex::new(r"window\.[A-Za-z_][A-Za-z0-9_]*\s*=[^=]").unwrap(); |
| 143 |
let binding = |
| 144 |
regex::Regex::new(r"(?:const|let|var)\s+([A-Za-z_][A-Za-z0-9_]*)\s*=\s*window\b").unwrap(); |
| 145 |
|
| 146 |
let mut total = direct.find_iter(src).count(); |
| 147 |
|
| 148 |
for bound in binding.captures_iter(src) { |
| 149 |
let alias = &bound[1]; |
| 150 |
let through = |
| 151 |
regex::Regex::new(&format!(r"\b{alias}\.[A-Za-z_][A-Za-z0-9_]*\s*=[^=]")).unwrap(); |
| 152 |
total += through.find_iter(src).count(); |
| 153 |
} |
| 154 |
|
| 155 |
total |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn frontend_globals_do_not_grow() { |
| 160 |
let count: usize = sources() |
| 161 |
.iter() |
| 162 |
.map(|path| { |
| 163 |
let src = fs::read_to_string(path).unwrap_or_default(); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
let classic = path.parent().and_then(Path::file_name) == Some("static".as_ref()); |
| 168 |
count_globals(&src) |
| 169 |
+ if classic { |
| 170 |
count_script_globals(&src) |
| 171 |
} else { |
| 172 |
0 |
| 173 |
} |
| 174 |
}) |
| 175 |
.sum(); |
| 176 |
|
| 177 |
assert!( |
| 178 |
count <= HIGH_WATER, |
| 179 |
"globals across static/*.js and frontend/src rose to {count} \ |
| 180 |
(HIGH_WATER {HIGH_WATER}). New frontend code must be a typed ES module in \ |
| 181 |
frontend/src registered through the dispatcher, not a global -- and note \ |
| 182 |
that a bare top-level `function` in a classic script is one too. If you \ |
| 183 |
REMOVED globals, lower HIGH_WATER to {count}." |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn the_seal_sees_a_bare_top_level_function_in_a_classic_script() { |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
assert_eq!( |
| 193 |
count_script_globals("function exportItemSalesCSV() {\n}\n"), |
| 194 |
1 |
| 195 |
); |
| 196 |
assert_eq!(count_script_globals("async function loadThing(id) {}"), 1); |
| 197 |
|
| 198 |
|
| 199 |
assert_eq!( |
| 200 |
count_script_globals("(function () {\n function inner() {}\n})();"), |
| 201 |
0, |
| 202 |
"a declaration inside an IIFE is a function scope" |
| 203 |
); |
| 204 |
assert_eq!(count_script_globals("const f = function named() {};"), 0); |
| 205 |
} |
| 206 |
|
| 207 |
#[test] |
| 208 |
fn the_two_counters_do_not_double_count_one_name() { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
let src = "function draw() {}\nwindow.draw = draw;\n"; |
| 213 |
assert_eq!(count_script_globals(src), 1); |
| 214 |
assert_eq!(count_globals(src), 1); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn the_seal_sees_a_global_assigned_through_an_alias() { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
let bridged = "const w = window as unknown as Record<string, unknown>;\n\ |
| 224 |
w.escapeHtml = escapeHtml;\n\ |
| 225 |
w.showToast = showToast;\n"; |
| 226 |
assert_eq!(count_globals(bridged), 2); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
let ordinary = "const w = document.body;\nw.className = 'x';\n"; |
| 231 |
assert_eq!(count_globals(ordinary), 0); |
| 232 |
|
| 233 |
|
| 234 |
let compared = "if (window.foo === bar) {}\nconst w = window;\nif (w.foo == bar) {}\n"; |
| 235 |
assert_eq!(count_globals(compared), 0); |
| 236 |
} |
| 237 |
|