Craft Anything: understand how any software works, and craft your own.
Rust
Comparing app maps notices an edited inline script (#269)
BFlat4ae6ce70d256
diff --git a/crates/analysis/src/js_app.rs b/crates/analysis/src/js_app.rsindex 47a14b91..cde0b683 100644--- a/crates/analysis/src/js_app.rs+++ b/crates/analysis/src/js_app.rs@@ -353,6 +353,8 @@ struct Analysed {html: Option<HtmlFacts>,/// For inline HTML scripts: the page they came from.host: Option<String>,+ /// For inline HTML scripts: the hash of their code (they have no file of their own, #236).+ code_sha256: Option<String>,}/// Build the application graph.@@ -404,7 +406,7 @@ pub fn analyze_application(input: &AppInput, opts: &AppOptions, cancelled: &dynif is_script(&f.path) {let text = String::from_utf8_lossy(&f.bytes);let a = js::analyze(&f.path, &text, &opts.limits);- analysed.push(Analysed { path: f.path.clone(), facts: a.facts, bundle: a.bundle, html: None, host: None });+ analysed.push(Analysed { path: f.path.clone(), facts: a.facts, bundle: a.bundle, html: None, host: None, code_sha256: None });} else if is_html(&f.path) {let text = String::from_utf8_lossy(&f.bytes);let h = scan_html(&text);@@ -413,10 +415,18 @@ pub fn analyze_application(input: &AppInput, opts: &AppOptions, cancelled: &dynlet code = craft_anything_js::util::slice(&text, a, z);let name = format!("{}#inline-{n}", f.path);let an = js::analyze(&name, code, &opts.limits);- analysed.push(Analysed { path: name, facts: an.facts, bundle: an.bundle, html: None, host: Some(f.path.clone()) });+ let code_sha256 = Some(sha256_hex(code.as_bytes()));+ analysed.push(Analysed { path: name, facts: an.facts, bundle: an.bundle, html: None, host: Some(f.path.clone()), code_sha256 });}}- analysed.push(Analysed { path: f.path.clone(), facts: ModuleFacts::default(), bundle: js::BundleInfo::default(), html: Some(h), host: None });+ analysed.push(Analysed {+ path: f.path.clone(),+ facts: ModuleFacts::default(),+ bundle: js::BundleInfo::default(),+ html: Some(h),+ host: None,+ code_sha256: None,+ });}}@@ -819,7 +829,7 @@ pub fn analyze_application(input: &AppInput, opts: &AppOptions, cancelled: &dyn}// Module summary.let bytes_len = input.files.iter().find(|x| x.path == a.path).map_or(f.bytes as u64, |x| x.bytes.len() as u64);- let sha = input.files.iter().find(|x| x.path == a.path).map_or_else(String::new, |x| sha256_hex(&x.bytes));+ let sha = input.files.iter().find(|x| x.path == a.path).map(|x| sha256_hex(&x.bytes)).or_else(|| a.code_sha256.clone()).unwrap_or_default();g.modules.push(ModuleSummary {path: a.path.clone(),role,diff --git a/crates/analysis/src/js_compare.rs b/crates/analysis/src/js_compare.rsindex d3771c47..ea2384c0 100644--- a/crates/analysis/src/js_compare.rs+++ b/crates/analysis/src/js_compare.rs@@ -46,6 +46,16 @@ fn without_build_hash(path: &str) -> Option<String> {None}+/// Same content: by hash, or, for a module saved without one (an inline script in a map from+/// before #236), by its structure, size and strings.+fn same_content(a: &ModuleSummary, b: &ModuleSummary) -> bool {+ if a.sha256.is_empty() || b.sha256.is_empty() {+ a.fingerprint == b.fingerprint && a.bytes == b.bytes && a.strings == b.strings+ } else {+ a.sha256 == b.sha256+ }+}+/// Match modules: same path and content → unchanged; same path → changed; same content/// elsewhere → moved; the same name apart from a bundler's build hash (one on each side) →/// changed, with the old name; same structure (fingerprint) elsewhere → renamed. Each right@@ -72,7 +82,7 @@ fn match_modules<'a>(l: &'a [ModuleSummary], r: &'a [ModuleSummary]) -> Value {if let Some(i) = r.iter().position(|x| x.path == m.path) {used.insert(i);match r.get(i) {- Some(x) if x.sha256 == m.sha256 => unchanged += 1,+ Some(x) if same_content(x, m) => unchanged += 1,Some(x) => changed.push(json!({"path": m.path,"same_structure": x.fingerprint.is_some() && x.fingerprint == m.fingerprint,@@ -320,6 +330,28 @@ mod tests {assert_eq!(same["modules"]["unchanged"], 4);}+ /// #236: an edited inline script had no hash on either side and counted as unchanged.+ #[test]+ fn an_edited_inline_script_is_changed() {+ let page = |says: &str| graph_of(vec![("index.html", format!("<html><script>console.log(\"{says}\");</script></html>"))]);+ let (a, b) = (page("A"), page("B"));+ assert!(a.modules.iter().all(|m| !m.sha256.is_empty()), "{:?}", a.modules);+ let d = compare_versions(&a, &b);+ let changed: Vec<&str> = d["modules"]["changed"].as_array().unwrap().iter().filter_map(|c| c["path"].as_str()).collect();+ assert_eq!(changed, ["index.html#inline-0", "index.html"], "{d}");+ assert_eq!(compare_versions(&a, &a)["modules"]["unchanged"], 2);+ // Maps saved before inline scripts had a hash.+ let unhashed = |g: &AppGraph| {+ g.modules+ .iter()+ .cloned()+ .map(|m| ModuleSummary { sha256: if m.path.contains("#inline-") { String::new() } else { m.sha256 }, ..m })+ .collect::<Vec<_>>()+ };+ assert_eq!(match_modules(&unhashed(&a), &unhashed(&a))["unchanged"], 2);+ assert_eq!(match_modules(&unhashed(&a), &unhashed(&b))["changed"].as_array().unwrap().len(), 2);+ }+#[test]fn export_outlines() {let a = graph_of(vec![("lib.js", "exports.a = 1; exports.b = 2;".into())]);