diff --git a/Cargo.lock b/Cargo.lock index 0539a23..a4e5c5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -133,6 +133,12 @@ version = "0.1.0" dependencies = [ "anyhow", "hex", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_parser", + "oxc_semantic", + "oxc_span", "rayon", "regex", "serde", @@ -166,6 +172,7 @@ dependencies = [ "oxc_ast_visit", "oxc_codegen", "oxc_parser", + "oxc_semantic", "oxc_span", "rayon", "sha2", diff --git a/Cargo.toml b/Cargo.toml index ac8ef38..722e1d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ oxc_ast = "0.143" oxc_ast_visit = "0.143" oxc_codegen = "0.143" oxc_parser = "0.143" +oxc_semantic = "0.143" oxc_span = "0.143" cellar-core = { path = "crates/cellar-core", version = "0.1.0" } diff --git a/README.md b/README.md index 0fffd70..ae4872d 100644 --- a/README.md +++ b/README.md @@ -135,3 +135,11 @@ Copyright (c) 2026 Rajeh Taher Copyright (c) 2026 Polymorfa (Softium LLC) Licensed under the MIT License. See [LICENSE](LICENSE) for the full text. + +## Semantic diffs + +Run `cellar diff OLD NEW --semantic` to compare local bindings without treating a rename as a program change. Free names, literals, declaration kinds, async/generator flags, optional access, array-pattern holes and `for` clauses remain significant. Parse diagnostics produce an `unknown` verdict. Semantic classification covers the primary definition and stored variants even when the textual diff exceeds `maxDiffBytes`. + +Functions are labelled by the name they are published under (`exports.foo = f` or `module.exports.foo = f`). A function that refers to a binding declared outside it is compared by what that binding is (its export name, the dependency it was bound to, its parameter slot or its literal value), not by its position, so inserting an unrelated declaration earlier in the module does not mark its consumers as changed. + +Text, JSON and Markdown reports include the verdict and localized function names. An enclosing function is collapsed into its changed inner functions only when nothing else in its body changed; an enclosing function with its own edit is listed too. Whole function additions and removals retain their outer name. Counts describe this localized report. `--include-noise` also shows modules classified as rename-only. diff --git a/crates/cellar-core/Cargo.toml b/crates/cellar-core/Cargo.toml index 27dae83..59d4b57 100644 --- a/crates/cellar-core/Cargo.toml +++ b/crates/cellar-core/Cargo.toml @@ -15,6 +15,14 @@ categories = ["development-tools", "parser-implementations"] doctest = false [dependencies] +# The shape engine needs a real AST and, more importantly, a symbol table: an +# identifier is only erasable once you know which binding it resolves to. +oxc_allocator = { workspace = true } +oxc_ast = { workspace = true } +oxc_ast_visit = { workspace = true } +oxc_parser = { workspace = true } +oxc_semantic = { workspace = true } +oxc_span = { workspace = true } anyhow = { version = "1.0", default-features = false, features = ["std"] } hex = { version = "0.4", default-features = false, features = ["std"] } rayon = "1.12" diff --git a/crates/cellar-core/src/diff.rs b/crates/cellar-core/src/diff.rs index e841c9c..737839b 100644 --- a/crates/cellar-core/src/diff.rs +++ b/crates/cellar-core/src/diff.rs @@ -23,6 +23,7 @@ use similar::{ChangeTag, TextDiff}; use crate::filter::FilterSet; use crate::model::{BundleId, ModuleEntry, ModuleIndex, SCHEMA_VERSION}; +use crate::shape; /// Supplies module source text on demand. Implemented by /// [`BundleHandle`](crate::store::BundleHandle); a test can supply its own. @@ -53,6 +54,14 @@ pub struct DiffOptions { /// Report modules whose only changed lines are noise. When false they are /// counted in the summary but left out of `changes`. pub include_noise_only: bool, + /// Classify each changed module by comparing the two programs with their + /// local names erased, and name the functions that really changed. + /// + /// Off by default because it parses both sides of every changed module, + /// which the sha256 comparison does not. It costs roughly a second per + /// thousand modules and is the difference between "7,400 modules differ" + /// and a list you can read. + pub semantic: bool, } impl Default for DiffOptions { @@ -63,6 +72,7 @@ impl Default for DiffOptions { max_diff_bytes: 1 << 20, max_hunk_bytes: 64 * 1024, include_noise_only: false, + semantic: false, } } } @@ -103,6 +113,34 @@ pub struct ModuleChange { /// Every changed line matched the filter's `noiseCode` rules. #[serde(default, skip_serializing_if = "is_false")] pub noise_only: bool, + /// What the program did, as opposed to what the text did. + /// + /// `noiseOnly` above is a line-level guess: it can recognise a changed line + /// that *looks* like transpiler output, and nothing more. It cannot see a + /// rename cascade, which is the dominant form of noise in a minified bundle + /// — inserting one binding renames every local after it, and each of those + /// lines looks like real code because it is real code that means exactly + /// what it meant before. This field is decided by comparing the two modules + /// with their local names erased, so `renamedOnly` is a proof rather than a + /// pattern match. Absent when the semantic pass was not requested or the + /// module did not parse. + #[serde(skip_serializing_if = "Option::is_none")] + pub verdict: Option, + /// The functions that actually changed, by the name they are exported + /// under. A module reported as changed with this list empty changed only + /// outside its functions — a dependency, a top-level constant. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub functions_changed: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub functions_added: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub functions_removed: Vec, + /// Functions whose program is unchanged however they were renamed or moved, + /// and the total. `40/46 kept` is the sentence a reviewer needs. + #[serde(skip_serializing_if = "Option::is_none")] + pub functions_kept: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub functions_total: Option, #[serde(skip_serializing_if = "Option::is_none")] pub lines_added: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -192,6 +230,12 @@ pub fn diff_bundles( deps_added: retain_signal(&n.deps, &[], filter), deps_removed: vec![], deps_noise_only: false, + verdict: None, + functions_changed: Vec::new(), + functions_added: Vec::new(), + functions_removed: Vec::new(), + functions_kept: None, + functions_total: None, exports_added: n.exports.clone(), exports_removed: vec![], similarity: None, @@ -214,6 +258,12 @@ pub fn diff_bundles( deps_added: vec![], deps_removed: retain_signal(&o.deps, &[], filter), deps_noise_only: false, + verdict: None, + functions_changed: Vec::new(), + functions_added: Vec::new(), + functions_removed: Vec::new(), + functions_kept: None, + functions_total: None, exports_added: vec![], exports_removed: o.exports.clone(), similarity: None, @@ -242,12 +292,51 @@ pub fn diff_bundles( "module exceeds maxDiffBytes ({} bytes); old={} new={}", opts.max_diff_bytes, o.stored_len, n.stored_len )); - } else { + } + if !too_big || opts.semantic { let old_text = old_src.load(o)?; let new_text = new_src.load(n)?; - apply_text_diff(&mut change, &old_text, &new_text, filter, opts); + if !too_big { + apply_text_diff(&mut change, &old_text, &new_text, filter, opts); + } + if opts.semantic { + if o.variants.is_empty() && n.variants.is_empty() { + apply_semantic_diff(&mut change, &old_text, &new_text); + } else { + let definitions = |entry: &ModuleEntry, + primary: String, + loader: &dyn SourceLoader| + -> Result> { + let mut texts = vec![primary]; + for variant in &entry.variants { + let mut v = entry.clone(); + v.file = variant.file.clone(); + v.raw_sha256 = variant.sha256.clone(); + texts.push(loader.load(&v)?); + } + Ok(texts) + }; + apply_definition_diff( + &mut change, + definitions(o, old_text, old_src)?, + definitions(n, new_text, new_src)?, + ); + } + } } + // A proof outranks a guess. The line rules can only recognise + // changed lines that *look* like transpiler output; the shape + // comparison knows the two programs are the same one, whatever + // the lines say. Where they disagree, take the proof — that is + // the whole reason for computing it. + match change.verdict { + Some(shape::Verdict::RenamedOnly) => change.noise_only = true, + Some(shape::Verdict::Changed | shape::Verdict::Unknown) => { + change.noise_only = false + } + _ => {} + } if change.noise_only { summary.noise_only += 1; if !opts.include_noise_only { @@ -322,6 +411,12 @@ fn modified_change( exports_removed: diff_lists(&o.exports, &n.exports), similarity: None, noise_only: false, + verdict: None, + functions_changed: Vec::new(), + functions_added: Vec::new(), + functions_removed: Vec::new(), + functions_kept: None, + functions_total: None, lines_added: None, lines_removed: None, hunks: None, @@ -329,6 +424,80 @@ fn modified_change( } } +/// Decide what the *program* did, and name the functions it did it in. +/// +/// The text diff above answers "which lines are different", which in a minified +/// bundle is mostly a question about the minifier. This answers "is this a +/// different program", and when it is, "which functions" — the two things a +/// reviewer actually wants and neither of which a line diff can supply. +fn apply_semantic_diff(change: &mut ModuleChange, old_text: &str, new_text: &str) { + let verdict = shape::compare(old_text, new_text); + change.verdict = Some(verdict); + // Nothing to localise: either the program is the same, or one side did not + // parse and any function list would be invented rather than measured. + if !matches!(verdict, shape::Verdict::Changed) { + return; + } + + let d = shape::diff_functions(old_text, new_text); + change.functions_changed = d.changed.iter().map(|c| c.label.clone()).collect(); + change.functions_added = d.added.iter().map(|f| f.label.clone()).collect(); + change.functions_removed = d.removed.iter().map(|f| f.label.clone()).collect(); + change.functions_kept = Some(d.kept as u32); + change.functions_total = Some(d.total as u32); +} + +// Compare the complete definition multiset, not only whichever copy was primary. +// Exact canonical matches are removed before pairing remaining changed definitions. +fn apply_definition_diff(change: &mut ModuleChange, old: Vec, new: Vec) { + let shapes = |texts: Vec| -> Option> { + texts + .into_iter() + .map(|text| shape::shape(&text).map(|s| (s.hash, text))) + .collect() + }; + let (Some(mut old), Some(mut new)) = (shapes(old), shapes(new)) else { + change.verdict = Some(shape::Verdict::Unknown); + return; + }; + old.sort(); + new.sort(); + new.retain(|(hash, _)| { + if let Some(i) = old.iter().position(|(h, _)| h == hash) { + old.remove(i); + false + } else { + true + } + }); + if old.is_empty() && new.is_empty() { + change.verdict = Some(shape::Verdict::RenamedOnly); + return; + } + change.verdict = Some(shape::Verdict::Changed); + let mut kept = 0; + let mut total = 0; + for i in 0..old.len().max(new.len()) { + let a = old.get(i).map(|(_, t)| t.as_str()).unwrap_or(""); + let b = new.get(i).map(|(_, t)| t.as_str()).unwrap_or(""); + let d = shape::diff_functions(a, b); + kept += d.kept; + total += d.total; + let prefix = |label: &str| format!("definition {}/{label}", i + 1); + change + .functions_changed + .extend(d.changed.iter().map(|c| prefix(&c.label))); + change + .functions_added + .extend(d.added.iter().map(|f| prefix(&f.label))); + change + .functions_removed + .extend(d.removed.iter().map(|f| prefix(&f.label))); + } + change.functions_kept = Some(kept as u32); + change.functions_total = Some(total as u32); +} + fn apply_text_diff( change: &mut ModuleChange, old_text: &str, @@ -426,6 +595,104 @@ mod tests { } } + #[test] + fn semantic_runs_above_hunk_cap_and_overrides_line_noise() { + let a = idx(1, vec![entry("M", "a", &[], 2048)]); + let b = idx(2, vec![entry("M", "b", &[], 2048)]); + let opts = DiffOptions { + semantic: true, + max_diff_bytes: 1, + ..Default::default() + }; + let d = diff_bundles( + &a, + &loader(&[("M", "var a=babelHelpers.x")]), + &b, + &loader(&[("M", "const a=babelHelpers.x")]), + &keep_all(), + &opts, + ) + .unwrap(); + assert_eq!(d.changes.len(), 1); + let m = &d.changes[0]; + assert!(m.hunks_omitted.is_some()); + assert_eq!(m.verdict, Some(shape::Verdict::Changed)); + assert!(!m.noise_only); + } + #[test] + fn semantic_compares_primary_and_variants() { + use crate::model::ModuleVariant; + struct Files(BTreeMap); + impl SourceLoader for Files { + fn load(&self, e: &ModuleEntry) -> Result { + Ok(self.0[&e.file].clone()) + } + } + let mut a = entry("M", "primary", &[], 10); + let mut b = a.clone(); + a.variants.push(ModuleVariant { + sha256: "a".into(), + file: "old.js".into(), + raw_len: 10, + deps_added: vec![], + deps_removed: vec![], + }); + b.variants.push(ModuleVariant { + sha256: "b".into(), + file: "new.js".into(), + raw_len: 10, + deps_added: vec![], + deps_removed: vec![], + }); + let files = Files(BTreeMap::from([ + ( + "modules/M.js".into(), + "var e={};e.primary=function(){return 0}".into(), + ), + ( + "old.js".into(), + "var e={};e.variant=function(){return 1}".into(), + ), + ( + "new.js".into(), + "var e={};e.variant=function(){return 2}".into(), + ), + ])); + let d = diff_bundles( + &idx(1, vec![a.clone()]), + &files, + &idx(2, vec![b.clone()]), + &files, + &keep_all(), + &DiffOptions { + semantic: true, + ..Default::default() + }, + ) + .unwrap(); + assert_eq!(d.changes[0].verdict, Some(shape::Verdict::Changed)); + assert!( + d.changes[0] + .functions_changed + .iter() + .any(|s| s.ends_with("/variant")) + ); + b.variants.clear(); + let d = diff_bundles( + &idx(1, vec![a]), + &files, + &idx(2, vec![b]), + &files, + &keep_all(), + &DiffOptions { + semantic: true, + ..Default::default() + }, + ) + .unwrap(); + assert_eq!(d.changes[0].verdict, Some(shape::Verdict::Changed)); + assert!(!d.changes[0].functions_removed.is_empty()); + } fn keep_all() -> FilterSet { FilterSet::compile(Filter { name: "all".into(), @@ -521,6 +788,67 @@ mod tests { assert!(d.changes[0].hunks.as_ref().unwrap().contains("sendStanza")); } + /// The case the line rules cannot reach. Every changed line here is real + /// code that reads as real code; only erasing the names shows the two + /// programs are the same one. + #[test] + fn a_rename_cascade_is_noise_the_line_rules_cannot_see() { + let old = idx(1, vec![entry("M", "aaa", &[], 90)]); + let new = idx(2, vec![entry("M", "bbb", &[], 90)]); + let ol = loader(&[("M", "var h = 1, y = 2;\nfunction f() { return h + y }\n")]); + let nl = loader(&[("M", "var y = 1, C = 2;\nfunction g() { return y + C }\n")]); + + // Without the semantic pass this is three changed lines of ordinary + // code, and the line rules are right to call it a change on what they + // can see. + let d = diff_bundles(&old, &ol, &new, &nl, &keep_all(), &DiffOptions::default()).unwrap(); + assert_eq!(d.changes.len(), 1); + assert!(!d.changes[0].noise_only); + + let opts = DiffOptions { + semantic: true, + include_noise_only: true, + ..Default::default() + }; + let d = diff_bundles(&old, &ol, &new, &nl, &keep_all(), &opts).unwrap(); + assert_eq!(d.changes[0].verdict, Some(shape::Verdict::RenamedOnly)); + assert!(d.changes[0].noise_only, "a proof outranks the line rules"); + assert_eq!(d.summary.noise_only, 1); + } + + /// And it must not swallow a real edit hiding inside that same cascade. + #[test] + fn a_real_change_inside_a_cascade_is_named() { + let old = idx(1, vec![entry("M", "aaa", &[], 90)]); + let new = idx(2, vec![entry("M", "bbb", &[], 90)]); + // A real module shape: the factory's last parameter is the exports + // object, and that is what makes `check` a name rather than a letter. + let ol = loader(&[( + "M", + "__d(\"M\", [], (function(t, n, r, o, a, i, l) {\n\ + \x20 var h = 1;\n function f(t) { return t > 4 }\n l.check = f;\n}), 98)\n", + )]); + let nl = loader(&[( + "M", + "__d(\"M\", [], (function(e, s, d, f, g, h, j) {\n\ + \x20 var y = 1;\n function q(n) { return n > 5 }\n j.check = q;\n}), 98)\n", + )]); + + let opts = DiffOptions { + semantic: true, + ..Default::default() + }; + let d = diff_bundles(&old, &ol, &new, &nl, &keep_all(), &opts).unwrap(); + let c = &d.changes[0]; + assert_eq!(c.verdict, Some(shape::Verdict::Changed)); + assert!(!c.noise_only); + assert_eq!( + c.functions_changed, + vec!["check"], + "named, not just counted" + ); + } + #[test] fn noise_deps_are_excluded_but_the_fact_of_change_is_kept() { let old = idx(1, vec![entry("M", "aaa", &["React"], 10)]); diff --git a/crates/cellar-core/src/factory.rs b/crates/cellar-core/src/factory.rs new file mode 100644 index 0000000..9140ea9 --- /dev/null +++ b/crates/cellar-core/src/factory.rs @@ -0,0 +1,167 @@ +//! Which parameters of a Metro module factory are `module` and `exports`. +//! +//! Shared by the indexer, which lists a module's exports, and the shape engine, +//! which labels functions by the name they are exported under. The two used to +//! carry separate copies of this decision and drifted; one answer keeps the +//! export list and the function labels describing the same object. + +use oxc_ast::ast::{AssignmentExpression, BindingPattern, Expression, Function}; +use oxc_ast_visit::{Visit, walk}; +use oxc_semantic::{Scoping, SymbolId}; + +/// Where `module` and `exports` sit, for a factory of this many parameters. +/// +/// Stock Metro passes seven parameters and puts `exports` fifth. This bundle is +/// Meta's own fork and does neither. A small number of its modules ship +/// unminified and spell the signature out, which is where these come from — over +/// one revision, 135 modules name their parameters, in two shapes: +/// +/// ```text +/// 133× (global, require, requireDynamic, requireLazy, module, exports) +/// 2× (global, require, importDefault, importNamespace, requireLazy, module, exports, …) +/// ``` +/// +/// Anything after `exports` in the long form is a dependency the loader injects +/// directly — the two unminified examples receive `invariant` and `fbt` that way +/// — so `exports` is *not* the last parameter, and treating it as one silently +/// loses the exports of every `.react` module, about a fifth of the bundle. +fn by_arity(arity: usize) -> Option<(usize, usize)> { + match arity { + 6 => Some((4, 5)), + 7.. => Some((5, 6)), + _ => None, + } +} + +/// The first slot `module` can occupy in either convention. Earlier slots are +/// `global`, `require` and loader helpers, and a property written onto one of +/// those (`global.__DEV__ = …`) says nothing about the module's exports. +const FIRST_MODULE_SLOT: usize = 4; + +/// The factory's `module` and `exports` bindings. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct FactoryBindings { + pub module: Option, + pub exports: Option, +} + +fn symbol(scoping: &Scoping, id: &oxc_ast::ast::IdentifierReference<'_>) -> Option { + id.reference_id + .get() + .and_then(|id| scoping.get_reference(id).symbol_id()) +} + +/// Which bindings have properties assigned onto them, split by whether the +/// property was `exports`. Resolved through the symbol table, so a nested +/// binding that shadows a factory parameter is a different symbol and never +/// counts for the parameter. +struct Assigned<'s> { + scoping: &'s Scoping, + /// `X.exports = …` and `X.exports.y = …` — X is the module object. + dot_exports: Vec, + /// `X.anythingElse = …` — X is a candidate exports object. + other: Vec, +} + +impl<'a> Visit<'a> for Assigned<'_> { + fn visit_assignment_expression(&mut self, e: &AssignmentExpression<'a>) { + if let Some(member) = e.left.as_member_expression() + && let Some(prop) = member.static_property_name() + { + match member.object() { + Expression::Identifier(obj) if prop == "exports" => { + if let Some(id) = symbol(self.scoping, obj) { + self.dot_exports.push(id); + } + } + Expression::Identifier(obj) => { + if let Some(id) = symbol(self.scoping, obj) { + self.other.push(id); + } + } + // `X.exports.prop = …`. The object is itself a member + // expression, so the plain identifier case never sees it, and + // without this the one form that most clearly identifies the + // module object was the one form that identified nothing. + other => { + if let Some(inner) = other.as_member_expression() + && inner.static_property_name() == Some("exports") + && let Expression::Identifier(obj) = inner.object() + && let Some(id) = symbol(self.scoping, obj) + { + self.dot_exports.push(id); + } + } + } + } + walk::walk_assignment_expression(self, e); + } +} + +/// Decide which parameters are `module` and `exports`, in that order of +/// confidence: what the factory calls them, what it does with them, and only +/// then where they sit. +/// +/// Position is the weakest of the three and is why this is not simply a pair of +/// constants. Behaviour is a fact about *this* module — the exports object is +/// the one with properties hung off it, and `module` is the one carrying +/// `.exports` — so it stays right across bundler versions and across the two +/// conventions this bundle mixes. Behaviour only counts on slots where `module` +/// or `exports` can sit at all. +pub fn module_and_exports(factory: &Function<'_>, scoping: &Scoping) -> FactoryBindings { + let named = |want: &str| { + factory.params.items.iter().find_map(|p| match &p.pattern { + BindingPattern::BindingIdentifier(id) if id.name == want => id.symbol_id.get(), + _ => None, + }) + }; + + let params: Vec> = factory + .params + .items + .iter() + .map(|p| match &p.pattern { + BindingPattern::BindingIdentifier(id) => id.symbol_id.get(), + _ => None, + }) + .collect(); + + let mut assigned = Assigned { + scoping, + dot_exports: vec![], + other: vec![], + }; + if let Some(body) = &factory.body { + assigned.visit_function_body(body); + } + let slot = |id: &SymbolId| { + params + .iter() + .position(|p| *p == Some(*id)) + .filter(|&i| i >= FIRST_MODULE_SLOT) + }; + + // `X.exports = …` identifies `module`, never `exports`. + let by_module = assigned.dot_exports.iter().filter_map(slot).max(); + // Everything else identifies `exports`. The later slot wins, because + // `module` precedes `exports` in every convention and a module writing to + // both is exporting through both. + let by_exports = assigned.other.iter().filter_map(slot).max(); + + let (m, e) = by_arity(params.len()).unwrap_or((usize::MAX, usize::MAX)); + FactoryBindings { + exports: named("exports") + // `exports` always immediately follows `module`, so knowing one + // gives the other. This matters because arity cannot separate the + // six-parameter convention plus an injected dependency from the + // seven-parameter convention — `GeminiEmojiCategories` is seven + // parameters of the former, and reading it by arity looks for + // exports on the injected `fbt` and finds none. + .or_else(|| by_module.and_then(|i| params.get(i + 1).copied().flatten())) + .or_else(|| by_exports.and_then(|i| params[i])) + .or_else(|| params.get(e).copied().flatten()), + module: named("module") + .or_else(|| by_module.and_then(|i| params[i])) + .or_else(|| params.get(m).copied().flatten()), + } +} diff --git a/crates/cellar-core/src/lib.rs b/crates/cellar-core/src/lib.rs index 7de7271..3068316 100644 --- a/crates/cellar-core/src/lib.rs +++ b/crates/cellar-core/src/lib.rs @@ -20,11 +20,13 @@ pub mod builtin; pub mod diff; +pub mod factory; pub mod filter; pub mod graph; pub mod model; pub mod naming; pub mod search; +pub mod shape; pub mod store; pub use diff::{BundleDiff, ChangeKind, DiffOptions, ModuleChange, SourceLoader, diff_bundles}; diff --git a/crates/cellar-core/src/shape.rs b/crates/cellar-core/src/shape.rs new file mode 100644 index 0000000..9060879 --- /dev/null +++ b/crates/cellar-core/src/shape.rs @@ -0,0 +1,1640 @@ +//! Telling a rename from a change. +//! +//! Half the WhatsApp modules in a release differ byte-for-byte, which is ~7,400 +//! diffs nobody reads. Most of that is not change at all. Minifiers assign names +//! by position, so inserting one constant renames everything after it: +//! +//! ```text +//! - var … h = "16505361212@c.us", y = /^1313555\d{4}$/, C = 99, b = 4, v = (…) +//! + var … h = "1807055946647698", y = "16505361212@c.us", C = /^1313555\d{4}$/, b = 99, v = 4, S = (…) +//! ``` +//! +//! Every local shifted by one. A text diff calls that five changes; it is zero. +//! And buried in the same module were three real ones — a dependency dropped, an +//! argument removed, a gating branch deleted — which is exactly the failure +//! mode: the noise is not merely loud, it hides the signal. +//! +//! # The shape +//! +//! A module is walked to a canonical token stream in which every identifier that +//! *binds within the module* is replaced by the order it was first encountered. +//! Two modules that differ only in the names of their locals produce identical +//! streams, so a rename is invisible by construction rather than by heuristic. +//! +//! Three things are deliberately **not** normalized, because each is a real +//! difference that a looser comparison would swallow: +//! +//! * **Free identifiers.** `babelHelpers`, `Promise`, `Symbol` are not this +//! module's to rename; if one changes, something changed. +//! * **String and numeric literals.** They are the wire values this whole +//! project exists to track. `"pin"` becoming `"pin_v1"` is the change. +//! * **Order.** Statement order is semantics, not formatting. +//! +//! Minifier artifacts that *are* pure encoding — `!0` for `true`, `!1` for +//! `false`, `void 0` for `undefined` — are folded, since a minifier that changes +//! its mind about those has not changed the program. + +use oxc_allocator::Allocator; +use oxc_ast::{AstKind, ast::*}; +use oxc_ast_visit::Visit; +use oxc_parser::Parser; +use oxc_semantic::{AstNodes, NodeId, Scoping, Semantic, SemanticBuilder, SymbolId}; +use oxc_span::{SourceType, Span}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +use crate::diff::SourceLoader; +use crate::factory; +use crate::model::ModuleEntry; + +/// A module's structure, with local names erased. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Shape { + /// FNV-1a of the canonical stream. + pub hash: String, + /// How many tokens it took. A cheap size signal for ranking diffs. + pub tokens: usize, +} + +/// Parse and bind a module, or `None` if the parser reported anything at all. +/// +/// Oxc recovers from many errors and hands back a best-effort tree with +/// diagnostics attached. Hashing that tree would compare two guesses, and two +/// malformed revisions that recover to the same guess would come back +/// `renamedOnly` and be suppressed as noise. Any diagnostic is therefore +/// treated as "does not parse". +fn analyze(src: &str, f: impl FnOnce(&Program<'_>, &Semantic<'_>) -> R) -> Option { + let alloc = Allocator::default(); + let parsed = Parser::new(&alloc, src, SourceType::cjs()).parse(); + if parsed.panicked || !parsed.diagnostics.is_empty() { + return None; + } + // Symbol ids are what make a rename invisible, and they only exist once the + // binder has run. + let semantic = SemanticBuilder::new() + .with_build_nodes(true) + .build(&parsed.program) + .semantic; + Some(f(&parsed.program, &semantic)) +} + +/// A nested function left out of an enclosing function's canonical stream. +#[derive(Debug, Clone, Copy)] +struct Excised { + start: u32, + end: u32, + /// Changed on both sides: stands in as one placeholder token, so the + /// enclosing body keeps its structure. Added or removed: dropped entirely, + /// because it exists on one side only. + placeholder: bool, +} + +struct Canon<'s> { + scoping: &'s Scoping, + out: String, + tokens: usize, + /// symbol -> the order it was first seen. A minifier's choice of letter is + /// not information; the position in the binding order is. + seen: HashMap, + /// The function being shaped on its own. A binding declared outside it is a + /// capture, encoded by [`capture_keys`] rather than by ordinal. + root: Option, + captures: Option<&'s HashMap>, + excise: &'s [Excised], +} + +impl<'s> Canon<'s> { + fn new(scoping: &'s Scoping) -> Self { + Self { + scoping, + out: String::new(), + tokens: 0, + seen: HashMap::new(), + root: None, + captures: None, + excise: &[], + } + } + + fn for_function( + scoping: &'s Scoping, + span: Span, + captures: &'s HashMap, + ) -> Self { + Self { + root: Some(span), + captures: Some(captures), + ..Self::new(scoping) + } + } + + fn push(&mut self, s: &str) { + self.out.push_str(s); + self.out.push('\x1f'); + self.tokens += 1; + } + + fn ident(&mut self, symbol: Option, name: &str) { + match symbol { + // Bound here: emit its ordinal, so `y` and `C` are the same thing + // when they occupy the same position in the binding order. + Some(id) => { + if self.root.is_some_and(|span| { + let declared = self.scoping.symbol_span(id); + declared.start < span.start || declared.end > span.end + }) { + // A capture. Its ordinal inside this function says nothing + // about *which* outer binding it is, and its symbol index + // shifts with every declaration inserted earlier in the + // module; the key describes the binding instead. + let key = self + .captures + .and_then(|c| c.get(&id)) + .map_or("capture", String::as_str) + .to_string(); + self.push(&key); + return; + } + let next = self.seen.len() as u32; + let n = *self.seen.entry(id).or_insert(next); + self.push(&format!("${n}")); + } + // Free: `babelHelpers`, `Promise`, a global. Not ours to rename, so + // a difference in one is a difference in the program. + None => self.push(&format!("@{name}")), + } + } + + /// Whether a nested function is left out of this stream, and how. + fn excised(&self, span: Span) -> Option { + if self.root == Some(span) { + return None; + } + self.excise + .iter() + .find(|x| x.start == span.start && x.end == span.end) + .copied() + } + + fn emit_excised(&mut self, x: Excised) { + if x.placeholder { + self.push("fn-changed"); + self.push(")"); + } + } +} + +fn reference_symbol(scoping: &Scoping, r: &IdentifierReference<'_>) -> Option { + r.reference_id + .get() + .and_then(|id| scoping.get_reference(id).symbol_id()) +} + +fn param_index(nodes: &AstNodes<'_>, node: NodeId, span: Span) -> Option { + match nodes.parent_kind(node) { + AstKind::FormalParameters(ps) => ps.items.iter().position(|p| p.span == span), + _ => None, + } +} + +/// What a binding's initializer says about the binding, minus any local name. +fn init_descriptor(init: Option<&Expression<'_>>, semantic: &Semantic<'_>) -> String { + let Some(init) = init else { + return "none".into(); + }; + match init.get_inner_expression() { + // `r("WAWebWid")`: the dependency it binds is the stable part. + Expression::CallExpression(c) => { + let callee = match c.callee.get_inner_expression() { + Expression::Identifier(r) => match reference_symbol(semantic.scoping(), r) { + None => format!("@{}", r.name), + Some(s) => { + let node = semantic.scoping().symbol_declaration(s); + match semantic.nodes().kind(node) { + AstKind::FormalParameter(p) => { + match param_index(semantic.nodes(), node, p.span) { + Some(i) => format!("p{i}"), + None => "local".into(), + } + } + _ => "local".into(), + } + } + }, + Expression::StaticMemberExpression(m) => format!(".{}", m.property.name), + _ => "expr".into(), + }; + let arg = match c.arguments.first().and_then(Argument::as_expression) { + Some(Expression::StringLiteral(s)) => format!("{:?}", s.value.as_str()), + _ => String::new(), + }; + format!("call({callee}){arg}") + } + Expression::StringLiteral(s) => format!("s{:?}", s.value.as_str()), + Expression::NumericLiteral(n) => format!("n{}", n.value), + Expression::BooleanLiteral(b) => format!("b{}", b.value), + Expression::NullLiteral(_) => "null".into(), + Expression::FunctionExpression(f) => format!("fn{}", f.params.items.len()), + Expression::ArrowFunctionExpression(f) => format!("fn{}", f.params.items.len()), + Expression::ClassExpression(_) => "class".into(), + other => format!("{:?}", std::mem::discriminant(other)), + } +} + +/// A stable name for every binding, for use when a function captures it. +/// +/// A function shaped on its own cannot number the outer bindings it refers to: +/// a fresh numbering calls `return a()` and `return b()` the same program, and +/// the parser's symbol index shifts whenever a declaration is inserted earlier +/// in the module, which is exactly the rename cascade this engine exists to +/// absorb. So a captured binding is described by what survives minification — +/// the name it is exported under, the dependency it was bound to, its parameter +/// slot, the literal it holds — at its scope depth, plus its rank among +/// same-described bindings in the same scope. Only inserting a binding with an +/// identical description earlier in the same scope moves a key. +fn capture_keys( + semantic: &Semantic<'_>, + exported: &HashMap, +) -> HashMap { + let scoping = semantic.scoping(); + let nodes = semantic.nodes(); + let mut described: Vec<(String, oxc_semantic::ScopeId, u32, SymbolId)> = Vec::new(); + for id in scoping.symbol_ids() { + let base = match exported.get(&id) { + Some(name) => format!("export:{name}"), + None => { + let node = scoping.symbol_declaration(id); + match nodes.kind(node) { + AstKind::VariableDeclarator(d) => { + let kind = match nodes.parent_kind(node) { + AstKind::VariableDeclaration(v) => format!("{:?}", v.kind), + _ => "var".into(), + }; + format!("{kind}:{}", init_descriptor(d.init.as_ref(), semantic)) + } + AstKind::FormalParameter(p) => match param_index(nodes, node, p.span) { + Some(i) => format!("param{i}"), + None => "param".into(), + }, + AstKind::Function(f) => format!("fn{}", f.params.items.len()), + AstKind::Class(_) => "class".into(), + AstKind::CatchParameter(_) => "catch".into(), + other => format!("{:?}", std::mem::discriminant(&other)), + } + } + }; + let scope = scoping.symbol_scope_id(id); + described.push((base, scope, scoping.symbol_span(id).start, id)); + } + described.sort_by(|a, b| (&a.0, a.1, a.2).cmp(&(&b.0, b.1, b.2))); + let mut out = HashMap::with_capacity(described.len()); + let mut rank = 0u32; + for (i, (base, scope, _, id)) in described.iter().enumerate() { + rank = match i.checked_sub(1).map(|j| &described[j]) { + Some((b, s, ..)) if b == base && s == scope => rank + 1, + _ => 0, + }; + let depth = scoping.scope_ancestors(*scope).count(); + out.insert(*id, format!("capture:{base}@{depth}#{rank}")); + } + out +} + +impl<'a, 's> Visit<'a> for Canon<'s> { + fn enter_node(&mut self, kind: AstKind<'a>) { + match kind { + AstKind::BindingIdentifier(b) => { + let name = b.name.as_str(); + self.ident(b.symbol_id.get(), name); + } + AstKind::IdentifierReference(r) => { + let name = r.name.as_str(); + let symbol = r + .reference_id + .get() + .and_then(|id| self.scoping.get_reference(id).symbol_id()); + self.ident(symbol, name); + } + // Literals carry the wire values this project tracks. Never folded. + AstKind::StringLiteral(s) => self.push(&format!("s{:?}", s.value.as_str())), + AstKind::NumericLiteral(n) => self.push(&format!("n{}", n.value)), + AstKind::BigIntLiteral(b) => self.push(&format!("N{}", b.value)), + AstKind::BooleanLiteral(b) => self.push(&format!("b{}", b.value)), + AstKind::RegExpLiteral(r) => self.push(&format!("r{}", r.regex)), + AstKind::NullLiteral(_) => self.push("null"), + // A property name is part of the shape: `.foo` and `.bar` differ. + AstKind::IdentifierName(i) => self.push(&format!(".{}", i.name)), + AstKind::PrivateIdentifier(i) => self.push(&format!("#{}", i.name)), + // Operators are semantics, and the discriminant does not carry them: + // `a > b` and `a >= b` are both `BinaryExpression`. Erasing names + // must not erase meaning. + AstKind::BinaryExpression(e) => self.push(&format!("op{}", e.operator.as_str())), + AstKind::LogicalExpression(e) => self.push(&format!("op{}", e.operator.as_str())), + AstKind::AssignmentExpression(e) => self.push(&format!("op{}", e.operator.as_str())), + AstKind::UnaryExpression(e) => self.push(&format!("op{}", e.operator.as_str())), + AstKind::VariableDeclaration(d) => self.push(&format!("decl{:?}", d.kind)), + AstKind::Function(f) => { + self.push(&format!("fn{:?}:{}:{}", f.r#type, f.r#async, f.generator)) + } + AstKind::ArrowFunctionExpression(f) => self.push(&format!( + "arrow{}:{}", + f.r#async, + matches!(f.body, ArrowFunctionBody::FunctionBody(_)) + )), + AstKind::TemplateElement(t) => { + self.push(&format!("template{:?}:{}", t.value.raw, t.tail)) + } + AstKind::ObjectProperty(p) => { + self.push(&format!("prop{:?}:{}:{}", p.kind, p.method, p.computed)) + } + AstKind::CallExpression(e) => self.push(&format!("call{}", e.optional)), + AstKind::StaticMemberExpression(e) => self.push(&format!("member{}", e.optional)), + AstKind::ComputedMemberExpression(e) => self.push(&format!("computed{}", e.optional)), + AstKind::PrivateFieldExpression(e) => self.push(&format!("private{}", e.optional)), + AstKind::MethodDefinition(m) => { + self.push(&format!("method{:?}:{}:{}", m.kind, m.r#static, m.computed)) + } + AstKind::PropertyDefinition(p) => { + self.push(&format!("field{}:{}", p.r#static, p.computed)) + } + AstKind::ForOfStatement(f) => self.push(&format!("forof{}", f.r#await)), + AstKind::YieldExpression(e) => self.push(&format!("yield{}", e.delegate)), + AstKind::LabelIdentifier(i) => self.push(&format!("label{}", i.name)), + AstKind::UpdateExpression(e) => { + self.push(&format!("op{}{}", e.operator.as_str(), e.prefix)) + } + // Holes have no node of their own in a pattern, so `[, a] = x` and + // `[a] = x` would otherwise emit the same stream. + AstKind::ArrayPattern(p) => self.push(&format!( + "arraypat{}", + p.elements + .iter() + .map(|e| if e.is_some() { '1' } else { '0' }) + .collect::() + )), + AstKind::ArrayAssignmentTarget(p) => self.push(&format!( + "arraytarget{}", + p.elements + .iter() + .map(|e| if e.is_some() { '1' } else { '0' }) + .collect::() + )), + // Likewise the three optional clauses of `for (;;)`: without + // markers `for (a;;)` and `for (;a;)` are the same stream. + AstKind::ForStatement(f) => self.push(&format!( + "for{}:{}:{}", + f.init.is_some(), + f.test.is_some(), + f.update.is_some() + )), + AstKind::AccessorProperty(p) => { + self.push(&format!("accessor{}:{}", p.r#static, p.computed)) + } + other => { + // The discriminant, not `debug_name()`. That helpfully embeds + // the identifier — `VariableDeclarator(h)` — so every parent + // node leaked the very name the normalization exists to erase, + // and two renamed-but-identical modules never matched. + self.push(&format!("{:?}", std::mem::discriminant(&other))); + } + } + } + + fn leave_node(&mut self, _: AstKind<'a>) { + self.push(")"); + } + + fn visit_function(&mut self, f: &Function<'a>, flags: oxc_semantic::ScopeFlags) { + match self.excised(f.span) { + Some(x) => self.emit_excised(x), + None => oxc_ast_visit::walk::walk_function(self, f, flags), + } + } + + fn visit_arrow_function_expression(&mut self, f: &ArrowFunctionExpression<'a>) { + match self.excised(f.span) { + Some(x) => self.emit_excised(x), + None => oxc_ast_visit::walk::walk_arrow_function_expression(self, f), + } + } + + /// `!0` and `!1` are how a minifier writes `true` and `false`. + /// + /// Folded because a minifier changing its mind between the two encodings has + /// not changed the program — and it does, between builds. + fn visit_unary_expression(&mut self, expr: &UnaryExpression<'a>) { + if expr.operator == UnaryOperator::LogicalNot + && let Expression::NumericLiteral(n) = &expr.argument + && (n.value == 0.0 || n.value == 1.0) + { + self.push(&format!("b{}", n.value == 0.0)); + // The close token too: `true` costs enter+leave, so a fold that + // emits only one would differ from the literal it folds to. + self.push(")"); + return; + } + // `void 0` is `undefined`, for the same reason. + if expr.operator == UnaryOperator::Void + && let Expression::NumericLiteral(n) = &expr.argument + && n.value == 0.0 + { + self.push("@undefined"); + self.push(")"); + return; + } + oxc_ast_visit::walk::walk_unary_expression(self, expr); + } +} + +fn fnv1a(s: &str) -> String { + let mut h: u64 = 0xcbf2_9ce4_8422_2325; + for b in s.as_bytes() { + h ^= *b as u64; + h = h.wrapping_mul(0x100_0000_01b3); + } + format!("{h:016x}") +} + +/// The canonical shape of a module, or `None` if it does not parse. +pub fn shape(src: &str) -> Option { + analyze(src, |program, semantic| { + let mut c = Canon::new(semantic.scoping()); + c.visit_program(program); + Shape { + hash: fnv1a(&c.out), + tokens: c.tokens, + } + }) +} + +/// What a comparison of two revisions of one module concluded. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Verdict { + /// Byte-identical. + Same, + /// Different text, identical shape: renaming and nothing else. + RenamedOnly, + /// The program changed. + Changed, + /// One side did not parse. Reported, never guessed at. + Unknown, +} + +/// The shape of every function in a module, independently. +/// +/// Module-level comparison turned out to be nearly useless on real revisions: +/// every module a release touches contains *something* real, so 7,520 of them +/// come back "changed" and the reviewer is no better off. The unit that reduces +/// the work is the function — a module with sixty functions and one real edit +/// should cost you one function to read, not sixty. +pub fn function_shapes(src: &str) -> Vec { + struct Fns<'s> { + scoping: &'s Scoping, + captures: &'s HashMap, + out: Vec, + } + impl<'a, 's> Visit<'a> for Fns<'s> { + fn visit_function(&mut self, f: &Function<'a>, flags: oxc_semantic::ScopeFlags) { + let mut c = Canon::for_function(self.scoping, f.span, self.captures); + c.visit_function(f, flags); + self.out.push(Shape { + hash: fnv1a(&c.out), + tokens: c.tokens, + }); + oxc_ast_visit::walk::walk_function(self, f, flags); + } + fn visit_arrow_function_expression(&mut self, f: &ArrowFunctionExpression<'a>) { + let mut c = Canon::for_function(self.scoping, f.span, self.captures); + c.visit_arrow_function_expression(f); + self.out.push(Shape { + hash: fnv1a(&c.out), + tokens: c.tokens, + }); + oxc_ast_visit::walk::walk_arrow_function_expression(self, f); + } + } + + analyze(src, |program, semantic| { + let exported = export_names(program, semantic.scoping()); + let captures = capture_keys(semantic, &exported); + let mut v = Fns { + scoping: semantic.scoping(), + captures: &captures, + out: Vec::new(), + }; + v.visit_program(program); + v.out + }) + .unwrap_or_default() +} + +/// How much of a changed module actually changed. +/// +/// Returns (functions whose shape is unchanged, total functions in the new +/// revision). Shapes are matched as a multiset: a function that merely moved is +/// not a change, and moving is most of what a minifier does. +pub fn function_overlap(before: &str, after: &str) -> (usize, usize) { + use std::collections::HashMap; + let mut old: HashMap = HashMap::new(); + for s in function_shapes(before) { + *old.entry(s.hash).or_default() += 1; + } + let new = function_shapes(after); + let mut kept = 0; + for s in &new { + if let Some(n) = old.get_mut(&s.hash) + && *n > 0 + { + *n -= 1; + kept += 1; + } + } + (kept, new.len()) +} + +pub fn compare(before: &str, after: &str) -> Verdict { + if before == after { + return Verdict::Same; + } + match (shape(before), shape(after)) { + (Some(a), Some(b)) if a.hash == b.hash => Verdict::RenamedOnly, + (Some(_), Some(_)) => Verdict::Changed, + _ => Verdict::Unknown, + } +} + +// --- naming the functions -------------------------------------------------- + +/// One function, canonically shaped and labelled by whatever survives minification. +/// +/// The label deliberately excludes local binding names. A minifier renames +/// `function e(t)` to `function u(n)` between builds, so matching on that would +/// re-introduce exactly the noise the shape exists to remove — and do worse than +/// not matching at all, because it would pair up unrelated functions and report +/// both as changed. +/// +/// What survives is the **export name**. A Metro module declares its functions +/// under minified locals and then publishes them — `l.encodeServerErrorReceipt = +/// u` — and that property name is stable across builds because it is the +/// module's interface. So the local is resolved through the export table before +/// it is used as a label, which is the same walk the source viewer's +/// go-to-definition does, in the other direction. Failing that, anything written +/// directly as a property (`{parse: …}`, a class method) names itself. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Func { + /// `encodeServerErrorReceipt`, `encode/#2` for a closure nested inside + /// `encode`, `#7` for something anonymous at the top level. + pub label: String, + pub hash: String, + pub tokens: usize, + /// How many parameters it declares. Used only to pair up unnamed functions, + /// where it is the one stable attribute a minifier does not touch. + pub params: usize, + /// Byte offsets into the module, so a viewer can jump straight to it. + pub start: u32, + pub end: u32, +} + +/// Which module-local symbols are published, and under what name. +/// +/// `__d("M", [...], function(a, b, …, module, exports) { … exports.foo = u … })`: +/// the factory's `module` and `exports` parameters are resolved by the same +/// rules the indexer uses ([`factory::module_and_exports`]), so an assignment +/// onto `exports` or `module.exports` maps a stable public name onto whatever +/// letter the minifier happened to give the local this build. +fn export_names(program: &Program<'_>, scoping: &Scoping) -> HashMap { + struct Exports<'s> { + scoping: &'s Scoping, + /// The factory's bindings, once the module factory has been recognised. + bindings: factory::FactoryBindings, + out: HashMap, + depth: u32, + } + + impl<'s> Exports<'s> { + fn symbol_of(&self, r: &IdentifierReference) -> Option { + reference_symbol(self.scoping, r) + } + + /// `exports` itself, or `module.exports`. + fn is_export_object(&self, obj: &Expression<'_>) -> bool { + match obj { + Expression::Identifier(o) => { + let s = self.symbol_of(o); + s.is_some() && s == self.bindings.exports + } + Expression::StaticMemberExpression(inner) if inner.property.name == "exports" => { + matches!(&inner.object, Expression::Identifier(o) + if self.symbol_of(o).is_some() && self.symbol_of(o) == self.bindings.module) + } + _ => false, + } + } + } + + impl<'a, 's> Visit<'a> for Exports<'s> { + fn visit_function(&mut self, f: &Function<'a>, flags: oxc_semantic::ScopeFlags) { + if self.depth == 0 { + self.bindings = factory::module_and_exports(f, self.scoping); + } + self.depth += 1; + oxc_ast_visit::walk::walk_function(self, f, flags); + self.depth -= 1; + } + + fn visit_assignment_expression(&mut self, e: &AssignmentExpression<'a>) { + if let AssignmentTarget::StaticMemberExpression(m) = &e.left + && self.is_export_object(&m.object) + && let Expression::Identifier(local) = &e.right + && let Some(sym) = self.symbol_of(local) + { + // First name wins. A local published twice is published under + // both, and picking either consistently beats picking whichever + // the walk happened to reach last. + self.out + .entry(sym) + .or_insert_with(|| m.property.name.to_string()); + } + oxc_ast_visit::walk::walk_assignment_expression(self, e); + } + } + + let mut v = Exports { + scoping, + bindings: factory::FactoryBindings::default(), + out: HashMap::new(), + depth: 0, + }; + v.visit_program(program); + v.out +} + +/// Every function in a module, shaped and labelled. +pub fn functions(src: &str) -> Vec { + struct Funcs<'s> { + scoping: &'s Scoping, + captures: &'s HashMap, + /// Local symbol -> the name it is published under. + exported: &'s HashMap, + out: Vec, + /// Labels of the functions we are currently inside. + path: Vec, + /// Set by a parent immediately before descending into its function + /// value, consumed by that function. `take()` is what keeps a stale + /// hint from being picked up by an unrelated sibling. + hint: Option, + anon: u32, + } + + impl<'s> Funcs<'s> { + fn record( + &mut self, + span: Span, + params: usize, + named: Option, + canon: &Canon<'s>, + ) -> String { + let label = match named.or_else(|| self.hint.take()) { + Some(n) => n, + None => { + self.anon += 1; + format!("#{}", self.anon) + } + }; + // Only named ancestors qualify a label. Every function in a Metro + // module is nested inside the module factory, which is itself + // unnamed, so including anonymous ancestors would prefix `#1/` onto + // the entire module and hang every label off an ordinal that shifts + // whenever an earlier closure is added. + let named_path: Vec<&str> = self + .path + .iter() + .filter(|p| !p.starts_with('#')) + .map(String::as_str) + .collect(); + let full = if named_path.is_empty() { + label.clone() + } else { + format!("{}/{}", named_path.join("/"), label) + }; + self.out.push(Func { + label: full, + hash: fnv1a(&canon.out), + tokens: canon.tokens, + params, + start: span.start, + end: span.end, + }); + label + } + } + + fn key_name(k: &PropertyKey) -> Option { + match k { + PropertyKey::StaticIdentifier(i) => Some(i.name.to_string()), + PropertyKey::StringLiteral(s) => Some(s.value.to_string()), + _ => None, + } + } + + impl<'a, 's> Visit<'a> for Funcs<'s> { + fn visit_function(&mut self, f: &Function<'a>, flags: oxc_semantic::ScopeFlags) { + let mut c = Canon::for_function(self.scoping, f.span, self.captures); + c.visit_function(f, flags); + // `function u(t)` says nothing; `exports.encodeServerErrorReceipt = + // u` says everything, and it is the same `u`. + let named = + f.id.as_ref() + .and_then(|id| id.symbol_id.get()) + .and_then(|s| self.exported.get(&s).cloned()); + let label = self.record(f.span, f.params.items.len(), named, &c); + self.path.push(label); + oxc_ast_visit::walk::walk_function(self, f, flags); + self.path.pop(); + } + + fn visit_arrow_function_expression(&mut self, f: &ArrowFunctionExpression<'a>) { + let mut c = Canon::for_function(self.scoping, f.span, self.captures); + c.visit_arrow_function_expression(f); + let label = self.record(f.span, f.params.items.len(), None, &c); + self.path.push(label); + oxc_ast_visit::walk::walk_arrow_function_expression(self, f); + self.path.pop(); + } + + /// `{parse: function(){…}}` — the key names the value. + fn visit_object_property(&mut self, p: &ObjectProperty<'a>) { + if matches!( + p.value, + Expression::FunctionExpression(_) | Expression::ArrowFunctionExpression(_) + ) { + self.hint = key_name(&p.key); + } + oxc_ast_visit::walk::walk_object_property(self, p); + } + + /// `l.assertTag = function(){…}` — the export surface, and the one name + /// in a minified module that means the same thing next release. + fn visit_assignment_expression(&mut self, e: &AssignmentExpression<'a>) { + if matches!( + e.right, + Expression::FunctionExpression(_) | Expression::ArrowFunctionExpression(_) + ) && let AssignmentTarget::StaticMemberExpression(m) = &e.left + { + self.hint = Some(m.property.name.to_string()); + } + oxc_ast_visit::walk::walk_assignment_expression(self, e); + } + + fn visit_method_definition(&mut self, m: &MethodDefinition<'a>) { + self.hint = key_name(&m.key); + oxc_ast_visit::walk::walk_method_definition(self, m); + } + + /// `var u = function(){…}`, where `u` is later published. Same trick as + /// a function declaration, different syntax for it. + fn visit_variable_declarator(&mut self, d: &VariableDeclarator<'a>) { + if matches!( + d.init, + Some(Expression::FunctionExpression(_)) + | Some(Expression::ArrowFunctionExpression(_)) + ) && let BindingPattern::BindingIdentifier(id) = &d.id + && let Some(name) = id.symbol_id.get().and_then(|s| self.exported.get(&s)) + { + self.hint = Some(name.clone()); + } + oxc_ast_visit::walk::walk_variable_declarator(self, d); + } + + fn visit_property_definition(&mut self, p: &PropertyDefinition<'a>) { + if matches!( + p.value, + Some(Expression::FunctionExpression(_)) + | Some(Expression::ArrowFunctionExpression(_)) + ) { + self.hint = key_name(&p.key); + } + oxc_ast_visit::walk::walk_property_definition(self, p); + } + } + + analyze(src, |program, semantic| { + let exported = export_names(program, semantic.scoping()); + let captures = capture_keys(semantic, &exported); + let mut v = Funcs { + scoping: semantic.scoping(), + captures: &captures, + exported: &exported, + out: Vec::new(), + path: Vec::new(), + hint: None, + anon: 0, + }; + v.visit_program(program); + v.out + }) + .unwrap_or_default() +} + +/// A function that exists in both revisions under one name, and differs. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct FnChange { + pub label: String, + pub before: Func, + pub after: Func, +} + +/// What actually happened to a module's functions between two revisions. +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct FnDiff { + /// Same name, different program. The thing you want to read. + pub changed: Vec, + pub added: Vec, + pub removed: Vec, + /// Functions whose shape is unchanged, however they moved or were renamed. + pub kept: usize, + pub total: usize, +} + +impl FnDiff { + /// Whether anything real happened here. + pub fn is_change(&self) -> bool { + !self.changed.is_empty() || !self.added.is_empty() || !self.removed.is_empty() + } +} + +/// Match a module's functions across two revisions, by shape and then by name. +/// +/// Shape first, as a multiset: a function that is the same program has not +/// changed no matter where it moved to or what its locals became, and moving is +/// most of what a minifier does. Only what is left over is matched by name, +/// which is why an unchanged function sharing a label with a changed one never +/// confuses the pairing. +pub fn diff_functions(before: &str, after: &str) -> FnDiff { + let old = functions(before); + let new = functions(after); + + let mut pool: HashMap<&str, Vec> = HashMap::new(); + for (i, f) in old.iter().enumerate() { + pool.entry(f.hash.as_str()).or_default().push(i); + } + + let mut old_used = vec![false; old.len()]; + let mut leftover_new: Vec<&Func> = Vec::new(); + let mut d = FnDiff { + total: new.len(), + ..Default::default() + }; + + for f in &new { + match pool.get_mut(f.hash.as_str()).and_then(|v| { + let slot = v + .iter() + .position(|&i| old[i].label == f.label) + .unwrap_or(v.len().saturating_sub(1)); + if v.is_empty() { + None + } else { + Some(v.remove(slot)) + } + }) { + Some(i) => { + old_used[i] = true; + d.kept += 1; + } + None => leftover_new.push(f), + } + } + + let mut leftover_old: HashMap<&str, Vec<&Func>> = HashMap::new(); + for (i, f) in old.iter().enumerate() { + if !old_used[i] { + leftover_old.entry(f.label.as_str()).or_default().push(f); + } + } + + let is_named = |f: &Func| !f.label.rsplit('/').next().unwrap_or("#").starts_with('#'); + + let mut unnamed_new: Vec<&Func> = Vec::new(); + for f in leftover_new { + // An anonymous label is a position, not a name: `#4` in one revision and + // `#4` in the next are each the fourth unnamed function, which two + // insertions earlier makes meaningless. Pairing on it directly would + // manufacture changes out of nothing. + if !is_named(f) { + unnamed_new.push(f); + continue; + } + match leftover_old.get_mut(f.label.as_str()).and_then(|v| v.pop()) { + Some(prev) => d.changed.push(FnChange { + label: f.label.clone(), + before: prev.clone(), + after: f.clone(), + }), + None => d.added.push(f.clone()), + } + } + + // What is left is unnamed on both sides. Reporting each as one addition + // *and* one removal doubles the count and reads as far more churn than + // happened — the module factory itself is unnamed, so every module that + // changed at all was showing up twice. Pair them on the two things a + // minifier leaves alone: which named function they sit inside, and how many + // parameters they take. That is not identity and cannot be; it is enough to + // stop one edit from being counted as two, and the label stays `#n` so + // nobody mistakes the pairing for a name. + let mut by_shape: HashMap<(&str, usize), Vec<&Func>> = HashMap::new(); + for rest in leftover_old.values() { + for f in rest.iter().filter(|f| !is_named(f)) { + let parent = f.label.rsplit_once('/').map(|(p, _)| p).unwrap_or(""); + by_shape.entry((parent, f.params)).or_default().push(f); + } + } + for f in unnamed_new { + let parent = f.label.rsplit_once('/').map(|(p, _)| p).unwrap_or(""); + match by_shape.get_mut(&(parent, f.params)).and_then(|v| v.pop()) { + Some(prev) => d.changed.push(FnChange { + label: f.label.clone(), + before: prev.clone(), + after: f.clone(), + }), + None => d.added.push(f.clone()), + } + } + for rest in leftover_old.values() { + for f in rest.iter().filter(|f| is_named(f)) { + d.removed.push((*f).clone()); + } + } + for rest in by_shape.into_values() { + for f in rest { + d.removed.push(f.clone()); + } + } + + narrow(&mut d, before, after); + d.total = d.kept + d.changed.len() + d.added.len(); + + // Biggest first: a 400-token function that changed is the release, a + // 6-token one is a constant someone bumped. + d.changed.sort_by_key(|c| std::cmp::Reverse(c.after.tokens)); + d.added.sort_by_key(|f| std::cmp::Reverse(f.tokens)); + d.removed.sort_by_key(|f| std::cmp::Reverse(f.tokens)); + d +} + +/// Keep only the innermost thing that changed. +/// +/// A function's shape includes its body, so editing one line inside a closure +/// marks that closure, the function around it, and the module factory around +/// that — every module reports its own factory as changed, on top of whatever +/// really happened. Reading a chain of ancestors tells you nothing the innermost +/// entry did not already say. +/// +/// Containment alone is not proof that an ancestor merely inherited its change, +/// though: the enclosing function can have an edit of its own beside the +/// nested one. So an ancestor is collapsed only when its *skeleton* — its +/// canonical stream with every changed descendant replaced by one placeholder, +/// and every added or removed descendant left out — is the same in both +/// revisions. An ancestor that changed independently stays in the report. +/// +/// Containment comes from spans rather than labels, because an unnamed ancestor +/// is deliberately stripped out of its descendants' labels and cannot be +/// recovered from them. The two directions are resolved separately: `changed` +/// and `added` carry offsets into the new revision, `removed` into the old, and +/// comparing across the two would be comparing coordinates from different files. +fn narrow(d: &mut FnDiff, before: &str, after: &str) { + let new_excise: Vec = d + .changed + .iter() + .map(|c| (c.after.start, c.after.end, true)) + .chain(d.added.iter().map(|f| (f.start, f.end, false))) + .map(|(start, end, placeholder)| Excised { + start, + end, + placeholder, + }) + .collect(); + let old_excise: Vec = d + .changed + .iter() + .map(|c| (c.before.start, c.before.end, true)) + .chain(d.removed.iter().map(|f| (f.start, f.end, false))) + .map(|(start, end, placeholder)| Excised { + start, + end, + placeholder, + }) + .collect(); + let contains = |s: u32, e: u32, spans: &[Excised]| { + spans + .iter() + .any(|x| (x.start, x.end) != (s, e) && s <= x.start && x.end <= e) + }; + let ancestors: Vec = (0..d.changed.len()) + .filter(|&i| { + let c = &d.changed[i]; + contains(c.after.start, c.after.end, &new_excise) + || contains(c.before.start, c.before.end, &old_excise) + }) + .collect(); + if !ancestors.is_empty() { + let old_roots: Vec<_> = ancestors + .iter() + .map(|&i| (d.changed[i].before.start, d.changed[i].before.end)) + .collect(); + let new_roots: Vec<_> = ancestors + .iter() + .map(|&i| (d.changed[i].after.start, d.changed[i].after.end)) + .collect(); + let old_skeletons = skeletons(before, &old_roots, &old_excise); + let new_skeletons = skeletons(after, &new_roots, &new_excise); + let explained: Vec = ancestors + .iter() + .enumerate() + .filter(|&(k, _)| old_skeletons[k].is_some() && old_skeletons[k] == new_skeletons[k]) + .map(|(_, &i)| i) + .collect(); + let previous = d.changed.len(); + let mut i = 0; + d.changed.retain(|_| { + i += 1; + !explained.contains(&(i - 1)) + }); + // Ancestors narrowed out of the report are structural overlap, not extra edits. + d.kept += previous - d.changed.len(); + } + // Whole additions/removals retain their outer API, not only its callbacks. + let added: Vec<_> = d.added.iter().map(|f| (f.start, f.end)).collect(); + d.added.retain(|f| { + !added + .iter() + .any(|&(a, b)| (a, b) != (f.start, f.end) && a <= f.start && f.end <= b) + }); + let removed: Vec<_> = d.removed.iter().map(|f| (f.start, f.end)).collect(); + d.removed.retain(|f| { + !removed + .iter() + .any(|&(a, b)| (a, b) != (f.start, f.end) && a <= f.start && f.end <= b) + }); +} + +/// The canonical stream of each function rooted at `roots`, with the nested +/// functions in `excise` left out or replaced by a placeholder. `None` for a +/// root that is not found or a module that does not parse. +fn skeletons(src: &str, roots: &[(u32, u32)], excise: &[Excised]) -> Vec> { + struct Find<'s> { + scoping: &'s Scoping, + captures: &'s HashMap, + roots: &'s [(u32, u32)], + excise: &'s [Excised], + out: Vec>, + } + impl<'s> Find<'s> { + fn slot(&self, span: Span) -> Option { + self.roots.iter().position(|&r| r == (span.start, span.end)) + } + fn canon(&self, span: Span) -> Canon<'s> { + Canon { + excise: self.excise, + ..Canon::for_function(self.scoping, span, self.captures) + } + } + } + impl<'a, 's> Visit<'a> for Find<'s> { + fn visit_function(&mut self, f: &Function<'a>, flags: oxc_semantic::ScopeFlags) { + if let Some(k) = self.slot(f.span) { + let mut c = self.canon(f.span); + c.visit_function(f, flags); + self.out[k] = Some(c.out); + } + oxc_ast_visit::walk::walk_function(self, f, flags); + } + fn visit_arrow_function_expression(&mut self, f: &ArrowFunctionExpression<'a>) { + if let Some(k) = self.slot(f.span) { + let mut c = self.canon(f.span); + c.visit_arrow_function_expression(f); + self.out[k] = Some(c.out); + } + oxc_ast_visit::walk::walk_arrow_function_expression(self, f); + } + } + analyze(src, |program, semantic| { + let exported = export_names(program, semantic.scoping()); + let captures = capture_keys(semantic, &exported); + let mut v = Find { + scoping: semantic.scoping(), + captures: &captures, + roots, + excise, + out: vec![None; roots.len()], + }; + v.visit_program(program); + v.out + }) + .unwrap_or_else(|| vec![None; roots.len()]) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The whole point of the label: it names the one function that changed, + /// while the minifier renames every local around it and moves the others. + #[test] + fn the_changed_function_is_named() { + let before = r#"var e = {}; + e.encode = function(t, n) { return t + n }; + e.decode = function(r) { return r * 2 }; + e.check = function(o) { return o > 4 };"#; + // Every local renamed, order shuffled, and `check` given a real edit. + let after = r#"var u = {}; + u.decode = function(a) { return a * 2 }; + u.check = function(s) { return s > 5 }; + u.encode = function(l, c) { return l + c };"#; + + let d = diff_functions(before, after); + assert_eq!(d.kept, 2, "encode and decode moved but did not change"); + assert_eq!(d.changed.len(), 1); + assert_eq!(d.changed[0].label, "check"); + assert!(d.added.is_empty() && d.removed.is_empty()); + } + + /// The real shape of a Metro module: functions declared under minified + /// locals, published at the bottom. The local is noise and the export name + /// is the contract, so the label has to come from the second, not the first. + #[test] + fn a_function_is_labelled_by_the_name_it_is_exported_under() { + let before = r#"__d("M", ["dep"], (function(t, n, r, o, a, i, l) { + function u(t) { return t + 1 } + function c(e) { return e * 2 } + l.encodeReceipt = u; + l.decodeReceipt = c; + }), 98)"#; + // Same module, every local renamed by the minifier, and `decodeReceipt` + // given a real edit. + let after = r#"__d("M", ["dep"], (function(e, s, d, f, g, h, j) { + function q(y) { return y + 1 } + function w(z) { return z * 3 } + j.encodeReceipt = q; + j.decodeReceipt = w; + }), 98)"#; + + let labels: Vec<_> = functions(before).into_iter().map(|f| f.label).collect(); + assert!(labels.contains(&"encodeReceipt".to_string()), "{labels:?}"); + + let d = diff_functions(before, after); + assert_eq!(d.changed.len(), 1, "one real edit: {:?}", d.changed); + assert_eq!(d.changed[0].label, "decodeReceipt"); + assert!( + d.added.is_empty() && d.removed.is_empty(), + "+{:?} -{:?}", + d.added, + d.removed + ); + } + + /// The module factory is itself unnamed. Reporting it as one addition and + /// one removal doubled the count for every module that changed at all. + #[test] + fn the_unnamed_module_factory_is_one_change_not_two() { + let before = r#"__d("M", ["dep"], (function(t, n, r, o, a, i, l) { l.x = 1 }), 98)"#; + let after = r#"__d("M", ["dep"], (function(t, n, r, o, a, i, l) { l.x = 2 }), 98)"#; + let d = diff_functions(before, after); + assert_eq!(d.changed.len(), 1, "{d:?}"); + assert!(d.added.is_empty() && d.removed.is_empty(), "{d:?}"); + } + + /// A closure inside a named function inherits the path, so a change deep + /// inside `encode` is still reported as being in `encode`. + #[test] + fn nested_closures_carry_the_path() { + let src = + r#"var e = {}; e.encode = function(t) { return t.map(function(n) { return n + 1 }) };"#; + let labels: Vec<_> = functions(src).into_iter().map(|f| f.label).collect(); + assert!(labels.contains(&"encode".to_string()), "{labels:?}"); + assert!( + labels.iter().any(|l| l.starts_with("encode/")), + "{labels:?}" + ); + } + + /// Anonymous functions are numbered by position, and a position is not a + /// name. Pairing on it would turn one insertion into a wall of changes. + #[test] + fn anonymous_functions_are_never_paired_by_position() { + let before = r#"f(function(a) { return a }, function(b) { return b * 2 });"#; + // One function inserted at the front: every ordinal after it shifts. + let after = r#"f(function(z) { return z + 9 }, function(a) { return a }, function(b) { return b * 2 });"#; + let d = diff_functions(before, after); + assert_eq!(d.kept, 2, "the two originals are unchanged"); + assert_eq!(d.changed.len(), 0, "an ordinal shift is not a change"); + assert_eq!(d.added.len(), 1); + } + + /// A minifier inserting one binding renames everything after it. This is the + /// exact cascade seen in `WAWebWid` between two real revisions. + #[test] + fn a_rename_cascade_is_not_a_change() { + let before = r#"var h = "a", y = /x/, C = 99, b = 4; function f(){ return C + b }"#; + let after = r#"var y = "a", C = /x/, b = 99, v = 4; function g(){ return b + v }"#; + assert_eq!(compare(before, after), Verdict::RenamedOnly); + } + + /// The same cascade, plus one real edit buried in it. + #[test] + fn a_change_hiding_inside_a_rename_is_still_found() { + let before = r#"var h = "a", y = /x/, C = 99, b = 4; function f(){ return C + b }"#; + let after = r#"var y = "a", C = /x/, b = 99, v = 5; function g(){ return b + v }"#; + assert_eq!(compare(before, after), Verdict::Changed); + } + + /// Every category of edit that must survive normalization. + #[test] + fn real_edits_are_all_detected() { + let base = r#"function f(a, b) { if (a > b) { return g(a, "pin"); } return 0 }"#; + for (what, edited) in [ + ( + "a literal", + r#"function f(a, b) { if (a > b) { return g(a, "pin_v1"); } return 0 }"#, + ), + ( + "an operator", + r#"function f(a, b) { if (a >= b) { return g(a, "pin"); } return 0 }"#, + ), + ( + "an argument", + r#"function f(a, b) { if (a > b) { return g(a); } return 0 }"#, + ), + ( + "a callee", + r#"function f(a, b) { if (a > b) { return h(a, "pin"); } return 0 }"#, + ), + ( + "a statement", + r#"function f(a, b) { if (a > b) { return g(a, "pin"); } }"#, + ), + ( + "a property", + r#"function f(a, b) { if (a > b) { return g.x(a, "pin"); } return 0 }"#, + ), + ( + "order", + r#"function f(a, b) { if (a > b) { return g("pin", a); } return 0 }"#, + ), + ] { + assert_eq!(compare(base, edited), Verdict::Changed, "missed: {what}"); + } + } + + /// Renaming a *parameter* is still only a rename. + #[test] + fn parameters_and_nested_scopes_normalize() { + let before = r#"function f(e, t) { return function (n) { return e + t + n } }"#; + let after = r#"function q(a, b) { return function (c) { return a + b + c } }"#; + assert_eq!(compare(before, after), Verdict::RenamedOnly); + } + + /// A global is not ours to rename, so a different one is a different program. + #[test] + fn free_identifiers_are_never_normalized() { + let before = r#"function f() { return Promise.resolve(1) }"#; + let after = r#"function f() { return Symbol.resolve(1) }"#; + assert_eq!(compare(before, after), Verdict::Changed); + } + + /// `!0`/`!1` are how a minifier spells booleans, and it changes its mind. + #[test] + fn boolean_encodings_fold() { + assert_eq!( + compare("var a = !0, b = !1;", "var a = true, b = false;"), + Verdict::RenamedOnly + ); + // But a flipped boolean is a real change. + assert_eq!(compare("var a = !0;", "var a = !1;"), Verdict::Changed); + } +} + +/// Prove the classifier on real code, rather than on examples chosen to pass. +/// +/// Hand-written cases test what the author thought of. This takes modules out of +/// the actual bundle and mutates them two ways, where the right answer is known +/// by construction: +/// +/// * **renames** — every local given a fresh name. The program is identical, so +/// anything but `RenamedOnly` is a false alarm: noise the engine failed to +/// absorb, which is the failure that makes a release unreviewable. +/// * **edits** — one literal, operator, or argument altered. The program is +/// different, so anything but `Changed` is a miss: a real change reported as +/// noise, which is the failure that ships a bug. +/// +/// The two error rates are not symmetric in cost and are returned separately. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FuzzReport { + /// Renamed mutants correctly absorbed. + pub renames_absorbed: u32, + /// Renamed mutants reported as changes. Noise the engine failed to absorb, + /// which is the failure that makes a release unreviewable. + pub false_alarms: u32, + /// Edited mutants correctly caught. + pub edits_caught: u32, + /// Edited mutants reported as noise. The failure that ships a bug. + pub misses: u32, + /// Mutations that found nothing to change in a module and were skipped. + pub skipped: u32, + pub modules: u32, +} + +/// The two error rates are not symmetric in cost and are returned separately. +pub fn fuzz(src: &dyn SourceLoader, entries: &[ModuleEntry], limit: usize) -> FuzzReport { + use regex::Regex; + let bundle = src; + + // Rename every binding, using the symbol table. + // + // The first two attempts did this textually and both were wrong in the same + // way: `\b[a-z]\b` cannot tell a binding from a property key or a character + // inside a regex, so it produced mutants that really *were* different + // programs and then blamed the classifier for saying so. Distinguishing + // those two things is the entire problem — a harness that cannot do it is + // not measuring the engine, it is measuring itself. + // + // `rename_locals` renames exactly the identifiers that resolve to a symbol + // declared in the module, so the result is α-equivalent by construction and + // any verdict other than `RenamedOnly` is a genuine miss. + type Mutation = (&'static str, fn(&str) -> Option); + let edits: [Mutation; 3] = [ + ("literal", |s| { + let re = Regex::new(r#""([A-Za-z_][A-Za-z0-9_]{3,})""#).ok()?; + let c = re.captures(s)?; + Some(s.replacen(&format!("\"{}\"", &c[1]), &format!("\"{}X\"", &c[1]), 1)) + }), + ("operator", |s| { + s.contains(" === ").then(|| s.replacen(" === ", " !== ", 1)) + }), + ("number", |s| { + let re = Regex::new(r"\b([1-9][0-9]{1,4})\b").ok()?; + let c = re.captures(s)?; + let n: u64 = c[1].parse().ok()?; + Some(s.replacen(&c[1], &(n + 1).to_string(), 1)) + }), + ]; + + let (mut rn_ok, mut rn_bad) = (0u32, 0u32); + let (mut ed_ok, mut ed_bad, mut ed_skip) = (0u32, 0u32, 0u32); + + let mut modules = 0u32; + for e in entries.iter().take(limit) { + let Ok(src) = bundle.load(e) else { continue }; + modules += 1; + if shape(&src).is_none() { + continue; + } + + if let Some(renamed) = rename_locals(&src) { + match compare(&src, &renamed) { + Verdict::RenamedOnly | Verdict::Same => rn_ok += 1, + _ => rn_bad += 1, + } + } + + for (_, edit) in &edits { + match edit(&src) { + Some(m) if m != src => match compare(&src, &m) { + Verdict::Changed => ed_ok += 1, + _ => ed_bad += 1, + }, + _ => ed_skip += 1, + } + } + } + + FuzzReport { + renames_absorbed: rn_ok, + false_alarms: rn_bad, + edits_caught: ed_ok, + misses: ed_bad, + skipped: ed_skip, + modules, + } +} + +/// An α-rename of every module-local binding, done through the symbol table. +/// +/// Every identifier that resolves to a symbol declared in this module becomes +/// `q`; everything else — property names, globals, string and regex bodies — +/// is left exactly as it was. The result is a different text and the same +/// program, by construction rather than by care. +/// +/// Public because it is the only honest way to generate a known-rename: doing it +/// textually cannot tell `obj.a` from `var a`. +pub fn rename_locals(src: &str) -> Option { + let alloc = Allocator::default(); + let parsed = Parser::new(&alloc, src, SourceType::cjs()).parse(); + if parsed.panicked || !parsed.diagnostics.is_empty() { + return None; + } + let semantic = SemanticBuilder::new() + .with_build_nodes(true) + .build(&parsed.program) + .semantic; + + struct Spans<'s> { + scoping: &'s Scoping, + hits: Vec<(u32, u32, SymbolId)>, + } + impl<'a, 's> Visit<'a> for Spans<'s> { + fn enter_node(&mut self, kind: AstKind<'a>) { + match kind { + AstKind::BindingIdentifier(b) => { + if let Some(id) = b.symbol_id.get() { + self.hits.push((b.span.start, b.span.end, id)); + } + } + AstKind::IdentifierReference(r) => { + if let Some(id) = r + .reference_id + .get() + .and_then(|i| self.scoping.get_reference(i).symbol_id()) + { + self.hits.push((r.span.start, r.span.end, id)); + } + } + _ => {} + } + } + } + + let mut v = Spans { + scoping: semantic.scoping(), + hits: Vec::new(), + }; + v.visit_program(&parsed.program); + struct Shorthand { + spans: Vec, + } + impl<'a> Visit<'a> for Shorthand { + fn visit_object_property(&mut self, p: &ObjectProperty<'a>) { + if p.shorthand { + self.spans.push(p.span); + } + oxc_ast_visit::walk::walk_object_property(self, p); + } + fn visit_binding_property(&mut self, p: &BindingProperty<'a>) { + if p.shorthand { + self.spans.push(p.span); + } + oxc_ast_visit::walk::walk_binding_property(self, p); + } + } + let mut short = Shorthand { spans: vec![] }; + short.visit_program(&parsed.program); + let excluded: std::collections::HashSet<_> = v + .hits + .iter() + .filter(|(a, b, _)| short.spans.iter().any(|s| s.start <= *a && *b <= s.end)) + .map(|(_, _, id)| *id) + .collect(); + v.hits.retain(|(_, _, id)| !excluded.contains(id)); + if v.hits.is_empty() { + return None; + } + + // Spliced back to front so earlier offsets stay valid. + v.hits.sort_by_key(|(start, ..)| std::cmp::Reverse(*start)); + let mut out = src.to_string(); + for (start, end, id) in v.hits { + out.replace_range(start as usize..end as usize, &format!("q{}", id.index())); + } + Some(out) +} + +#[cfg(test)] +mod regression_tests { + use super::*; + #[test] + fn behavior_fields_are_not_renames() { + for (a, b) in [ + ("var a=1", "const a=1"), + ("function a(){return 1}", "async function a(){return 1}"), + ("function a(){return 1}", "function* a(){return 1}"), + ("var a=`old`", "var a=`new`"), + ("a.b()", "a?.b()"), + ("var a={get x(){return 1}}", "var a={x(){return 1}}"), + ("var [,a]=x", "var [a]=x"), + ("var a;[,a]=x", "var a;[a]=x"), + ("for(a;;);", "for(;a;);"), + ("for(;a;);", "for(;;a);"), + ] { + assert_eq!(compare(a, b), Verdict::Changed, "{a} -> {b}"); + } + } + #[test] + fn captures_and_duplicate_shapes_keep_the_edited_export() { + let a = "__d('M',[],function(g,r,d,l,m,e){var a=r('A'),b=r('B');function x(){return a()}function y(){return a()}e.A=x;e.B=y})"; + let b = a.replace("function y(){return a()}", "function y(){return b()}"); + let d = diff_functions(a, &b); + assert_eq!(d.changed.len(), 1); + assert_eq!(d.changed[0].label, "B"); + assert!(d.added.is_empty() && d.removed.is_empty()); + assert_eq!(d.total - d.kept, 1); + } + /// Inserting a binding earlier in the module shifts every later symbol + /// index. A function capturing a later binding is still the same program. + #[test] + fn inserting_a_binding_before_a_capture_does_not_change_its_consumers() { + for (a, b) in [ + ( + "__d('M',[],function(g,r,d,l,m,e){var a=1;e.foo=function(){return a}})", + "__d('M',[],function(g,r,d,l,m,e){var z=2;var a=1;e.foo=function(){return a}})", + ), + ( + "__d('M',[],function(g,r,d,l,m,e){var a=r('A'),b=r('B');function x(){return b()}e.X=x})", + "__d('M',[],function(g,r,d,l,m,e){var z=1;var a=r('A'),b=r('B');function x(){return b()}e.X=x})", + ), + ] { + let d = diff_functions(a, b); + let labels: Vec<_> = d.changed.iter().map(|c| c.label.as_str()).collect(); + // The factory itself gained a statement; nothing inside it changed. + assert_eq!(labels.len(), 1, "{labels:?}"); + assert!(labels[0].starts_with('#'), "{labels:?}"); + assert!(d.added.is_empty() && d.removed.is_empty(), "{d:?}"); + } + } + /// Captured bindings with the same description are still told apart. + #[test] + fn captures_of_distinct_local_functions_are_distinct() { + let a = "__d('M',[],function(g,r,d,l,m,e){function p(){}function q(){}e.A=function(){return p()}})"; + let d = diff_functions(a, &a.replace("return p()", "return q()")); + let labels: Vec<_> = d.changed.iter().map(|c| c.label.as_str()).collect(); + assert_eq!(labels, ["A"]); + } + /// An enclosing function with an edit of its own stays in the report; one + /// whose only change is the nested edit collapses into it. + #[test] + fn ancestors_with_independent_edits_are_kept() { + let a = "var e={};e.outer=function(){let f=()=>1;return 2}"; + let both = diff_functions(a, "var e={};e.outer=function(){let f=()=>3;return 4}"); + let mut labels: Vec<_> = both.changed.iter().map(|c| c.label.as_str()).collect(); + labels.sort(); + assert_eq!(labels, ["outer", "outer/#1"]); + assert_eq!(both.total - both.kept, 2); + let inner = diff_functions(a, "var e={};e.outer=function(){let f=()=>3;return 2}"); + let labels: Vec<_> = inner.changed.iter().map(|c| c.label.as_str()).collect(); + assert_eq!(labels, ["outer/#1"]); + assert_eq!(inner.total - inner.kept, 1); + } + #[test] + fn module_exports_members_name_functions() { + let a = "__d('M',[],function(g,r,i,a,m,e){function f(){return 1}m.exports.foo=f})"; + let d = diff_functions(a, &a.replace("return 1", "return 2")); + let labels: Vec<_> = d.changed.iter().map(|c| c.label.as_str()).collect(); + assert_eq!(labels, ["foo"]); + } + #[test] + fn injected_parameter_is_not_exports() { + let a = "__d('M',['fbt'],function(g,r,d,l,m,e,fbt){function x(){return 1}m.exports={};e.real=x;})"; + let d = diff_functions(a, &a.replace("return 1", "return 2")); + assert_eq!(d.changed[0].label, "real"); + } + #[test] + fn outer_additions_and_nested_deletions_are_localized() { + let d = diff_functions( + "", + "var e={};e.encode=function(){return [1].map(function(x){return x})}", + ); + assert_eq!(d.added.len(), 1); + assert_eq!(d.added[0].label, "encode"); + let a = "var e={};e.encode=function(){function inner(){return 1}return 2}"; + let b = "var e={};e.encode=function(){return 2}"; + let d = diff_functions(a, b); + assert!(d.changed.is_empty()); + assert_eq!(d.removed.len(), 1); + } + #[test] + fn parser_diagnostics_and_undefined_are_honest() { + assert_eq!(compare("var a=;", "var b=;"), Verdict::Unknown); + assert_eq!( + compare("var a=void 0", "var b=undefined"), + Verdict::RenamedOnly + ); + assert_eq!( + compare( + "function f(undefined){return void 0}", + "function f(undefined){return undefined}" + ), + Verdict::Changed + ); + } + #[test] + fn fuzz_does_not_rename_shorthand_keys() { + let a = "var a=1,b=2;use({a},b)"; + let b = rename_locals(a).unwrap(); + assert!(b.contains("{a}")); + assert_eq!(compare(a, &b), Verdict::RenamedOnly); + let a = "var {x}=obj;use(x)"; + assert!(rename_locals(a).is_none()); + } +} diff --git a/crates/cellar-index/Cargo.toml b/crates/cellar-index/Cargo.toml index 50ca467..9d32fa5 100644 --- a/crates/cellar-index/Cargo.toml +++ b/crates/cellar-index/Cargo.toml @@ -24,5 +24,6 @@ oxc_ast_visit = { workspace = true } oxc_codegen = { workspace = true } oxc_parser = { workspace = true } oxc_span = { workspace = true } +oxc_semantic = { workspace = true } rayon = "1.12" sha2 = { version = "0.11", default-features = false } diff --git a/crates/cellar-index/examples/probe.rs b/crates/cellar-index/examples/probe.rs new file mode 100644 index 0000000..d51612d --- /dev/null +++ b/crates/cellar-index/examples/probe.rs @@ -0,0 +1,44 @@ +//! Run the module extractor over real bundle files and report coverage. +//! +//! Exists because the unit fixtures were all hand-written and all wrong in the +//! same two ways — unparenthesized factories, stock Metro's parameter order — +//! so they passed while every real module returned nothing. A synthetic test +//! proves the code does what its author expected; this proves it does what the +//! bundle needs. + +use std::env; +use std::fs; + +fn main() { + let (mut total, mut with_exports, mut with_functions) = (0usize, 0usize, 0usize); + + for path in env::args().skip(1) { + let Ok(src) = fs::read_to_string(&path) else { + continue; + }; + let alloc = oxc_allocator::Allocator::default(); + let parsed = oxc_parser::Parser::new(&alloc, &src, oxc_span::SourceType::cjs()).parse(); + let Some(facts) = cellar_index::extract::facts_of_module(&parsed.program) else { + continue; + }; + total += 1; + if !facts.exports.is_empty() { + with_exports += 1; + } + if !facts.functions.is_empty() { + with_functions += 1; + } + } + + let pct = |n: usize| { + n.checked_mul(100) + .and_then(|x| x.checked_div(total)) + .unwrap_or(0) + }; + println!("{total} modules"); + println!(" with exports: {with_exports} ({}%)", pct(with_exports)); + println!( + " with functions: {with_functions} ({}%)", + pct(with_functions) + ); +} diff --git a/crates/cellar-index/src/extract.rs b/crates/cellar-index/src/extract.rs index 7e0aea2..db86e90 100644 --- a/crates/cellar-index/src/extract.rs +++ b/crates/cellar-index/src/extract.rs @@ -4,23 +4,21 @@ //! minified, so identifiers are single letters and the same three characters mean //! different things in different modules: a regex for `e\.(\w+)\s*=` finds export //! assignments in one module and unrelated local writes in the next. Resolving the -//! factory's *parameters* instead — Metro calls the factory as -//! `factory(global, require, importDefault, importAll, module, exports, dependencyMap)` -//! — makes "assignment onto exports" a question about a binding, and it stays -//! correct whatever the minifier named it. +//! factory's *parameters* instead makes "assignment onto exports" a question +//! about a binding, and it stays correct whatever the minifier named it — as +//! long as the right parameter is picked, which is not stock Metro's. See +//! [`cellar_core::factory::module_and_exports`], shared with the shape engine. +use cellar_core::factory; use oxc_ast::ast::{ Argument, AssignmentTarget, BindingPattern, Expression, Function, Program, Statement, }; use oxc_ast_visit::{Visit, walk}; +use oxc_semantic::{Scoping, SemanticBuilder, SymbolId}; /// The Metro module-definition function. pub const DEFINE_FN: &str = "__d"; -/// Positional meaning of the factory's parameters, per Metro's calling convention. -const MODULE_PARAM: usize = 4; -const EXPORTS_PARAM: usize = 5; - /// What one module declares. #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct ModuleFacts { @@ -38,6 +36,8 @@ pub struct ModuleFacts { /// Returns `None` when the program is not a module definition, which is a real /// case (bundle bootstrap code) and not an error. pub fn facts_of_module(program: &Program<'_>) -> Option { + let semantic = SemanticBuilder::new().build(program).semantic; + let scoping = semantic.scoping(); for stmt in &program.body { let Statement::ExpressionStatement(es) = stmt else { continue; @@ -70,7 +70,7 @@ pub fn facts_of_module(program: &Program<'_>) -> Option { } if let Some(factory) = factory_function(&call.arguments[2]) { - let mut scan = FactoryScan::new(factory); + let mut scan = FactoryScan::new(factory, scoping); if let Some(body) = &factory.body { scan.visit_function_body(body); scan.collect_top_level(body); @@ -91,35 +91,42 @@ fn sorted_unique(mut v: Vec) -> Vec { } fn factory_function<'b, 'a>(arg: &'b Argument<'a>) -> Option<&'b Function<'a>> { - match arg.as_expression()? { + // `get_inner_expression`, not a bare match: the bundle writes the factory + // parenthesized — `__d("M", [], (function(…){…}), 98)` — and oxc preserves + // parentheses in the AST, so matching `FunctionExpression` directly saw a + // `ParenthesizedExpression` and gave up. Name and deps come from arguments + // 0 and 1 and were unaffected, so every module still indexed and looked + // fine; `exports` and `functions` were simply empty for all 187,892 of + // them, which reads exactly like "this module exports nothing". + match arg.as_expression()?.get_inner_expression() { Expression::FunctionExpression(f) => Some(f), _ => None, } } -/// Name of a simple identifier parameter at `position`, if it is one. -fn param_name<'a>(factory: &Function<'a>, position: usize) -> Option<&'a str> { - let param = factory.params.items.get(position)?; - match ¶m.pattern { - BindingPattern::BindingIdentifier(id) => Some(id.name.as_str()), - _ => None, - } +fn symbol(scoping: &Scoping, id: &oxc_ast::ast::IdentifierReference<'_>) -> Option { + id.reference_id + .get() + .and_then(|id| scoping.get_reference(id).symbol_id()) } -struct FactoryScan<'a> { +struct FactoryScan<'s> { + scoping: &'s Scoping, /// The binding the factory receives `exports` as, e.g. `e`. - exports_binding: Option<&'a str>, + exports_binding: Option, /// The binding the factory receives `module` as, e.g. `m`. - module_binding: Option<&'a str>, + module_binding: Option, exports: Vec, functions: Vec, } -impl<'a> FactoryScan<'a> { - fn new(factory: &Function<'a>) -> Self { +impl<'s> FactoryScan<'s> { + fn new(factory: &Function<'_>, scoping: &'s Scoping) -> Self { + let bindings = factory::module_and_exports(factory, scoping); Self { - exports_binding: param_name(factory, EXPORTS_PARAM), - module_binding: param_name(factory, MODULE_PARAM), + scoping, + exports_binding: bindings.exports, + module_binding: bindings.module, exports: Vec::new(), functions: Vec::new(), } @@ -127,7 +134,7 @@ impl<'a> FactoryScan<'a> { /// Top-level declarations of the factory body — the module's own functions, /// as opposed to every closure nested anywhere inside it. - fn collect_top_level(&mut self, body: &oxc_ast::ast::FunctionBody<'a>) { + fn collect_top_level(&mut self, body: &oxc_ast::ast::FunctionBody<'_>) { for stmt in &body.statements { match stmt { Statement::FunctionDeclaration(f) => { @@ -163,7 +170,7 @@ impl<'a> FactoryScan<'a> { /// `.name = …` → `name`; `.exports = …` → `default`; /// `.exports.name = …` → `name`. - fn record_export_target(&mut self, target: &AssignmentTarget<'a>) { + fn record_export_target(&mut self, target: &AssignmentTarget<'_>) { let Some(member) = target.as_member_expression() else { return; }; @@ -173,7 +180,8 @@ impl<'a> FactoryScan<'a> { // `.prop = …` if let Expression::Identifier(obj) = member.object() - && Some(obj.name.as_str()) == self.exports_binding + && symbol(self.scoping, obj).is_some() + && symbol(self.scoping, obj) == self.exports_binding { self.exports.push(prop.to_string()); return; @@ -181,7 +189,8 @@ impl<'a> FactoryScan<'a> { // `.exports = …` if let Expression::Identifier(obj) = member.object() - && Some(obj.name.as_str()) == self.module_binding + && symbol(self.scoping, obj).is_some() + && symbol(self.scoping, obj) == self.module_binding && prop == "exports" { self.exports.push("default".to_string()); @@ -192,14 +201,15 @@ impl<'a> FactoryScan<'a> { if let Some(inner) = member.object().as_member_expression() && inner.static_property_name() == Some("exports") && let Expression::Identifier(obj) = inner.object() - && Some(obj.name.as_str()) == self.module_binding + && symbol(self.scoping, obj).is_some() + && symbol(self.scoping, obj) == self.module_binding { self.exports.push(prop.to_string()); } } } -impl<'a> Visit<'a> for FactoryScan<'a> { +impl<'a> Visit<'a> for FactoryScan<'_> { fn visit_assignment_expression(&mut self, expr: &oxc_ast::ast::AssignmentExpression<'a>) { self.record_export_target(&expr.left); walk::walk_assignment_expression(self, expr); @@ -219,6 +229,27 @@ mod tests { facts_of_module(&ret.program).expect("a module definition") } + #[test] + fn block_shadow_and_captured_exports_are_distinct() { + let f = facts( + "__d('M',[],function(g,r,i,n,l,m,e){ {let e={};e.tmp=1} function f(){e.real=1} })", + ); + assert_eq!(f.exports, vec!["real"]); + } + #[test] + fn nested_parameter_shadow_does_not_select_exports() { + let f = facts("__d('M',[],function(g,r,i,n,l,m,e){function f(e){e.tmp=1}m.real=1})"); + assert_eq!(f.exports, vec!["real"]); + } + /// A property written onto `global` or `require` is not an export, even + /// when the module writes to nothing else. + #[test] + fn writes_to_loader_parameters_are_not_exports() { + let f = facts("__d('M',[],function(t,n,r,o,a,i,l){t.__DEV__=1;n.cache=2})"); + assert!(f.exports.is_empty(), "{:?}", f.exports); + let f = facts("__d('M',[],function(t,n,r,o,a,i){t.exports=1})"); + assert!(f.exports.is_empty(), "{:?}", f.exports); + } #[test] fn reads_name_and_positional_deps() { let f = facts(r#"__d("WAWebFoo",["A","B"],function(g,r,i,a,m,e,d){},1);"#); @@ -240,6 +271,64 @@ mod tests { assert_eq!(f.exports, ["parse", "sendStanza"]); } + /// The shape the bundle actually ships, which no fixture here had. + /// + /// Two things differ from a hand-written example and each was enough on its + /// own to return nothing: the factory is *parenthesized*, and `exports` is + /// the seventh parameter rather than Metro's fifth. Every one of the 187,892 + /// modules in a real bundle indexed with an empty `exports` list, which + /// reads exactly like "this module exports nothing". + #[test] + fn the_real_bundle_shape_is_read() { + let f = facts( + r#"__d("WAWebCryptoMediaRetry", ["WABinary"], (function(t, n, r, o, a, i, l) { + function u(t) { return t; } + l.encryptServerErrorReceipt = u, l.ENC_IV_SIZE = 12; + }), 98);"#, + ); + assert_eq!(f.name, "WAWebCryptoMediaRetry"); + assert_eq!(f.exports, ["ENC_IV_SIZE", "encryptServerErrorReceipt"]); + assert_eq!(f.functions, ["u"], "the factory body was reached at all"); + } + + /// A `.react` module: the loader injects dependencies *after* `exports`, so + /// the last parameter is `fbt` and taking it as exports finds nothing. About + /// a fifth of the bundle looks like this. + #[test] + fn injected_dependencies_after_exports_do_not_displace_it() { + let f = facts( + r#"__d("M.react", [], (function(t, n, r, o, a, i, l, s) { + l.Component = function() { return s.render(); }; + }), 98);"#, + ); + assert_eq!(f.exports, ["Component"]); + } + + /// Seven parameters, but the *six*-parameter convention plus one injected + /// dependency — which arity alone cannot distinguish from the seven-parameter + /// convention. `module.exports` settles it, and `exports` follows `module`. + /// Taken from `GeminiEmojiCategories`. + #[test] + fn six_parameters_plus_an_injected_dependency_is_not_the_long_convention() { + let f = facts( + r#"__d("M", ["fbt"], (function(t, n, r, o, a, i, l) { + a.exports = { name: l._("") }; + i.helper = 1; + }), 98);"#, + ); + // `a` is module, so `i` is exports — not `l`, which is the injected fbt. + assert_eq!(f.exports, ["default", "helper"]); + } + + /// The other convention: six parameters, `exports` fifth. Both shapes appear + /// in one bundle, so neither may be assumed. + #[test] + fn the_six_parameter_convention_is_read_too() { + let f = + facts(r#"__d("M", [], (function(t, n, r, o, a, i) { i.CollectionName = 1; }), 98);"#); + assert_eq!(f.exports, ["CollectionName"]); + } + #[test] fn module_exports_forms_are_recognised() { let f = facts(r#"__d("M",[],function(g,r,i,a,m,e,d){ m.exports = {}; },1);"#); diff --git a/crates/cellar/src/main.rs b/crates/cellar/src/main.rs index d8dc798..707c435 100644 --- a/crates/cellar/src/main.rs +++ b/crates/cellar/src/main.rs @@ -286,6 +286,14 @@ struct DiffArgs { /// Include modules whose only changes are transpiler churn. #[arg(long)] include_noise: bool, + /// Tell a rename from a real change, and name the functions that changed. + /// + /// Compares each changed module with its local names erased. A minifier + /// names by position, so inserting one binding renames every local after it + /// — the dominant form of noise in a bundle, and one no line-level rule can + /// recognise, because renamed code looks exactly like code. + #[arg(long)] + semantic: bool, /// Context lines in each hunk. #[arg(long, short = 'C', default_value_t = 3)] context: usize, @@ -565,6 +573,7 @@ fn diff(ctx: &Ctx, args: DiffArgs) -> Result<()> { include_hunks: !args.no_hunks, context_lines: args.context, include_noise_only: args.include_noise, + semantic: args.semantic, ..DiffOptions::default() }; let value = ctx.diff(&old, &new, Some(&args.filter), &opts, args.limit)?; diff --git a/crates/cellar/src/mcp.rs b/crates/cellar/src/mcp.rs index 123ae57..52692a5 100644 --- a/crates/cellar/src/mcp.rs +++ b/crates/cellar/src/mcp.rs @@ -401,7 +401,13 @@ fn tools() -> Vec { modified, with dependency and export deltas and unified diffs. Changes whose \ every altered line is transpiler churn are counted as `noiseOnly` in the summary \ and withheld unless `includeNoise` is set. Start with `includeHunks: false` for \ - an overview, then re-run for the modules you care about.", + an overview, then re-run for the modules you care about. Set `semantic: true` \ + to tell a rename from a real change: each changed module is compared with its \ + local names erased, giving a `verdict` of `renamedOnly` or `changed` and, for \ + the latter, the names of the functions that actually changed. In a minified \ + bundle most byte-level diffs are rename cascades, which no line-level rule can \ + recognise, so this is usually the difference between thousands of changed \ + modules and a list worth reading.", json!({ "type": "object", "properties": { @@ -411,6 +417,7 @@ fn tools() -> Vec { "includeHunks": { "type": "boolean", "default": true }, "contextLines": { "type": "integer", "default": 3 }, "includeNoise": { "type": "boolean", "default": false }, + "semantic": { "type": "boolean", "default": false, "description": "Classify each change as renamedOnly or changed by comparing programs with local names erased, and name the changed functions. Parses both sides of every changed module." }, "limit": { "type": "integer", "description": "Cap the change list; the summary still reports true totals." } }, "required": ["old", "new"], @@ -639,6 +646,7 @@ fn call_tool(ctx: &Ctx, name: &str, args: &Value) -> Result { include_hunks: arg_bool(args, "includeHunks", true), context_lines: arg_usize(args, "contextLines", 3), include_noise_only: arg_bool(args, "includeNoise", false), + semantic: arg_bool(args, "semantic", false), ..DiffOptions::default() }; ctx.diff( diff --git a/crates/cellar/src/render.rs b/crates/cellar/src/render.rs index 078152c..6b4764d 100644 --- a/crates/cellar/src/render.rs +++ b/crates/cellar/src/render.rs @@ -315,6 +315,38 @@ pub fn diff_text(v: &Value) { n(c, "linesRemoved") ); } + // The line counts above measure the text; this measures the program. + // Printed second so the two read as claim and correction where they + // disagree, which on a minified bundle is most of the time. + if let Some(verdict) = c.get("verdict").and_then(Value::as_str) { + match verdict { + "renamedOnly" => { + println!(" renamed only — the text differs, the program does not") + } + "unknown" => { + println!(" unparseable, so no verdict is offered rather than guessed") + } + _ => { + if let Some(total) = c.get("functionsTotal").and_then(Value::as_u64) { + let kept = c.get("functionsKept").and_then(Value::as_u64).unwrap_or(0); + println!( + " {} of {total} localized functions changed", + total.saturating_sub(kept) + ); + } + for (label, key) in [ + ("changed", "functionsChanged"), + ("added", "functionsAdded"), + ("removed", "functionsRemoved"), + ] { + let fns = arr(c, key); + if !fns.is_empty() { + println!(" {label}: {}", join(fns, 8)); + } + } + } + } + } if let Some(path) = c.get("newFile").and_then(Value::as_str) { println!(" new: {}/{}", s(v, "newDir"), path); } @@ -377,6 +409,25 @@ pub fn diff_markdown(v: &Value) { if !arr(c, "exportsAdded").is_empty() { println!("- exports added: {}", join(arr(c, "exportsAdded"), 20)); } + if let Some(verdict) = c.get("verdict").and_then(Value::as_str) { + println!("- semantic verdict: `{verdict}`"); + if let Some(total) = c.get("functionsTotal").and_then(Value::as_u64) { + let kept = c.get("functionsKept").and_then(Value::as_u64).unwrap_or(0); + println!( + "- {} of {total} localized functions changed", + total.saturating_sub(kept) + ); + } + for (label, key) in [ + ("changed", "functionsChanged"), + ("added", "functionsAdded"), + ("removed", "functionsRemoved"), + ] { + if !arr(c, key).is_empty() { + println!("- {label}: {}", join(arr(c, key), 20)); + } + } + } if let Some(h) = c.get("hunks").and_then(Value::as_str) { println!("\n```diff\n{h}\n```"); }