From 3aa21ac958bc3ec507ad6b5594287301b98a6339 Mon Sep 17 00:00:00 2001 From: NianJiuZst <3235467914@qq.com> Date: Sun, 1 Nov 2026 09:11:35 -0600 Subject: [PATCH 1/2] feat: port upstream overload, framework and dynamic-dispatch resolution Close the gaps against upstream CodeGraph in three areas. Types and overloads: - Calls record their argument shapes (Java, Kotlin, TS/JS) and pick the overload whose arity and argument types fit, instead of the first declared. - Java/Kotlin calls on a field receiver (`repo.find()`, `this.repo.find()`) are typed by the field; same-package Java types resolve through the package node, so `Token.Tag` names the nested type. - Go structural interfaces match by method key, parameter/result counts and types; interface dispatch follows upstream's per-type override pass. Frameworks: - Next.js routes named by path with upstream segments; router.push, redirect and navigation; middleware/proxy matcher coverage. - React Router and TanStack Router route tables, links and navigation. - NestJS HTTP, GraphQL, microservice and WebSocket routes. - Express chained router.route() and cross-file mount prefixes. - A decorator route's bare handler name falls back to the method it sits on. Dynamic and cross-boundary calls: - Callback collections, object registries, HTTP client -> route, queue job -> consumer, event bus and sockets, Celery tasks, Spring events. - Synthesized edges merge by (source, target) in upstream pass order. Engine rule version 5; segment format unchanged. Incremental sync recomputes the cross-file passes and re-resolves names whose signatures changed. Minified files are skipped by the new site scanners, and facts compute columns incrementally so a one-line bundle no longer costs quadratic time. Co-Authored-By: Claude Opus 5.5 --- crates/cg-core/src/brand.rs | 2 +- crates/cg-core/src/text.rs | 6 + crates/cg-extract/src/facts/args.rs | 298 ++++ crates/cg-extract/src/facts/go.rs | 15 + crates/cg-extract/src/facts/java.rs | 8 +- crates/cg-extract/src/facts/js.rs | 8 +- crates/cg-extract/src/facts/kotlin.rs | 4 + crates/cg-extract/src/facts/mod.rs | 25 + crates/cg-extract/src/facts/util.rs | 23 +- crates/cg-resolve/src/engine.rs | 32 + crates/cg-resolve/src/frameworks/href.rs | 527 ++++++ crates/cg-resolve/src/frameworks/jsroutes.rs | 994 ++++++++++++ crates/cg-resolve/src/frameworks/mod.rs | 26 +- crates/cg-resolve/src/frameworks/nav.rs | 822 ++++++++++ crates/cg-resolve/src/frameworks/routes.rs | 249 +-- crates/cg-resolve/src/lib.rs | 1 + crates/cg-resolve/src/overload.rs | 684 ++++++++ crates/cg-resolve/src/project.rs | 6 + crates/cg-resolve/src/resolve.rs | 367 ++++- crates/cg-resolve/src/synth/dynamic.rs | 1531 ++++++++++++++++++ crates/cg-resolve/src/synth/go.rs | 321 ++++ crates/cg-resolve/src/synth/mod.rs | 375 +++-- crates/cg-resolve/src/synth/scan.rs | 183 +++ crates/cg-resolve/src/synth/sites.rs | 48 + crates/cg-resolve/tests/upstream_parity.rs | 393 +++++ crates/cg-store/src/build.rs | 30 +- crates/cg-sync/src/sync.rs | 4 +- crates/cg-sync/tests/incremental_parity.rs | 274 ++++ 28 files changed, 6960 insertions(+), 296 deletions(-) create mode 100644 crates/cg-extract/src/facts/args.rs create mode 100644 crates/cg-resolve/src/frameworks/href.rs create mode 100644 crates/cg-resolve/src/frameworks/jsroutes.rs create mode 100644 crates/cg-resolve/src/frameworks/nav.rs create mode 100644 crates/cg-resolve/src/overload.rs create mode 100644 crates/cg-resolve/src/synth/dynamic.rs create mode 100644 crates/cg-resolve/src/synth/go.rs create mode 100644 crates/cg-resolve/src/synth/scan.rs create mode 100644 crates/cg-resolve/tests/upstream_parity.rs create mode 100644 crates/cg-sync/tests/incremental_parity.rs diff --git a/crates/cg-core/src/brand.rs b/crates/cg-core/src/brand.rs index 8d42f0e..8740215 100644 --- a/crates/cg-core/src/brand.rs +++ b/crates/cg-core/src/brand.rs @@ -17,7 +17,7 @@ pub const UPSTREAM_CONFIG_FILE: &str = "codegraph.json"; pub const CONFIG_FILE: &str = "cgr.json"; /// Version of the resolution rules: changing resolver behaviour bumps this, which /// marks older indexes as "built by an older engine" (design doc 5.8). -pub const ENGINE_VERSION: u32 = 4; +pub const ENGINE_VERSION: u32 = 5; /// Version of the on-disk segment format; a mismatch forces a full rebuild. pub const FORMAT_VERSION: u32 = 2; /// The upstream commit whose kernel is vendored (see crates/cg-kernel/UPSTREAM_COMMIT). diff --git a/crates/cg-core/src/text.rs b/crates/cg-core/src/text.rs index 323124c..574dd89 100644 --- a/crates/cg-core/src/text.rs +++ b/crates/cg-core/src/text.rs @@ -63,3 +63,9 @@ pub fn truncate16(s: &str, max_units: usize) -> &str { pub const fn pack_pos(line: u32, col16: u32) -> u64 { ((line as u64) << 32) | col16 as u64 } + +/// Inverse of [`pack_pos`]: (line, utf16 column). +#[inline] +pub const fn unpack_pos(pos: u64) -> (u32, u32) { + ((pos >> 32) as u32, pos as u32) +} diff --git a/crates/cg-extract/src/facts/args.rs b/crates/cg-extract/src/facts/args.rs new file mode 100644 index 0000000..d28a8dd --- /dev/null +++ b/crates/cg-extract/src/facts/args.rs @@ -0,0 +1,298 @@ +//! Argument shapes of call sites, for overload selection (upstream `javaOverloadsFitting`): +//! a call's arguments decide which of several same-named methods it reaches. Each argument +//! is read from syntax alone and stored as one short string: +//! +//! * `T:` - a literal, `new T(..)` or a cast: the static type as written +//! * `V:` - a local, parameter or field, typed at resolution from its binding +//! * `F:` - `this.`: a field of the enclosing type +//! * `#this` - `this` +//! * `N` - `null` / `undefined` +//! * `L` - a lambda / function literal taking `k` parameters; `L` alone: a method +//! reference, whose parameter count the text does not say +//! * `?` - anything else (a call, an operator, ...): fits every parameter +//! +//! A spread argument (`...xs`) or a named one (Kotlin `f(n = 1)`) makes the positional +//! reading unreliable, and such a call records nothing. + +use super::util::{kids, Cx}; +use super::CallArgsFact; +use cg_core::text::unpack_pos; +use tree_sitter::Node; + +/// First item of a reference's candidate list that carries argument shapes. +pub const ARGS_MARK: &str = "#args"; + +/// More arguments than this are not worth a list (and no overload set differs that late). +const MAX_ARGS: usize = 24; + +fn push(cx: &mut Cx, at: Node, tail: &str, args: Vec) { + if tail.is_empty() || args.len() > MAX_ARGS { + return; + } + let (line, col) = unpack_pos(cx.start(at)); + cx.facts.call_args.push(CallArgsFact { line, col, tail: tail.to_owned(), args }); +} + +fn lambda(params: usize) -> String { + format!("L{params}") +} + +// ------------------------------------------------------------------- Java + +/// `foo(a, "x")`, `obj.foo(1)`, `this.foo()`, `super.foo()`: the kernel's reference sits at +/// the start of the `method_invocation`. +pub fn java_call(cx: &mut Cx, call: Node) { + let Some(name) = call.child_by_field_name("name") else { return }; + let Some(list) = call.child_by_field_name("arguments") else { return }; + let tail = cx.text(name).to_owned(); + let mut args = Vec::new(); + for a in kids(list) { + if !a.is_named() || a.kind().ends_with("comment") { + continue; + } + args.push(java_shape(cx, a)); + } + push(cx, call, &tail, args); +} + +fn java_type(cx: &Cx, t: Node) -> String { + match t.kind() { + "generic_type" => t.named_child(0).map(|c| java_type(cx, c)).unwrap_or_default(), + "annotated_type" => kids(t).find(|c| !matches!(c.kind(), "annotation" | "marker_annotation")).map(|c| java_type(cx, c)).unwrap_or_default(), + "array_type" => { + let el = t.child_by_field_name("element").map(|c| java_type(cx, c)).unwrap_or_default(); + if el.is_empty() { + String::new() + } else { + format!("{el}[]") + } + } + "type_identifier" if cx.text(t) == "var" => String::new(), + "type_identifier" | "scoped_type_identifier" | "integral_type" | "floating_point_type" | "boolean_type" => cx.text(t).to_owned(), + _ => String::new(), + } +} + +fn java_shape(cx: &Cx, a: Node) -> String { + let text = cx.text(a); + let typed = |t: &str| format!("T:{t}"); + match a.kind() { + "string_literal" | "text_block" => typed("String"), + "character_literal" => typed("char"), + "decimal_integer_literal" | "hex_integer_literal" | "octal_integer_literal" | "binary_integer_literal" => { + typed(if text.ends_with(['l', 'L']) { "long" } else { "int" }) + } + "decimal_floating_point_literal" | "hex_floating_point_literal" => typed(if text.ends_with(['f', 'F']) { "float" } else { "double" }), + "true" | "false" => typed("boolean"), + "null_literal" => "N".into(), + "this" => "#this".into(), + "identifier" => format!("V:{text}"), + "object_creation_expression" => match a.child_by_field_name("type").map(|t| java_type(cx, t)) { + Some(t) if !t.is_empty() => typed(&t), + _ => "?".into(), + }, + "array_creation_expression" => match a.child_by_field_name("type").map(|t| java_type(cx, t)) { + Some(t) if !t.is_empty() => typed(&format!("{t}[]")), + _ => "?".into(), + }, + "cast_expression" => match a.child_by_field_name("type").map(|t| java_type(cx, t)) { + Some(t) if !t.is_empty() => typed(&t), + _ => "?".into(), + }, + "field_access" => match (a.child_by_field_name("object"), a.child_by_field_name("field")) { + (Some(o), Some(f)) if o.kind() == "this" => format!("F:{}", cx.text(f)), + _ => "?".into(), + }, + "lambda_expression" => { + let n = match a.child_by_field_name("parameters") { + Some(p) if p.kind() == "identifier" => 1, + Some(p) => kids(p).filter(|c| c.is_named() && !c.kind().ends_with("comment")).count(), + None => 0, + }; + lambda(n) + } + "method_reference" => "L".into(), + "parenthesized_expression" => a.named_child(0).map(|c| java_shape(cx, c)).unwrap_or_else(|| "?".into()), + "unary_expression" => match a.child_by_field_name("operand") { + // `-1`, `-2.5`: the operand's numeric type + Some(o) if text.starts_with(['-', '+']) && o.kind().contains("literal") => java_shape(cx, o), + _ => "?".into(), + }, + _ => "?".into(), + } +} + +// ------------------------------------------------------------------- Kotlin + +/// `f(a, b)`, `obj.f(1)`, `f(1) { .. }`: the kernel's reference sits at the start of the +/// `call_expression` (the inner one, for a trailing lambda). +pub fn kotlin_call(cx: &mut Cx, call: Node) { + let mut c = call.walk(); + let parts: Vec = call.named_children(&mut c).collect(); + let (Some(callee), Some(suffix)) = (parts.first().copied(), parts.get(1).copied()) else { return }; + if suffix.kind() != "call_suffix" { + return; + } + let tail = match callee.kind() { + "simple_identifier" => cx.text(callee).to_owned(), + "navigation_expression" => match kids(callee).filter(|k| k.kind() == "navigation_suffix").last() { + Some(s) => kids(s).find(|k| k.kind() == "simple_identifier").map(|k| cx.text(k).to_owned()).unwrap_or_default(), + None => String::new(), + }, + _ => String::new(), + }; + let mut args = Vec::new(); + for part in kids(suffix) { + match part.kind() { + "value_arguments" => { + for va in kids(part).filter(|k| k.kind() == "value_argument") { + let mut vc = va.walk(); + let inner: Vec = va.named_children(&mut vc).collect(); + // `name = value` and `*spread` are not positional + if inner.len() != 1 || kids(va).any(|k| !k.is_named() && matches!(cx.text(k), "*" | "=")) { + return; + } + args.push(kotlin_shape(cx, inner[0])); + } + } + "annotated_lambda" => args.push(kotlin_lambda(cx, part)), + _ => {} + } + } + // `f(1) { .. }` parses as a call of the call `f(1)` with the lambda: it is one more + // argument of the inner call. + if let Some(parent) = call.parent() { + if parent.kind() == "call_expression" && parent.named_child(0) == Some(call) { + if let Some(outer) = parent.named_child(1).filter(|s| s.kind() == "call_suffix") { + if kids(outer).all(|k| !k.is_named() || k.kind() == "annotated_lambda") { + for l in kids(outer).filter(|k| k.kind() == "annotated_lambda") { + args.push(kotlin_lambda(cx, l)); + } + } + } + } + } + push(cx, call, &tail, args); +} + +fn kotlin_lambda(cx: &Cx, n: Node) -> String { + let lit = if n.kind() == "lambda_literal" { Some(n) } else { kids(n).find(|k| k.kind() == "lambda_literal") }; + let Some(lit) = lit else { return "?".into() }; + let params = kids(lit).find(|k| k.kind() == "lambda_parameters").map_or(0, |p| kids(p).filter(|k| k.is_named()).count()); + let _ = cx; + // `{ it }` takes one implicit parameter or none: the count is unknown + if params == 0 { + "L".into() + } else { + lambda(params) + } +} + +fn kotlin_shape(cx: &Cx, a: Node) -> String { + let text = cx.text(a); + let typed = |t: &str| format!("T:{t}"); + match a.kind() { + "string_literal" | "line_string_literal" | "multi_line_string_literal" => typed("String"), + "character_literal" => typed("Char"), + "integer_literal" | "hex_literal" | "bin_literal" => typed("Int"), + "long_literal" => typed("Long"), + "real_literal" => typed(if text.ends_with(['f', 'F']) { "Float" } else { "Double" }), + "boolean_literal" => typed("Boolean"), + "null_literal" => "N".into(), + "this_expression" => "#this".into(), + "simple_identifier" if text == "null" => "N".into(), + "simple_identifier" => format!("V:{text}"), + "lambda_literal" | "annotated_lambda" => kotlin_lambda(cx, a), + "anonymous_function" => "L".into(), + "callable_reference" => "L".into(), + "parenthesized_expression" => a.named_child(0).map(|c| kotlin_shape(cx, c)).unwrap_or_else(|| "?".into()), + "as_expression" => match a.named_child(1) { + Some(t) => typed(cx.text(t).trim_end_matches('?')), + None => "?".into(), + }, + "prefix_expression" if text.starts_with(['-', '+']) => match a.named_child(a.named_child_count().saturating_sub(1)) { + Some(o) if o.kind().ends_with("literal") => kotlin_shape(cx, o), + _ => "?".into(), + }, + _ => "?".into(), + } +} + +// ------------------------------------------------------------------- TypeScript / JavaScript + +/// `f(a)`, `obj.f(1)`, `this.f()`: the kernel's reference sits at the call, or at its callee +/// when an `await` sits inside the callee; both positions are recorded. +pub fn js_call(cx: &mut Cx, call: Node) { + let Some(func) = call.child_by_field_name("function") else { return }; + let Some(list) = call.child_by_field_name("arguments") else { return }; + if list.kind() != "arguments" { + return; // a tagged template + } + let tail = match func.kind() { + "identifier" => cx.text(func).to_owned(), + "member_expression" => func.child_by_field_name("property").map(|p| cx.text(p).to_owned()).unwrap_or_default(), + _ => return, + }; + let mut args = Vec::new(); + for a in kids(list) { + if !a.is_named() || a.kind() == "comment" { + continue; + } + if a.kind() == "spread_element" { + return; + } + args.push(js_shape(cx, a)); + } + if cx.start(func) != cx.start(call) { + push(cx, func, &tail, args.clone()); + } + push(cx, call, &tail, args); +} + +fn js_type(cx: &Cx, t: Node) -> String { + match t.kind() { + "type_annotation" | "parenthesized_type" => t.named_child(0).map(|c| js_type(cx, c)).unwrap_or_default(), + "predefined_type" | "type_identifier" | "nested_type_identifier" => cx.text(t).to_owned(), + "generic_type" => t.child_by_field_name("name").map(|c| cx.text(c).to_owned()).unwrap_or_default(), + _ => String::new(), + } +} + +fn js_shape(cx: &Cx, a: Node) -> String { + let typed = |t: &str| format!("T:{t}"); + match a.kind() { + "string" | "template_string" => typed("string"), + "number" => typed("number"), + "true" | "false" => typed("boolean"), + "null" | "undefined" => "N".into(), + "this" => "#this".into(), + "identifier" if cx.text(a) == "undefined" => "N".into(), + "identifier" => format!("V:{}", cx.text(a)), + "member_expression" => match (a.child_by_field_name("object"), a.child_by_field_name("property")) { + (Some(o), Some(p)) if o.kind() == "this" && p.kind() == "property_identifier" => format!("F:{}", cx.text(p)), + _ => "?".into(), + }, + "new_expression" => match a.child_by_field_name("constructor") { + Some(c) if matches!(c.kind(), "identifier" | "member_expression") => typed(cx.text(c)), + _ => "?".into(), + }, + "arrow_function" | "function_expression" | "function" => { + let n = if a.child_by_field_name("parameter").is_some() { + 1 + } else { + a.child_by_field_name("parameters").map_or(0, |p| kids(p).filter(|c| c.is_named() && c.kind() != "comment").count()) + }; + lambda(n) + } + "parenthesized_expression" | "non_null_expression" => a.named_child(0).map(|c| js_shape(cx, c)).unwrap_or_else(|| "?".into()), + "as_expression" | "satisfies_expression" => match a.named_child(1).map(|t| js_type(cx, t)) { + Some(t) if !t.is_empty() => typed(&t), + _ => "?".into(), + }, + "unary_expression" => match a.child_by_field_name("argument") { + Some(o) if o.kind() == "number" && matches!(cx.text(a).as_bytes().first(), Some(b'-' | b'+')) => typed("number"), + _ => "?".into(), + }, + _ => "?".into(), + } +} diff --git a/crates/cg-extract/src/facts/go.rs b/crates/cg-extract/src/facts/go.rs index 132ed2c..853fd9a 100644 --- a/crates/cg-extract/src/facts/go.rs +++ b/crates/cg-extract/src/facts/go.rs @@ -44,6 +44,13 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) { "function_declaration" | "method_declaration" => function(cx, n, scope), "func_literal" => function(cx, n, scope), "type_spec" => type_spec(cx, n, scope), + "type_alias" => { + // `type A = B` names another type; a defined `type A B` is a type of its own + if let Some(name) = n.child_by_field_name("name") { + let name = cx.text(name).to_owned(); + cx.facts.go_types.push(GoTypeFact { name, alias: true, type_params: Vec::new() }); + } + } "short_var_declaration" => { let (l, r) = (n.child_by_field_name("left"), n.child_by_field_name("right")); if let (Some(l), Some(r)) = (l, r) { @@ -186,6 +193,14 @@ fn function(cx: &mut Cx, n: Node, scope: u32) { } fn type_spec(cx: &mut Cx, n: Node, scope: u32) { + if let (Some(name), Some(tp)) = (n.child_by_field_name("name"), n.child_by_field_name("type_parameters")) { + let params: Vec = kids(tp) + .filter(|d| d.kind() == "type_parameter_declaration") + .flat_map(|d| kids(d).filter(|c| c.kind() == "identifier").map(|c| cx.text(c).to_owned()).collect::>()) + .collect(); + let name = cx.text(name).to_owned(); + cx.facts.go_types.push(GoTypeFact { name, alias: false, type_params: params }); + } let Some(ty) = n.child_by_field_name("type") else { return }; if ty.kind() != "struct_type" { return; diff --git a/crates/cg-extract/src/facts/java.rs b/crates/cg-extract/src/facts/java.rs index ed974ff..4b3279c 100644 --- a/crates/cg-extract/src/facts/java.rs +++ b/crates/cg-extract/src/facts/java.rs @@ -26,6 +26,10 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) { "class_declaration" | "interface_declaration" | "enum_declaration" | "record_declaration" | "annotation_type_declaration" => class(cx, n, scope), "method_declaration" | "constructor_declaration" | "compact_constructor_declaration" => function(cx, n, scope), "lambda_expression" => lambda(cx, n, scope), + "method_invocation" => { + super::args::java_call(cx, n); + walk_children(cx, n, scope); + } "local_variable_declaration" => { declare(cx, n, scope, BindKind::Local); walk_children(cx, n, scope); @@ -69,9 +73,11 @@ fn java_type_name(cx: &Cx, n: Node) -> String { match n.kind() { "type_identifier" if cx.text(n) == "var" => String::new(), "type_identifier" | "scoped_type_identifier" => cx.text(n).to_owned(), + // primitives type their arguments for overload selection (`args`); they name no member + "integral_type" | "floating_point_type" | "boolean_type" => cx.text(n).to_owned(), "generic_type" => n.named_child(0).map(|c| java_type_name(cx, c)).unwrap_or_default(), "annotated_type" => kids(n).find(|c| !matches!(c.kind(), "annotation" | "marker_annotation")).map(|c| java_type_name(cx, c)).unwrap_or_default(), - _ => String::new(), // primitives, arrays, `var`, void + _ => String::new(), // arrays, `var`, void } } diff --git a/crates/cg-extract/src/facts/js.rs b/crates/cg-extract/src/facts/js.rs index 86d259e..b84f524 100644 --- a/crates/cg-extract/src/facts/js.rs +++ b/crates/cg-extract/src/facts/js.rs @@ -41,6 +41,10 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) { "function_expression" | "function" | "generator_function" | "arrow_function" | "method_definition" => function(cx, n, scope), "class_declaration" | "abstract_class_declaration" | "class" => class(cx, n, scope), "lexical_declaration" | "variable_declaration" => var_decl(cx, n, scope), + "call_expression" => { + super::args::js_call(cx, n); + walk_children(cx, n, scope); + } "for_in_statement" => { if let Some(l) = n.child_by_field_name("left") { bind_pattern(cx, l, scope, BindKind::Local, "", ""); @@ -99,6 +103,8 @@ fn ts_type_name(cx: &Cx, n: Node) -> String { match n.kind() { "type_annotation" => n.named_child(0).map(|c| ts_type_name(cx, c)).unwrap_or_default(), "type_identifier" | "identifier" => cx.text(n).to_owned(), + // primitives type arguments for overload selection (`args`); `any` / `unknown` say nothing + "predefined_type" if matches!(cx.text(n), "string" | "number" | "boolean" | "bigint" | "symbol") => cx.text(n).to_owned(), "nested_type_identifier" | "member_expression" => cx.text(n).to_owned(), "generic_type" => n.child_by_field_name("name").map(|c| ts_type_name(cx, c)).unwrap_or_default(), "parenthesized_type" | "readonly_type" | "required_parameter" => n.named_child(0).map(|c| ts_type_name(cx, c)).unwrap_or_default(), @@ -108,7 +114,7 @@ fn ts_type_name(cx: &Cx, n: Node) -> String { continue; } let t = ts_type_name(cx, ch); - if !t.is_empty() && t != "null" && t != "undefined" { + if !t.is_empty() && !matches!(t.as_str(), "null" | "undefined" | "string" | "number" | "boolean" | "bigint" | "symbol") { return t; } } diff --git a/crates/cg-extract/src/facts/kotlin.rs b/crates/cg-extract/src/facts/kotlin.rs index 06ba49c..c749160 100644 --- a/crates/cg-extract/src/facts/kotlin.rs +++ b/crates/cg-extract/src/facts/kotlin.rs @@ -35,6 +35,10 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) { "class_declaration" | "object_declaration" | "companion_object" => class(cx, n, scope), "function_declaration" | "secondary_constructor" | "anonymous_function" => function(cx, n, scope), "lambda_literal" => lambda(cx, n, scope), + "call_expression" => { + super::args::kotlin_call(cx, n); + walk_children(cx, n, scope); + } "property_declaration" => { property(cx, n, scope); walk_children(cx, n, scope); diff --git a/crates/cg-extract/src/facts/mod.rs b/crates/cg-extract/src/facts/mod.rs index 939f25b..f8f974b 100644 --- a/crates/cg-extract/src/facts/mod.rs +++ b/crates/cg-extract/src/facts/mod.rs @@ -6,6 +6,7 @@ use crate::rows::FileRows; use cg_core::{text, Lang, NONE}; use tree_sitter::{Node, Tree}; +pub mod args; mod go; mod java; mod js; @@ -140,6 +141,11 @@ pub struct Facts { /// Precise call paths lost by the compatibility kernel. Applied to matching /// call references before storage; node identities and sites stay unchanged. pub call_names: Vec, + /// Argument shapes of call sites, for overload selection (see `args`). + pub call_args: Vec, + /// Go type declarations whose names stand for other types: true aliases and the type + /// parameters of generic types (structural interface matching leaves them open). + pub go_types: Vec, /// A supplementary parse exhausted its deadline. The pipeline exposes this /// as an incomplete file rather than silently advertising complete facts. pub timed_out: bool, @@ -154,6 +160,25 @@ pub struct CallNameFact { pub name: String, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct GoTypeFact { + pub name: String, + /// `type A = B` + pub alias: bool, + pub type_params: Vec, +} + +/// The arguments of one call, keyed like the kernel's call reference: its position and the +/// called name's last segment. Stored with the reference (`args::ARGS_MARK` list). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CallArgsFact { + pub line: u32, + pub col: u32, + pub tail: String, + /// One shape per argument, in order (see `args`). + pub args: Vec, +} + impl Facts { pub fn new(file_end: u64) -> Facts { Facts { scopes: vec![ScopeFact { kind: ScopeKind::File, parent: NONE, start: 0, end: file_end }], ..Default::default() } diff --git a/crates/cg-extract/src/facts/util.rs b/crates/cg-extract/src/facts/util.rs index 074777d..839acc1 100644 --- a/crates/cg-extract/src/facts/util.rs +++ b/crates/cg-extract/src/facts/util.rs @@ -11,6 +11,9 @@ pub struct Cx<'a> { pub parse_timeout: std::time::Duration, line_starts: Vec, row_by_start: Option>, + /// Last (row, byte, UTF-16 column) computed: positions are asked in source order, and on + /// a minified one-line file counting each column from the line start is quadratic. + last_pos: std::cell::Cell<(usize, usize, u32)>, } impl<'a> Cx<'a> { @@ -18,7 +21,15 @@ impl<'a> Cx<'a> { let line_starts = text::line_starts(src); let end_line = line_starts.len() as u32; let facts = Facts::new(pack_pos(end_line + 1, 0)); - Cx { src, rows, facts, parse_timeout: std::time::Duration::from_secs(5), line_starts, row_by_start: None } + Cx { + src, + rows, + facts, + parse_timeout: std::time::Duration::from_secs(5), + line_starts, + row_by_start: None, + last_pos: std::cell::Cell::new((usize::MAX, 0, 0)), + } } /// Kernel node row whose span starts exactly at `pos` (packed line / UTF-16 column), skipping @@ -51,9 +62,17 @@ impl<'a> Cx<'a> { /// Packed (1-based line, UTF-16 column) of a byte offset. pub fn pos_at(&self, byte: usize, row: usize) -> u64 { let ls = self.line_starts[row.min(self.line_starts.len() - 1)] as usize; + let (last_row, last_byte, last_col) = self.last_pos.get(); + if last_row == row && last_byte <= byte && byte - last_byte < byte - ls && self.src.is_char_boundary(last_byte) && self.src.is_char_boundary(byte) { + let col = last_col + self.src[last_byte..byte].chars().map(|c| c.len_utf16() as u32).sum::(); + self.last_pos.set((row, byte, col)); + return pack_pos(row as u32 + 1, col); + } let le = self.line_starts.get(row + 1).map(|v| *v as usize).unwrap_or(self.src.len()); let line = &self.src[ls..le]; - pack_pos(row as u32 + 1, col16(line, byte - ls)) + let col = col16(line, byte - ls); + self.last_pos.set((row, byte, col)); + pack_pos(row as u32 + 1, col) } pub fn start(&self, n: Node) -> u64 { diff --git a/crates/cg-resolve/src/engine.rs b/crates/cg-resolve/src/engine.rs index 677e51c..b214a03 100644 --- a/crates/cg-resolve/src/engine.rs +++ b/crates/cg-resolve/src/engine.rs @@ -275,12 +275,23 @@ impl<'v> Engine<'v> { if p.is_none() { continue; } + let jvm = view.file(f).lang().family() == LangFamily::Jvm; for n in self.top_level_nodes(f) { let node = view.node(n); // Go methods are not package-level names; Java/Kotlin nest members under classes if node.kind() == NodeKind::Method { continue; } + // a Java / Kotlin file's declarations sit inside its `package` namespace node + if jvm && node.kind() == NodeKind::Namespace { + for e in view.out_edges(n).filter(|e| e.kind == EdgeKind::Contains) { + let child = view.node(e.dst); + if child.kind().is_definition() && child.kind() != NodeKind::Method { + defs[p.0 as usize].entry(child.name()).or_default().push(e.dst); + } + } + continue; + } defs[p.0 as usize].entry(node.name()).or_default().push(n); } } @@ -504,6 +515,27 @@ impl<'v> Engine<'v> { None } + /// Every callable member called `name` declared in the body of `t` or of one of its + /// ancestors, nearest type first and in declaration order: the overload set a call picks + /// from. Members declared outside a body (Go receivers, Rust impls) have no overloads. + pub fn member_overloads(&self, t: NodeIdx, name: &str) -> SmallVec<[NodeIdx; 4]> { + let mut out = SmallVec::new(); + for ty in std::iter::once(t).chain(self.ancestors(t)) { + for e in self.view.out_edges(ty) { + if e.kind == EdgeKind::Contains { + let c = self.view.node(e.dst); + if c.name() == name && c.kind().is_callable() && !out.contains(&e.dst) { + out.push(e.dst); + } + } + } + if out.len() > 32 { + break; + } + } + out + } + /// Go: promoted members through embedded structs are covered by `ancestors` /// (embedding is recorded as `extends`). pub fn node_is_type(&self, n: NodeIdx) -> bool { diff --git a/crates/cg-resolve/src/frameworks/href.rs b/crates/cg-resolve/src/frameworks/href.rs new file mode 100644 index 0000000..f2b7580 --- /dev/null +++ b/crates/cg-resolve/src/frameworks/href.rs @@ -0,0 +1,527 @@ +//! Destinations written as strings (upstream `expo-router.ts` / `nextjs.ts` readers): a +//! navigation's href, a client's request path. A literal, a template (its `${…}` parts become +//! holes), an object's `pathname` / `to`, or a conditional whose every arm is one of those. The +//! matcher pairs an href with the one route of a table that serves it: a literal segment beats +//! a parameter, which beats a hole; a tie is no answer. + +use cg_core::NodeIdx; +use rustc_hash::FxHashMap; + +/// Stands for a `${…}` hole inside a stored path. +pub const HOLE: char = '\u{1}'; + +/// One destination: the path (query and hash removed, holes kept) and the text as written. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Href { + pub path: String, +} + +impl Href { + pub fn display(&self) -> String { + self.path.replace(HOLE, "${…}") + } +} + +/// Byte offset of the `)`/`]`/`}` closing the bracket at `open`, skipping strings and +/// templates; `None` when unbalanced. +pub fn match_bracket(s: &str, open: usize) -> Option { + let b = s.as_bytes(); + let mut depth = 0i32; + let mut i = open; + while i < b.len() { + match b[i] { + b'"' | b'\'' => i = string_end(s, i)?, + b'`' => i = template_end(s, i)?, + b'(' | b'[' | b'{' => depth += 1, + b')' | b']' | b'}' => { + depth -= 1; + if depth == 0 { + return Some(i); + } + } + _ => {} + } + i += 1; + } + None +} + +/// Offset of the closing quote of the string opening at `at`. +pub fn string_end(s: &str, at: usize) -> Option { + let b = s.as_bytes(); + let q = b[at]; + if q == b'`' { + return template_end(s, at); + } + let mut i = at + 1; + while i < b.len() { + match b[i] { + b'\\' => i += 1, + c if c == q => return Some(i), + b'\n' => return None, + _ => {} + } + i += 1; + } + None +} + +/// Offset of the backtick closing the template opening at `open`. +pub fn template_end(s: &str, open: usize) -> Option { + let b = s.as_bytes(); + let mut i = open + 1; + while i < b.len() { + match b[i] { + b'\\' => i += 1, + b'`' => return Some(i), + b'$' if b.get(i + 1) == Some(&b'{') => { + let mut depth = 0; + i += 1; + while i < b.len() { + match b[i] { + b'{' => depth += 1, + b'}' => { + depth -= 1; + if depth == 0 { + break; + } + } + b'`' => i = template_end(s, i)?, + b'"' | b'\'' => i = string_end(s, i)?, + _ => {} + } + i += 1; + } + } + _ => {} + } + i += 1; + } + None +} + +/// The string literal starting at `at`, with each `${…}` replaced by [`HOLE`]; `None` when +/// `at` holds no string. +pub fn read_string_at(s: &str, at: usize) -> Option { + let b = s.as_bytes(); + let q = *b.get(at)?; + match q { + b'"' | b'\'' => { + let end = string_end(s, at)?; + Some(unescape(&s[at + 1..end])) + } + b'`' => { + let end = template_end(s, at)?; + let body = &s[at + 1..end]; + let mut out = String::new(); + let bb = body.as_bytes(); + let mut i = 0; + let mut lit = 0; + while i < bb.len() { + if bb[i] == b'\\' { + i += 2; + continue; + } + if bb[i] == b'$' && bb.get(i + 1) == Some(&b'{') { + out.push_str(&unescape(&body[lit..i])); + let close = match_bracket(body, i + 1)?; + out.push(HOLE); + i = close + 1; + lit = i; + continue; + } + i += 1; + } + out.push_str(&unescape(&body[lit.min(body.len())..])); + Some(out) + } + _ => None, + } +} + +fn unescape(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + let mut chars = s.chars(); + while let Some(c) = chars.next() { + if c == '\\' { + if let Some(n) = chars.next() { + out.push(n); + } + } else { + out.push(c); + } + } + out +} + +/// Offset of the first `ch` at bracket depth 0 and outside strings at or after `from`. +pub fn index_at_depth0(s: &str, ch: u8, from: usize) -> Option { + let b = s.as_bytes(); + let mut depth = 0i32; + let mut i = from; + while i < b.len() { + match b[i] { + b'"' | b'\'' => i = string_end(s, i).unwrap_or(b.len()), + b'`' => i = template_end(s, i).unwrap_or(b.len()), + b'(' | b'[' | b'{' => depth += 1, + b')' | b']' | b'}' => depth -= 1, + b'?' if matches!(b.get(i + 1), Some(b'.' | b'?')) && ch == b'?' => i += 1, + c if depth == 0 && c == ch => return Some(i), + _ => {} + } + i += 1; + } + None +} + +/// The `:` closing the ternary opened at `q`, nested ternaries paired. +fn ternary_colon(s: &str, q: usize) -> Option { + let mut depth = 0; + let mut i = q + 1; + for _ in 0..64 { + let next_q = index_at_depth0(s, b'?', i); + let colon = index_at_depth0(s, b':', i)?; + if let Some(nq) = next_q.filter(|nq| *nq < colon) { + depth += 1; + i = nq + 1; + continue; + } + if depth == 0 { + return Some(colon); + } + depth -= 1; + i = colon + 1; + } + None +} + +/// An href from a literal: the path before `?` / `#`. +pub fn to_href(literal: &str) -> Option { + let cut = literal.find(['?', '#']).unwrap_or(literal.len()); + let path = &literal[..cut]; + (!path.is_empty()).then(|| Href { path: path.to_owned() }) +} + +fn strip_cast(s: &str) -> &str { + // `expr as any`, `expr satisfies Href` + for kw in [" as ", " satisfies "] { + if let Some(i) = s.rfind(kw) { + let tail = &s[i + kw.len()..]; + if tail.chars().all(|c| c.is_alphanumeric() || "_$.<>[]|& ".contains(c)) { + return s[..i].trim_end(); + } + } + } + s +} + +/// Every destination an argument expression names (`key` reads an object's field: `pathname` +/// for an `Href` object, `to` for TanStack's): one per arm of a conditional. +pub fn parse_href_expression(expr: &str, key: &str) -> Vec { + let mut args = strip_cast(expr.trim()).trim(); + while args.starts_with('(') && match_bracket(args, 0) == Some(args.len() - 1) { + args = strip_cast(args[1..args.len() - 1].trim()).trim(); + } + if args.is_empty() { + return Vec::new(); + } + if let Some(q) = index_at_depth0(args, b'?', 0).filter(|q| *q > 0) { + if let Some(colon) = ternary_colon(args, q) { + let mut arms = parse_href_expression(&args[q + 1..colon], key); + for arm in parse_href_expression(&args[colon + 1..], key) { + if !arms.contains(&arm) { + arms.push(arm); + } + } + return arms; + } + } + if args.starts_with('{') { + return object_field(args, key).and_then(|v| read_string_at(v, 0)).and_then(|l| to_href(&l)).into_iter().collect(); + } + read_string_at(args, 0).and_then(|l| to_href(&l)).into_iter().collect() +} + +/// The text of field `key`'s value in the object literal `obj` (`{ a: 1, key: 'x' }`). +pub fn object_field<'a>(obj: &'a str, key: &str) -> Option<&'a str> { + let end = match_bracket(obj, 0)?; + let body = &obj[1..end]; + let mut i = 0; + let b = body.as_bytes(); + while i < b.len() { + let rest = &body[i..]; + let trimmed = rest.trim_start(); + let at = i + (rest.len() - trimmed.len()); + let name_len = trimmed.find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$')).unwrap_or(trimmed.len()); + let name = &trimmed[..name_len]; + let after = trimmed[name_len..].trim_start(); + if name == key && after.starts_with(':') { + let value = after[1..].trim_start(); + let start = body.len() - value.len(); + let stop = index_at_depth0(body, b',', start).unwrap_or(body.len()); + return Some(body[start..stop].trim()); + } + // skip to the next entry + match index_at_depth0(body, b',', at.max(i)) { + Some(c) => i = c + 1, + None => break, + } + } + None +} + +/// The directory of the app owning a file: everything before the first conventional source +/// directory (upstream `appRootFor`). +pub fn app_root_for(path: &str) -> &str { + let mut at = 0; + for seg in path.split('/') { + if matches!(seg, "src" | "pages" | "app" | "routes") && at + seg.len() < path.len() { + return &path[..at]; + } + at += seg.len() + 1; + } + match path.rfind('/') { + Some(i) => &path[..=i], + None => "", + } +} + +fn is_param(seg: &str) -> bool { + (seg.starts_with('[') && seg.ends_with(']')) || (seg.starts_with(':') && !seg.ends_with('*')) +} + +fn is_catch_all(seg: &str) -> bool { + (seg.starts_with("[...") && seg.ends_with(']')) || (seg.starts_with(':') && seg.ends_with('*')) +} + +/// Segments of an href path: `/users/${id}` -> `["users", "*"]`; an absolute URL keeps its +/// path; a relative href is nothing. +pub fn href_segments(path: &str) -> Option> { + let mut p = path; + if let Some(i) = p.find("//") { + let scheme = &p[..i]; + if scheme.is_empty() || scheme.ends_with(':') && scheme[..scheme.len() - 1].chars().all(|c| c.is_ascii_alphanumeric() || "+.-".contains(c)) { + let rest = &p[i + 2..]; + p = rest.find('/').map_or("/", |j| &rest[j..]); + } + } + if !p.starts_with('/') { + return None; + } + Some(p.split('/').filter(|s| !s.is_empty()).map(|s| if s.contains(HOLE) { "*".to_owned() } else { s.to_owned() }).collect()) +} + +/// Score of an href's segments against a route's, `None` when it does not match. +fn score(href: &[String], route: &[&str]) -> Option { + let mut score = 0; + let mut i = 0; + for seg in route { + if is_catch_all(seg) { + if i >= href.len() { + return None; + } + score += (href.len() - i) as u32; + i = href.len(); + continue; + } + let h = href.get(i)?; + if is_param(seg) { + score += 2; + } else if h == seg { + score += 3; + } else if h == "*" { + score += 1; + } else { + return None; + } + i += 1; + } + (i == href.len()).then_some(score) +} + +/// Routes by path; dynamic ones are matched segment by segment. +#[derive(Default)] +pub struct RouteTable { + pub exact: FxHashMap, + pub dynamic: Vec<(NodeIdx, Vec)>, +} + +impl RouteTable { + /// The first route to claim an address keeps it. + pub fn add(&mut self, path: &str, node: NodeIdx) { + self.exact.entry(path.to_owned()).or_insert(node); + if path.contains(':') || path.contains('[') { + self.dynamic.push((node, path.split('/').skip(1).map(str::to_owned).collect())); + } + } + + pub fn is_empty(&self) -> bool { + self.exact.is_empty() + } + + /// The one route the segments denote. + pub fn match_segments(&self, segs: &[String]) -> Option { + if let Some(n) = self.exact.get(&format!("/{}", segs.join("/"))) { + return Some(*n); + } + let mut best: Option<(NodeIdx, u32)> = None; + let mut tied = false; + for (node, route) in &self.dynamic { + let route: Vec<&str> = route.iter().map(String::as_str).collect(); + let Some(s) = score(segs, &route) else { continue }; + match best { + Some((_, b)) if s < b => {} + Some((_, b)) if s == b => tied = true, + _ => { + best = Some((*node, s)); + tied = false; + } + } + } + best.filter(|_| !tied).map(|b| b.0) + } + + /// The routes an href's arms name, each once, with the arm that named it. + pub fn destinations<'h>(&self, arms: &'h [Href]) -> Vec<(NodeIdx, &'h Href)> { + let mut out: Vec<(NodeIdx, &Href)> = Vec::new(); + for arm in arms { + let Some(segs) = href_segments(&arm.path) else { continue }; + if let Some(n) = self.match_segments(&segs) { + if !out.iter().any(|(m, _)| *m == n) { + out.push((n, arm)); + } + } + } + out + } +} + +/// Route tables split by the app each route belongs to (a monorepo has a `/` per app). +#[derive(Default)] +pub struct RootedTables { + pub by_root: Vec<(String, RouteTable)>, +} + +impl RootedTables { + pub fn add(&mut self, root: &str, path: &str, node: NodeIdx) { + match self.by_root.iter_mut().find(|(r, _)| r == root) { + Some((_, t)) => t.add(path, node), + None => { + let mut t = RouteTable::default(); + t.add(path, node); + self.by_root.push((root.to_owned(), t)); + } + } + } + + /// The table of the app `path` belongs to: the longest root it is under. + pub fn for_file(&self, path: &str) -> Option<&RouteTable> { + self.by_root.iter().filter(|(r, _)| path.starts_with(r.as_str())).max_by_key(|(r, _)| r.len()).map(|(_, t)| t) + } + + pub fn is_empty(&self) -> bool { + self.by_root.is_empty() + } +} + +/// Encode an href's arms for a site payload, one per line. +pub fn encode_arms(arms: &[Href]) -> String { + arms.iter().map(|a| a.path.as_str()).collect::>().join("\n") +} + +pub fn decode_arms(payload: &str) -> Vec { + payload.split('\n').filter(|p| !p.is_empty()).map(|p| Href { path: p.to_owned() }).collect() +} + +/// React Router's nesting: a child path is relative unless it starts with `/`. +pub fn compose_route_path(parts: &[&str]) -> String { + let mut path = String::new(); + for part in parts { + if part.starts_with('/') { + path = (*part).to_owned(); + } else if !part.is_empty() { + path = format!("{}/{part}", path.trim_end_matches('/')); + } + } + let mut out = format!("/{}", path.trim_start_matches('/')); + while out.contains("//") { + out = out.replace("//", "/"); + } + if out.len() > 1 && out.ends_with('/') { + out.pop(); + } + out +} + +/// A TanStack path in the form the other routers use: `$id` -> `:id`, `$` -> a splat; `_auth` +/// (pathless layout) and `(group)` segments are not in the URL; a trailing `_` un-nests. +pub fn tanstack_path(raw: &str) -> Option { + if !raw.starts_with('/') { + return None; + } + let mut segs = Vec::new(); + for seg in raw.split('/') { + if seg.is_empty() || seg.starts_with('_') || (seg.starts_with('(') && seg.ends_with(')')) { + continue; + } + let bare = seg.strip_suffix('_').unwrap_or(seg); + if bare == "$" { + segs.push(":splat*".to_owned()); + } else if let Some(p) = bare.strip_prefix('$') { + segs.push(format!(":{p}")); + } else { + segs.push(bare.to_owned()); + } + } + Some(format!("/{}", segs.join("/"))) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn paths(v: &[Href]) -> Vec { + v.iter().map(Href::display).collect() + } + + #[test] + fn reads_destinations() { + assert_eq!(paths(&parse_href_expression("'/users?tab=1'", "pathname")), vec!["/users"]); + assert_eq!(paths(&parse_href_expression("`/users/${u.id}/edit`", "pathname")), vec!["/users/${…}/edit"]); + assert_eq!(paths(&parse_href_expression("{ pathname: '/p/[id]', query: { id } }", "pathname")), vec!["/p/[id]"]); + assert_eq!(paths(&parse_href_expression("isAdmin ? '/admin' : ok ? '/a' : '/b'", "pathname")), vec!["/admin", "/a", "/b"]); + assert_eq!(paths(&parse_href_expression("{ to: '/posts/$postId', params }", "to")), vec!["/posts/$postId"]); + assert!(parse_href_expression("next", "pathname").is_empty()); + assert_eq!(paths(&parse_href_expression("('/x' as any)", "pathname")), vec!["/x"]); + } + + #[test] + fn matches_routes() { + let mut t = RouteTable::default(); + t.add("/users", NodeIdx(1)); + t.add("/users/:id", NodeIdx(2)); + t.add("/users/top", NodeIdx(3)); + t.add("/docs/:all*", NodeIdx(4)); + let m = |p: &str| href_segments(p).and_then(|s| t.match_segments(&s)); + assert_eq!(m("/users"), Some(NodeIdx(1))); + assert_eq!(m("/users/5"), Some(NodeIdx(2))); + assert_eq!(m("/users/top"), Some(NodeIdx(3))); + assert_eq!(m(&format!("/users/{HOLE}")), Some(NodeIdx(2))); + assert_eq!(m("/docs/a/b"), Some(NodeIdx(4))); + assert_eq!(m("https://x.com/users"), Some(NodeIdx(1))); + assert_eq!(m("users"), None); + assert_eq!(m("/nope"), None); + } + + #[test] + fn paths_and_roots() { + assert_eq!(compose_route_path(&["/dashboard", "team", ""]), "/dashboard/team"); + assert_eq!(compose_route_path(&["/a", "/b"]), "/b"); + assert_eq!(compose_route_path(&["", ""]), "/"); + assert_eq!(tanstack_path("/posts/$postId").as_deref(), Some("/posts/:postId")); + assert_eq!(tanstack_path("/_auth/invoices/").as_deref(), Some("/invoices")); + assert_eq!(app_root_for("apps/web/src/App.tsx"), "apps/web/"); + assert_eq!(app_root_for("src/App.tsx"), ""); + assert_eq!(app_root_for("lib/x.ts"), "lib/"); + } +} diff --git a/crates/cg-resolve/src/frameworks/jsroutes.rs b/crates/cg-resolve/src/frameworks/jsroutes.rs new file mode 100644 index 0000000..58e9f39 --- /dev/null +++ b/crates/cg-resolve/src/frameworks/jsroutes.rs @@ -0,0 +1,994 @@ +//! Route tables of the JavaScript frameworks (upstream `frameworks/{nextjs,nestjs,react, +//! tanstack-router}.ts`): Next.js file routes, NestJS decorators across HTTP, GraphQL, +//! microservice and WebSocket transports, React Router's `` markup and route objects, +//! and TanStack Router's file and code routes. A page is named by its path alone and reaches +//! the component it renders; a server route is `METHOD path` and references its handler. + +use super::href::{compose_route_path, match_bracket, object_field, read_string_at, tanstack_path}; +use super::routes::{Handler, Route}; +use cg_core::{EdgeKind, RefKind}; +use regex::Regex; +use std::sync::OnceLock; + +macro_rules! re { + ($name:ident, $pat:expr) => { + fn $name() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new($pat).expect("static regex")) + } + }; +} + +/// 1-based line of a byte offset. +fn line_at(src: &str, off: usize) -> u32 { + src.as_bytes()[..off.min(src.len())].iter().filter(|b| **b == b'\n').count() as u32 + 1 +} + +// ------------------------------------------------------------------- Next.js + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum NextKind { + /// `app/**/page.tsx`, `pages/**`: a page component. + Page, + /// `app/**/route.ts`: one route per exported HTTP method. + Handler, + /// `pages/api/**`: `ANY /api/...` bound to the default export. + Api, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct NextRoute { + pub kind: NextKind, + /// `/blog/:slug` + pub path: String, + /// The directory the Next app lives in (`""`, `apps/web/`). + pub root: String, +} + +/// `[slug]` -> `:slug`, `[...all]` / `[[...all]]` -> `:all*`. +fn next_segment(seg: &str) -> String { + if let Some(inner) = seg.strip_prefix("[[...").and_then(|s| s.strip_suffix("]]")) { + return format!(":{inner}*"); + } + if let Some(inner) = seg.strip_prefix("[...").and_then(|s| s.strip_suffix(']')) { + return format!(":{inner}*"); + } + if let Some(inner) = seg.strip_prefix('[').and_then(|s| s.strip_suffix(']')) { + return format!(":{inner}"); + } + seg.to_owned() +} + +/// The prefix before the first `src//` or `/` directory, and the rest. +fn split_at_dir<'a>(rel: &'a str, dir: &str) -> Option<(&'a str, &'a str)> { + let mut at = 0; + let segs: Vec<&str> = rel.split('/').collect(); + for (i, seg) in segs.iter().enumerate() { + if i + 1 < segs.len() { + if *seg == dir { + return Some((&rel[..at], &rel[at + seg.len() + 1..])); + } + if *seg == "src" && segs.get(i + 1) == Some(&dir) && i + 2 < segs.len() { + return Some((&rel[..at], &rel[at + 4 + dir.len() + 1..])); + } + } + at += seg.len() + 1; + } + None +} + +const NEXT_EXTS: &[&str] = &["tsx", "ts", "jsx", "js", "mjs", "cjs", "md", "mdx"]; + +/// What a file is to the Next router, or `None` for a file that is no route. +pub fn next_route_of(rel: &str) -> Option { + if rel.split('/').any(|s| matches!(s, "__tests__" | "__mocks__" | "node_modules")) { + return None; + } + if let Some((root, rest)) = split_at_dir(rel, "app") { + let (dir, file) = rest.rsplit_once('/').unwrap_or(("", rest)); + let (stem, ext) = file.rsplit_once('.')?; + if !NEXT_EXTS.contains(&ext) || !matches!(stem, "page" | "route") { + return None; + } + let segs: Vec<&str> = dir.split('/').filter(|s| !s.is_empty()).collect(); + // parallel (`@slot`) and intercepting (`(.)photo`) routes are not modelled + if segs.iter().any(|s| s.starts_with('@') || s.starts_with("(.)") || s.starts_with("(..)") || s.starts_with("(...)")) { + return None; + } + let kept: Vec = segs.iter().filter(|s| !(s.starts_with('(') && s.ends_with(')'))).map(|s| next_segment(s)).collect(); + let kind = if stem == "page" { NextKind::Page } else { NextKind::Handler }; + return Some(NextRoute { kind, path: format!("/{}", kept.join("/")), root: root.to_owned() }); + } + let (root, rest) = split_at_dir(rel, "pages")?; + let (bare, ext) = rest.rsplit_once('.')?; + if !NEXT_EXTS.contains(&ext) { + return None; + } + let mut segs: Vec<&str> = bare.split('/').collect(); + let base = *segs.last()?; + if base.starts_with('_') || [".test", ".spec", ".stories", ".config", ".d"].iter().any(|s| bare.ends_with(s)) { + return None; + } + if segs.last() == Some(&"index") { + segs.pop(); + } + let kind = if segs.first() == Some(&"api") { NextKind::Api } else { NextKind::Page }; + let path = format!("/{}", segs.iter().map(|s| next_segment(s)).collect::>().join("/")); + Some(NextRoute { kind, path, root: root.to_owned() }) +} + +re!( + next_handler_re, + r"\bexport\s+(?:async\s+)?function\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\b|\bexport\s+(?:const|let)\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\s*=" +); +re!(default_fn_re, r"export\s+default\s+(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)"); +re!(default_class_re, r"export\s+default\s+class\s+([A-Za-z_$][\w$]*)"); +re!(default_wrapped_re, r"export\s+default\s+(?:[A-Za-z_$][\w$]*\.)?[A-Za-z_$][\w$]*\s*\(\s*([A-Za-z_$][\w$]*)\s*[,)]"); +re!(default_ident_re, r"(?m)export\s+default\s+([A-Za-z_$][\w$]*)\s*;?\s*$"); +re!(default_as_re, r"export\s*\{\s*([A-Za-z_$][\w$]*)\s+as\s+default\s*\}"); + +/// The name the module exports as its default (a wrapper such as `memo(Page)` is looked +/// through), with its offset. +pub fn default_export_name(src: &str) -> Option<(String, usize)> { + for re in [default_fn_re(), default_class_re(), default_wrapped_re(), default_ident_re(), default_as_re()] { + if let Some(c) = re.captures(src) { + let g = c.get(1)?; + if g.as_str() != "default" { + return Some((g.as_str().to_owned(), c.get(0)?.start())); + } + } + } + None +} + +pub fn scan_next(rel: &str, src: &str) -> Vec { + let Some(file) = next_route_of(rel) else { return Vec::new() }; + let mut out = Vec::new(); + match file.kind { + NextKind::Handler => { + let mut seen = Vec::new(); + for c in next_handler_re().captures_iter(src) { + let Some(m) = c.get(1).or_else(|| c.get(2)) else { continue }; + if seen.contains(&m.as_str()) { + continue; + } + seen.push(m.as_str()); + let line = line_at(src, c.get(0).unwrap().start()); + out.push(Route { + method: m.as_str().to_owned(), + path: file.path.clone(), + line, + handlers: vec![Handler { name: m.as_str().to_owned(), line, col: 0 }], + router: Some("next"), + ..Default::default() + }); + } + } + NextKind::Page | NextKind::Api => { + let handlers = default_export_name(src).map(|(name, at)| Handler { name, line: line_at(src, at), col: 0 }).into_iter().collect(); + let api = file.kind == NextKind::Api; + out.push(Route { + method: if api { "ANY".into() } else { String::new() }, + path: file.path, + line: 1, + handlers, + ref_kind: if api { RefKind::FunctionRef } else { RefKind::Edge(EdgeKind::Calls) }, + router: Some("next"), + }); + } + } + out +} + +// ------------------------------------------------------------------- NestJS + +/// `@Name(` at `at`: the arguments' text and the offset after `)`. +fn decorator_args(src: &str, open: usize) -> Option<(&str, usize)> { + let close = match_bracket(src, open)?; + Some((&src[open + 1..close], close + 1)) +} + +/// Every `@Name(...)` decorator named in `names`: (name, args, start, end). +fn decorators<'s>(src: &'s str, names: &[&str]) -> Vec<(&'s str, &'s str, usize, usize)> { + let mut out = Vec::new(); + let b = src.as_bytes(); + let mut i = 0; + while let Some(rel) = src[i..].find('@') { + let at = i + rel; + i = at + 1; + let name_end = src[at + 1..].find(|c: char| !(c.is_alphanumeric() || c == '_')).map_or(src.len(), |e| at + 1 + e); + let name = &src[at + 1..name_end]; + if !names.contains(&name) { + continue; + } + let mut open = name_end; + while open < b.len() && b[open].is_ascii_whitespace() { + open += 1; + } + if b.get(open) != Some(&b'(') { + continue; + } + if let Some((args, end)) = decorator_args(src, open) { + out.push((name, args, at, end)); + i = end; + } + } + out +} + +/// The method a decorator ending at `from` sits on: skip stacked decorators and modifiers. +fn method_after(src: &str, from: usize) -> Option<(String, usize)> { + let b = src.as_bytes(); + let mut i = from; + let ws = |i: &mut usize| { + while *i < b.len() && b[*i].is_ascii_whitespace() { + *i += 1; + } + }; + loop { + ws(&mut i); + if b.get(i) != Some(&b'@') { + break; + } + i += 1; + while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_' || b[i] == b'.') { + i += 1; + } + ws(&mut i); + if b.get(i) == Some(&b'(') { + i = match_bracket(src, i)? + 1; + } + } + loop { + ws(&mut i); + let rest = &src[i..]; + let m = ["public", "private", "protected", "async", "static", "readonly", "override"] + .iter() + .find(|m| rest.starts_with(**m) && rest[m.len()..].starts_with(|c: char| !(c.is_alphanumeric() || c == '_'))); + match m { + Some(m) => i += m.len(), + None => break, + } + } + let rest = &src[i..]; + let end = rest.find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$')).unwrap_or(rest.len()); + if end == 0 { + return None; + } + let after = rest[end..].trim_start(); + (after.starts_with('(') || after.starts_with('<')).then(|| (rest[..end].to_owned(), i)) +} + +re!(first_string_re, r#"['"`]([^'"`]*)['"`]"#); +re!(path_field_re, r#"path\s*:\s*['"`]([^'"`]*)['"`]"#); +re!(namespace_re, r#"namespace\s*:\s*['"`]([^'"`]*)['"`]"#); +re!(name_field_re, r#"name\s*:\s*['"`]([^'"`]*)['"`]"#); +re!(lead_string_re, r#"^\s*['"`]([^'"`]*)['"`]"#); + +fn first_string(args: &str) -> String { + first_string_re().captures(args).map(|c| c[1].to_owned()).unwrap_or_default() +} + +fn join_http(prefix: &str, sub: &str) -> String { + let parts: Vec<&str> = [prefix, sub].iter().map(|p| p.trim().trim_matches('/')).filter(|p| !p.is_empty()).collect(); + format!("/{}", parts.join("/")) +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum NestClass { + Controller, + Resolver, + Gateway, + Other, +} + +pub fn scan_nest(src: &str) -> Vec { + // class scopes: each runs from its decorator to the next class decorator + let mut raw: Vec<(NestClass, String, usize)> = Vec::new(); + for (name, args, at, _) in decorators(src, &["Controller", "Resolver", "WebSocketGateway", "Injectable", "Module", "Catch"]) { + let (kind, prefix) = match name { + "Controller" => (NestClass::Controller, path_field_re().captures(args).map(|c| c[1].to_owned()).unwrap_or_else(|| first_string(args))), + "Resolver" => (NestClass::Resolver, String::new()), + "WebSocketGateway" => (NestClass::Gateway, namespace_re().captures(args).map(|c| c[1].to_owned()).unwrap_or_default()), + _ => (NestClass::Other, String::new()), + }; + raw.push((kind, prefix, at)); + } + raw.sort_by_key(|r| r.2); + let scope_for = |at: usize| -> Option<(NestClass, &str)> { + let i = raw.partition_point(|r| r.2 <= at); + (i > 0).then(|| (raw[i - 1].0, raw[i - 1].1.as_str())) + }; + let mut out = Vec::new(); + let mut add = |at: usize, method: String, path: String, handler: Option<(String, usize)>| { + let line = line_at(src, at); + let handlers = handler.map(|(h, _)| Handler { name: h, line, col: 0 }).into_iter().collect(); + out.push(Route { method, path, line, handlers, ..Default::default() }); + }; + for (name, args, at, end) in decorators(src, &["Get", "Post", "Put", "Patch", "Delete", "Head", "Options", "All"]) { + let prefix = match scope_for(at) { + Some((NestClass::Controller, p)) => p, + _ => "", + }; + add(at, name.to_ascii_uppercase(), join_http(prefix, &first_string(args)), method_after(src, end)); + } + for (name, args, at, end) in decorators(src, &["Query", "Mutation", "Subscription"]) { + // `@Query()` is also REST's parameter decorator: only a resolver's is GraphQL + if !matches!(scope_for(at), Some((NestClass::Resolver, _))) { + continue; + } + let handler = method_after(src, end); + let op = name_field_re() + .captures(args) + .or_else(|| lead_string_re().captures(args)) + .map(|c| c[1].to_owned()) + .or_else(|| handler.as_ref().map(|h| h.0.clone())) + .unwrap_or_default(); + add(at, name.to_ascii_uppercase(), op, handler); + } + for (name, args, at, end) in decorators(src, &["MessagePattern", "EventPattern"]) { + let handler = method_after(src, end); + let verb = if name == "EventPattern" { "EVENT" } else { "MESSAGE" }; + let pattern = Some(first_string(args)).filter(|s| !s.is_empty()).or_else(|| handler.as_ref().map(|h| h.0.clone())).unwrap_or_default(); + add(at, verb.into(), pattern, handler); + } + for (_, args, at, end) in decorators(src, &["SubscribeMessage"]) { + let ns = match scope_for(at) { + Some((NestClass::Gateway, p)) => p, + _ => "", + }; + let handler = method_after(src, end); + let event = Some(first_string(args)).filter(|s| !s.is_empty()).or_else(|| handler.as_ref().map(|h| h.0.clone())).unwrap_or_default(); + add(at, "WS".into(), if ns.is_empty() { event } else { format!("{ns}:{event}") }, handler); + } + out.sort_by_key(|r| r.line); + out +} + +// ------------------------------------------------------------------- React Router + +/// A route-bearing `` or route object, with the span its children sit in. +#[derive(Clone, Debug)] +struct RrScope { + /// The path as written (`None`: a path the file does not spell out). + part: Option, + at: usize, + start: usize, + end: usize, + component: Option, + jsx: bool, + index: bool, + pathless: bool, + outlet: bool, +} + +re!(capital_tag_re, r"<\s*([A-Z][\w.]*)"); +re!(wrapper_re, r"^(?:Suspense|ErrorBoundary|\w*(?:Guard|Provider|Wrapper|Route)|(?:Protected|Private|Auth|Require\w*)\w*)$"); +re!(router_element_re, r"^(?:Browser|Hash|Memory|Native|Static|History|unstable_History)?Router$"); +re!(router_call_re, r"\b(?:createBrowserRouter|createHashRouter|createMemoryRouter|createRoutesFromElements|useRoutes)\b"); + +/// The component an `element` renders: the first tag that is no guard, provider or outlet. +fn element_component(value: &str) -> Option { + let inner = value.trim().trim_start_matches('(').trim_start(); + if !inner.starts_with('<') { + return None; + } + let tags: Vec<&str> = capital_tag_re().captures_iter(inner).filter_map(|c| c.get(1)).map(|m| m.as_str()).collect(); + let content = |t: &&&str| !wrapper_re().is_match(t) && **t != "Outlet"; + tags.iter().find(content).or_else(|| tags.iter().rev().find(|t| **t != "Outlet")).or(tags.first()).map(|t| t.to_string()) +} + +fn literal(value: &str) -> Option { + let v = value.trim(); + let v = v.strip_prefix('{').and_then(|x| x.strip_suffix('}')).map(str::trim).unwrap_or(v); + if (v.starts_with('"') || v.starts_with('\'')) && v.len() >= 2 && v.ends_with(&v[..1]) { + return Some(v[1..v.len() - 1].to_owned()); + } + None +} + +struct RrScan<'a> { + s: &'a str, + b: &'a [u8], + jsx_ok: bool, + objects: bool, + scopes: Vec, + routers: Vec<(usize, usize)>, + route_tags: usize, + depth: u32, +} + +impl<'a> RrScan<'a> { + fn trivia(&self, mut at: usize) -> usize { + while at < self.b.len() && self.b[at].is_ascii_whitespace() { + at += 1; + } + at + } + + fn token_before(&self, at: usize) -> &'a str { + let mut j = at; + while j > 0 && self.b[j - 1].is_ascii_whitespace() { + j -= 1; + } + &self.s[j.saturating_sub(12)..j] + } + + /// One syntactic unit starting at `at`: a string, a JSX element, a bracketed group or one + /// byte. Returns the offset after it. + fn unit(&mut self, at: usize) -> usize { + if self.depth > 200 { + return self.b.len(); + } + let Some(&c) = self.b.get(at) else { return at }; + match c { + b'"' | b'\'' => return super::href::string_end(self.s, at).map_or(at + 1, |e| e + 1), + b'`' => return super::href::template_end(self.s, at).map_or(self.b.len(), |e| e + 1), + b'<' if self.jsx_ok => { + let next = self.b.get(at + 1).copied().unwrap_or(0); + let before = self.token_before(at); + let opens = next.is_ascii_alphabetic() || next == b'>'; + let after_value = before.ends_with(|ch: char| ch.is_alphanumeric() || "_$)]'\"".contains(ch)) && !before.ends_with("return"); + if opens && !after_value { + self.depth += 1; + let e = self.jsx(at); + self.depth -= 1; + return e; + } + return at + 1; + } + b'{' | b'[' | b'(' => {} + _ => return at + 1, + } + let close = match c { + b'{' => b'}', + b'[' => b']', + _ => b')', + }; + let routers_call = c == b'(' && self.s[at.saturating_sub(48)..at].trim_end().ends_with(|ch: char| ch.is_alphanumeric()) && { + let before = self.s[at.saturating_sub(48)..at].trim_end(); + before.ends_with("createRoutesFromElements") || before.ends_with("createRoutesFromChildren") + }; + let mut fields: Vec<(&'a str, &'a str, usize)> = Vec::new(); + let mut i = at + 1; + self.depth += 1; + loop { + i = self.trivia(i); + if i >= self.b.len() || self.b[i] == close { + break; + } + let entry = i; + let key = if c == b'{' { + let rest = &self.s[i..]; + let name_len = rest.find(|ch: char| !(ch.is_alphanumeric() || ch == '_' || ch == '$')).unwrap_or(0); + (name_len > 0 && rest[name_len..].trim_start().starts_with(':')).then(|| &rest[..name_len]) + } else { + None + }; + if let Some(name) = key { + i = self.s[i..].find(':').map_or(self.b.len(), |k| i + k + 1); + let value_at = self.trivia(i); + i = value_at; + let mut value_end = i; + loop { + i = self.trivia(i); + if i >= self.b.len() || self.b[i] == b',' || self.b[i] == close { + break; + } + i = self.unit(i); + value_end = i; + } + fields.push((name, self.s[value_at..value_end.max(value_at)].trim(), entry)); + } else { + loop { + i = self.trivia(i); + if i >= self.b.len() || self.b[i] == b',' || self.b[i] == close { + break; + } + i = self.unit(i); + } + } + if self.b.get(i) == Some(&b',') { + i += 1; + } + } + self.depth -= 1; + if self.objects && c == b'{' && i < self.b.len() { + let get = |k: &str| fields.iter().find(|f| f.0 == k); + let path = get("path").and_then(|f| literal(f.1)); + let shown = get("element") + .and_then(|f| element_component(f.1)) + .or_else(|| get("Component").map(|f| f.1.trim().to_owned()).filter(|v| v.starts_with(|c: char| c.is_ascii_uppercase()))); + let outlet = shown.as_deref() == Some("Outlet") && path.as_deref() != Some(""); + let component = shown.filter(|s| s != "Outlet"); + if let Some(p) = get("path") { + let opaque = path.is_none(); + self.scopes.push(RrScope { + part: path, + at: p.2, + start: at, + end: i, + component, + jsx: false, + index: false, + pathless: false, + outlet: outlet && !opaque, + }); + } else if get("index").is_some_and(|f| f.1 == "true") { + let at_index = get("index").map_or(at, |f| f.2); + self.scopes.push(RrScope { + part: Some(String::new()), + at: at_index, + start: at, + end: i, + component, + jsx: false, + index: true, + pathless: false, + outlet: false, + }); + } else if component.is_some() && get("children").is_some() { + self.scopes.push(RrScope { + part: Some(String::new()), + at, + start: at, + end: i, + component, + jsx: false, + index: false, + pathless: true, + outlet: false, + }); + } + } + if routers_call { + self.routers.push((at, i)); + } + (i + 1).min(self.b.len()) + } + + fn jsx(&mut self, at: usize) -> usize { + let rest = &self.s[at + 1..]; + let tag_len = rest.find(|c: char| !(c.is_alphanumeric() || "_.:-".contains(c))).unwrap_or(rest.len()); + let tag = &rest[..tag_len]; + let mut i = at + 1 + tag_len; + let mut attrs: Vec<(&'a str, &'a str)> = Vec::new(); + loop { + i = self.trivia(i); + if i >= self.b.len() || self.b[i] == b'>' || self.s[i..].starts_with("/>") { + break; + } + let rest = &self.s[i..]; + let name_len = rest.find(|c: char| !(c.is_alphanumeric() || "_:-".contains(c))).unwrap_or(0); + if name_len == 0 { + i = self.unit(i); + continue; + } + let name = &rest[..name_len]; + i = self.trivia(i + name_len); + if self.b.get(i) != Some(&b'=') { + attrs.push((name, "{true}")); + continue; + } + i = self.trivia(i + 1); + let start = i; + i = self.unit(i); + attrs.push((name, &self.s[start..i.min(self.b.len())])); + } + let get = |k: &str| attrs.iter().find(|a| a.0 == k).map(|a| a.1); + let mut scope: Option = None; + let mut addressless: Option<(RrScope, usize, bool)> = None; + let router = router_element_re().is_match(tag).then(|| { + self.routers.push((at, self.b.len())); + self.routers.len() - 1 + }); + if tag == "Route" && i < self.b.len() { + let path_text = get("path"); + let path = path_text.and_then(literal); + self.route_tags += 1; + let expr = |k: &str| get(k).map(|v| v.trim().trim_start_matches('{').trim_end_matches('}').trim()); + let element = expr("element").and_then(element_component).filter(|c| c != "Outlet"); + let component = expr("component") + .filter(|v| v.starts_with(|c: char| c.is_ascii_uppercase()) && v.chars().all(|c| c.is_alphanumeric() || c == '_')) + .map(str::to_owned) + .or(element.clone()); + let route = RrScope { + part: Some(path.clone().unwrap_or_default()), + at, + start: at, + end: self.b.len(), + component, + jsx: true, + index: false, + pathless: false, + outlet: false, + }; + match (&path, path_text) { + (Some(p), _) if !p.is_empty() => { + self.scopes.push(route); + scope = Some(self.scopes.len() - 1); + } + (_, None) | (Some(_), Some(_)) => { + if get("index").is_some_and(|v| v.trim_matches(['{', '}', ' ']) == "true") { + if route.component.is_some() { + self.scopes.push(RrScope { index: true, ..route }); + scope = Some(self.scopes.len() - 1); + } + } else { + let page = path.as_deref() == Some("") && element.is_some(); + addressless = Some((route, self.route_tags, page)); + } + } + (None, Some(_)) => { + // `path={paths.home}`: an address the file does not spell out + self.scopes.push(RrScope { part: None, ..route }); + scope = Some(self.scopes.len() - 1); + } + } + } + let mut close = |me: &mut Self, end: usize| -> usize { + if let Some(s) = scope { + me.scopes[s].end = end; + } + if let Some(r) = router { + me.routers[r].1 = end; + } + if let Some((mut route, tags, page)) = addressless.take() { + route.end = end; + if me.route_tags > tags { + if route.component.is_some() { + me.scopes.push(RrScope { pathless: true, ..route }); + } + } else if page { + me.scopes.push(RrScope { index: true, ..route }); + } + } + end + }; + if self.s[i..].starts_with("/>") { + return close(self, i + 2); + } + i += 1; + while i < self.b.len() { + if self.s[i..].starts_with("').map_or(self.b.len(), |e| i + 2 + e + 1); + return close(self, end); + } + match self.b[i] { + b'<' if self.b.get(i + 1).is_some_and(|c| c.is_ascii_alphabetic() || *c == b'>') => { + self.depth += 1; + i = self.jsx(i); + self.depth -= 1; + } + b'{' => i = self.unit(i), + _ => i += 1, + } + } + close(self, i) + } +} + +/// The routes a file's own text declares (upstream `scanRoutes` for one file). +pub fn scan_react_router(rel: &str, src: &str) -> Vec { + let objects = router_call_re().is_match(src); + if !objects && !src.contains("= end; + let around = |start: usize, end: usize| -> Vec<&RrScope> { + let mut v: Vec<&RrScope> = scopes.iter().filter(|o| holds(o, start, end)).collect(); + v.sort_by_key(|o| o.start); + v + }; + let path_of = |s: &RrScope| -> Option { + let mut parts: Vec<&str> = Vec::new(); + for o in around(s.start, s.end) { + parts.push(o.part.as_deref()?); + } + parts.push(s.part.as_deref()?); + Some(compose_route_path(&parts)) + }; + let in_router = |s: &RrScope| routers.iter().any(|r| r.0 < s.start && r.1 >= s.end); + let addressed = |s: &RrScope| { + if !s.index { + return true; + } + let outer = around(s.start, s.end); + !outer.iter().any(|c| c.part.is_none()) && (outer.iter().any(|c| c.part.as_deref().is_some_and(|p| !p.is_empty())) || in_router(s)) + }; + let pages: Vec<&RrScope> = + scopes.iter().filter(|s| !s.pathless && s.part.is_some() && (s.jsx || s.outlet || s.component.is_some()) && addressed(s)).collect(); + let mut out = Vec::new(); + for s in &pages { + let Some(path) = path_of(s) else { continue }; + // a route around others is their layout when one of them claims its address + let claimed = pages.iter().any(|inner| { + !std::ptr::eq(*inner, *s) + && inner.jsx == s.jsx + && inner.component.is_some() + && holds(s, inner.start, inner.end) + && path_of(inner).as_deref() == Some(path.as_str()) + }); + if claimed || path.ends_with('*') { + continue; + } + let line = line_at(src, s.at); + let handlers = s.component.iter().map(|c| Handler { name: c.clone(), line, col: 0 }).collect(); + out.push(Route { method: String::new(), path, line, handlers, ref_kind: RefKind::Edge(EdgeKind::References), router: Some("react-router") }); + } + out.sort_by_key(|r| r.line); + out.dedup_by(|a, b| a.path == b.path && a.line == b.line); + out +} + +// ------------------------------------------------------------------- TanStack Router + +re!(tanstack_factory_re, r"\bcreate(?:File|Lazy(?:File)?|Root)?Route\s*\("); +re!(tanstack_file_re, r"\bcreate(?:Lazy)?FileRoute\s*\("); +re!(tanstack_named_re, r"\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]*?)?=\s*create(Root)?Route\s*\(\s*\{"); +re!(tanstack_lazy_import_re, r#"\bimport\s*\(\s*['"`]([^'"`]+)['"`]"#); +re!(tanstack_component_re, r"(?:^|[^\w$])component\s*:\s*([A-Z][A-Za-z0-9_]*)"); +re!(parent_arrow_re, r"=>\s*([A-Za-z_$][\w$]*)"); + +/// The component a route names: an identifier, or the file a lazy import names. +fn tanstack_component(text: &str) -> Option { + if let Some(c) = tanstack_lazy_import_re().captures(text) { + let file = c[1].rsplit('/').next().unwrap_or(""); + let stem = file.rsplit_once('.').map_or(file, |x| x.0); + return (!stem.is_empty()).then(|| stem.to_owned()); + } + if let Some(c) = tanstack_component_re().captures(text) { + return Some(c[1].to_owned()); + } + let t = text.trim(); + (t.starts_with(|c: char| c.is_ascii_uppercase()) && t.chars().all(|c| c.is_alphanumeric() || c == '_')).then(|| t.to_owned()) +} + +/// The call chain after a factory call: `({ … })` and any `.update({ … })` / `.lazy(…)`. +fn chain_after(s: &str, at: usize) -> &str { + let b = s.as_bytes(); + let mut i = at; + for _ in 0..8 { + while i < b.len() && b[i].is_ascii_whitespace() { + i += 1; + } + if b.get(i) == Some(&b'.') { + i += 1; + while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_' || b[i] == b'$') { + i += 1; + } + while i < b.len() && b[i].is_ascii_whitespace() { + i += 1; + } + } + if b.get(i) != Some(&b'(') { + break; + } + match match_bracket(s, i) { + Some(c) => i = c + 1, + None => break, + } + } + &s[at..i.min(s.len())] +} + +fn is_pathless_layout(raw: &str) -> bool { + if raw.len() > 1 && raw.ends_with('/') { + return false; // the index route inside the layout + } + raw.split('/').rfind(|s| !s.is_empty()).is_some_and(|l| l.starts_with('_')) +} + +pub fn scan_tanstack(rel: &str, src: &str) -> Vec { + if !tanstack_factory_re().is_match(src) { + return Vec::new(); + } + let base = rel.rsplit('/').next().unwrap_or(rel); + let layout_file = + base == "route.tsx" || base == "route.ts" || base == "route.jsx" || base == "route.js" || base.contains(".route.") || src.contains(", bool, u32)> = Vec::new(); + for m in tanstack_file_re().find_iter(src) { + let open = m.end() - 1; + let Some(close) = match_bracket(src, open) else { continue }; + let inner = src[open + 1..close].trim_start(); + let Some(raw) = read_string_at(inner, 0) else { continue }; + let Some(path) = tanstack_path(&raw) else { continue }; + if is_pathless_layout(&raw) || layout_file { + continue; + } + let index = raw.len() > 1 && raw.ends_with('/'); + entries.push((path, tanstack_component(chain_after(src, close + 1)), index, line_at(src, m.start()))); + } + // code-based routes: a path per route, composed through `getParentRoute` + struct Decl { + name: String, + path: Option, + parent: Option, + component: Option, + root: bool, + at: usize, + } + let mut decls: Vec = Vec::new(); + for c in tanstack_named_re().captures_iter(src) { + let whole = c.get(0).unwrap(); + let brace = whole.end() - 1; + let Some(end) = match_bracket(src, brace) else { continue }; + let obj = &src[brace..=end]; + let root = c.get(2).is_some(); + let path = if root { Some("/".to_owned()) } else { object_field(obj, "path").and_then(|v| read_string_at(v, 0)) }; + let parent = object_field(obj, "getParentRoute").and_then(|v| parent_arrow_re().captures(v)).map(|p| p[1].to_owned()); + let component = object_field(obj, "component").and_then(tanstack_component); + decls.push(Decl { name: c[1].to_owned(), path, parent, component, root, at: whole.start() }); + } + let wraps_index: Vec<&str> = decls.iter().filter(|d| d.path.as_deref() == Some("/") && d.parent.is_some()).filter_map(|d| d.parent.as_deref()).collect(); + for d in &decls { + let Some(own) = &d.path else { continue }; + if d.root || wraps_index.contains(&d.name.as_str()) { + continue; + } + // compose through the parents declared in this file + let mut segs: Vec = Vec::new(); + let mut cur = Some(d.name.as_str()); + let mut ok = true; + for _ in 0..24 { + let Some(name) = cur else { break }; + let Some(decl) = decls.iter().find(|x| x.name == name) else { + ok = false; + break; + }; + if let Some(p) = &decl.path { + let mut own: Vec = p.split('/').filter(|s| !s.is_empty()).map(str::to_owned).collect(); + own.extend(segs); + segs = own; + } + cur = decl.parent.as_deref(); + } + if !ok { + continue; + } + let Some(path) = tanstack_path(&format!("/{}", segs.join("/"))) else { continue }; + entries.push((path, d.component.clone(), own == "/", line_at(src, d.at))); + } + // one route per address: an index route outranks the layout that wraps it + let mut by_path: Vec<(String, Option, bool, u32)> = Vec::new(); + for e in entries { + match by_path.iter_mut().find(|x| x.0 == e.0) { + Some(held) if e.2 && !held.2 => *held = e, + Some(_) => {} + None => by_path.push(e), + } + } + by_path + .into_iter() + .map(|(path, component, _, line)| Route { + method: String::new(), + path, + line, + handlers: component.map(|c| Handler { name: c, line, col: 0 }).into_iter().collect(), + ref_kind: RefKind::Edge(EdgeKind::Calls), + router: Some("tanstack-router"), + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn names(r: &[Route]) -> Vec { + r.iter() + .map(|r| { + format!( + "{}{} -> {}", + if r.method.is_empty() { String::new() } else { format!("{} ", r.method) }, + r.path, + r.handlers.iter().map(|h| h.name.as_str()).collect::>().join("|") + ) + }) + .collect() + } + + #[test] + fn next_files() { + let r = |p: &str| next_route_of(p).map(|n| (n.kind, n.path, n.root)); + assert_eq!(r("app/users/[id]/page.tsx"), Some((NextKind::Page, "/users/:id".into(), "".into()))); + assert_eq!(r("apps/web/src/app/(shop)/cart/page.tsx"), Some((NextKind::Page, "/cart".into(), "apps/web/".into()))); + assert_eq!(r("app/docs/[...all]/page.mdx"), Some((NextKind::Page, "/docs/:all*".into(), "".into()))); + assert_eq!(r("app/api/x/route.ts"), Some((NextKind::Handler, "/api/x".into(), "".into()))); + assert_eq!(r("app/@modal/x/page.tsx"), None); + assert_eq!(r("app/layout.tsx"), None); + assert_eq!(r("pages/index.tsx"), Some((NextKind::Page, "/".into(), "".into()))); + assert_eq!(r("pages/api/users.ts"), Some((NextKind::Api, "/api/users".into(), "".into()))); + assert_eq!(r("pages/_app.tsx"), None); + assert_eq!(r("src/pages/blog/[slug].tsx"), Some((NextKind::Page, "/blog/:slug".into(), "".into()))); + assert_eq!(names(&scan_next("app/cart/page.tsx", "export default function Cart() { return null }")), vec!["/cart -> Cart"]); + assert_eq!(names(&scan_next("app/a/page.tsx", "function A() {}\nexport default memo(A);")), vec!["/a -> A"]); + assert_eq!( + names(&scan_next("app/api/x/route.ts", "export async function GET(req) {}\nexport const POST = async () => {}")), + vec!["GET /api/x -> GET", "POST /api/x -> POST"] + ); + } + + #[test] + fn nest_decorators() { + let src = r#" +@Controller('users') +export class UsersController { + constructor(private readonly svc: UsersService) {} + @Get(':id') + @UseGuards(AuthGuard) + async findOne(@Param('id') id: string) {} + @Post() + create(@Body() dto: Dto) {} +} +@Resolver(() => User) +export class UsersResolver { + @Query(() => [User], { name: 'users' }) + all() {} +} +@WebSocketGateway({ namespace: 'chat' }) +export class ChatGateway { + @SubscribeMessage('message') + handle() {} +} +@Controller() +export class Jobs { + @EventPattern('user.created') + onCreated() {} +} +"#; + assert_eq!( + names(&scan_nest(src)), + vec!["GET /users/:id -> findOne", "POST /users -> create", "QUERY users -> all", "WS chat:message -> handle", "EVENT user.created -> onCreated"] + ); + } + + #[test] + fn react_router_markup_and_objects() { + let src = r#" +export function App() { + return ( + + + }> + } /> + }> + } /> + + } /> + + + + + ); +} +"#; + assert_eq!( + names(&scan_react_router("src/App.tsx", src)), + vec!["/ -> Home", "/dashboard -> Dashboard", "/dashboard/team -> Team", "/payment -> PaymentScreen"] + ); + let objects = "const router = createBrowserRouter([\n { path: '/', element: , children: [\n { index: true, element: },\n { path: 'posts/:id', element: },\n ] },\n]);\n"; + assert_eq!(names(&scan_react_router("src/router.tsx", objects)), vec!["/ -> Index", "/posts/:id -> Post"]); + // a menu list that never reaches the router is no route table + assert!(scan_react_router("src/menu.ts", "export const menu = [{ path: '/x', element: Foo }];").is_empty()); + } + + #[test] + fn tanstack_routes() { + let file = "export const Route = createFileRoute('/posts/$postId')({\n component: PostComponent,\n})\n"; + assert_eq!(names(&scan_tanstack("src/routes/posts.$postId.tsx", file)), vec!["/posts/:postId -> PostComponent"]); + assert!(scan_tanstack("src/routes/_auth.tsx", "export const Route = createFileRoute('/_auth')({ component: A })").is_empty()); + let code = "const rootRoute = createRootRoute({ component: Root })\nconst postsRoute = createRoute({ getParentRoute: () => rootRoute, path: 'posts', component: Posts })\nconst postRoute = createRoute({ getParentRoute: () => postsRoute, path: '$postId', component: Post })\n"; + assert_eq!(names(&scan_tanstack("src/main.tsx", code)), vec!["/posts -> Posts", "/posts/:postId -> Post"]); + } +} diff --git a/crates/cg-resolve/src/frameworks/mod.rs b/crates/cg-resolve/src/frameworks/mod.rs index 25e974a..436c1eb 100644 --- a/crates/cg-resolve/src/frameworks/mod.rs +++ b/crates/cg-resolve/src/frameworks/mod.rs @@ -1,6 +1,9 @@ //! Framework rules (design doc 6.7): detection from manifests, extract hooks that add route //! nodes, resolve rules. The hook list is empty for projects that use none. +pub mod href; +pub mod jsroutes; +pub mod nav; pub mod routes; use crate::project::ProjectInfo; @@ -9,10 +12,24 @@ use routes::{Framework, RouteHook}; use std::sync::Arc; pub fn hooks(project: &ProjectInfo) -> Vec> { - let mut v: Vec> = Vec::new(); // synthesis site scanners: cheap literal prefilters keep them off files that cannot match - v.push(Arc::new(crate::synth::sites::SiteScanner)); - for name in project.detect_frameworks() { + let mut v: Vec> = vec![ + Arc::new(crate::synth::sites::SiteScanner), + Arc::new(crate::synth::sites::GoTypeSites), + Arc::new(crate::synth::dynamic::CallbackScanner), + Arc::new(crate::synth::dynamic::RegistryScanner), + Arc::new(crate::synth::dynamic::TierScanner), + Arc::new(crate::synth::dynamic::CeleryScanner), + Arc::new(crate::synth::dynamic::SpringEventScanner), + ]; + let frameworks = project.detect_frameworks(); + if frameworks.iter().any(|f| matches!(f.as_str(), "next" | "react-router" | "tanstack-router")) { + v.push(Arc::new(nav::NavScanner)); + } + if frameworks.iter().any(|f| f == "next") { + v.push(Arc::new(nav::ProxyScanner)); + } + for name in frameworks { let fw = match name.as_str() { "express" => Framework::Express, "flask" => Framework::Flask, @@ -22,6 +39,9 @@ pub fn hooks(project: &ProjectInfo) -> Vec> { "spring" => Framework::Spring, "axum" => Framework::Axum, "next" => Framework::Next, + "nestjs" => Framework::Nest, + "react-router" => Framework::ReactRouter, + "tanstack-router" => Framework::TanStack, _ => continue, }; v.push(Arc::new(RouteHook { fw })); diff --git a/crates/cg-resolve/src/frameworks/nav.rs b/crates/cg-resolve/src/frameworks/nav.rs new file mode 100644 index 0000000..aae1592 --- /dev/null +++ b/crates/cg-resolve/src/frameworks/nav.rs @@ -0,0 +1,822 @@ +//! Navigation between pages (upstream `nextjs.ts`, `react-router.ts`, `tanstack-router.ts` +//! resolvers and their link synthesizers) and the Next.js proxy (`next-proxy.ts`). +//! +//! A navigation is a string: `router.push('/users')`, `navigate({ to: '/posts/$id' })`, +//! `redirect('/login')`, ``, ``. Extraction records each call +//! and link with its destination expression (a bare identifier is read through its local +//! `const`); the link step matches the destination against the router's own route table and +//! draws a `navigates` edge from the function it is written in to the page. A computed +//! destination, a path no page serves, or a tie between routes draws nothing. +//! +//! A Next proxy (`middleware.ts`, `proxy.ts`) is the one file nothing imports: each route its +//! `config.matcher` accepts gets a `calls` edge to what runs in it. + +use super::href::{self, app_root_for, match_bracket, object_field, parse_href_expression, read_string_at, tanstack_path, Href, RootedTables, RouteTable}; +use super::jsroutes::next_route_of; +use crate::engine::Engine; +use crate::synth::scan::{call_args, enclosing_row, is_minified, local_initializer, mid_chain, LineIndex, FN_KINDS}; +use crate::synth::sites::{PASS_NAV, PASS_PROXY}; +use crate::synth::Out; +use cg_core::{EdgeKind, FileId, Lang, LangFamily, NodeIdx, NodeKind, NONE}; +use cg_extract::{FileHook, HookCtx, HookOut, SiteFact}; +use regex::Regex; +use rustc_hash::{FxHashMap, FxHashSet}; +use std::sync::OnceLock; + +pub const ROLE_NAV_CALL: u8 = 0; +pub const ROLE_NAV_LINK: u8 = 1; + +/// Links a single component may carry before it is a navigation menu, not a decision. +const MAX_LINKS_PER_COMPONENT: usize = 24; + +macro_rules! re { + ($name:ident, $pat:expr) => { + fn $name() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new($pat).expect("static regex")) + } + }; +} + +re!(nav_call_re, r"((?:[A-Za-z_$][\w$]*\s*\??\.\s*)*)(push|replace|prefetch|navigate|redirect|permanentRedirect)\s*\("); +re!(link_tag_re, r#"<(Link|NextLink|a|NavLink|Navigate|Redirect|LinkContainer|IndexLinkContainer)\b((?:[^>]|=>)*?)\b(href|to)\s*=\s*"#); +re!(next_nav_re, r"(?:^|\.)(push|replace|prefetch)$|^(redirect|permanentRedirect)$|^(?:NextResponse|Response)\.(redirect)$"); +re!(router_word_re, r"(?i)(?:router|navigation|history)$"); +re!(rr_nav_re, r"^(?:history|navigate|router)\.(?:push|replace|navigate)$|^(?:navigate|redirect)$"); +re!(tanstack_nav_re, r"^(?:navigate|redirect)$|^(?:router|Route)\.navigate$"); + +/// Destination text of a call's first argument, a bare identifier read through its local +/// declaration in the enclosing function. +fn destination_text<'a>(lines: &LineIndex<'a>, arg: &'a str, line: u32, floor: u32) -> &'a str { + let ident = arg.trim(); + if ident.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '$') && !ident.is_empty() { + if let Some(init) = local_initializer(lines, ident, line, floor) { + return init; + } + } + arg +} + +/// Records navigation calls and link markup of JS / TS files. +pub struct NavScanner; + +impl FileHook for NavScanner { + fn name(&self) -> &'static str { + "navigation-sites" + } + fn applies(&self, lang: Lang, rel: &str) -> bool { + lang.family() == LangFamily::Js && !cg_scan::testpath::is_test_path(rel) + } + fn triggers(&self) -> &'static [&'static str] { + &["push(", "replace(", "prefetch(", "navigate(", "redirect(", "Redirect(", "href", "to="] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + if is_minified(ctx.src) { + return; + } + let safe = cg_extract::post::strip_js_comments(ctx.src); + let src = safe.as_str(); + let lines = LineIndex::new(src); + for c in nav_call_re().captures_iter(src) { + let whole = c.get(0).unwrap(); + if mid_chain(src, whole.start()) { + continue; + } + let callee: String = whole.as_str()[..whole.as_str().len() - 1].chars().filter(|ch| !ch.is_whitespace() && *ch != '?').collect(); + let open = whole.end() - 1; + let Some(args) = call_args(src, open) else { continue }; + let Some(first) = args.first().copied().filter(|a| !a.is_empty()) else { continue }; + let line = lines.line(whole.start()); + let row = enclosing_row(ctx, line, FN_KINDS); + let floor = row.and_then(|r| ctx.rows.nodes().nth(r as usize)).map_or(line.saturating_sub(40), |n| n.start_line()); + let dest = destination_text(&lines, first, line, floor); + if dest.len() > 600 { + continue; + } + out.sites.push(SiteFact { + pass: PASS_NAV, + role: ROLE_NAV_CALL, + key_kind: 0, + key_a: c[2].to_owned(), + key_b: format!("{}|{callee}", lines.col(whole.start())), + node_row: row.unwrap_or(NONE), + line, + payload: dest.to_owned(), + }); + } + for c in link_tag_re().captures_iter(src) { + let whole = c.get(0).unwrap(); + let at = whole.end(); + let value = match src.as_bytes().get(at) { + Some(b'"' | b'\'') => href::string_end(src, at).map(|e| &src[at..=e]), + Some(b'{') => match_bracket(src, at).map(|e| src[at + 1..e].trim()), + _ => None, + }; + let Some(value) = value.filter(|v| v.len() <= 600) else { continue }; + let line = lines.line(whole.start()); + let Some(row) = enclosing_row(ctx, line, FN_KINDS) else { continue }; + out.sites.push(SiteFact { + pass: PASS_NAV, + role: ROLE_NAV_LINK, + key_kind: 0, + key_a: c[1].to_owned(), + key_b: c[3].to_owned(), + node_row: row, + line, + payload: value.to_owned(), + }); + } + } +} + +/// A TanStack destination: `{ to: '/posts/$id' }` or a string, normalised as a route name. +fn tanstack_destination(expr: &str) -> Vec { + let t = expr.trim(); + let literal = if t.starts_with('{') { object_field(t, "to").and_then(|v| read_string_at(v, 0)) } else { read_string_at(t, 0) }; + match literal.and_then(|l| tanstack_path(&l)) { + Some(p) => href::to_href(&p).into_iter().collect(), + None => parse_href_expression(t, "to"), + } +} + +/// `new URL('/login', req.url)` -> `'/login'`. +fn unwrap_url(expr: &str) -> &str { + let t = expr.trim(); + if let Some(rest) = t.strip_prefix("new URL") { + let open = t.len() - rest.trim_start().len(); + if t.as_bytes().get(open) == Some(&b'(') { + if let Some(args) = call_args(t, open) { + if let Some(first) = args.first() { + return first; + } + } + } + } + t +} + +struct Tables { + next: RouteTable, + next_roots: Vec, + react: RootedTables, + tanstack: RootedTables, +} + +fn route_tables(e: &Engine<'_>) -> Tables { + let view = e.view; + let mut t = Tables { next: RouteTable::default(), next_roots: Vec::new(), react: RootedTables::default(), tanstack: RootedTables::default() }; + let mut optional: Vec<(String, String, NodeIdx)> = Vec::new(); + for f in view.files() { + if view.file(f).lang().family() != LangFamily::Js { + continue; + } + let path = view.file(f).path(); + for n in view.nodes_in_file(f) { + let n = NodeIdx(n); + let node = view.node(n); + if node.kind() != NodeKind::Route || !node.name().starts_with('/') { + continue; + } + match node.signature() { + Some("next") => { + if let Some(r) = next_route_of(path) { + t.next.add(node.name(), n); + if !t.next_roots.contains(&r.root) { + t.next_roots.push(r.root); + } + } + } + Some("react-router") if !node.name().ends_with('*') => { + let root = app_root_for(path); + let name = node.name(); + let p = if name.len() > 1 { name.trim_end_matches('/') } else { name }; + t.react.add(root, p, n); + // `/cart/:id?` serves a bare `/cart` too, unless a route claims it + let mut segs: Vec<&str> = p.split('/').skip(1).collect(); + while segs.len() > 1 && segs.last().is_some_and(|s| s.starts_with(':') && s.ends_with('?')) { + segs.pop(); + optional.push((root.to_owned(), format!("/{}", segs.join("/")), n)); + } + } + Some("tanstack-router") => t.tanstack.add(app_root_for(path), node.name(), n), + _ => {} + } + } + } + for (root, path, n) in optional { + if t.react.for_file(&root).is_some_and(|tb| !tb.exact.contains_key(&path)) { + t.react.add(&root, &path, n); + } + } + t +} + +fn registered(e: &Engine<'_>, f: FileId, line: u32) -> String { + format!("{}:{}", e.view.file(f).path(), line) +} + +/// `navigates` edges for every recorded navigation call and link. +pub(crate) fn navigation(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + let tables = route_tables(e); + if tables.next.is_empty() && tables.react.is_empty() && tables.tanstack.is_empty() { + return; + } + let mut per_component: FxHashMap<(NodeIdx, &'static str), usize> = FxHashMap::default(); + let mut seen: FxHashSet<(NodeIdx, NodeIdx, u32, &'static str)> = FxHashSet::default(); + for f in view.files() { + let sites: Vec<_> = view.sites_of(f).iter().filter(|s| s.pass == PASS_NAV).collect(); + if sites.is_empty() { + continue; + } + let path = view.file(f).path(); + let in_next = !tables.next.is_empty() && tables.next_roots.iter().any(|r| path.starts_with(r.as_str())); + for s in sites { + let payload = view.text_str(s.payload); + let verb = view.text_str(cg_core::StrId(s.key_a)); + let key_b = view.text_str(cg_core::StrId(s.key_b)); + // (router, its routes, the arms, how it navigates) + let mut tries: Vec<(&'static str, &RouteTable, Vec, String)> = Vec::new(); + if s.role == ROLE_NAV_CALL { + let (col, callee) = key_b.split_once('|').unwrap_or(("", key_b)); + let _ = col; + // upstream takes any `x.push('/page')`; a receiver that is no router (an + // array, a queue) is not navigating, so it has to say so + let router_receiver = callee.rsplit_once('.').is_none_or(|(recv, _)| router_word_re().is_match(recv)); + if in_next && router_receiver { + if let Some(c) = next_nav_re().captures(callee) { + let how = if c.get(3).is_some() { "response.redirect".to_owned() } else { verb.to_owned() }; + tries.push(("next-navigation", &tables.next, parse_href_expression(unwrap_url(payload), "pathname"), how)); + } + } + if rr_nav_re().is_match(callee) { + if let Some(t) = tables.react.for_file(path) { + tries.push(("react-router-navigation", t, parse_href_expression(payload, "pathname"), verb.to_owned())); + } + } + if tanstack_nav_re().is_match(callee) { + if let Some(t) = tables.tanstack.for_file(path) { + tries.push(("tanstack-navigation", t, tanstack_destination(payload), verb.to_owned())); + } + } + } else { + let how = match verb { + "a" => "a", + "Navigate" => "navigate", + "Redirect" => "redirect", + _ => "link", + }; + match key_b { + "href" if in_next && matches!(verb, "Link" | "NextLink" | "a") => { + let arms: Vec = + parse_href_expression(payload, "pathname").into_iter().filter(|a| verb != "a" || a.path.starts_with('/')).collect(); + tries.push(("next-link", &tables.next, arms, how.to_owned())); + } + "to" => { + if let Some(t) = tables.react.for_file(path) { + let arms = parse_href_expression(payload, "pathname").into_iter().filter(|a| a.path.starts_with('/')).collect(); + tries.push(("react-router-link", t, arms, how.to_owned())); + } + if matches!(verb, "Link" | "Navigate") { + if let Some(t) = tables.tanstack.for_file(path) { + tries.push(("tanstack-link", t, tanstack_destination(payload), how.to_owned())); + } + } + } + _ => {} + } + } + let src = s.node; + for (by, table, arms, how) in tries { + let dests = table.destinations(&arms); + if dests.is_empty() { + continue; + } + let col = if s.role == ROLE_NAV_CALL { key_b.split_once('|').and_then(|(c, _)| c.parse::().ok()) } else { None }; + for (route, arm) in dests { + if route == src || !seen.insert((src, route, s.line, by)) { + continue; + } + if s.role == ROLE_NAV_LINK { + let count = per_component.entry((src, by)).or_default(); + *count += 1; + if *count > MAX_LINKS_PER_COMPONENT { + continue; + } + } + let meta = serde_json::json!({ "synthesizedBy": by, "href": arm.display(), "navMethod": how, "registeredAt": registered(e, f, s.line) }); + out.push_meta(EdgeKind::Navigates, src, route, s.line, col.unwrap_or(NONE), meta, crate::synth::order_of(by)); + } + // the first router that names a page answers the navigation + break; + } + } + } +} + +// ------------------------------------------------------------------- Next proxy + +const PROXY_EXTS: &[&str] = &["ts", "tsx", "js", "jsx", "mjs", "cjs"]; + +/// `middleware.ts` / `proxy.ts`: what runs in it and what its `config.matcher` accepts. +pub struct ProxyScanner; + +re!(export_config_re, r"\bexport\s+const\s+config\b[^=;]*=\s*"); +re!(default_fn_re, r"\bexport\s+default\s+(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)"); +re!(default_ident_re, r"(?m)\bexport\s+default\s+([A-Za-z_$][\w$]*)\s*;?\s*$"); +re!(export_as_re, r"\bexport\s*\{[^}]*?\b([A-Za-z_$][\w$]*)\s+as\s+(?:default|middleware|proxy)\b[^}]*\}"); + +fn proxy_file_kind(rel: &str) -> Option<&'static str> { + let base = rel.rsplit('/').next()?; + let (stem, ext) = base.rsplit_once('.')?; + if !PROXY_EXTS.contains(&ext) { + return None; + } + match stem { + "middleware" => Some("middleware"), + "proxy" => Some("proxy"), + _ => None, + } +} + +impl FileHook for ProxyScanner { + fn name(&self) -> &'static str { + "next-proxy-sites" + } + fn applies(&self, lang: Lang, rel: &str) -> bool { + lang.family() == LangFamily::Js && proxy_file_kind(rel).is_some() + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + let Some(kind) = proxy_file_kind(ctx.rel) else { return }; + let safe = cg_extract::post::strip_js_comments(ctx.src); + let src = safe.as_str(); + let lines = LineIndex::new(src); + // what runs: the export named after the file, else the default export's function + let named = ctx + .rows + .nodes() + .any(|n| n.name() == kind && n.is_exported() == Some(true) && matches!(n.kind(), NodeKind::Function | NodeKind::Constant | NodeKind::Variable)); + let entry = if named { + kind.to_owned() + } else { + default_fn_re() + .captures(src) + .or_else(|| default_ident_re().captures(src)) + .or_else(|| export_as_re().captures(src).filter(|c| !src[c.get(0).unwrap().end()..].trim_start().starts_with("from"))) + .map(|c| c[1].to_owned()) + .unwrap_or_default() + }; + // matcher entries: `line\tsource` per entry; `*` for no matcher (every request) + let payload = match export_config_re().find(src) { + None => "*".to_owned(), + Some(m) if src.as_bytes().get(m.end()) == Some(&b'{') => match match_bracket(src, m.end()) { + Some(close) => { + let obj = &src[m.end()..=close]; + match object_field(obj, "matcher") { + None if !obj.contains("matcher") => "*".to_owned(), + None => String::new(), + Some(value) => { + let base = m.end() + (value.as_ptr() as usize - obj.as_ptr() as usize); + matcher_entries(value).into_iter().map(|(off, s)| format!("{}\t{s}", lines.line(base + off))).collect::>().join("\n") + } + } + } + None => String::new(), + }, + Some(_) => String::new(), + }; + out.sites.push(SiteFact { pass: PASS_PROXY, role: 0, key_kind: 0, key_a: entry, key_b: kind.to_owned(), node_row: NONE, line: 1, payload }); + } +} + +/// The strings a matcher value names, with their offsets: a string, or an array of strings +/// and `{ source }` objects. A value it cannot read names nothing. +fn matcher_entries(value: &str) -> Vec<(usize, String)> { + let v = value.trim(); + let lead = value.len() - value.trim_start().len(); + if !v.starts_with('[') { + return read_string_at(v, 0).filter(|s| !s.contains(href::HOLE)).map(|s| vec![(lead, s)]).unwrap_or_default(); + } + let Some(end) = match_bracket(v, 0) else { return Vec::new() }; + let mut out = Vec::new(); + let body = &v[1..end]; + let mut start = 0; + loop { + let stop = href::index_at_depth0(body, b',', start).unwrap_or(body.len()); + let item = body[start..stop].trim(); + let item_at = lead + 1 + start + (body[start..stop].len() - body[start..stop].trim_start().len()); + if !item.is_empty() { + let s = if item.starts_with('{') { object_field(item, "source").and_then(|x| read_string_at(x, 0)) } else { read_string_at(item, 0) }; + match s.filter(|s| !s.contains(href::HOLE)) { + Some(s) => out.push((item_at, s)), + None => return Vec::new(), + } + } + if stop >= body.len() { + break; + } + start = stop + 1; + } + out +} + +/// A matcher entry compiled the way path-to-regexp reads it: anchored at both ends, `/:p*` +/// any number of segments, `(?!api|_next).*` everything but the excluded prefixes. An entry +/// this does not read matches nothing. +#[derive(Debug, PartialEq)] +enum Pat { + Lit(String), + /// `/:name` with an optional `(…)` body and a `?` / `*` / `+` modifier. + Param(Body, Option), + Group(Body), +} + +#[derive(Debug, PartialEq)] +enum Body { + Segment, + Any { + plus: bool, + }, + Options(Vec), + /// `(?!a|b).*`: the rest must not start with one of the exclusions. + Except(Vec, bool), +} + +#[derive(Debug, PartialEq)] +enum Excl { + Prefix(String, Anchor), + /// A path whose last segment has a dot (a file with an extension). + FileExtension, +} + +#[derive(Debug, PartialEq)] +enum Anchor { + None, + End, + SegmentOrEnd, +} + +fn closing_paren(s: &str, open: usize) -> Option { + let b = s.as_bytes(); + let mut depth = 0; + let mut i = open; + while i < b.len() { + match b[i] { + b'\\' => i += 1, + b'[' => { + while i < b.len() && b[i] != b']' { + i += if b[i] == b'\\' { 2 } else { 1 }; + } + } + b'(' => depth += 1, + b')' => { + depth -= 1; + if depth == 0 { + return Some(i); + } + } + _ => {} + } + i += 1; + } + None +} + +fn top_level_bars(s: &str) -> Vec<&str> { + let b = s.as_bytes(); + let mut out = Vec::new(); + let (mut from, mut i) = (0, 0); + while i < b.len() { + match b[i] { + b'\\' => i += 1, + b'(' => match closing_paren(s, i) { + Some(c) => i = c, + None => return vec![s], + }, + b'|' => { + out.push(&s[from..i]); + from = i + 1; + } + _ => {} + } + i += 1; + } + out.push(&s[from..]); + out +} + +fn read_exclusion(branch: &str) -> Option { + for lead in ["[^?]*\\.", ".*\\.", ".+\\."] { + if branch.starts_with(lead) { + return Some(Excl::FileExtension); + } + } + if branch.starts_with('[') && branch.contains("]+\\.") { + return Some(Excl::FileExtension); + } + let (body, anchor) = if let Some(b) = branch.strip_suffix("(?:/|$)") { + (b, Anchor::SegmentOrEnd) + } else if let Some(b) = branch.strip_suffix('$') { + (b, Anchor::End) + } else { + (branch, Anchor::None) + }; + let lit = body.replace("\\.", "."); + let ok = lit.chars().all(|c| c.is_alphanumeric() || "_/~.-".contains(c)); + ok.then_some(Excl::Prefix(lit, anchor)) +} + +fn read_group(body: &str) -> Option { + match body { + ".*" => return Some(Body::Any { plus: false }), + ".+" => return Some(Body::Any { plus: true }), + _ => {} + } + if body.starts_with("(?!") { + let close = closing_paren(body, 0)?; + let tail = &body[close + 1..]; + let plus = match tail { + ".*" => false, + ".+" => true, + _ => return None, + }; + let excls = top_level_bars(&body[3..close]).into_iter().map(read_exclusion).collect::>>()?; + return Some(Body::Except(excls, plus)); + } + let opts = top_level_bars(body.strip_prefix("?:").unwrap_or(body)); + opts.iter() + .all(|o| !o.is_empty() && o.chars().all(|c| c.is_alphanumeric() || "_./~-".contains(c))) + .then(|| Body::Options(opts.iter().map(|o| o.to_string()).collect())) +} + +fn compile_matcher(source: &str) -> Option> { + if !source.starts_with('/') || source.len() > 2048 { + return None; + } + let mut out = Vec::new(); + let b = source.as_bytes(); + let mut i = 0; + let mut lit = String::new(); + while i < b.len() { + if b[i] == b'/' && b.get(i + 1) == Some(&b':') { + let name_end = source[i + 2..].find(|c: char| !(c.is_alphanumeric() || c == '_')).map_or(source.len(), |e| i + 2 + e); + if name_end == i + 2 { + return None; + } + if !lit.is_empty() { + out.push(Pat::Lit(std::mem::take(&mut lit))); + } + i = name_end; + let mut body = Body::Segment; + if b.get(i) == Some(&b'(') { + let close = closing_paren(source, i)?; + body = read_group(&source[i + 1..close])?; + i = close + 1; + } + let modifier = b.get(i).filter(|c| matches!(c, b'?' | b'*' | b'+')).map(|c| *c as char); + if modifier.is_some() { + i += 1; + } + out.push(Pat::Param(body, modifier)); + continue; + } + if b[i] == b'(' { + let close = closing_paren(source, i)?; + if b.get(close + 1).is_some_and(|c| matches!(c, b'?' | b'*' | b'+')) { + return None; + } + if !lit.is_empty() { + out.push(Pat::Lit(std::mem::take(&mut lit))); + } + out.push(Pat::Group(read_group(&source[i + 1..close])?)); + i = close + 1; + continue; + } + let c = source[i..].chars().next()?; + if !(c.is_alphanumeric() || "_./~-".contains(c)) { + return None; + } + lit.push(c); + i += c.len_utf8(); + } + if !lit.is_empty() { + out.push(Pat::Lit(lit)); + } + Some(out) +} + +fn excluded(excls: &[Excl], rest: &str) -> bool { + excls.iter().any(|e| match e { + Excl::FileExtension => rest.rsplit('/').next().is_some_and(|last| last.contains('.')), + Excl::Prefix(p, anchor) => { + rest.starts_with(p.as_str()) + && match anchor { + Anchor::None => true, + Anchor::End => rest.len() == p.len(), + Anchor::SegmentOrEnd => rest.len() == p.len() || rest[p.len()..].starts_with('/'), + } + } + }) +} + +/// Lengths `body` can consume of `s` (shortest first). +fn body_spans(body: &Body, s: &str) -> Vec { + match body { + Body::Segment => { + let n = s.find('/').unwrap_or(s.len()); + if n > 0 { + vec![n] + } else { + vec![] + } + } + Body::Any { plus } => (if *plus { 1 } else { 0 }..=s.len()).filter(|n| s.is_char_boundary(*n)).collect(), + Body::Options(opts) => opts.iter().filter(|o| s.starts_with(o.as_str())).map(|o| o.len()).collect(), + Body::Except(excls, plus) => { + if excluded(excls, s) { + vec![] + } else { + (if *plus { 1 } else { 0 }..=s.len()).filter(|n| s.is_char_boundary(*n)).collect() + } + } + } +} + +fn matches_at(pats: &[Pat], s: &str, budget: &mut u32) -> bool { + if *budget == 0 { + return false; + } + *budget -= 1; + let Some((first, rest)) = pats.split_first() else { + return s.is_empty() || s == "/"; + }; + match first { + Pat::Lit(l) => s.strip_prefix(l.as_str()).is_some_and(|r| matches_at(rest, r, budget)), + Pat::Group(body) => body_spans(body, s).into_iter().any(|n| matches_at(rest, &s[n..], budget)), + Pat::Param(body, modifier) => { + // one `/` occurrence, repeated as the modifier allows + let (min, max) = match modifier { + None => (1, 1), + Some('?') => (0, 1), + Some('*') => (0, 64), + _ => (1, 64), + }; + fn rep(body: &Body, rest: &[Pat], s: &str, left: (usize, usize), budget: &mut u32) -> bool { + if left.0 == 0 && matches_at(rest, s, budget) { + return true; + } + if left.1 == 0 { + return false; + } + let Some(after) = s.strip_prefix('/') else { return false }; + body_spans(body, after).into_iter().any(|n| n > 0 && rep(body, rest, &after[n..], (left.0.saturating_sub(1), left.1 - 1), budget)) + } + rep(body, rest, s, (min, max), budget) + } + } +} + +/// Addresses a route serves, one per shape: a parameter is a segment no literal stands for. +fn route_addresses(path: &str) -> Vec { + let segs: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect(); + let filled: Vec<&str> = segs.iter().map(|s| if s.starts_with(':') { "\u{1}" } else { s }).collect(); + let mut out = vec![format!("/{}", filled.join("/"))]; + if segs.last().is_some_and(|l| l.ends_with('*')) { + out.push(format!("/{}", filled[..filled.len() - 1].join("/"))); + } + out +} + +fn covers(pats: &[Pat], route_path: &str) -> bool { + route_addresses(route_path).iter().any(|a| { + let mut budget = 20_000; + matches_at(pats, a, &mut budget) + }) +} + +/// The directory an app's proxy must sit in: beside `pages`, else beside `app`, at the app's +/// root before its `src/`. +fn proxy_dir(root: &str, route_files: &[&str]) -> String { + for dir in ["pages/", "app/"] { + if route_files.iter().any(|f| f.starts_with(&format!("{root}{dir}"))) { + return root.to_owned(); + } + if route_files.iter().any(|f| f.starts_with(&format!("{root}src/{dir}"))) { + return format!("{root}src/"); + } + } + root.to_owned() +} + +/// A Next route of an app: its node, its path and its file. +type AppRoute<'v> = (NodeIdx, String, &'v str); +/// A matcher entry: the line it is written on, its source and its compiled form. +type Matcher = (u32, String, Option>); + +/// `calls` edges from every Next route a proxy runs before to what runs in it. +pub(crate) fn next_proxy(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + // app root -> (route node, its path, its file) + let mut apps: Vec<(String, Vec)> = Vec::new(); + for f in view.files() { + let path = view.file(f).path(); + let Some(r) = next_route_of(path) else { continue }; + for n in view.nodes_in_file(f) { + let n = NodeIdx(n); + let node = view.node(n); + if node.kind() != NodeKind::Route || node.signature() != Some("next") { + continue; + } + match apps.iter_mut().find(|(root, _)| *root == r.root) { + Some((_, v)) => v.push((n, r.path.clone(), path)), + None => apps.push((r.root.clone(), vec![(n, r.path.clone(), path)])), + } + } + } + for (root, routes) in &apps { + let files: Vec<&str> = routes.iter().map(|r| r.2).collect(); + let dir = proxy_dir(root, &files); + let find = |stem: &str| PROXY_EXTS.iter().find_map(|ext| view.file_by_path(&format!("{dir}{stem}.{ext}"))); + let Some(file) = find("middleware").or_else(|| find("proxy")) else { continue }; + let Some(site) = view.sites_of(file).iter().find(|s| s.pass == PASS_PROXY) else { continue }; + let entry_name = view.text_str(cg_core::StrId(site.key_a)); + let entry = view + .nodes_in_file(file) + .map(NodeIdx) + .find(|n| { + !entry_name.is_empty() + && view.node(*n).name() == entry_name + && matches!(view.node(*n).kind(), NodeKind::Function | NodeKind::Constant | NodeKind::Variable) + }) + .unwrap_or(view.file(file).rec.node_start); + let payload = view.text_str(site.payload); + let matchers: Option> = (payload != "*").then(|| { + payload + .lines() + .filter_map(|l| l.split_once('\t')) + .map(|(line, source)| (line.parse().unwrap_or(1), source.to_owned(), compile_matcher(source))) + .collect() + }); + let file_path = view.file(file).path(); + for (route, path, _) in routes { + let hit = match &matchers { + None => None, + Some(list) => match list.iter().find(|(_, _, p)| p.as_ref().is_some_and(|p| covers(p, path))) { + Some(h) => Some(h), + None => continue, + }, + }; + let mut meta = serde_json::Map::new(); + meta.insert("synthesizedBy".into(), "next-proxy".into()); + if let Some((_, source, _)) = hit { + meta.insert("matcher".into(), source.clone().into()); + } + let line = hit.map_or(view.node(entry).start_line().max(1), |h| h.0); + meta.insert("registeredAt".into(), format!("{file_path}:{line}").into()); + out.push_meta( + EdgeKind::Calls, + *route, + entry, + view.node(*route).start_line(), + NONE, + serde_json::Value::Object(meta), + crate::synth::order_of("next-proxy"), + ); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn cov(m: &str, path: &str) -> bool { + covers(&compile_matcher(m).unwrap(), path) + } + + #[test] + fn proxy_matchers() { + assert!(cov("/about", "/about")); + assert!(!cov("/about", "/about/team")); + assert!(cov("/about/:path*", "/about")); + assert!(cov("/about/:path*", "/about/team/x")); + assert!(cov("/dashboard/:path+", "/dashboard/a")); + assert!(!cov("/dashboard/:path+", "/dashboard")); + let m = "/((?!api|_next/static|_next/image|favicon.ico).*)"; + assert!(cov(m, "/")); + assert!(cov(m, "/dashboard/:id")); + assert!(!cov(m, "/api/users")); + assert!(!cov("/((?!.*\\..*).*)", "/robots.txt")); + assert!(cov("/((?!.*\\..*).*)", "/blog")); + assert!(cov("/(en|fr)/:path*", "/fr/docs")); + assert!(compile_matcher("/x/(.*)+").is_none()); + assert_eq!(matcher_entries(" ['/a', { source: '/b/:p*' }]").into_iter().map(|e| e.1).collect::>(), vec!["/a", "/b/:p*"]); + assert!(matcher_entries("[`/a/${x}`]").is_empty()); + } + + #[test] + fn destinations() { + assert_eq!(tanstack_destination("{ to: '/posts/$postId', params }").iter().map(Href::display).collect::>(), vec!["/posts/:postId"]); + assert_eq!(unwrap_url("new URL('/login', req.url)"), "'/login'"); + } +} diff --git a/crates/cg-resolve/src/frameworks/routes.rs b/crates/cg-resolve/src/frameworks/routes.rs index 8ecf579..2cff8e1 100644 --- a/crates/cg-resolve/src/frameworks/routes.rs +++ b/crates/cg-resolve/src/frameworks/routes.rs @@ -19,6 +19,9 @@ pub enum Framework { Spring, Axum, Next, + Nest, + ReactRouter, + TanStack, } #[derive(Debug, PartialEq, Eq)] @@ -30,10 +33,23 @@ pub struct Handler { #[derive(Debug, PartialEq, Eq)] pub struct Route { + /// HTTP method, transport verb (`EVENT`, `WS`), or empty for a page named by its path. pub method: String, pub path: String, pub line: u32, pub handlers: Vec, + /// How the route reaches its handlers: a function value (`references`), or the page + /// component it renders (`calls`). + pub ref_kind: RefKind, + /// The router that declared a page (`next`, `react-router`, `tanstack-router`), stored as + /// the node's signature so the navigation pass finds its own routes. + pub router: Option<&'static str>, +} + +impl Default for Route { + fn default() -> Self { + Route { method: String::new(), path: String::new(), line: 0, handlers: Vec::new(), ref_kind: RefKind::FunctionRef, router: None } + } } struct Lines { @@ -68,6 +84,9 @@ macro_rules! re { }; } +re!(express_chain_re, r#"\b([A-Za-z_$][\w$]*)\s*\.\s*route\s*\(\s*(?:['"`])(/[^'"`\n]*)['"`]\s*\)"#); +re!(express_link_re, r"^\s*\.\s*(get|post|put|patch|delete|all)\s*\("); +re!(express_mount_re, r#"\b[A-Za-z_$][\w$]*\s*\.\s*use\s*\(\s*(['"`])(/[^'"`\n]*)['"`]\s*,"#); re!(express_re, r#"(?m)\b([A-Za-z_$][\w$]*)\s*\.\s*(get|post|put|delete|patch|options|head|all|use)\s*\(\s*(['"`])(/[^'"`\n]*)['"`]\s*,"#); re!( py_decorator_re, @@ -89,8 +108,6 @@ re!(spring_class_re, r"(?m)^[ \t]*(?:public[ \t]+|open[ \t]+|abstract[ \t]+)*(?: re!(axum_route_re, r#"\.route\(\s*"([^"\n]*)"\s*,\s*"#); re!(axum_method_re, r"(get|post|put|delete|patch|head|options|any)\s*\(\s*([\w:]+)\s*\)"); re!(ident_path_re, r"^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$"); -re!(next_default_re, r"(?m)export\s+default\s+(?:async\s+)?(?:function\s+)?([A-Za-z_$][\w$]*)"); -re!(next_method_re, r"(?m)export\s+(?:async\s+)?(?:function|const)\s+(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)\b"); /// Argument list of the call whose `(` is at/after `from`: top-level arguments as /// `(offset, text)`, plus the byte offset after the closing parenthesis. Strings and comments @@ -168,17 +185,19 @@ fn handlers_in(src: &str, lines: &Lines, args: &[(usize, &str)]) -> Vec out } +fn looks_like_router(recv: &str) -> bool { + matches!(recv, "app" | "router" | "server" | "api" | "route" | "routes" | "r") + || recv.ends_with("Router") + || recv.ends_with("App") + || recv.ends_with("Api") + || recv.ends_with("Routes") +} + pub fn scan_express(src: &str) -> Vec { let lines = Lines::new(src); let mut out = Vec::new(); for m in express_re().captures_iter(src) { - let recv = &m[1]; - let looks_like_router = matches!(recv, "app" | "router" | "server" | "api" | "route" | "routes" | "r") - || recv.ends_with("Router") - || recv.ends_with("App") - || recv.ends_with("Api") - || recv.ends_with("Routes"); - if !looks_like_router { + if !looks_like_router(&m[1]) { continue; } let method = m[2].to_ascii_uppercase(); @@ -188,7 +207,42 @@ pub fn scan_express(src: &str) -> Vec { let (line, _) = lines.at(src, whole.start()); let Some((args, _)) = call_args(src, open) else { continue }; let handlers = handlers_in(src, &lines, &args[1..]); - out.push(Route { method, path, line, handlers }); + out.push(Route { method, path, line, handlers, ..Default::default() }); + } + // the chained form: `router.route('/:id').get(getOne).put(auth, update)`, one route per + // method at the line of its `.method(` + for m in express_chain_re().captures_iter(src) { + if !looks_like_router(&m[1]) { + continue; + } + let path = m[2].to_owned(); + let mut at = m.get(0).unwrap().end(); + for _ in 0..8 { + let Some(link) = express_link_re().captures(&src[at..(at + 64).min(src.len())]) else { break }; + let open = at + link.get(0).unwrap().end() - 1; + let Some((args, end)) = call_args(src, open) else { break }; + let (line, _) = lines.at(src, open); + let handlers = handlers_in(src, &lines, &args); + out.push(Route { method: link[1].to_ascii_uppercase(), path: path.clone(), line, handlers, ..Default::default() }); + at = end; + } + } + out.sort_by_key(|r| r.line); + out +} + +/// `x.use('/prefix', …, target)`: where a router file is mounted, as (prefix, target text, +/// line). The target is the last argument: an identifier or an inline `require('./x')`. +pub fn express_mounts(src: &str) -> Vec<(String, String, u32)> { + let lines = Lines::new(src); + let mut out = Vec::new(); + for m in express_mount_re().captures_iter(src) { + let whole = m.get(0).unwrap(); + let Some(open) = whole.as_str().find('(').map(|i| whole.start() + i) else { continue }; + let Some((args, _)) = call_args(src, open) else { continue }; + let Some(last) = args.last().map(|a| a.1.trim()).filter(|a| !a.is_empty() && args.len() > 1) else { continue }; + let (line, _) = lines.at(src, whole.start()); + out.push((m[2].to_owned(), last.to_owned(), line)); } out } @@ -267,7 +321,13 @@ pub fn scan_python_decorators(src: &str) -> Vec { let (hline, hcol) = lines.at(src, call_end + dm.start()); let (line, _) = lines.at(src, whole.start()); for method in methods { - out.push(Route { method, path: path.clone(), line, handlers: vec![Handler { name: dm.as_str().to_owned(), line: hline, col: hcol }] }); + out.push(Route { + method, + path: path.clone(), + line, + handlers: vec![Handler { name: dm.as_str().to_owned(), line: hline, col: hcol }], + ..Default::default() + }); } } out @@ -288,7 +348,7 @@ pub fn scan_django(src: &str) -> Vec { let mut name = h.as_str().to_owned(); name = name.trim_end_matches(".as_view").to_owned(); let path = if m[2].starts_with('/') { m[2].to_owned() } else { format!("/{}", &m[2]) }; - Route { method: "ANY".into(), path, line, handlers: vec![Handler { name, line: hl, col: hc }] } + Route { method: "ANY".into(), path, line, handlers: vec![Handler { name, line: hl, col: hc }], ..Default::default() } }) .collect() } @@ -302,7 +362,7 @@ pub fn scan_gin(src: &str) -> Vec { let Some((args, _)) = call_args(src, open) else { continue }; let handlers = handlers_in(src, &lines, &args[1..]); let (line, _) = lines.at(src, whole.start()); - out.push(Route { method: m[2].to_ascii_uppercase(), path: m[3].to_owned(), line, handlers }); + out.push(Route { method: m[2].to_ascii_uppercase(), path: m[3].to_owned(), line, handlers, ..Default::default() }); } out } @@ -338,6 +398,7 @@ pub fn scan_spring(src: &str, kotlin: bool) -> Vec { path: if full.is_empty() { "/".into() } else { full }, line, handlers: vec![Handler { name: hm.as_str().to_owned(), line: hl, col: hc }], + ..Default::default() }); } out @@ -360,70 +421,7 @@ pub fn scan_axum(src: &str) -> Vec { path: m[1].to_owned(), line, handlers: vec![Handler { name: h.as_str().to_owned(), line: hl, col: hc }], - }); - } - } - out -} - -/// Route path of a Next.js file (`pages/users/[id].tsx` -> `/users/[id]`), or `None`. -pub fn next_route_of(rel: &str) -> Option<(String, bool)> { - let strip_ext = |s: &str| -> Option { - let (stem, ext) = s.rsplit_once('.')?; - matches!(ext, "ts" | "tsx" | "js" | "jsx" | "mjs").then(|| stem.to_owned()) - }; - let parts: Vec<&str> = rel.split('/').collect(); - let at = parts.iter().position(|p| *p == "pages" || *p == "app")?; - let (kind, rest) = (parts[at], &parts[at + 1..]); - if rest.is_empty() { - return None; - } - let last = strip_ext(rest[rest.len() - 1])?; - let mut segs: Vec = rest[..rest.len() - 1].iter().filter(|s| !(s.starts_with('(') && s.ends_with(')'))).map(|s| s.to_string()).collect(); - if kind == "pages" { - if last.starts_with('_') { - return None; - } - if last != "index" { - segs.push(last); - } - Some((format!("/{}", segs.join("/")), false)) - } else { - match last.as_str() { - "page" => Some((format!("/{}", segs.join("/")), false)), - "route" => Some((format!("/{}", segs.join("/")), true)), - _ => None, - } - } -} - -pub fn scan_next(rel: &str, src: &str) -> Vec { - let Some((path, is_handler)) = next_route_of(rel) else { return Vec::new() }; - let lines = Lines::new(src); - let mut out = Vec::new(); - let is_pages_api = rel.contains("pages/api/") || is_handler; - if is_handler && !rel.contains("pages/") { - for m in next_method_re().captures_iter(src) { - let g = m.get(1).unwrap(); - let (l, c) = lines.at(src, g.start()); - out.push(Route { - method: g.as_str().to_owned(), - path: path.clone(), - line: l, - handlers: vec![Handler { name: g.as_str().to_owned(), line: l, col: c }], - }); - } - return out; - } - if let Some(m) = next_default_re().captures(src) { - let g = m.get(1).unwrap(); - if !matches!(g.as_str(), "function" | "async" | "class") { - let (l, c) = lines.at(src, g.start()); - out.push(Route { - method: if is_pages_api { "ANY".into() } else { "GET".into() }, - path, - line: l, - handlers: vec![Handler { name: g.as_str().to_owned(), line: l, col: c }], + ..Default::default() }); } } @@ -434,7 +432,7 @@ pub fn scan(fw: Framework, lang: Lang, rel: &str, src: &str) -> Vec { // Framework repositories contain route examples in API comments. Those // snippets are documentation, not registrations in the program's graph. let code; - let src = if matches!(fw, Framework::Express | Framework::Gin) { + let src = if matches!(fw, Framework::Express | Framework::Gin | Framework::Next | Framework::Nest | Framework::ReactRouter | Framework::TanStack) { code = cg_extract::post::strip_js_comments(src); code.as_str() } else { @@ -453,7 +451,10 @@ pub fn scan(fw: Framework, lang: Lang, rel: &str, src: &str) -> Vec { Framework::Gin => scan_gin(src), Framework::Spring => scan_spring(src, lang == Lang::Kotlin), Framework::Axum => scan_axum(src), - Framework::Next => scan_next(rel, src), + Framework::Next => super::jsroutes::scan_next(rel, src), + Framework::Nest => super::jsroutes::scan_nest(src), + Framework::ReactRouter => super::jsroutes::scan_react_router(rel, src), + Framework::TanStack => super::jsroutes::scan_tanstack(rel, src), } } @@ -472,11 +473,15 @@ impl FileHook for RouteHook { Framework::Spring => "spring-routes", Framework::Axum => "axum-routes", Framework::Next => "next-routes", + Framework::Nest => "nest-routes", + Framework::ReactRouter => "react-router-routes", + Framework::TanStack => "tanstack-router-routes", } } - fn applies(&self, lang: Lang, _rel: &str) -> bool { + fn applies(&self, lang: Lang, rel: &str) -> bool { match self.fw { - Framework::Express | Framework::Next => lang.family() == LangFamily::Js, + Framework::Next => lang.family() == LangFamily::Js && super::jsroutes::next_route_of(rel).is_some(), + Framework::Express | Framework::Nest | Framework::ReactRouter | Framework::TanStack => lang.family() == LangFamily::Js, Framework::Flask | Framework::FastApi | Framework::Django => lang == Lang::Python, Framework::Gin => lang == Lang::Go, Framework::Spring => matches!(lang, Lang::Java | Lang::Kotlin), @@ -491,30 +496,71 @@ impl FileHook for RouteHook { Framework::Gin => &[".GET(", ".POST(", ".PUT(", ".DELETE(", ".PATCH(", ".Any(", ".OPTIONS(", ".HEAD("], Framework::Spring => &["Mapping"], Framework::Axum => &[".route("], - Framework::Next => &["export"], + Framework::Next => &[], + Framework::Nest => &[ + "@Get", + "@Post", + "@Put", + "@Patch", + "@Delete", + "@Head", + "@Options", + "@All", + "@Query", + "@Mutation", + "@Subscription", + "@MessagePattern", + "@EventPattern", + "@SubscribeMessage", + ], + Framework::ReactRouter => &[" &["createFileRoute", "createLazyFileRoute", "createRoute", "createRootRoute"], } } fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { let routes = scan(self.fw, ctx.lang, ctx.rel, ctx.src); + if self.fw == Framework::Express && ctx.src.contains(".use(") { + let code = cg_extract::post::strip_js_comments(ctx.src); + for (prefix, target, line) in express_mounts(&code) { + out.sites.push(cg_extract::SiteFact { + pass: crate::synth::sites::PASS_HTTP, + role: crate::synth::dynamic::ROLE_HTTP_MOUNT, + key_kind: 0, + key_a: prefix, + key_b: String::new(), + node_row: cg_core::NONE, + line, + payload: target, + }); + } + } let kcount = ctx.rows.node_count() as u32; for r in routes { - let name = format!("{} {}", r.method, r.path); + // a page is named by its path alone; a server route by its method and path + let (name, qname) = if r.method.is_empty() { + (r.path.clone(), format!("{}::route:{}", ctx.rel, r.path)) + } else { + (format!("{} {}", r.method, r.path), format!("{}::{}:{}", ctx.rel, r.method, r.path)) + }; + if out.nodes.iter().any(|n| n.qname == qname && n.start_line == r.line) { + continue; + } let row = kcount + out.nodes.len() as u32; out.nodes.push(ExtraNode { kind: NodeKind::Route, - qname: format!("{}::{}:{}", ctx.rel, r.method, r.path), + qname, digest: node_digest(ctx.rel, NodeKind::Route, &name, r.line), name, start_line: r.line, end_line: r.line, start_col: 0, end_col: 0, - signature: None, + signature: r.router.map(str::to_owned), parent_row: 0, exported: None, }); for h in r.handlers { - out.refs.push(ExtraRef { from_row: row, kind: RefKind::FunctionRef, name: h.name, line: h.line, col: h.col }); + out.refs.push(ExtraRef { from_row: row, kind: r.ref_kind, name: h.name, line: h.line, col: h.col }); } } } @@ -543,6 +589,24 @@ mod tests { ); } + #[test] + fn express_chains_and_mounts() { + let src = "router.route('/').get(getProducts).post(protect, admin, createProduct)\nrouter\n .route('/:id')\n .get(getProductById)\n .delete(protect, deleteProduct)\napp.use('/api/products', productRoutes)\napp.use('/api/users', require('./routes/users'))\napp.use(notFound)\n"; + assert_eq!( + names(&scan_express(src)), + vec![ + "GET / -> getProducts", + "POST / -> protect|admin|createProduct", + "GET /:id -> getProductById", + "DELETE /:id -> protect|deleteProduct", + "USE /api/products -> productRoutes", + "USE /api/users -> " + ] + ); + let mounts: Vec<_> = express_mounts(src).into_iter().map(|m| format!("{} {}", m.0, m.1)).collect(); + assert_eq!(mounts, vec!["/api/products productRoutes", "/api/users require('./routes/users')"]); + } + #[test] fn flask_and_fastapi() { let src = "@app.route('/a', methods=['GET', 'POST'])\n@login_required\ndef a():\n pass\n\n@router.get(\"/items/{id}\")\nasync def read_item(id: int):\n pass\n\n@app.route('/plain')\ndef plain(): pass\n"; @@ -580,17 +644,4 @@ mod tests { let src = "let app = Router::new()\n .route(\"/\", get(root))\n .route(\"/users\", get(list).post(handlers::create));"; assert_eq!(names(&scan_axum(src)), vec!["GET / -> root", "GET /users -> list", "POST /users -> handlers::create"]); } - - #[test] - fn next() { - assert_eq!(next_route_of("pages/users/[id].tsx"), Some(("/users/[id]".into(), false))); - assert_eq!(next_route_of("src/pages/index.tsx"), Some(("/".into(), false))); - assert_eq!(next_route_of("app/(shop)/cart/page.tsx"), Some(("/cart".into(), false))); - assert_eq!(next_route_of("app/api/x/route.ts"), Some(("/api/x".into(), true))); - assert_eq!(next_route_of("pages/_app.tsx"), None); - let page = "export default function Cart() { return null }"; - assert_eq!(names(&scan_next("app/cart/page.tsx", page)), vec!["GET /cart -> Cart"]); - let api = "export async function GET(req) {}\nexport const POST = async () => {}"; - assert_eq!(names(&scan_next("app/api/x/route.ts", api)), vec!["GET /api/x -> GET", "POST /api/x -> POST"]); - } } diff --git a/crates/cg-resolve/src/lib.rs b/crates/cg-resolve/src/lib.rs index 706fd62..4903132 100644 --- a/crates/cg-resolve/src/lib.rs +++ b/crates/cg-resolve/src/lib.rs @@ -8,6 +8,7 @@ pub use modules::{ImportTarget, Modules, PkgId}; pub mod engine; pub use engine::{Bound, Engine, ExportEntry}; pub mod builtins; +pub mod overload; pub mod shape; pub use shape::{parse_shape, Shape}; pub mod resolve; diff --git a/crates/cg-resolve/src/overload.rs b/crates/cg-resolve/src/overload.rs new file mode 100644 index 0000000..1eb8b21 --- /dev/null +++ b/crates/cg-resolve/src/overload.rs @@ -0,0 +1,684 @@ +//! Overload selection (upstream `javaOverloadsFitting` / `retargetSelfOverload`): when one +//! type or file declares several functions of a name, the call's arguments decide which one +//! it reaches. Arity comes first (required, optional, variadic parameters), then each argument +//! the call site makes readable is scored against the parameter's declared type: the same +//! type, a widening or boxing conversion, a project supertype. An overload the arguments +//! certainly cannot reach is ruled out; anything unread fits. When the arguments settle +//! nothing, the first declared overload stands, as before. + +use cg_core::{Lang, LangFamily, NodeIdx}; +use cg_extract::facts::args::ARGS_MARK; +use cg_store::{RefRec, View}; +use smallvec::SmallVec; + +/// Argument shapes of a call reference (see `cg_extract::facts::args`). +pub struct CallArgs<'v> { + pub shapes: SmallVec<[&'v str; 6]>, +} + +impl<'v> CallArgs<'v> { + pub fn of(view: &'v View, r: &RefRec) -> Option> { + if r.candidates.0 == cg_core::NONE { + return None; + } + let list = view.list(r.candidates); + let (first, rest) = list.split_first()?; + (*first == ARGS_MARK).then(|| CallArgs { shapes: rest.iter().copied().collect() }) + } +} + +/// What an argument is, as far as the call site and its bindings say. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ArgTy { + Unknown, + Null, + /// A lambda / function value taking this many parameters (`None`: unknown). + Lambda(Option), + /// A static type as written (`int`, `String`, `Foo`) and the project type it names. + Named(String, Option), +} + +/// One declared parameter. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Param { + /// Declared type, simplified: no generics, annotations, modifiers or nullability; `[]` + /// kept; empty when not written. + pub ty: String, + pub optional: bool, + pub variadic: bool, + /// Kotlin `T?`: accepts `null`. + pub nullable: bool, + /// A function type (`(a: A) => B`, `(A) -> B`): its parameter count when written. + pub function: Option>, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Params { + pub items: SmallVec<[Param; 6]>, +} + +impl Params { + fn required(&self) -> usize { + self.items.iter().filter(|p| !p.optional && !p.variadic).count() + } + fn variadic(&self) -> bool { + self.items.last().is_some_and(|p| p.variadic) + } + /// Whether `n` arguments can be passed. + pub fn takes(&self, n: usize) -> bool { + n >= self.required() && (self.variadic() || n <= self.items.len()) + } + /// The parameter the `i`th argument binds to. + fn param_for(&self, i: usize) -> Option<&Param> { + self.items.get(i).or_else(|| self.items.last().filter(|p| p.variadic)) + } +} + +/// Split a list body at top-level commas; `<`/`>` nest except in `=>` and `->`. +fn split_top(s: &str) -> SmallVec<[&str; 6]> { + let mut out = SmallVec::new(); + let (mut depth, mut start) = (0i32, 0usize); + let b = s.as_bytes(); + let mut i = 0; + while i < b.len() { + match b[i] { + b'"' | b'\'' | b'`' => { + let q = b[i]; + i += 1; + while i < b.len() && b[i] != q { + if b[i] == b'\\' { + i += 1; + } + i += 1; + } + } + b'(' | b'[' | b'{' | b'<' => depth += 1, + b'>' if i > 0 && matches!(b[i - 1], b'=' | b'-') => {} + b')' | b']' | b'}' | b'>' => depth -= 1, + b',' if depth == 0 => { + out.push(s[start..i].trim()); + start = i + 1; + } + _ => {} + } + i += 1; + } + let last = s[start..].trim(); + if !last.is_empty() || !out.is_empty() { + out.push(last); + } + out +} + +/// Byte range of the parameter list: the first `(` outside type arguments, to its `)`. +fn param_list(sig: &str) -> Option<&str> { + let b = sig.as_bytes(); + let mut angle = 0i32; + let mut open = None; + for (i, c) in b.iter().enumerate() { + match c { + b'<' => angle += 1, + b'>' if i > 0 && matches!(b[i - 1], b'=' | b'-') => {} + b'>' => angle -= 1, + b'(' if angle <= 0 => { + open = Some(i); + break; + } + _ => {} + } + } + let open = open?; + let mut depth = 0i32; + for (i, c) in b.iter().enumerate().skip(open) { + match c { + b'(' => depth += 1, + b')' => { + depth -= 1; + if depth == 0 { + return Some(&sig[open + 1..i]); + } + } + _ => {} + } + } + None +} + +/// `Map` -> `Map`, `java.util.List[]` -> `List[]`, `Foo?` -> `Foo`. +pub fn simplify_type(t: &str) -> String { + let mut out = String::with_capacity(t.len()); + let mut depth = 0i32; + let b = t.as_bytes(); + for (i, c) in t.char_indices() { + match c { + '<' => depth += 1, + '>' if i > 0 && matches!(b[i - 1], b'=' | b'-') => {} + '>' => depth -= 1, + _ if depth > 0 => {} + ' ' | '\t' | '\n' | '?' | '!' => {} + _ => out.push(c), + } + } + // drop package qualifiers: `java.lang.String` -> `String`, `ns.Foo` -> `Foo` + let arrays = out.len() - out.trim_end_matches("[]").len(); + let base = &out[..out.len() - arrays]; + let base = base.rsplit(['.', ':']).next().unwrap_or(base); + format!("{base}{}", &out[out.len() - arrays..]) +} + +fn function_arity(t: &str, arrow: &str) -> Option> { + let t = t.trim(); + let at = t.find(arrow)?; + let head = t[..at].trim(); + let head = head.strip_prefix("suspend").map(str::trim).unwrap_or(head); + let inner = head.strip_prefix('(').and_then(|h| h.strip_suffix(')')); + Some(inner.map(|i| split_top(i).into_iter().filter(|p| !p.is_empty()).count() as u8)) +} + +/// Parameters read off a stored signature: Java `void (String s, int... n)`, Kotlin +/// `(s: String, n: Int = 0)`, TypeScript `(s: string, n?: number): void`. +pub fn parse_params(lang: Lang, sig: &str) -> Option { + let body = param_list(sig)?; + let mut items = SmallVec::new(); + for raw in split_top(body) { + if raw.is_empty() { + continue; + } + let p = match lang { + Lang::Kotlin => kotlin_param(raw), + Lang::Java => java_param(raw), + l if l.family() == LangFamily::Js => match js_param(raw) { + Some(p) => p, + None => continue, // `this: Foo` + }, + _ => return None, + }; + items.push(p); + } + Some(Params { items }) +} + +fn java_param(raw: &str) -> Param { + let mut tokens: Vec<&str> = Vec::new(); + let mut rest = raw; + // annotations, possibly with arguments + loop { + rest = rest.trim_start(); + if let Some(r) = rest.strip_prefix('@') { + let end = r.find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '.')).unwrap_or(r.len()); + rest = &r[end..]; + if rest.trim_start().starts_with('(') { + if let Some(close) = rest.find(')') { + rest = &rest[close + 1..]; + } + } + continue; + } + if let Some(r) = rest.strip_prefix("final ") { + rest = r; + continue; + } + break; + } + let variadic = rest.contains("..."); + let rest = rest.replace("...", " "); + tokens.extend(rest.split_whitespace()); + // the last token is the name; the type is what precedes it + let ty = if tokens.len() >= 2 { tokens[..tokens.len() - 1].join("") } else { tokens.first().map(|t| t.to_string()).unwrap_or_default() }; + Param { ty: simplify_type(&ty), optional: false, variadic, nullable: true, function: None } +} + +fn kotlin_param(raw: &str) -> Param { + let variadic = raw.split_whitespace().any(|t| t == "vararg"); + let (_, ty) = raw.split_once(':').unwrap_or(("", raw)); + let parts = split_eq(ty); + let ty = parts.0.trim(); + let optional = parts.1; + let nullable = ty.ends_with('?'); + let function = function_arity(ty, "->"); + let ty = if function.is_some() { "Function".to_owned() } else { simplify_type(ty) }; + Param { ty, optional, variadic, nullable, function } +} + +/// Split `Type = default` at a top-level `=` that is not part of `=>` / `==`. +fn split_eq(s: &str) -> (&str, bool) { + let b = s.as_bytes(); + let mut depth = 0i32; + for i in 0..b.len() { + match b[i] { + b'(' | b'[' | b'{' | b'<' => depth += 1, + b')' | b']' | b'}' => depth -= 1, + b'>' if i > 0 && matches!(b[i - 1], b'=' | b'-') => {} + b'>' => depth -= 1, + b'=' if depth == 0 && b.get(i + 1) != Some(&b'>') && b.get(i + 1) != Some(&b'=') && (i == 0 || !matches!(b[i - 1], b'=' | b'!' | b'<' | b'>')) => { + return (&s[..i], true); + } + _ => {} + } + } + (s, false) +} + +fn js_param(raw: &str) -> Option { + let mut s = raw.trim(); + for m in ["public ", "private ", "protected ", "readonly ", "override "] { + while let Some(r) = s.strip_prefix(m) { + s = r.trim_start(); + } + } + let variadic = s.starts_with("..."); + let s = s.trim_start_matches("..."); + let (head, has_default) = split_eq(s); + // the name (or pattern) ends at the first top-level `:` / `?:` + let b = head.as_bytes(); + let mut depth = 0i32; + let mut colon = None; + for (i, c) in b.iter().enumerate() { + match c { + b'(' | b'[' | b'{' | b'<' => depth += 1, + b')' | b']' | b'}' | b'>' => depth -= 1, + b':' if depth == 0 => { + colon = Some(i); + break; + } + _ => {} + } + } + let (name, ty) = match colon { + Some(i) => (&head[..i], head[i + 1..].trim()), + None => (head, ""), + }; + let name = name.trim(); + if name == "this" { + return None; + } + let optional = has_default || name.ends_with('?'); + let ty_simple = if ty.contains("=>") { + "Function".to_owned() + } else if ty.contains('|') { + // a union takes several types: it rules nothing out by name + String::new() + } else { + simplify_type(ty) + }; + Some(Param { ty: ty_simple, optional, variadic, nullable: true, function: function_arity(ty, "=>") }) +} + +// ------------------------------------------------------------------- fitting + +const JAVA_PRIMITIVES: &[&str] = &["byte", "short", "int", "long", "float", "double", "char", "boolean"]; + +fn java_box(p: &str) -> Option<&'static str> { + Some(match p { + "byte" => "Byte", + "short" => "Short", + "int" => "Integer", + "long" => "Long", + "float" => "Float", + "double" => "Double", + "char" => "Character", + "boolean" => "Boolean", + _ => return None, + }) +} + +fn java_unbox(p: &str) -> Option<&'static str> { + JAVA_PRIMITIVES.iter().copied().find(|prim| java_box(prim) == Some(p)) +} + +/// Java's widening primitive conversions (JLS 5.1.2). +fn java_widens(from: &str, to: &str) -> bool { + let order: &[&str] = match from { + "byte" => &["short", "int", "long", "float", "double"], + "short" | "char" => &["int", "long", "float", "double"], + "int" => &["long", "float", "double"], + "long" => &["float", "double"], + "float" => &["double"], + _ => &[], + }; + order.contains(&to) +} + +/// JDK functional interfaces and the parameter count of their single method. +fn jdk_functional(t: &str) -> Option { + Some(match t { + "Runnable" | "Callable" | "Supplier" | "BooleanSupplier" | "IntSupplier" | "LongSupplier" | "DoubleSupplier" => 0, + "Consumer" | "Function" | "Predicate" | "UnaryOperator" | "IntFunction" | "IntPredicate" | "IntConsumer" | "IntUnaryOperator" | "LongFunction" + | "ToIntFunction" | "ToLongFunction" | "ToDoubleFunction" | "DoubleFunction" | "LongConsumer" | "DoubleConsumer" => 1, + "BiConsumer" | "BiFunction" | "BiPredicate" | "BinaryOperator" | "Comparator" | "IntBinaryOperator" | "ToIntBiFunction" => 2, + _ => return None, + }) +} + +/// Types every value of a JDK leaf type (String, a boxed primitive) is assignable to. +fn java_value_supertype(arg: &str, param: &str) -> bool { + match param { + "Object" | "Serializable" | "Comparable" | "Constable" | "ConstantDesc" => true, + "CharSequence" => arg == "String", + "Number" => java_unbox(arg).is_some_and(|p| !matches!(p, "char" | "boolean")) || JAVA_PRIMITIVES.contains(&arg) && !matches!(arg, "char" | "boolean"), + _ => false, + } +} + +/// A type name that stands for any type at all: a type parameter or a top type. +fn open_type(family: LangFamily, t: &str, type_params: &[&str]) -> bool { + if t.is_empty() || type_params.contains(&t) { + return true; + } + // `T`, `E`, `K2`: a single capital (and digits) is conventionally a type variable + let mut c = t.chars(); + if c.next().is_some_and(|f| f.is_ascii_uppercase()) && c.all(|x| x.is_ascii_digit()) { + return true; + } + match family { + LangFamily::Jvm => matches!(t, "Object" | "Any"), + LangFamily::Js => matches!(t, "any" | "unknown" | "object" | "Object" | "{}"), + _ => true, + } +} + +const KOTLIN_NUMBERS: &[&str] = &["Byte", "Short", "Int", "Long", "Float", "Double", "UByte", "UShort", "UInt", "ULong"]; +const JS_PRIMITIVES: &[&str] = &["string", "number", "boolean", "bigint", "symbol"]; + +/// How the project relates an argument to a parameter type written in one candidate's file: +/// `Some(true)` when it is that type or a subtype, `Some(false)` when the parameter is a +/// project type the argument cannot be, `None` when it cannot say. +pub type Relate<'a> = dyn Fn(&ArgTy, &str) -> Option + 'a; + +/// Score of passing `arg` for `param`: `None` when the language refuses it. +pub fn fit_arg(family: LangFamily, arg: &ArgTy, param: &Param, type_params: &[&str], relate: &Relate<'_>) -> Option { + let pt = param.ty.as_str(); + match arg { + ArgTy::Unknown => Some(1), + ArgTy::Null => match family { + LangFamily::Jvm if JAVA_PRIMITIVES.contains(&pt) => None, + LangFamily::Jvm if !param.nullable => None, + _ => Some(2), + }, + ArgTy::Lambda(k) => { + if let Some(want) = param.function { + return match (want, k) { + (Some(w), Some(k)) if w != *k => None, + _ => Some(3), + }; + } + if open_type(family, pt, type_params) { + return Some(1); + } + match family { + LangFamily::Jvm => { + if let Some(n) = jdk_functional(pt) { + return match k { + Some(k) if *k != n => None, + _ => Some(3), + }; + } + if JAVA_PRIMITIVES.contains(&pt) + || java_unbox(pt).is_some() + || KOTLIN_NUMBERS.contains(&pt) + || matches!(pt, "String" | "Boolean" | "Char") + || pt.ends_with("[]") + { + return None; + } + // a project class or enum can't take a lambda; an interface may be functional + match relate(&ArgTy::Lambda(*k), pt) { + Some(false) => None, + _ => Some(1), + } + } + LangFamily::Js => { + if JS_PRIMITIVES.contains(&pt) || pt.ends_with("[]") { + None + } else if pt == "Function" { + Some(3) + } else { + Some(1) + } + } + _ => Some(1), + } + } + ArgTy::Named(at, _) => { + let at = at.as_str(); + if open_type(family, pt, type_params) { + return Some(1); + } + if at == pt { + return Some(3); + } + if param.function.is_some() || pt == "Function" { + // a named value may hold a function; a primitive cannot + return if JAVA_PRIMITIVES.contains(&at) || JS_PRIMITIVES.contains(&at) || matches!(at, "String" | "Int" | "Long" | "Boolean") { + None + } else { + Some(1) + }; + } + match family { + LangFamily::Jvm => { + let at_prim = JAVA_PRIMITIVES.contains(&at); + let pt_prim = JAVA_PRIMITIVES.contains(&pt); + if at_prim && pt_prim { + return java_widens(at, pt).then_some(2); + } + if at_prim { + // boxing, then a widening reference conversion + if java_box(at) == Some(pt) { + return Some(2); + } + return java_value_supertype(at, pt).then_some(1); + } + if pt_prim { + // unboxing, then widening + return java_unbox(at).filter(|p| *p == pt || java_widens(p, pt)).map(|_| 2); + } + let kotlin_value = KOTLIN_NUMBERS.contains(&at) || matches!(at, "Boolean" | "Char"); + if kotlin_value || KOTLIN_NUMBERS.contains(&pt) { + // Kotlin: a numeric literal adapts to any numeric parameter + let both_numeric = KOTLIN_NUMBERS.contains(&at) && KOTLIN_NUMBERS.contains(&pt); + return if both_numeric { + Some(1) + } else if KOTLIN_NUMBERS.contains(&pt) || matches!(pt, "String" | "Boolean" | "Char") { + None + } else { + relate_or(relate, arg, pt, 1) + }; + } + if at == "String" || java_unbox(at).is_some() { + if java_value_supertype(at, pt) { + return Some(2); + } + return if matches!(pt, "String" | "Boolean" | "Char") || java_unbox(pt).is_some() || pt.ends_with("[]") { + None + } else { + relate_or(relate, arg, pt, 1) + }; + } + if at.ends_with("[]") != pt.ends_with("[]") { + return if pt.ends_with("[]") || matches!(pt, "String") || java_unbox(pt).is_some() { None } else { relate_or(relate, arg, pt, 1) }; + } + if matches!(pt, "String") || java_unbox(pt).is_some() { + // a project type is never a String or a boxed primitive + return if arg_is_project(arg) { None } else { Some(1) }; + } + relate_or(relate, arg, pt, 1) + } + LangFamily::Js => { + let at_prim = JS_PRIMITIVES.contains(&at); + let pt_prim = JS_PRIMITIVES.contains(&pt); + if at_prim || pt_prim { + // a primitive is only itself; a quoted literal type is a string + return if at == "string" && (pt.starts_with('\'') || pt.starts_with('"')) { + Some(2) + } else if at_prim && !pt_prim && !arg_is_project(arg) && relate(arg, pt).is_none() { + Some(1) + } else { + None + }; + } + relate_or(relate, arg, pt, 1) + } + _ => Some(1), + } + } + } +} + +/// A type no project declaration can be: a primitive, `String`, a boxed number. +pub fn is_value_type(t: &str) -> bool { + JAVA_PRIMITIVES.contains(&t) + || java_unbox(t).is_some() + || JS_PRIMITIVES.contains(&t) + || KOTLIN_NUMBERS.contains(&t) + || matches!(t, "String" | "Boolean" | "Char") +} + +/// Whether `n` arguments can be passed to the declaration with this signature (`None`: the +/// signature does not read). +pub fn takes(lang: Lang, sig: Option<&str>, n: usize) -> Option { + parse_params(lang, sig?).map(|p| p.takes(n)) +} + +fn arg_is_project(arg: &ArgTy) -> bool { + matches!(arg, ArgTy::Named(_, Some(_))) +} + +fn relate_or(relate: &Relate<'_>, arg: &ArgTy, pt: &str, unknown: u8) -> Option { + match relate(arg, pt) { + Some(true) => Some(2), + Some(false) => None, + None => Some(unknown), + } +} + +/// Fit of a whole overload: `None` when the arguments can't be passed to it, else a score +/// (variable arity ranks below fixed arity, as in Java's third phase). +pub fn fit(family: LangFamily, params: &Params, args: &[ArgTy], type_params: &[&str], relate: &Relate<'_>) -> Option { + if !params.takes(args.len()) { + return None; + } + let mut score = 10u32; + let mut spread = false; + for (i, a) in args.iter().enumerate() { + let p = params.param_for(i)?; + if p.variadic && i + 1 >= params.items.len() { + spread = true; + // `f(T... xs)` called with an array passes it whole + let mut elem = p.clone(); + if args.len() == params.items.len() && matches!(a, ArgTy::Named(t, _) if t.ends_with("[]")) { + elem.ty = format!("{}[]", p.ty); + } + score += fit_arg(family, a, &elem, type_params, relate)? as u32; + } else { + score += fit_arg(family, a, p, type_params, relate)? as u32; + } + } + if spread || (params.variadic() && args.len() < params.items.len()) { + score = score.saturating_sub(2); + } + Some(score) +} + +/// Index of the overload the arguments pick among the candidates (in preference order: the +/// first of several that fit alike wins), or `None` when none fits. +pub fn choose( + family: LangFamily, + sigs: &[Option], + args: &[ArgTy], + type_params: &[SmallVec<[&str; 2]>], + relate: &dyn Fn(usize, &ArgTy, &str) -> Option, +) -> Option { + let mut best: Option<(usize, u32)> = None; + for (i, sig) in sigs.iter().enumerate() { + let Some(sig) = sig else { continue }; + let tp: &[&str] = type_params.get(i).map_or(&[], |v| v.as_slice()); + if let Some(score) = fit(family, sig, args, tp, &|a: &ArgTy, t: &str| relate(i, a, t)) { + if best.is_none_or(|(_, b)| score > b) { + best = Some((i, score)); + } + } + } + best.map(|b| b.0) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn none(_: &ArgTy, _: &str) -> Option { + None + } + + fn none_i(_: usize, _: &ArgTy, _: &str) -> Option { + None + } + + fn named(t: &str) -> ArgTy { + ArgTy::Named(t.into(), None) + } + + #[test] + fn parses_signatures() { + let p = parse_params(Lang::Java, "void (int n, @Deprecated final long m)").unwrap(); + assert_eq!(p.items.iter().map(|p| p.ty.as_str()).collect::>(), vec!["int", "long"]); + let p = parse_params(Lang::Java, " void (Map> m, T... xs)").unwrap(); + assert_eq!(p.items.iter().map(|p| (p.ty.as_str(), p.variadic)).collect::>(), vec![("Map", false), ("T", true)]); + let p = parse_params(Lang::Kotlin, "(n: Int, m: Long = 0L, f: (Int) -> Unit)").unwrap(); + assert_eq!(p.items.iter().map(|p| (p.ty.as_str(), p.optional)).collect::>(), vec![("Int", false), ("Long", true), ("Function", false)]); + assert_eq!(p.items[2].function, Some(Some(1))); + let p = parse_params(Lang::Kotlin, "(vararg xs: Int)").unwrap(); + assert!(p.items[0].variadic && p.takes(0) && p.takes(3)); + let p = parse_params(Lang::TypeScript, "(this: Foo, n?: number, cb: (a: string) => void = noop, ...rest: string[]): void").unwrap(); + assert_eq!( + p.items.iter().map(|p| (p.ty.as_str(), p.optional, p.variadic)).collect::>(), + vec![("number", true, false), ("Function", true, false), ("string[]", false, true)] + ); + assert_eq!(parse_params(Lang::TypeScript, "(a: string, b = 3)").unwrap().items.len(), 2); + assert_eq!(simplify_type("java.util.List[]"), "List[]"); + } + + #[test] + fn java_conversions() { + let one = |sig: &str, args: &[ArgTy]| fit(LangFamily::Jvm, &parse_params(Lang::Java, sig).unwrap(), args, &[], &none); + assert!(one("void (String s)", &[named("int")]).is_none()); + assert!(one("void (long n)", &[named("int")]).is_some()); + assert!(one("void (Integer n)", &[named("int")]).is_some()); + assert!(one("void (int n)", &[named("long")]).is_none()); + assert!(one("void (Object o)", &[named("int")]).is_some()); + assert!(one("void (int n)", &[ArgTy::Null]).is_none()); + assert!(one("void (Runnable r)", &[ArgTy::Lambda(Some(0))]).is_some()); + assert!(one("void (Function f)", &[ArgTy::Lambda(Some(0))]).is_none()); + assert!(one("void (String s)", &[ArgTy::Lambda(Some(1))]).is_none()); + assert!(one("void (String... s)", &[named("String"), named("String")]).is_some()); + assert!(one("void (String a)", &[named("String"), named("String")]).is_none()); + // the exact type outranks a widening + let sigs = vec![parse_params(Lang::Java, "void (long n)"), parse_params(Lang::Java, "void (int n)")]; + assert_eq!(choose(LangFamily::Jvm, &sigs, &[named("int")], &[], &none_i), Some(1)); + // fixed arity outranks variable arity + let sigs = vec![parse_params(Lang::Java, "void (int... n)"), parse_params(Lang::Java, "void (int n)")]; + assert_eq!(choose(LangFamily::Jvm, &sigs, &[named("int")], &[], &none_i), Some(1)); + // nothing fits: nothing chosen + assert_eq!(choose(LangFamily::Jvm, &sigs, &[named("String"), named("String")], &[], &none_i), None); + // alike: the first declared stands + let sigs = vec![parse_params(Lang::Java, "void (Object o)"), parse_params(Lang::Java, "void (Object o)")]; + assert_eq!(choose(LangFamily::Jvm, &sigs, &[ArgTy::Unknown], &[], &none_i), Some(0)); + } + + #[test] + fn kotlin_and_ts() { + let k = |sig: &str, args: &[ArgTy]| fit(LangFamily::Jvm, &parse_params(Lang::Kotlin, sig).unwrap(), args, &[], &none); + assert!(k("(s: String)", &[named("Int")]).is_none()); + assert!(k("(n: Long)", &[named("Int")]).is_some(), "an integer literal adapts"); + assert!(k("(n: Int, m: Long = 0L)", &[named("Int")]).is_some()); + assert!(k("(s: String)", &[ArgTy::Null]).is_none()); + assert!(k("(s: String?)", &[ArgTy::Null]).is_some()); + assert!(k("(f: (Int) -> Unit)", &[ArgTy::Lambda(Some(2))]).is_none()); + let t = |sig: &str, args: &[ArgTy]| fit(LangFamily::Js, &parse_params(Lang::TypeScript, sig).unwrap(), args, &[], &none); + assert!(t("(s: string): void", &[named("number")]).is_none()); + assert!(t("(n: number, m?: number): void", &[named("number")]).is_some()); + assert!(t("(a: any, b?: any): void", &[named("string"), named("number")]).is_some()); + assert!(t("(cb: () => void)", &[named("string")]).is_none()); + assert!(t("(mode: 'a' | 'b')", &[named("string")]).is_some()); + } +} diff --git a/crates/cg-resolve/src/project.rs b/crates/cg-resolve/src/project.rs index c7c2d3b..3d5c8b5 100644 --- a/crates/cg-resolve/src/project.rs +++ b/crates/cg-resolve/src/project.rs @@ -182,6 +182,12 @@ impl ProjectInfo { add(npm("express"), "express"); add(npm("react") || npm("react-dom"), "react"); add(npm("next"), "next"); + add(self.packages.iter().any(|p| p.deps.iter().any(|d| d.starts_with("@nestjs/"))), "nestjs"); + add(npm("react-router") || npm("react-router-dom") || npm("react-router-native"), "react-router"); + add( + ["@tanstack/react-router", "@tanstack/solid-router", "@tanstack/router", "@tanstack/react-start", "@tanstack/start"].iter().any(|d| npm(d)), + "tanstack-router", + ); add(self.py_deps.contains("fastapi"), "fastapi"); add(self.py_deps.contains("flask"), "flask"); add(self.py_deps.contains("django"), "django"); diff --git a/crates/cg-resolve/src/resolve.rs b/crates/cg-resolve/src/resolve.rs index 96418c9..a26c563 100644 --- a/crates/cg-resolve/src/resolve.rs +++ b/crates/cg-resolve/src/resolve.rs @@ -6,6 +6,7 @@ use crate::builtins::{is_builtin, is_common_method}; use crate::engine::{Bound, Engine}; use crate::modules::{ImportTarget, PkgId}; +use crate::overload::{self, ArgTy, CallArgs}; use crate::shape::{parse_shape, split_head, Shape}; use cg_core::{text::pack_pos, EdgeKind, FileId, Lang, LangFamily, NodeIdx, NodeKind, RefKind, Visibility, NONE}; use cg_extract::facts::COMMONJS_MODULE; @@ -841,6 +842,169 @@ impl<'e, 'v> FileCx<'e, 'v> { result } + // ------------------------------------------------------- overload selection + /// What each argument of the call `r` is, from its shape and the bindings in scope. + fn arg_types(&self, wcx: &mut WorkerCx<'e, 'v>, args: &CallArgs<'v>, r: &RefRec) -> SmallVec<[ArgTy; 6]> { + let scope = self.scope_at(r.line, r.col); + args.shapes.iter().map(|s| self.arg_type(wcx, s, scope, r)).collect() + } + + fn named_arg(&self, wcx: &mut WorkerCx<'e, 'v>, written: &str) -> ArgTy { + let simple = overload::simplify_type(written); + if simple.is_empty() { + return ArgTy::Unknown; + } + let node = if overload::is_value_type(&simple) || simple.ends_with("[]") { + None + } else { + match self.lookup_type(wcx, written) { + TypeRes::Node(n) => Some(n), + _ => None, + } + }; + ArgTy::Named(simple, node) + } + + fn node_arg(&self, t: NodeIdx) -> ArgTy { + ArgTy::Named(self.view.node(t).name().to_owned(), Some(t)) + } + + fn arg_type(&self, wcx: &mut WorkerCx<'e, 'v>, shape: &str, scope: u32, r: &RefRec) -> ArgTy { + match shape { + "?" => ArgTy::Unknown, + "N" => ArgTy::Null, + "L" => ArgTy::Lambda(None), + "#this" => self.enclosing_type(r.from).map_or(ArgTy::Unknown, |t| self.node_arg(t)), + _ => { + if let Some(t) = shape.strip_prefix("T:") { + return self.named_arg(wcx, t); + } + if let Some(v) = shape.strip_prefix("V:") { + if let Some(b) = self.lookup_binding(v, scope, r.line, false) { + if b.kind == BindKind::LocalFn as u8 { + return ArgTy::Lambda(None); + } + let dt = self.view.text_str(b.decl_type); + if !dt.is_empty() { + return self.named_arg(wcx, dt); + } + return self.binding_type(wcx, b).map_or(ArgTy::Unknown, |t| self.node_arg(t)); + } + // a bare field: Java / Kotlin only (JS reads fields through `this.`) + return if self.family == LangFamily::Jvm { self.field_arg(wcx, r, v) } else { ArgTy::Unknown }; + } + if let Some(f) = shape.strip_prefix("F:") { + return self.field_arg(wcx, r, f); + } + match shape.strip_prefix('L').map(str::parse::) { + Some(Ok(k)) => ArgTy::Lambda(Some(k)), + _ => ArgTy::Unknown, + } + } + } + } + + /// Field `name` of the type enclosing the reference, by its declared type (primitives too). + fn field_arg(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str) -> ArgTy { + let Some(t) = self.enclosing_type(r.from) else { return ArgTy::Unknown }; + for ty in std::iter::once(t).chain(self.e.ancestors(t)) { + let cx = wcx.file_cx(self.e, self.view.hot(ty).file); + let Some(cs) = cx.class_scope_for(ty) else { continue }; + let Some(b) = cx.bindings.get(&(cs, name)).and_then(|l| l.iter().copied().find(|b| b.kind == BindKind::Field as u8)) else { continue }; + let dt = self.view.text_str(b.decl_type); + if !dt.is_empty() { + return cx.named_arg(wcx, dt); + } + return cx.binding_type(wcx, b).map_or(ArgTy::Unknown, |n| cx.node_arg(n)); + } + ArgTy::Unknown + } + + /// The overload of `cands` (same name, preference order) the call's arguments reach: + /// by count first, then by type. `None` when nothing is known or nothing fits. + fn pick_overload(&self, wcx: &mut WorkerCx<'e, 'v>, cands: &[NodeIdx], args: &CallArgs<'v>, r: &RefRec) -> Option { + if cands.len() < 2 { + return None; + } + let view = self.view; + let n = args.shapes.len(); + let lang_of = |c: NodeIdx| view.file(view.hot(c).file).lang(); + // arity settles most sets without typing a single argument + let by_count: SmallVec<[NodeIdx; 4]> = + cands.iter().copied().filter(|c| overload::takes(lang_of(*c), view.node(*c).signature(), n) != Some(false)).collect(); + match by_count.len() { + 0 => return None, + 1 => return Some(by_count[0]), + _ => {} + } + let types = self.arg_types(wcx, args, r); + let mut sigs = Vec::with_capacity(by_count.len()); + let mut tparams: Vec> = Vec::with_capacity(by_count.len()); + let mut param_nodes: Vec>> = Vec::with_capacity(by_count.len()); + for &c in &by_count { + let node = view.node(c); + let params = node.signature().and_then(|s| overload::parse_params(lang_of(c), s)); + let mut map = FxHashMap::default(); + if let Some(p) = ¶ms { + let cx = wcx.file_cx(self.e, node.file()); + for item in &p.items { + let base = item.ty.trim_end_matches("[]"); + if base.is_empty() || map.contains_key(&item.ty) || overload::is_value_type(base) || !base.starts_with(|ch: char| ch.is_ascii_uppercase()) { + continue; + } + let t = match cx.lookup_type(wcx, base) { + TypeRes::Node(t) => Some(t), + _ => None, + }; + map.insert(item.ty.clone(), t); + } + } + sigs.push(params); + tparams.push(node.type_params().into_iter().filter_map(|t| t.split(|c: char| !(c.is_alphanumeric() || c == '_')).next()).collect()); + param_nodes.push(map); + } + let e = self.e; + let relate = |i: usize, a: &ArgTy, pt: &str| -> Option { + let p = (*param_nodes[i].get(pt)?)?; + match a { + ArgTy::Named(_, Some(at)) => Some(*at == p || e.ancestors(*at).contains(&p)), + ArgTy::Named(name, None) => overload::is_value_type(name).then_some(false), + ArgTy::Lambda(_) => { + (!matches!(view.node(p).kind(), NodeKind::Interface | NodeKind::Trait | NodeKind::Protocol | NodeKind::TypeAlias)).then_some(false) + } + _ => None, + } + }; + overload::choose(self.family, &sigs, &types, &tparams, &relate).map(|i| by_count[i]) + } + + /// The overload of member `name` of `t` (or its ancestors) a call reaches, when the type + /// declares several. + fn overloaded_member(&self, wcx: &mut WorkerCx<'e, 'v>, t: NodeIdx, name: &str, args: Option<&CallArgs<'v>>, r: &RefRec) -> Option { + let args = args?; + if !matches!(self.family, LangFamily::Jvm | LangFamily::Js) { + return None; + } + let cands = self.e.member_overloads(t, name); + self.pick_overload(wcx, &cands, args, r) + } + + /// Same-named siblings of `pick` in its file (functions declared several times). + fn sibling_overloads(&self, pick: NodeIdx) -> SmallVec<[NodeIdx; 4]> { + let view = self.view; + let h = view.hot(pick); + let name = view.name_str(h.name); + let mut out: SmallVec<[NodeIdx; 4]> = view + .by_name(name) + .filter(|c| { + let ch = view.hot(*c); + ch.file == h.file && ch.parent == h.parent && view.node(*c).kind().is_callable() + }) + .collect(); + out.sort(); + out + } + // ------------------------------------------------------------ name lookup fn visible(&self, cand: NodeIdx, name: &str) -> bool { let h = self.view.hot(cand); @@ -887,7 +1051,7 @@ impl<'e, 'v> FileCx<'e, 'v> { } /// Name match over the whole project (design doc 6.5). - fn global_match(&self, name: &str, accept: Accept, bare_callable: bool) -> NameRes { + fn global_match(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str, accept: Accept, bare_callable: bool, args: Option<(&CallArgs<'v>, &RefRec)>) -> NameRes { let mut best: SmallVec<[NodeIdx; 4]> = SmallVec::new(); let mut best_score = 0u32; let mut seen = 0usize; @@ -934,10 +1098,12 @@ impl<'e, 'v> FileCx<'e, 'v> { NameRes::Found(best[0], by_, conf) } k => { - // overloads under one parent: the first declaration wins + // overloads under one parent: the arguments pick one, else the first declared let p0 = self.view.hot(best[0]).parent; if best.iter().all(|c| self.view.hot(*c).parent == p0 && self.view.node(*c).file() == self.view.node(best[0]).file()) { - return NameRes::Found(best[0], by::PACKAGE, 80); + best.sort(); + let pick = args.and_then(|(a, r)| self.pick_overload(wcx, &best, a, r)).unwrap_or(best[0]); + return NameRes::Found(pick, by::PACKAGE, 80); } NameRes::Ambiguous(k as u32) } @@ -945,9 +1111,14 @@ impl<'e, 'v> FileCx<'e, 'v> { } /// Member `name` of the type enclosing `from` (and of its outer types for Java / Kotlin). - fn enclosing_member(&self, from: Option, name: &str) -> Option { + fn enclosing_member(&self, wcx: &mut WorkerCx<'e, 'v>, from: Option, name: &str, args: Option<(&CallArgs<'v>, &RefRec)>) -> Option { let view = self.view; let t = self.enclosing_type(from?)?; + if let Some((a, r)) = args { + if let Some(m) = self.overloaded_member(wcx, t, name, Some(a), r) { + return Some(m); + } + } if let Some(m) = self.e.find_member(t, name) { return Some(m); } @@ -972,13 +1143,29 @@ impl<'e, 'v> FileCx<'e, 'v> { #[allow(clippy::too_many_arguments)] pub fn resolve_name( &self, - _wcx: &mut WorkerCx<'e, 'v>, + wcx: &mut WorkerCx<'e, 'v>, + name: &str, + accept: Accept, + scope: u32, + line: u32, + from: Option, + allow_global: bool, + ) -> NameRes { + self.resolve_name_args(wcx, name, accept, scope, line, from, allow_global, None) + } + + /// [`Self::resolve_name`] for a call whose arguments may pick among overloads. + #[allow(clippy::too_many_arguments)] + fn resolve_name_args( + &self, + wcx: &mut WorkerCx<'e, 'v>, name: &str, accept: Accept, scope: u32, line: u32, from: Option, allow_global: bool, + args: Option<(&CallArgs<'v>, &RefRec)>, ) -> NameRes { let view = self.view; let e = self.e; @@ -999,7 +1186,7 @@ impl<'e, 'v> FileCx<'e, 'v> { } // members of the enclosing class are visible by bare name in these languages if matches!(self.family, LangFamily::Jvm | LangFamily::Python) { - if let Some(m) = self.enclosing_member(from, name) { + if let Some(m) = self.enclosing_member(wcx, from, name, args) { return NameRes::Found(m, by::RECEIVER, 90); } } @@ -1036,7 +1223,7 @@ impl<'e, 'v> FileCx<'e, 'v> { } // JS / TS: `this.method()` is emitted as a bare `method`; an explicit import wins over it if accept != Accept::Type && self.family == LangFamily::Js { - if let Some(m) = self.enclosing_member(from, name) { + if let Some(m) = self.enclosing_member(wcx, from, name, args) { return NameRes::Found(m, by::RECEIVER, 88); } } @@ -1055,8 +1242,14 @@ impl<'e, 'v> FileCx<'e, 'v> { same.push(cand); } if !same.is_empty() { - // prefer top-level definitions, then the first declared - let pick = same.iter().copied().find(|n| view.node(*n).parent().is_some_and(|p| kind_of(view, p) == NodeKind::File)).unwrap_or(same[0]); + // prefer top-level definitions, then the first declared; overloads by the arguments + let mut pick = same.iter().copied().find(|n| view.node(*n).parent().is_some_and(|p| kind_of(view, p) == NodeKind::File)).unwrap_or(same[0]); + if let Some((a, r)) = args.filter(|_| same.len() > 1 && matches!(self.family, LangFamily::Jvm | LangFamily::Js)) { + let set = self.sibling_overloads(pick); + if let Some(m) = self.pick_overload(wcx, &set, a, r) { + pick = m; + } + } return NameRes::Found(pick, by::LEXICAL, 92); } // 5. same package / module @@ -1106,19 +1299,37 @@ impl<'e, 'v> FileCx<'e, 'v> { return NameRes::Builtin; } if allow_global { - return self.global_match(name, accept, bare_callable); + return self.global_match(wcx, name, accept, bare_callable, args); } NameRes::NotFound } // ------------------------------------------------------------ call forms - fn member_of(&self, wcx: &mut WorkerCx<'e, 'v>, mut t: NodeIdx, path: &[&str], method: &str, conf: u8) -> Outcome { + fn member_of(&self, wcx: &mut WorkerCx<'e, 'v>, t: NodeIdx, path: &[&str], method: &str, conf: u8) -> Outcome { + self.member_call(wcx, t, path, method, conf, None) + } + + /// `member_of` for a call: its arguments pick among overloads of the final member. + fn member_call( + &self, + wcx: &mut WorkerCx<'e, 'v>, + mut t: NodeIdx, + path: &[&str], + method: &str, + conf: u8, + args: Option<(&CallArgs<'v>, &RefRec)>, + ) -> Outcome { for f in path { match self.field_type(wcx, t, f) { Some(ft) => t = ft, None => return Outcome::st(status::NO_CANDIDATE), } } + if let Some((a, r)) = args { + if let Some(m) = self.overloaded_member(wcx, t, method, Some(a), r) { + return Outcome::found(m, by::RECEIVER, conf); + } + } match self.e.find_member(t, method) { Some(m) => Outcome::found(m, by::RECEIVER, conf), None => Outcome::st(status::NO_CANDIDATE), @@ -1198,9 +1409,9 @@ impl<'e, 'v> FileCx<'e, 'v> { } } - fn call_bare(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str, accept: Accept) -> Outcome { + fn call_bare(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str, accept: Accept, args: Option<&CallArgs<'v>>) -> Outcome { let scope = self.scope_at(r.line, r.col); - match self.resolve_name(wcx, name, accept, scope, r.line, Some(r.from), true) { + match self.resolve_name_args(wcx, name, accept, scope, r.line, Some(r.from), true, args.map(|a| (a, r))) { NameRes::Found(n, b, c) => Outcome::found(n, b, c), NameRes::Local => Outcome::st(status::LOCAL_BINDING), NameRes::External | NameRes::Module(_) | NameRes::Pkg(_) => Outcome::st(status::EXTERNAL), @@ -1210,15 +1421,16 @@ impl<'e, 'v> FileCx<'e, 'v> { } } - fn call_member(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, root: &str, path: &[&str], method: &str) -> Outcome { + fn call_member(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, root: &str, path: &[&str], method: &str, args: Option<&CallArgs<'v>>) -> Outcome { let scope = self.scope_at(r.line, r.col); let view = self.view; + let call = args.map(|a| (a, r)); // (a) a local variable, parameter or field if let Some(b) = self.lookup_binding(root, scope, r.line, false) { if b.kind != BindKind::LocalFn as u8 { return match self.binding_value(wcx, b, 0) { Some(t) if t.method_receiver(method).is_some() => { - let o = self.member_of(wcx, t.node, path, method, 90); + let o = self.member_call(wcx, t.node, path, method, 90, call); if o.status == status::RESOLVED { o } else { @@ -1249,6 +1461,13 @@ impl<'e, 'v> FileCx<'e, 'v> { }; } } + // (a2) Java / Kotlin: a field of the enclosing type or an outer one, `this.` implied + // (the kernel writes `this.repo.find()` as `repo.find`) + if self.family == LangFamily::Jvm { + if let Some(o) = self.call_on_field(wcx, r, root, path, method, call) { + return o; + } + } // (b) imported root if let Some(i) = self.visible_import(root, scope) { match self.e.bind_import(self.file, i as usize) { @@ -1266,7 +1485,7 @@ impl<'e, 'v> FileCx<'e, 'v> { Bound::Node(n) => { let k = kind_of(view, n); if k.is_type() { - return self.member_of(wcx, n, path, method, 92); + return self.member_call(wcx, n, path, method, 92, call); } // an imported module-level variable: the type of its initialiser { @@ -1312,7 +1531,7 @@ impl<'e, 'v> FileCx<'e, 'v> { match self.lookup_type(wcx, root) { TypeRes::Node(t) => { if view.node(t).kind().is_type() { - return self.member_of(wcx, t, path, method, 90); + return self.member_call(wcx, t, path, method, 90, call); } } TypeRes::External => return Outcome::st(status::EXTERNAL), @@ -1330,12 +1549,52 @@ impl<'e, 'v> FileCx<'e, 'v> { } } - fn call_this(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, kw: &str, fields: &[&str], method: &str) -> Outcome { + /// A call on field `root` of the type enclosing `r` (or of an outer type). `None` when no + /// such field is declared; a field whose declared type is outside the project ends here. + fn call_on_field( + &self, + wcx: &mut WorkerCx<'e, 'v>, + r: &RefRec, + root: &str, + path: &[&str], + method: &str, + call: Option<(&CallArgs<'v>, &RefRec)>, + ) -> Option { + let view = self.view; + let mut t = self.enclosing_type(r.from); + for _ in 0..8 { + let ty = t?; + if let Some(ft) = self.field_type(wcx, ty, root) { + let o = self.member_call(wcx, ft, path, method, 90, call); + return Some(if o.status == status::RESOLVED { o } else { Outcome::st(status::NO_CANDIDATE) }); + } + if let Some(declared) = self.field_decl_type(wcx, ty, root) { + // declared, not a project type: nothing in the project is its member + return (!declared.is_empty()).then(|| Outcome::st(status::EXTERNAL)); + } + t = view.node(ty).parent().filter(|p| kind_of(view, *p).is_type()); + } + None + } + + /// Declared type text of field `name` of `t` or an ancestor (`Some("")`: untyped). + fn field_decl_type(&self, wcx: &mut WorkerCx<'e, 'v>, t: NodeIdx, name: &str) -> Option { + for ty in std::iter::once(t).chain(self.e.ancestors(t)) { + let cx = wcx.file_cx(self.e, self.view.hot(ty).file); + let Some(cs) = cx.class_scope_for(ty) else { continue }; + if let Some(b) = cx.bindings.get(&(cs, name)).and_then(|l| l.iter().copied().find(|b| b.kind == BindKind::Field as u8)) { + return Some(self.view.text_str(b.decl_type).to_owned()); + } + } + None + } + + fn call_this(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, kw: &str, fields: &[&str], method: &str, args: Option<&CallArgs<'v>>) -> Outcome { let Some(t) = self.enclosing_type(r.from) else { return if fields.is_empty() { self.fallback_method(method) } else { Outcome::st(status::NO_CANDIDATE) }; }; let _ = kw; - let o = self.member_of(wcx, t, fields, method, 95); + let o = self.member_call(wcx, t, fields, method, 95, args.map(|a| (a, r))); if o.status == status::RESOLVED { return o; } @@ -1359,24 +1618,26 @@ impl<'e, 'v> FileCx<'e, 'v> { Outcome::st(status::NO_CANDIDATE) } - fn call_super(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, method: &str) -> Outcome { - let _ = wcx; + fn call_super(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, method: &str, args: Option<&CallArgs<'v>>) -> Outcome { let Some(t) = self.enclosing_type(r.from) else { return Outcome::st(status::NO_CANDIDATE) }; for a in self.e.ancestors(t) { if let Some(m) = self.e.direct_member(a, method) { - return Outcome::found(m, by::RECEIVER, 90); + // the overloads the nearest declaring supertype sees, its own and inherited + let pick = self.overloaded_member(wcx, a, method, args, r).unwrap_or(m); + return Outcome::found(pick, by::RECEIVER, 90); } } Outcome::st(status::NO_CANDIDATE) } - fn call_result(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, inner: &str, method: &str) -> Outcome { + fn call_result(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, inner: &str, method: &str, args: Option<&CallArgs<'v>>) -> Outcome { // type produced by calling `inner` let value = match self.call_value(wcx, inner, self.scope_at(r.line, r.col), r.line, 0) { Some(value) => Some(value), None => { - // `inner` may be a method of a typed receiver: resolve it as a call first - let o = self.resolve_call_name(wcx, r, inner); + // `inner` may be a method of a typed receiver: resolve it as a call first (its + // own arguments are not this reference's) + let o = self.call_name_with(wcx, r, inner, None); if o.status != status::RESOLVED { return Outcome::st(status::NO_CANDIDATE); } @@ -1385,25 +1646,30 @@ impl<'e, 'v> FileCx<'e, 'v> { } }; let Some(t) = value.and_then(|v| v.method_receiver(method)) else { return Outcome::st(status::NO_CANDIDATE) }; - self.member_of(wcx, t, &[], method, 85) + self.member_call(wcx, t, &[], method, 85, args.map(|a| (a, r))) } pub fn resolve_call_name(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str) -> Outcome { + let args = CallArgs::of(self.view, r); + self.call_name_with(wcx, r, name, args.as_ref()) + } + + fn call_name_with(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str, args: Option<&CallArgs<'v>>) -> Outcome { match parse_shape(name) { - Shape::Bare(n) => self.call_bare(wcx, r, n, Accept::Callable), - Shape::Member { root, path, method } => self.call_member(wcx, r, root, &path, method), - Shape::This { kw, fields, method } => self.call_this(wcx, r, kw, &fields, method), - Shape::Super { method } => self.call_super(wcx, r, method), + Shape::Bare(n) => self.call_bare(wcx, r, n, Accept::Callable, args), + Shape::Member { root, path, method } => self.call_member(wcx, r, root, &path, method, args), + Shape::This { kw, fields, method } => self.call_this(wcx, r, kw, &fields, method, args), + Shape::Super { method } => self.call_super(wcx, r, method, args), Shape::New { class, method } => match self.lookup_type(wcx, class) { - TypeRes::Node(t) => self.member_of(wcx, t, &[], method, 90), + TypeRes::Node(t) => self.member_call(wcx, t, &[], method, 90, args.map(|a| (a, r))), TypeRes::External => Outcome::st(status::EXTERNAL), _ => Outcome::st(status::NO_CANDIDATE), }, - Shape::CallResult { inner, method } => self.call_result(wcx, r, inner, method), + Shape::CallResult { inner, method } => self.call_result(wcx, r, inner, method, args), Shape::ValueResult { inner, method } => { let value = self.expression_value(wcx, inner, self.scope_at(r.line, r.col), r.line, 0); match value.and_then(|v| v.method_receiver(method)) { - Some(t) => self.member_of(wcx, t, &[], method, 85), + Some(t) => self.member_call(wcx, t, &[], method, 85, args.map(|a| (a, r))), None => Outcome::st(status::NO_CANDIDATE), } } @@ -1447,6 +1713,31 @@ impl<'e, 'v> FileCx<'e, 'v> { } } + /// Framework rule for a route's handler that no scope or import names: the method the + /// route decorates (`@Get() list()`, `@GetMapping get()`), declared right after the + /// decorator in the route's file. A bare method name is invisible to lexical lookup, but a + /// decorator route sits on the method it routes to. + fn route_handler(&self, r: &RefRec, name: &str, kind: RefKind) -> Option { + let view = self.view; + if kind_of(view, r.from) != NodeKind::Route || !matches!(kind, RefKind::FunctionRef | RefKind::Edge(EdgeKind::Calls | EdgeKind::References)) { + return None; + } + let Shape::Bare(name) = parse_shape(name) else { return None }; + let mut after: Option<(u32, NodeIdx)> = None; + for c in view.by_name(name) { + let h = view.hot(c); + let k = NodeKind::from_u8(h.kind).unwrap_or(NodeKind::Variable); + // stacked decorators and annotations sit between the route and its method + if h.file != self.file || !matches!(k, NodeKind::Method | NodeKind::Function) || h.start_line < r.line || h.start_line > r.line + 12 { + continue; + } + if after.is_none_or(|(l, n)| (h.start_line, c) < (l, n)) { + after = Some((h.start_line, c)); + } + } + Some(Outcome::found(after?.1, by::FRAMEWORK, 92)) + } + fn file_node(&self, f: FileId) -> NodeIdx { self.view.file(f).rec.node_start } @@ -1522,6 +1813,16 @@ impl<'e, 'v> FileCx<'e, 'v> { pub fn resolve_ref(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec) -> Outcome { let name = self.view.text_str(r.name); let Some(kind) = RefKind::from_wire(r.kind) else { return Outcome::st(status::NO_CANDIDATE) }; + let o = self.resolve_kind(wcx, r, name, kind); + if o.status == status::NO_CANDIDATE { + if let Some(found) = self.route_handler(r, name, kind) { + return found; + } + } + o + } + + fn resolve_kind(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str, kind: RefKind) -> Outcome { match kind { RefKind::Edge(EdgeKind::Imports) => self.resolve_import_ref(name, r), RefKind::Edge(EdgeKind::Extends | EdgeKind::Implements | EdgeKind::TypeOf | EdgeKind::Returns) => self.resolve_type_ref(wcx, r, name, Accept::Type), diff --git a/crates/cg-resolve/src/synth/dynamic.rs b/crates/cg-resolve/src/synth/dynamic.rs new file mode 100644 index 0000000..445f655 --- /dev/null +++ b/crates/cg-resolve/src/synth/dynamic.rs @@ -0,0 +1,1531 @@ +//! Dynamic dispatch and cross-boundary channels (upstream `callback-synthesizer.ts`, +//! `tier-synthesizer.ts`): calls that only exist at run time, closed by the string or field +//! both sides name. +//! +//! * `callback` - a registrar (`onChange(cb)`) stores the callback in a field +//! (`this.listeners.push(cb)`) that a dispatcher of the same file iterates +//! (`this.listeners.forEach(..)`): the dispatcher calls every named function registered. +//! * `object-registry` - `handlers[action](..)` dispatches through an object literal of +//! functions (`const handlers = { create: createUser, remove: removeUser }`). +//! * `http-client` - `fetch('/api/users')`, `api.post('/users')` reaches the one route that +//! serves the method and path. +//! * `queue-job` - `queue.add('welcome')` reaches the consumer of that job / queue. +//! * `event-bus` - `eventEmitter.emit('user.created')` reaches `@OnEvent('user.created')`, and +//! socket events cross between client and server. +//! * `celery-dispatch` - `task.delay()` reaches the `@shared_task` function. +//! * `spring-event` - `publishEvent(new E(..))` reaches every `@EventListener` of `E`. +//! +//! Extraction records the sites while the source is in memory; linking reads only sites and +//! the view. Every pass is capped per key, and an ambiguous name links nothing. + +use super::scan::{call_args, enclosing_row, is_minified, mid_chain, LineIndex, FN_KINDS}; +use super::sites::{PASS_BUS, PASS_CALLBACK, PASS_CELERY, PASS_HTTP, PASS_QUEUE, PASS_REGISTRY, PASS_SPRING}; +use super::{order_of, Out}; +use crate::engine::{Bound, Engine}; +use crate::frameworks::href::{index_at_depth0, match_bracket, read_string_at, HOLE}; +use crate::resolve::{Accept, FileCx, NameRes, WorkerCx}; +use cg_core::{EdgeKind, FileId, Lang, LangFamily, NodeIdx, NodeKind, RefKind, StrId, NONE}; +use cg_extract::{FileHook, HookCtx, HookOut, SiteFact}; +use cg_store::{status, View}; +use regex::Regex; +use rustc_hash::{FxHashMap, FxHashSet}; +use std::sync::OnceLock; + +macro_rules! re { + ($name:ident, $pat:expr) => { + fn $name() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new($pat).expect("static regex")) + } + }; +} + +const MAX_CALLBACKS_PER_CHANNEL: usize = 40; +const REGISTRY_FANOUT_CAP: usize = 40; +const EVENT_FANOUT_CAP: usize = 6; +const CELERY_FANOUT_CAP: usize = 80; +const SPRING_FANOUT_CAP: usize = 80; + +fn site(pass: u16, role: u8, key_a: impl Into, key_b: impl Into, node_row: Option, line: u32, payload: impl Into) -> SiteFact { + SiteFact { pass, role, key_kind: 0, key_a: key_a.into(), key_b: key_b.into(), node_row: node_row.unwrap_or(NONE), line, payload: payload.into() } +} + +fn text(view: &View, id: u32) -> &str { + view.text_str(StrId(id)) +} + +fn registered(view: &View, f: FileId, line: u32) -> String { + format!("{}:{}", view.file(f).path(), line) +} + +/// Whether the site's node is the file itself (no function encloses it). +fn is_file_node(view: &View, n: NodeIdx) -> bool { + view.node(n).kind() == NodeKind::File +} + +// =================================================================== callback channels + +pub const ROLE_REGISTRAR: u8 = 0; +pub const ROLE_DISPATCHER: u8 = 1; +pub const ROLE_REGISTRATION: u8 = 2; + +re!(registrar_name_re, r"^(?:on[A-Z]\w*|subscribe|addListener|addEventListener|register|watch|listen|addCallback)$"); +re!(dispatcher_name_re, r"(?i)(?:emit|trigger|notify|dispatch|fire|publish|flush)"); +re!(registrar_field_re, r"this\.(\w+)\.(?:add|push|set)\("); +re!(dispatch_for_of_re, r"\bof\s+(?:Array\.from\(\s*)?this\.(\w+)"); +re!(dispatch_call_re, r"\b\w+\s*\("); +re!(dispatch_for_each_re, r"this\.(\w+)\.forEach\("); +re!(registration_call_re, r"\.\s*(on[A-Z]\w*|subscribe|addListener|addEventListener|register|watch|listen|addCallback)\s*\(\s*(this\.\w+|\w+)\s*[,)]"); + +/// Registrars, dispatchers and the calls that register a named callback. +pub struct CallbackScanner; + +impl FileHook for CallbackScanner { + fn name(&self) -> &'static str { + "callback-sites" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + lang.family() == LangFamily::Js || matches!(lang, Lang::Java | Lang::Kotlin) + } + fn triggers(&self) -> &'static [&'static str] { + &["this.", "on", "subscribe", "register", "watch", "listen"] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + if is_minified(ctx.src) { + return; + } + let lines = LineIndex::new(ctx.src); + if ctx.src.contains("this.") { + for (row, n) in ctx.rows.nodes().enumerate() { + if !matches!(n.kind(), NodeKind::Method | NodeKind::Function) { + continue; + } + let is_reg = registrar_name_re().is_match(n.name()); + let is_disp = dispatcher_name_re().is_match(n.name()); + if !is_reg && !is_disp { + continue; + } + let body = lines.lines(n.start_line(), n.end_line()); + if is_reg { + if let Some(c) = registrar_field_re().captures(body) { + out.sites.push(site(PASS_CALLBACK, ROLE_REGISTRAR, &c[1], "", Some(row as u32), n.start_line(), "")); + } + } + if is_disp { + let field = match dispatch_for_of_re().captures(body) { + Some(c) if dispatch_call_re().is_match(body) => Some(c[1].to_owned()), + _ => dispatch_for_each_re().captures(body).map(|c| c[1].to_owned()), + }; + if let Some(field) = field { + out.sites.push(site(PASS_CALLBACK, ROLE_DISPATCHER, field, "", Some(row as u32), n.start_line(), "")); + } + } + } + } + for c in registration_call_re().captures_iter(ctx.src) { + let at = c.get(0).unwrap().start(); + let line = lines.line(at); + let Some(row) = enclosing_row(ctx, line, FN_KINDS) else { continue }; + out.sites.push(site(PASS_CALLBACK, ROLE_REGISTRATION, &c[1], "", Some(row), line, &c[2])); + } + } +} + +/// The resolved target of the reference of `file` at `line` whose name ends in `tail` and +/// whose kind is `kind`, resolved on the spot (synthesis runs before resolution is stored). +fn resolved_at<'e, 'v>( + cx: &FileCx<'e, 'v>, + wcx: &mut WorkerCx<'e, 'v>, + view: &'v View, + from: NodeIdx, + line: u32, + tail: &str, + want: &[RefKind], +) -> Option { + let mut hit = None; + for r in view.refs_of_file(cx.file) { + if r.line != line || r.from != from { + continue; + } + let Some(kind) = RefKind::from_wire(r.kind) else { continue }; + if !want.contains(&kind) { + continue; + } + let name = view.text_str(r.name); + if name != tail && !name.ends_with(&format!(".{tail}")) { + continue; + } + let o = cx.resolve_ref(wcx, r); + if o.status != status::RESOLVED { + continue; + } + if hit.is_some_and(|h| h != NodeIdx(o.target)) { + return None; + } + hit = Some(NodeIdx(o.target)); + } + hit +} + +pub(crate) fn callbacks<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, 'v>) { + let view = e.view; + // (file, field) -> dispatchers; registrar node -> field + let mut dispatchers: FxHashMap<(FileId, &str), Vec> = FxHashMap::default(); + let mut registrars: FxHashMap = FxHashMap::default(); + let mut registrations: Vec<(FileId, NodeIdx, u32, &str, &str)> = Vec::new(); + for f in view.files() { + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_CALLBACK) { + match s.role { + ROLE_REGISTRAR => { + registrars.insert(s.node, text(view, s.key_a)); + } + ROLE_DISPATCHER => dispatchers.entry((f, text(view, s.key_a))).or_default().push(s.node), + _ => registrations.push((f, s.node, s.line, text(view, s.key_a), view.text_str(s.payload))), + } + } + } + if registrars.is_empty() || dispatchers.is_empty() { + return; + } + let mut added: FxHashMap = FxHashMap::default(); + for (f, caller, line, reg_name, arg) in registrations { + let cx = wcx.file_cx(e, f); + // the call must reach a registrar whose field a dispatcher of its file iterates + let Some(reg) = resolved_at(&cx, wcx, view, caller, line, reg_name, &[RefKind::Edge(EdgeKind::Calls)]) else { continue }; + let Some(field) = registrars.get(®) else { continue }; + let Some(disps) = dispatchers.get(&(view.hot(reg).file, *field)) else { continue }; + // the callback: the function value passed at that line + let bare = arg.strip_prefix("this.").unwrap_or(arg); + let Some(callback) = resolved_at(&cx, wcx, view, caller, line, bare, &[RefKind::FunctionRef]) else { continue }; + if !matches!(view.node(callback).kind(), NodeKind::Method | NodeKind::Function) { + continue; + } + if !arg.starts_with("this.") && view.hot(callback).file != f && !imported_into(e, f, callback) { + continue; + } + for &d in disps { + let n = added.entry(reg).or_default(); + if *n >= MAX_CALLBACKS_PER_CHANNEL || d == callback { + continue; + } + *n += 1; + let meta = + serde_json::json!({ "synthesizedBy": "callback", "via": view.node(reg).name(), "field": field, "registeredAt": registered(view, f, line) }); + out.push_meta(EdgeKind::Calls, d, callback, view.node(d).start_line(), NONE, meta, order_of("callback")); + } + } +} + +/// Whether `f` imports the declaration `n` (or the module that declares it). +fn imported_into(e: &Engine<'_>, f: FileId, n: NodeIdx) -> bool { + let target = e.view.hot(n).file; + (0..e.view.imports_of(f).len()).any(|i| match e.bind_import(f, i) { + Bound::Node(b) => b == n, + Bound::Module(m) => m == target, + _ => false, + }) +} + +// =================================================================== object registries + +pub const ROLE_REGISTRY: u8 = 0; +pub const ROLE_REGISTRY_DISPATCH: u8 = 1; + +re!(registry_dispatch_re, r"(?:\bnew\s+)?((?:this\.)?[A-Za-z_$][\w$]*)\s*\[\s*([A-Za-z_$][\w$.]*)\s*\]\s*(?:\(|\.[A-Za-z_$])"); +re!(registry_assign_re, r"(?:(?:const|let|var)\s+([A-Za-z_$][\w$]*)|((?:this\.)?[A-Za-z_$][\w$]*))\s*=\s*\{"); +re!(chained_re, r"\]\s*\([^)]*\)\s*\.\s*([A-Za-z_$][\w$]*)|\]\s*\.\s*([A-Za-z_$][\w$]*)"); +re!(registry_entry_re, r#"^\s*(?:\[[^\]]+\]|['"]?[\w$]+['"]?)\s*:\s*([A-Za-z_$][\w$]*)\s*$"#); + +const REGISTRY_CLASS_ENTRY: &[&str] = &["execute", "run", "handle", "perform", "process", "call", "apply", "dispatch"]; + +/// `key: Identifier` entries at the top level of an object literal's body. +fn registry_entries(body: &str) -> Vec { + let mut names = Vec::new(); + let mut start = 0; + loop { + let stop = index_at_depth0(body, b',', start).unwrap_or(body.len()); + if let Some(c) = registry_entry_re().captures(&body[start..stop]) { + let n = c[1].to_owned(); + if n.len() >= 3 && !names.contains(&n) { + names.push(n); + } + } + if stop >= body.len() { + break; + } + start = stop + 1; + } + names +} + +pub struct RegistryScanner; + +impl FileHook for RegistryScanner { + fn name(&self) -> &'static str { + "registry-sites" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + lang.family() == LangFamily::Js + } + fn triggers(&self) -> &'static [&'static str] { + &["["] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + let src = ctx.src; + if is_minified(src) { + return; + } + let safe = cg_extract::post::strip_js_comments(src); + let lines = LineIndex::new(&safe); + let mut dispatches: Vec<(String, u32, String)> = Vec::new(); + for c in registry_dispatch_re().captures_iter(&safe) { + let m = c.get(0).unwrap(); + let start = c.get(1).unwrap().start(); + if mid_chain(&safe, start) && !safe[..start].ends_with("this.") { + continue; + } + let win = &safe[m.start()..(m.start() + 160).min(safe.len())]; + let win = &win[..(0..=win.len()).rev().find(|i| win.is_char_boundary(*i)).unwrap_or(0)]; + let chained = chained_re().captures(win).and_then(|cc| cc.get(1).or_else(|| cc.get(2))).map(|g| g.as_str().to_owned()).unwrap_or_default(); + let reg = c[1].trim_start_matches("this.").to_owned(); + dispatches.push((reg, lines.line(m.start()), chained)); + } + if dispatches.is_empty() { + return; + } + let wanted: FxHashSet<&str> = dispatches.iter().map(|d| d.0.as_str()).collect(); + let mut registries: FxHashMap, u32)> = FxHashMap::default(); + for c in registry_assign_re().captures_iter(&safe) { + let lhs = c.get(1).or_else(|| c.get(2)).unwrap().as_str().trim_start_matches("this."); + if !wanted.contains(lhs) || registries.contains_key(lhs) { + continue; + } + let open = c.get(0).unwrap().end() - 1; + let Some(close) = match_bracket(&safe, open) else { continue }; + let names = registry_entries(&safe[open + 1..close]); + if names.len() >= 2 { + registries.insert(lhs.to_owned(), (names, lines.line(c.get(0).unwrap().start()))); + } + } + for (reg, line, chained) in dispatches { + let Some((names, reg_line)) = registries.get(®) else { continue }; + let Some(row) = enclosing_row(ctx, line, FN_KINDS) else { continue }; + out.sites.push(site(PASS_REGISTRY, ROLE_REGISTRY_DISPATCH, ®, chained, Some(row), line, format!("{reg_line}|{}", names.join(",")))); + } + } +} + +/// A registered handler's callable entry: the function, or a class's `execute`-like method. +fn registry_target<'e, 'v>(e: &'e Engine<'v>, cx: &FileCx<'e, 'v>, wcx: &mut WorkerCx<'e, 'v>, name: &str, chained: &str) -> Option { + let view = e.view; + let n = match cx.resolve_name(wcx, name, Accept::Any, 0, u32::MAX, None, true) { + NameRes::Found(n, ..) => n, + _ => return None, + }; + match view.node(n).kind() { + NodeKind::Function | NodeKind::Method => Some(n), + NodeKind::Class | NodeKind::Struct => { + let want = REGISTRY_CLASS_ENTRY.contains(&chained).then_some(chained); + let members: Vec = + view.out_edges(n).filter(|ed| ed.kind == EdgeKind::Contains && view.node(ed.dst).kind() == NodeKind::Method).map(|ed| ed.dst).collect(); + let by = |p: &dyn Fn(&str) -> bool| members.iter().copied().find(|m| p(view.node(*m).name())); + Some(want.and_then(|w| by(&|m| m == w)).or_else(|| by(&|m| REGISTRY_CLASS_ENTRY.contains(&m))).or_else(|| by(&|m| m == "constructor")).unwrap_or(n)) + } + _ => None, + } +} + +pub(crate) fn registries<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, 'v>) { + let view = e.view; + for f in view.files() { + let sites: Vec<_> = view.sites_of(f).iter().filter(|s| s.pass == PASS_REGISTRY).collect(); + if sites.is_empty() { + continue; + } + let cx = wcx.file_cx(e, f); + let mut seen: FxHashSet<(NodeIdx, NodeIdx)> = FxHashSet::default(); + for s in sites { + let payload = view.text_str(s.payload); + let Some((reg_line, names)) = payload.split_once('|') else { continue }; + let chained = text(view, s.key_b); + let mut added = 0; + for name in names.split(',') { + if added >= REGISTRY_FANOUT_CAP { + break; + } + let Some(target) = registry_target(e, &cx, wcx, name, chained) else { continue }; + if target == s.node || !seen.insert((s.node, target)) { + continue; + } + added += 1; + let meta = + serde_json::json!({ "synthesizedBy": "object-registry", "via": name, "registeredAt": format!("{}:{reg_line}", view.file(f).path()) }); + out.push_meta(EdgeKind::Calls, s.node, target, s.line, NONE, meta, order_of("object-registry")); + } + } + } +} + +// =================================================================== cross-tier channels + +pub const ROLE_HTTP_CALL: u8 = 0; +pub const ROLE_HTTP_CLIENT: u8 = 1; +/// `app.use('/prefix', router)` (recorded by the Express route hook). +pub const ROLE_HTTP_MOUNT: u8 = 2; +pub const ROLE_QUEUE_BINDING: u8 = 0; +pub const ROLE_QUEUE_PRODUCER: u8 = 1; +pub const ROLE_QUEUE_CONSUMER: u8 = 2; +pub const ROLE_BUS_DISPATCH: u8 = 0; +pub const ROLE_BUS_HANDLER: u8 = 1; + +const GENERIC: &str = r"(?:<[^()<>]*(?:<[^()<>]*>[^()<>]*)*>)?"; +re!(bare_client_re, &format!(r"(?:(?:window|globalThis|global)\s*\.\s*)?\b(fetch|\$fetch|ofetch|axios|ky|got|useFetch|useSWR)\s*{GENERIC}\s*\(")); +re!( + member_client_re, + &format!( + r"((?:this\s*\.\s*)?[A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)*)\s*\.\s*(get|post|put|patch|delete|head|options|request|\$get|\$post|\$put|\$patch|\$delete)\s*{GENERIC}\s*\(" + ) +); +re!( + client_factory_re, + r"\b(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=;]*?)?=\s*(?:await\s+)?(?:(?:axios|ky|got|ofetch|\$fetch|wretch|redaxios)\s*\.\s*(?:create|extend)|new\s+Axios|wretch)\s*\(" +); +re!(default_client_re, r"\bexport\s+default\s+(?:(?:axios|ky|got|ofetch|\$fetch|wretch|redaxios)\s*\.\s*(?:create|extend)|new\s+Axios|wretch)\s*\("); +re!( + client_names_re, + r"^(?:axios|ky|got|superagent|http|https|httpClient|httpService|api|apiClient|client|restClient|request|agent|fetcher|instance|\$api|\$http|\$axios|axiosInstance|Axios|HttpClient|backend|server)$" +); +re!(server_names_re, r"^(?:app|router|route|routes|express|fastify|koa|hono|elysia|apiRouter|v1|v2|r)$"); +re!(method_option_re, r#"\bmethod\s*:\s*(['"`])([A-Za-z]+)['"`]"#); +re!(base_url_re, r"\b(?:baseURL|baseUrl|prefixUrl|baseURI)\s*:\s*"); +re!(url_key_re, r"\burl\s*:\s*"); +re!(queue_add_re, r#"((?:this\s*\.\s*)?[A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)*)\s*\.\s*add\s*\(\s*(['"`])([^'"`]+)['"`]"#); +re!(queue_shaped_re, r"(?i)queue|jobs?$|worker|bull|flow|producer"); +re!(new_worker_re, r#"\bnew\s+Worker\s*(?:<[^>]*>)?\s*\(\s*(['"`])([^'"`]+)['"`]\s*,\s*"#); +re!( + queue_process_re, + r#"((?:this\s*\.\s*)?[A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)*)\s*\.\s*process\s*\(\s*(?:(['"`])([^'"`]+)['"`]\s*,\s*)?(?:\d+\s*,\s*)?"# +); +re!( + queue_binding_re, + r#"\b(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=;]*?)?=\s*(?:new\s+)?(?:Queue|Bull|BullQueue)\s*(?:<[^>]*>)?\s*\(\s*(['"`])([^'"`]+)['"`]"# +); +re!(inject_queue_re, r#"@InjectQueue\s*\(\s*(['"`])([^'"`]+)['"`]\s*\)\s*(?:(?:private|public|protected|readonly)\s+)*([A-Za-z_$][\w$]*)"#); +re!(handler_arg_re, r"^(?:(?:async\s+)?([A-Za-z_$][\w$.]*)\s*(?:[,)]|$)|(?:async\s*)?(?:\(|function\b|[A-Za-z_$][\w$]*\s*=>))"); +re!(emit_re, r#"((?:[\w$]+(?:\([^()]*\))?\s*\.\s*)*[\w$]+)\s*\.\s*(emit|emitAsync)\s*\(\s*(['"`])([^'"`\n]+)['"`]"#); +re!(socket_on_re, r#"((?:[\w$]+(?:\([^()]*\))?\s*\.\s*)*[\w$]+)\s*\.\s*(?:on|once)\s*\(\s*(['"`])([^'"`\n]+)['"`]\s*,\s*"#); +re!( + socket_words_re, + r"^(?:socket|io|ws|wss|client|server|namespace|nsp|conn|connection|gateway|broadcast|to|in|of|except|volatile|local|sockets|socketServer|wsServer|room|channel|pusher|ably|ioClient|socketClient|sock)$" +); +re!( + bus_words_re, + r"^(?:eventEmitter|emitter|events|eventBus|bus|dispatcher|pubsub|publisher|eventPublisher|ee|hub|mediator|broker|messageBus|appEvents|domainEvents|eventsService|eventService)$" +); +re!( + generic_event_re, + r"^(?:error|connect|connect_error|connect_failed|connection|disconnect|disconnecting|reconnect|reconnect_attempt|reconnecting|reconnect_error|reconnect_failed|close|open|end|data|ready|drain|finish|pipe|unpipe|listening|timeout|ping|pong|upgrade|newListener|removeListener)$" +); +re!( + socket_server_re, + r#"@WebSocketGateway\s*\(|@SubscribeMessage\s*\(|@WebSocketServer\s*\(|\bnew\s+(?:Server|SocketIOServer|WebSocketServer|WebSocket\.Server|WSServer)\b|\.on\s*\(\s*['"]connection['"]"# +); +re!(class_after_re, r"\bclass\s+([A-Za-z_$][\w$]*)"); +re!(literal_re, r#"(['"`])([^'"`]+)['"`]"#); + +fn first_literal(args: &str) -> Option { + literal_re().captures(args).map(|c| c[2].to_owned()) +} + +fn all_literals(args: &str) -> Vec { + literal_re().captures_iter(args).map(|c| c[2].to_owned()).collect() +} + +/// Every `@Name(...)` decorator: (args, start, end). +fn decorators<'s>(src: &'s str, name: &str) -> Vec<(&'s str, usize, usize)> { + let pat = format!("@{name}"); + let mut out = Vec::new(); + let mut from = 0; + while let Some(rel) = src[from..].find(&pat) { + let at = from + rel; + from = at + pat.len(); + let rest = &src[from..]; + if rest.starts_with(|c: char| c.is_alphanumeric() || c == '_') { + continue; + } + let open = from + (rest.len() - rest.trim_start().len()); + if src.as_bytes().get(open) != Some(&b'(') { + continue; + } + if let Some(close) = match_bracket(src, open) { + out.push((&src[open + 1..close], at, close + 1)); + from = close + 1; + } + } + out +} + +/// The method a decorator ending at `end` sits on (stacked decorators and modifiers skipped). +fn decorated_method(ctx: &HookCtx<'_>, lines: &LineIndex<'_>, src: &str, end: usize, within: Option<(u32, u32)>) -> Option { + let mut i = end; + let b = src.as_bytes(); + loop { + while i < b.len() && b[i].is_ascii_whitespace() { + i += 1; + } + if b.get(i) == Some(&b'@') { + i += 1; + while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_' || b[i] == b'.') { + i += 1; + } + while i < b.len() && b[i].is_ascii_whitespace() { + i += 1; + } + if b.get(i) == Some(&b'(') { + i = match_bracket(src, i)? + 1; + } + continue; + } + let rest = &src[i..]; + let kw = ["public", "private", "protected", "async", "static", "readonly", "override"] + .iter() + .find(|k| rest.starts_with(**k) && rest[k.len()..].starts_with(char::is_whitespace)); + match kw { + Some(k) => i += k.len(), + None => break, + } + } + let rest = &src[i..]; + let len = rest.find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$')).unwrap_or(rest.len()); + if len == 0 { + return None; + } + let name = &rest[..len]; + let line = lines.line(i); + ctx.rows.nodes().enumerate().find_map(|(row, n)| { + let inside = within.is_none_or(|(s, e)| n.start_line() >= s && n.end_line() <= e); + (matches!(n.kind(), NodeKind::Method | NodeKind::Function) && n.name() == name && n.start_line() + 1 >= line && n.start_line() <= line + 1 && inside) + .then_some(row as u32) + }) +} + +/// The function a handler argument names (resolved at link time) or the inline function it is. +fn handler_site(ctx: &HookCtx<'_>, text: &str, line: u32) -> Option<(Option, String)> { + let c = handler_arg_re().captures(text.trim_start())?; + match c.get(1) { + Some(name) => Some((enclosing_row(ctx, line, FN_KINDS), name.as_str().to_owned())), + None => { + // an inline function: the function (or value) it is written in + let row = enclosing_row(ctx, line, FN_KINDS).or_else(|| enclosing_row(ctx, line, &[NodeKind::Constant, NodeKind::Variable]))?; + Some((Some(row), String::new())) + } + } +} + +fn shape_of(receiver: &str) -> Option<&'static str> { + let mut flat = String::new(); + let mut depth = 0; + for c in receiver.chars() { + match c { + '(' => depth += 1, + ')' => depth -= 1, + _ if depth == 0 && !c.is_whitespace() => flat.push(c), + _ => {} + } + } + let segs: Vec<&str> = flat.split('.').filter(|s| *s != "this").collect(); + if segs.iter().any(|s| socket_words_re().is_match(s)) { + Some("socket") + } else if segs.iter().any(|s| bus_words_re().is_match(s)) { + Some("bus") + } else { + None + } +} + +/// HTTP client calls, queue producers / consumers and event dispatches / handlers. +pub struct TierScanner; + +impl FileHook for TierScanner { + fn name(&self) -> &'static str { + "tier-sites" + } + fn applies(&self, lang: Lang, rel: &str) -> bool { + lang.family() == LangFamily::Js && !cg_scan::testpath::is_test_path(rel) + } + fn triggers(&self) -> &'static [&'static str] { + &[ + "fetch", + "axios", + "ky", + "got", + "useSWR", + ".get", + ".post", + ".put", + ".patch", + ".delete", + ".head", + ".options", + ".request", + ".$", + ".add(", + "Processor", + "Worker", + ".process(", + ".emit", + ".on(", + ".once(", + "OnEvent", + "SubscribeMessage", + ] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + if is_minified(ctx.src) { + return; + } + let safe = cg_extract::post::strip_js_comments(ctx.src); + let src = safe.as_str(); + let lines = LineIndex::new(src); + // client instances this file makes: `const api = axios.create({ baseURL })` + let base_url = |open: usize| -> String { + call_args(src, open).and_then(|a| a.first().and_then(|c| base_url_re().find(c).and_then(|m| read_string_at(c, m.end())))).unwrap_or_default() + }; + for c in client_factory_re().captures_iter(src) { + let open = c.get(0).unwrap().end() - 1; + out.sites.push(site(PASS_HTTP, ROLE_HTTP_CLIENT, &c[1], "", None, lines.line(c.get(0).unwrap().start()), base_url(open))); + } + if let Some(m) = default_client_re().find(src) { + out.sites.push(site(PASS_HTTP, ROLE_HTTP_CLIENT, "default", "", None, lines.line(m.start()), base_url(m.end() - 1))); + } + let mut http = |at: usize, open: usize, verb: Option<&str>, receiver: &str| { + let line = lines.line(at); + let Some(row) = enclosing_row(ctx, line, FN_KINDS) else { return }; + let Some(args) = call_args(src, open) else { return }; + let Some(mut first) = args.first().copied().filter(|a| !a.is_empty()) else { return }; + let mut method = verb.map(str::to_owned); + if first.starts_with('{') { + // `axios({ url, method })`, `.request({ url, method })` + let Some(m) = url_key_re().find(first) else { return }; + method = method.or_else(|| method_option_re().captures(first).map(|c| c[2].to_ascii_uppercase())).or(Some("GET".into())); + first = &first[m.end()..]; + } else if method.is_none() { + method = Some(method_option_re().captures(&args[1..].join(",")).map(|c| c[2].to_ascii_uppercase()).unwrap_or_else(|| "GET".into())); + } + let first = first.trim_start(); + let first = first.strip_prefix("new URL(").map_or(first, |r| r.trim_start()); + let Some(literal) = read_string_at(first, 0) else { return }; + let callee: String = src[at..open].chars().filter(|c| !c.is_whitespace()).collect(); + let callee = callee.split('<').next().unwrap_or("").to_owned(); + out.sites.push(site( + PASS_HTTP, + ROLE_HTTP_CALL, + method.unwrap_or_default(), + format!("{}|{receiver}|{callee}", lines.col(at)), + Some(row), + line, + literal, + )); + }; + for m in bare_client_re().find_iter(src) { + let lead = &src[..m.start()]; + if lead.ends_with('.') && !m.as_str().starts_with(['w', 'g']) { + continue; // `this.fetch()` is a project method + } + if mid_chain(src, m.start()) && !lead.ends_with('.') { + continue; + } + http(m.start(), m.end() - 1, None, ""); + } + for c in member_client_re().captures_iter(src) { + let recv = c.get(1).unwrap(); + if mid_chain(src, recv.start()) { + continue; + } + let verb = c[2].trim_start_matches('$').to_ascii_uppercase(); + let receiver: String = recv.as_str().chars().filter(|ch| !ch.is_whitespace()).collect(); + http(recv.start(), c.get(0).unwrap().end() - 1, (verb != "REQUEST").then_some(verb.as_str()), &receiver); + } + + // queues: bindings, producers, consumers + for c in queue_binding_re().captures_iter(src) { + out.sites.push(site(PASS_QUEUE, ROLE_QUEUE_BINDING, &c[1], "", None, lines.line(c.get(0).unwrap().start()), &c[3])); + } + for c in inject_queue_re().captures_iter(src) { + out.sites.push(site(PASS_QUEUE, ROLE_QUEUE_BINDING, &c[3], "", None, lines.line(c.get(0).unwrap().start()), &c[2])); + } + for c in queue_add_re().captures_iter(src) { + let recv = c.get(1).unwrap(); + if mid_chain(src, recv.start()) { + continue; + } + let line = lines.line(recv.start()); + let Some(row) = enclosing_row(ctx, line, FN_KINDS) else { continue }; + let receiver: String = recv.as_str().chars().filter(|ch| !ch.is_whitespace()).collect(); + out.sites.push(site(PASS_QUEUE, ROLE_QUEUE_PRODUCER, &c[3], format!("{}|{receiver}", lines.col(recv.start())), Some(row), line, "")); + } + // `@Processor('email')` on a class: its `@Process('job')` methods, or its `process` + for (args, _, end) in decorators(src, "Processor") { + let queue = first_literal(args).unwrap_or_default(); + let Some(cm) = class_after_re().captures(&src[end..]) else { continue }; + let cls_line = lines.line(end + cm.get(0).unwrap().start()); + let Some(cls) = ctx.rows.nodes().find(|n| n.kind() == NodeKind::Class && n.name() == &cm[1] && n.start_line() + 2 >= cls_line) else { continue }; + let span = (cls.start_line(), cls.end_line()); + let mut any = false; + for (jargs, jat, jend) in decorators(src, "Process") { + let line = lines.line(jat); + if line < span.0 || line > span.1 { + continue; + } + let Some(row) = decorated_method(ctx, &lines, src, jend, Some(span)) else { continue }; + any = true; + out.sites.push(site(PASS_QUEUE, ROLE_QUEUE_CONSUMER, &queue, first_literal(jargs).unwrap_or_default(), Some(row), line, "")); + } + if !any { + if let Some((row, n)) = ctx + .rows + .nodes() + .enumerate() + .find(|(_, n)| n.kind() == NodeKind::Method && n.name() == "process" && n.start_line() >= span.0 && n.end_line() <= span.1) + { + out.sites.push(site(PASS_QUEUE, ROLE_QUEUE_CONSUMER, &queue, "", Some(row as u32), n.start_line(), "")); + } + } + } + // BullMQ `new Worker('email', handler)`, Bull `queue.process('job', handler)` + for c in new_worker_re().captures_iter(src) { + let m = c.get(0).unwrap(); + let line = lines.line(m.start()); + let tail = &src[m.end()..(m.end() + 200).min(src.len())]; + let tail = &tail[..(0..=tail.len()).rev().find(|i| tail.is_char_boundary(*i)).unwrap_or(0)]; + let Some((row, name)) = handler_site(ctx, tail, line) else { continue }; + out.sites.push(site(PASS_QUEUE, ROLE_QUEUE_CONSUMER, &c[2], "", row, line, name)); + } + for c in queue_process_re().captures_iter(src) { + let recv = c.get(1).unwrap(); + if mid_chain(src, recv.start()) { + continue; + } + let receiver: String = recv.as_str().chars().filter(|ch| !ch.is_whitespace()).collect(); + let m = c.get(0).unwrap(); + let line = lines.line(m.start()); + let tail = &src[m.end()..(m.end() + 200).min(src.len())]; + let tail = &tail[..(0..=tail.len()).rev().find(|i| tail.is_char_boundary(*i)).unwrap_or(0)]; + let Some((row, name)) = handler_site(ctx, tail, line) else { continue }; + // the queue is the receiver's binding, resolved at link time: `@receiver` keys it + let job = c.get(3).map_or("", |j| j.as_str()); + out.sites.push(site(PASS_QUEUE, ROLE_QUEUE_CONSUMER, format!("@{receiver}"), job, row, line, name)); + } + + // events: a bus, and sockets both ways + let side = if socket_server_re().is_match(src) { "server" } else { "client" }; + for c in emit_re().captures_iter(src) { + let recv = c.get(1).unwrap(); + if mid_chain(src, recv.start()) { + continue; + } + let Some(shape) = shape_of(recv.as_str()) else { continue }; + let event = &c[4]; + if generic_event_re().is_match(event) { + continue; + } + let line = lines.line(recv.start()); + let Some(row) = enclosing_row(ctx, line, FN_KINDS) else { continue }; + let callee: String = format!("{}.{}", recv.as_str().chars().filter(|ch| !ch.is_whitespace()).collect::(), &c[2]); + out.sites.push(site(PASS_BUS, ROLE_BUS_DISPATCH, event, format!("{shape}|{side}|{}|{callee}", lines.col(recv.start())), Some(row), line, "")); + } + for (args, at, end) in decorators(src, "OnEvent") { + let Some(row) = decorated_method(ctx, &lines, src, end, None) else { continue }; + let patterns = if args.trim_start().starts_with('[') { all_literals(args) } else { first_literal(args).into_iter().collect() }; + for p in patterns { + out.sites.push(site(PASS_BUS, ROLE_BUS_HANDLER, p, format!("bus|{side}"), Some(row), lines.line(at), "")); + } + } + for (args, at, end) in decorators(src, "SubscribeMessage") { + let (Some(row), Some(event)) = (decorated_method(ctx, &lines, src, end, None), first_literal(args)) else { continue }; + out.sites.push(site(PASS_BUS, ROLE_BUS_HANDLER, event, "socket|server", Some(row), lines.line(at), "")); + } + for c in socket_on_re().captures_iter(src) { + let recv = c.get(1).unwrap(); + if mid_chain(src, recv.start()) || shape_of(recv.as_str()) != Some("socket") || generic_event_re().is_match(&c[3]) { + continue; + } + let m = c.get(0).unwrap(); + let line = lines.line(m.start()); + let tail = &src[m.end()..(m.end() + 200).min(src.len())]; + let tail = &tail[..(0..=tail.len()).rev().find(|i| tail.is_char_boundary(*i)).unwrap_or(0)]; + let Some((row, name)) = handler_site(ctx, tail, line) else { continue }; + out.sites.push(site(PASS_BUS, ROLE_BUS_HANDLER, &c[3], format!("socket|{side}"), row, line, name)); + } + } +} + +/// An HTTP route: its node, method and path segments. +struct HttpRoute { + node: NodeIdx, + method: String, + segs: Vec, +} + +const HTTP_VERBS: &[&str] = &["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "ALL", "ANY"]; + +/// `/api` + `/users` -> `/api/users`; `/api/` + `/` -> `/api`. +fn join_paths(prefix: &str, path: &str) -> String { + let a = prefix.trim_end_matches('/'); + let b = path.trim_start_matches('/'); + let joined = if b.is_empty() { a.to_owned() } else { format!("{a}/{b}") }; + if joined.is_empty() { + "/".to_owned() + } else { + joined + } +} + +/// The prefix each mounted router file serves under (upstream Express `postExtract`): +/// `app.use('/api', routes)` + `router.use('/users', users)` puts `users`'s `POST /` at +/// `POST /api/users`. A file mounted at two different paths is left alone. +fn mount_prefixes(e: &Engine<'_>) -> FxHashMap { + let view = e.view; + let mut mounts: Vec<(FileId, String, FileId)> = Vec::new(); + for f in view.files() { + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_HTTP && s.role == ROLE_HTTP_MOUNT) { + let target = view.text_str(s.payload).trim(); + let lang = view.file(f).lang(); + let file = if let Some(spec) = target.strip_prefix("require(").map(|r| r.trim_start()).and_then(|r| read_string_at(r, 0)) { + e.modules.target_file(e.modules.resolve_import(lang, &spec, f)) + } else { + let ident = target.split('.').next().unwrap_or(""); + let suffix_ok = target.split_once('.').is_none_or(|(_, s)| matches!(s, "default" | "router" | "routes")); + let i = suffix_ok.then(|| view.imports_of(f).iter().position(|imp| view.text_str(imp.local) == ident)).flatten(); + i.and_then(|i| e.modules.target_file(e.imp_targets[f.idx()].get(i).copied()?)) + }; + if let Some(t) = file.filter(|t| *t != f) { + mounts.push((f, text(view, s.key_a).to_owned(), t)); + } + } + } + let mut prefix_of: FxHashMap = FxHashMap::default(); + for _ in 0..8 { + let mut next: FxHashMap = FxHashMap::default(); + let mut ambiguous: FxHashSet = FxHashSet::default(); + for (f, prefix, target) in &mounts { + let full = join_paths(prefix_of.get(f).map_or("", |p| p.as_str()), prefix); + match next.get(target) { + Some(seen) if *seen != full => { + ambiguous.insert(*target); + } + _ => { + next.insert(*target, full); + } + } + } + for a in ambiguous { + next.remove(&a); + } + if next == prefix_of { + break; + } + prefix_of = next; + } + prefix_of +} + +fn http_routes(e: &Engine<'_>) -> Vec { + let view = e.view; + let prefixes = mount_prefixes(e); + let mut out = Vec::new(); + for f in view.files() { + let prefix = prefixes.get(&f).map(String::as_str).filter(|p| *p != "/"); + for n in view.nodes_in_file(f) { + let n = NodeIdx(n); + let node = view.node(n); + if node.kind() != NodeKind::Route { + continue; + } + let Some((method, path)) = node.name().split_once(' ') else { continue }; + let method = method.to_ascii_uppercase(); + if !HTTP_VERBS.contains(&method.as_str()) || !path.trim().starts_with('/') { + continue; + } + let path = match prefix { + Some(p) => join_paths(p, path.trim()), + None => path.trim().to_owned(), + }; + out.push(HttpRoute { node: n, method, segs: path.split('/').filter(|s| !s.is_empty()).map(str::to_owned).collect() }); + } + } + out +} + +fn param_seg(s: &str) -> bool { + s.starts_with([':', '{', '[', '<', '*']) || s.ends_with('?') +} + +fn catch_all_seg(s: &str) -> bool { + s == "*" + || s.starts_with("[...") + || s.starts_with("{*") + || (s.starts_with(':') && s.ends_with('*')) + || (s.starts_with('{') && s.ends_with(":*}")) + || (s.starts_with('*') && s[1..].chars().all(|c| c.is_alphanumeric() || c == '_')) +} + +/// A literal match beats a parameter's; a hole fills a parameter, never a literal segment. +fn score_path(client: &[String], route: &[String]) -> Option { + let mut score = 0; + let mut i = 0; + for seg in route { + if catch_all_seg(seg) { + if i >= client.len() { + return None; + } + score += (client.len() - i) as u32; + i = client.len(); + continue; + } + let c = client.get(i)?; + if param_seg(seg) { + score += 2; + } else if c == seg { + score += 3; + } else { + return None; + } + i += 1; + } + (i == client.len()).then_some(score) +} + +/// Segments of a client path (`${…}` as `*`), and whether a base URL hides its prefix. +fn client_path(raw: &str, base: Option<&str>) -> Option<(Vec, bool)> { + let mut p = &raw[..raw.find(['?', '#']).unwrap_or(raw.len())]; + let mut suffix = false; + let owned; + if let Some(i) = p.find("//") { + let scheme = &p[..i]; + if scheme.is_empty() || (scheme.ends_with(':') && scheme[..scheme.len() - 1].chars().all(|c| c.is_ascii_alphanumeric() || "+.-".contains(c))) { + let rest = &p[i + 2..]; + p = rest.find('/').map_or("/", |j| &rest[j..]); + } + } + if !p.starts_with('/') { + if let Some(rest) = p.strip_prefix(HOLE) { + if !rest.starts_with('/') { + return None; + } + p = rest; + suffix = true; + } else if let Some(base) = base { + match client_path(base, None) { + Some((segs, s)) => { + suffix = s; + owned = format!("/{}/{}", segs.join("/"), p); + p = &owned; + } + None if base.contains(HOLE) => { + suffix = true; + owned = format!("/{p}"); + p = &owned; + } + None => return None, + } + } else { + return None; + } + } else if let Some(base) = base { + match client_path(base, None) { + Some((segs, s)) if !segs.is_empty() => { + suffix = s; + owned = format!("/{}{}", segs.join("/"), p); + p = &owned; + } + None if base.contains(HOLE) => suffix = true, + _ => {} + } + } + let segs: Vec = p.split('/').filter(|s| !s.is_empty()).map(|s| if s.contains(HOLE) { "*".to_owned() } else { s.to_owned() }).collect(); + if !segs.is_empty() && segs.iter().all(|s| s == "*") { + return None; + } + Some((segs, suffix)) +} + +fn match_http<'r>(method: &str, segs: &[String], suffix: bool, routes: &'r [HttpRoute]) -> Option<&'r HttpRoute> { + let mut best: Option<(&HttpRoute, u32)> = None; + let mut tied = false; + for r in routes { + if r.method != "ALL" && r.method != "ANY" && r.method != method { + continue; + } + let score = if suffix { + // a base URL hides a prefix the route may spell out: two segments of tail at least, + // two of hidden prefix at most + if segs.len() < 2 || r.segs.len() < segs.len() || r.segs.len() - segs.len() > 2 { + continue; + } + score_path(segs, &r.segs[r.segs.len() - segs.len()..]) + } else { + score_path(segs, &r.segs) + }; + let Some(score) = score else { continue }; + match best { + Some((_, b)) if score < b => {} + Some((_, b)) if score == b => tied = true, + _ => { + best = Some((r, score)); + tied = false; + } + } + } + best.filter(|_| !tied).map(|b| b.0) +} + +/// Sites of `pass` in the module an import of `f` binds `local` to: (module, exported name). +fn import_source<'v>(e: &Engine<'v>, f: FileId, local: &str) -> Option<(FileId, &'v str, bool)> { + let view = e.view; + let i = view.imports_of(f).iter().position(|imp| view.text_str(imp.local) == local)?; + let imp = &view.imports_of(f)[i]; + let target = e.modules.target_file(e.imp_targets[f.idx()].get(i).copied()?)?; + let imported = view.text_str(imp.imported); + Some((target, imported, imported == "default")) +} + +pub(crate) fn cross_tier<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, 'v>) { + let view = e.view; + let order = order_of("http-client"); + let mut seen: FxHashSet<(NodeIdx, NodeIdx)> = FxHashSet::default(); + + // ---- 1. HTTP client -> route + let routes = http_routes(e); + let clients = |f: FileId| -> FxHashMap<&str, &str> { + view.sites_of(f).iter().filter(|s| s.pass == PASS_HTTP && s.role == ROLE_HTTP_CLIENT).map(|s| (text(view, s.key_a), view.text_str(s.payload))).collect() + }; + if !routes.is_empty() { + for f in view.files() { + let calls: Vec<_> = view.sites_of(f).iter().filter(|s| s.pass == PASS_HTTP && s.role == ROLE_HTTP_CALL).collect(); + if calls.is_empty() { + continue; + } + let own = clients(f); + // a registration the route extractor read is no client call + let route_lines: FxHashSet = + view.nodes_in_file(f).map(NodeIdx).filter(|n| view.node(*n).kind() == NodeKind::Route).map(|n| view.node(n).start_line()).collect(); + for s in calls { + if route_lines.contains(&s.line) { + continue; + } + let key_b = text(view, s.key_b); + let mut parts = key_b.splitn(3, '|'); + let (col, receiver, callee) = (parts.next().unwrap_or(""), parts.next().unwrap_or(""), parts.next().unwrap_or("")); + // what the receiver is: a client this file or an import makes, or a client by name + let base: Option> = if receiver.is_empty() { + Some(None) + } else { + let chain: Vec<&str> = receiver.trim_start_matches("this.").split('.').collect(); + let (head, last) = (chain[0], chain[chain.len() - 1]); + if let Some(b) = own.get(head) { + Some(Some(*b).filter(|b| !b.is_empty())) + } else if let Some((m, name, default)) = import_source(e, f, head) { + let theirs = clients(m); + match theirs.get(if default { "default" } else { name }) { + Some(b) => Some(Some(*b).filter(|b| !b.is_empty())), + None if server_names_re().is_match(last) || server_names_re().is_match(head) => None, + None => client_names_re().is_match(last).then_some(None), + } + } else if server_names_re().is_match(last) || server_names_re().is_match(head) { + None + } else { + client_names_re().is_match(last).then_some(None) + } + }; + let Some(base) = base else { continue }; + let raw = view.text_str(s.payload); + let Some((segs, suffix)) = client_path(raw, base) else { continue }; + let method = text(view, s.key_a); + let Some(route) = match_http(method, &segs, suffix, &routes) else { continue }; + if route.node == s.node || is_file_node(view, s.node) || !seen.insert((s.node, route.node)) { + continue; + } + let display = format!("/{}", segs.iter().map(|s| if s == "*" { "${…}" } else { s.as_str() }).collect::>().join("/")); + let rn = view.node(route.node); + let meta = serde_json::json!({ + "synthesizedBy": "http-client", "channel": "http", "callee": callee, "tier": "client→server", + "method": method, "href": display, "registeredAt": registered(view, rn.file(), rn.start_line()), + }); + out.push_meta(EdgeKind::Calls, s.node, route.node, s.line, col.parse().unwrap_or(NONE), meta, order); + } + } + } + + // ---- 2. queue job -> consumer + struct Producer { + f: FileId, + node: NodeIdx, + line: u32, + col: u32, + callee: String, + queue: Option, + job: String, + } + struct Consumer { + node: NodeIdx, + f: FileId, + line: u32, + queue: Option, + job: Option, + } + let bindings = |f: FileId| -> FxHashMap<&str, &str> { + view.sites_of(f) + .iter() + .filter(|s| s.pass == PASS_QUEUE && s.role == ROLE_QUEUE_BINDING) + .map(|s| (text(view, s.key_a), view.text_str(s.payload))) + .collect() + }; + let queue_for = |f: FileId, receiver: &str| -> Option { + let head = receiver.trim_start_matches("this.").split('.').next().unwrap_or(""); + if let Some(q) = bindings(f).get(head) { + return Some(q.to_string()); + } + let (m, name, _) = import_source(e, f, head)?; + bindings(m).get(name).map(|q| q.to_string()) + }; + let mut producers = Vec::new(); + let mut consumers = Vec::new(); + for f in view.files() { + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_QUEUE) { + match s.role { + ROLE_QUEUE_PRODUCER => { + let key_b = text(view, s.key_b); + let (col, receiver) = key_b.split_once('|').unwrap_or(("", key_b)); + let queue = queue_for(f, receiver); + let last = receiver.rsplit('.').next().unwrap_or(receiver); + if queue.is_none() && !queue_shaped_re().is_match(last) { + continue; + } + producers.push(Producer { + f, + node: s.node, + line: s.line, + col: col.parse().unwrap_or(NONE), + callee: format!("{receiver}.add"), + queue, + job: text(view, s.key_a).to_owned(), + }); + } + ROLE_QUEUE_CONSUMER => { + let key_a = text(view, s.key_a); + let queue = match key_a.strip_prefix('@') { + Some(receiver) => { + let q = queue_for(f, receiver); + let last = receiver.rsplit('.').next().unwrap_or(receiver); + if q.is_none() && !queue_shaped_re().is_match(last) { + continue; + } + q + } + None => Some(key_a.to_owned()).filter(|q| !q.is_empty()), + }; + let job = Some(text(view, s.key_b).to_owned()).filter(|j| !j.is_empty()); + let handler = view.text_str(s.payload); + let node = if handler.is_empty() { Some(s.node).filter(|n| !is_file_node(view, *n)) } else { handler_node(e, wcx, f, handler) }; + let Some(node) = node else { continue }; + consumers.push(Consumer { node, f, line: s.line, queue, job }); + } + _ => {} + } + } + } + for p in &producers { + let mut cands: Vec<&Consumer> = + consumers.iter().filter(|c| (p.queue.is_none() || c.queue.is_none() || c.queue == p.queue) && c.job.as_ref().is_none_or(|j| *j == p.job)).collect(); + let exact: Vec<&Consumer> = + cands.iter().copied().filter(|c| c.job.as_deref() == Some(p.job.as_str()) && c.queue == p.queue && p.queue.is_some()).collect(); + if !exact.is_empty() { + cands = exact; + } else { + let by_job: Vec<&Consumer> = cands.iter().copied().filter(|c| c.job.as_deref() == Some(p.job.as_str())).collect(); + if !by_job.is_empty() { + cands = by_job; + } else if p.queue.is_none() { + continue; + } else { + cands.retain(|c| c.queue == p.queue); + } + } + if cands.is_empty() || cands.len() > EVENT_FANOUT_CAP { + continue; + } + for c in cands { + if c.node == p.node || !seen.insert((p.node, c.node)) { + continue; + } + let mut meta = serde_json::json!({ "synthesizedBy": "queue-job", "channel": "queue", "callee": p.callee, "event": p.job, "registeredAt": registered(view, c.f, c.line) }); + if let Some(q) = p.queue.as_ref().or(c.queue.as_ref()) { + meta["queue"] = q.clone().into(); + } + let _ = p.f; + out.push_meta(EdgeKind::Calls, p.node, c.node, p.line, p.col, meta, order); + } + } + + // ---- 3. events: a bus, and sockets both ways + struct Dispatch<'a> { + node: NodeIdx, + line: u32, + col: u32, + callee: &'a str, + event: &'a str, + shape: &'a str, + side: &'a str, + } + struct Handler<'a> { + node: NodeIdx, + f: FileId, + line: u32, + pattern: &'a str, + kind: &'a str, + side: &'a str, + } + let mut dispatches: Vec = Vec::new(); + let mut handlers: Vec = Vec::new(); + for f in view.files() { + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_BUS) { + let key_b = text(view, s.key_b); + let mut parts = key_b.splitn(4, '|'); + let shape = parts.next().unwrap_or(""); + let side = parts.next().unwrap_or(""); + if s.role == ROLE_BUS_DISPATCH { + let col = parts.next().and_then(|c| c.parse().ok()).unwrap_or(NONE); + dispatches.push(Dispatch { node: s.node, line: s.line, col, callee: parts.next().unwrap_or(""), event: text(view, s.key_a), shape, side }); + } else { + let handler = view.text_str(s.payload); + let node = if handler.is_empty() { Some(s.node).filter(|n| !is_file_node(view, *n)) } else { handler_node(e, wcx, f, handler) }; + let Some(node) = node else { continue }; + handlers.push(Handler { node, f, line: s.line, pattern: text(view, s.key_a), kind: shape, side }); + } + } + } + let mut by_event: FxHashMap<(&str, &str), usize> = FxHashMap::default(); + for d in &dispatches { + *by_event.entry((d.shape, d.event)).or_default() += 1; + } + for d in &dispatches { + if by_event[&(d.shape, d.event)] > EVENT_FANOUT_CAP { + continue; + } + let (matched, tier): (Vec<&Handler>, Option<&str>) = if d.shape == "bus" { + (handlers.iter().filter(|h| h.kind == "bus" && event_matches(h.pattern, d.event)).collect(), None) + } else if d.side == "client" { + (handlers.iter().filter(|h| h.kind == "socket" && h.side == "server" && h.pattern == d.event).collect(), Some("client→server")) + } else { + (handlers.iter().filter(|h| h.kind == "socket" && h.side == "client" && h.pattern == d.event).collect(), Some("server→client")) + }; + if matched.is_empty() || matched.len() > EVENT_FANOUT_CAP { + continue; + } + for h in matched { + if h.node == d.node || !seen.insert((d.node, h.node)) { + continue; + } + let mut meta = serde_json::json!({ + "synthesizedBy": "event-bus", "channel": if d.shape == "bus" { "event" } else { "socket" }, + "callee": d.callee, "event": d.event, "registeredAt": registered(view, h.f, h.line), + }); + if let Some(t) = tier { + meta["tier"] = t.into(); + } + out.push_meta(EdgeKind::Calls, d.node, h.node, d.line, d.col, meta, order); + } + } +} + +/// `user.*` matches one segment, `**` any number; anything else is exact. +fn event_matches(pattern: &str, event: &str) -> bool { + if pattern == event { + return true; + } + if !pattern.contains('*') { + return false; + } + fn go(p: &[&str], e: &[&str]) -> bool { + match (p.first(), e.first()) { + (None, None) => true, + (Some(&"**"), _) => (0..=e.len()).any(|k| go(&p[1..], &e[k..])), + (Some(seg), Some(ev)) => { + let ok = if seg.contains('*') { + let (pre, post) = seg.split_once('*').unwrap_or((seg, "")); + ev.len() > pre.len() + post.len() && ev.starts_with(pre) && ev.ends_with(post) + } else { + seg == ev + }; + ok && go(&p[1..], &e[1..]) + } + _ => false, + } + } + let p: Vec<&str> = pattern.split('.').collect(); + let e: Vec<&str> = event.split('.').collect(); + go(&p, &e) +} + +/// The function a handler name denotes, from the file it is written in. +fn handler_node<'e, 'v>(e: &'e Engine<'v>, wcx: &mut WorkerCx<'e, 'v>, f: FileId, name: &str) -> Option { + let view = e.view; + let last = name.rsplit('.').next().unwrap_or(name); + let cx = wcx.file_cx(e, f); + let n = if name.contains('.') { + // `this.handle` / `handlers.onJob`: a same-file declaration of that name, unique + let mut local = view.by_name(last).filter(|n| view.hot(*n).file == f && view.node(*n).kind().is_callable()); + let first = local.next(); + if local.next().is_some() { + return None; + } + first? + } else { + match cx.resolve_name(wcx, last, Accept::Callable, 0, u32::MAX, None, true) { + NameRes::Found(n, ..) => n, + _ => return None, + } + }; + matches!(view.node(n).kind(), NodeKind::Function | NodeKind::Method).then_some(n) +} + +// =================================================================== Celery + +pub const ROLE_CELERY_TASK: u8 = 0; +pub const ROLE_CELERY_DISPATCH: u8 = 1; + +re!(celery_dispatch_re, r"\b([A-Za-z_]\w*)\s*\.\s*(?:delay|apply_async)\s*\("); +re!(celery_decorator_re, r"@\s*(?:[A-Za-z_][\w.]*\.)?(?:shared_task|task)\b"); +re!(py_decl_re, r"^(?:async\s+def|def|class)\b"); + +pub struct CeleryScanner; + +impl FileHook for CeleryScanner { + fn name(&self) -> &'static str { + "celery-sites" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + lang == Lang::Python + } + fn triggers(&self) -> &'static [&'static str] { + &["task", ".delay(", ".apply_async("] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + let src = ctx.src; + let lines: Vec<&str> = src.split('\n').collect(); + // tasks: functions with a celery decorator above their `def` + if src.contains("task") { + for (row, n) in ctx.rows.nodes().enumerate() { + if n.kind() != NodeKind::Function { + continue; + } + let start = n.start_line() as usize; + for i in (start.saturating_sub(13)..start.saturating_sub(1)).rev() { + let t = lines.get(i).map_or("", |l| l.trim()); + if py_decl_re().is_match(t) { + break; + } + if celery_decorator_re().is_match(t) { + out.sites.push(site(PASS_CELERY, ROLE_CELERY_TASK, n.name(), "", Some(row as u32), n.start_line(), "")); + break; + } + } + } + } + if src.contains(".delay(") || src.contains(".apply_async(") { + let safe = crate::frameworks::routes::strip_python_noise(src); + let li = LineIndex::new(&safe); + for c in celery_dispatch_re().captures_iter(&safe) { + let line = li.line(c.get(0).unwrap().start()); + let Some(row) = enclosing_row(ctx, line, FN_KINDS) else { continue }; + out.sites.push(site(PASS_CELERY, ROLE_CELERY_DISPATCH, &c[1], "", Some(row), line, "")); + } + } + } +} + +pub(crate) fn celery(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + let mut tasks: FxHashMap<&str, Vec<(NodeIdx, FileId)>> = FxHashMap::default(); + let mut dispatches = Vec::new(); + for f in view.files() { + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_CELERY) { + if s.role == ROLE_CELERY_TASK { + tasks.entry(text(view, s.key_a)).or_default().push((s.node, f)); + } else { + dispatches.push((f, s.node, s.line, text(view, s.key_a))); + } + } + } + if tasks.is_empty() { + return; + } + let mut seen: FxHashSet<(NodeIdx, NodeIdx)> = FxHashSet::default(); + let mut per_file: FxHashMap = FxHashMap::default(); + for (f, disp, line, name) in dispatches { + let Some(cands) = tasks.get(name) else { continue }; + // a name two modules declare as a task is ambiguous unless one is the caller's own + let target = if cands.len() == 1 { Some(cands[0].0) } else { cands.iter().find(|c| c.1 == f).map(|c| c.0) }; + let Some(target) = target else { continue }; + let n = per_file.entry(f).or_default(); + if target == disp || *n >= CELERY_FANOUT_CAP || !seen.insert((disp, target)) { + continue; + } + *n += 1; + let meta = serde_json::json!({ "synthesizedBy": "celery-dispatch", "via": name, "registeredAt": registered(view, f, line) }); + out.push_meta(EdgeKind::Calls, disp, target, line, NONE, meta, order_of("celery-dispatch")); + } +} + +// =================================================================== Spring events + +pub const ROLE_SPRING_LISTENER: u8 = 0; +pub const ROLE_SPRING_PUBLISH: u8 = 1; + +re!(spring_publish_re, r"\.publishEvent\s*\(\s*new\s+([A-Z][A-Za-z0-9_]*)"); +re!(spring_listener_re, r"@(?:EventListener|TransactionalEventListener)\b"); +re!(spring_anno_type_re, r"@(?:EventListener|TransactionalEventListener)\s*\(\s*(?:classes\s*=\s*)?([A-Z][A-Za-z0-9_]*)\.class"); +re!(spring_app_listener_re, r"\bApplicationListener\s*<\s*([A-Z][A-Za-z0-9_]*)"); + +/// First parameter type of a Java method signature (`void (XEvent e)` -> `XEvent`). +fn first_param_type(sig: &str) -> Option { + let open = sig.find('(')?; + let close = sig[open..].find(')').map_or(sig.len(), |c| open + c); + let inner = sig[open + 1..close].trim(); + let first = inner.split(',').next()?.trim(); + let toks: Vec<&str> = first.split_whitespace().filter(|t| *t != "final" && !t.starts_with('@')).collect(); + if toks.len() < 2 { + return None; + } + let ty = toks[toks.len() - 2].split('<').next()?; + (ty.starts_with(|c: char| c.is_ascii_uppercase()) && ty.chars().all(|c| c.is_alphanumeric() || c == '_')).then(|| ty.to_owned()) +} + +pub struct SpringEventScanner; + +impl FileHook for SpringEventScanner { + fn name(&self) -> &'static str { + "spring-event-sites" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + lang == Lang::Java + } + fn triggers(&self) -> &'static [&'static str] { + &["publishEvent", "EventListener", "ApplicationListener"] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + let src = ctx.src; + let lines: Vec<&str> = src.split('\n').collect(); + let has_anno = src.contains("@EventListener") || src.contains("@TransactionalEventListener"); + let app_listener = spring_app_listener_re().captures(src).map(|c| c[1].to_owned()); + for (row, n) in ctx.rows.nodes().enumerate() { + if n.kind() != NodeKind::Method { + continue; + } + // the method's own annotation block: consecutive `@` lines from its first line + let mut head = String::new(); + let start = n.start_line() as usize; + for line in lines.iter().take((start + 7).min(lines.len())).skip(start.saturating_sub(1)) { + let t = line.trim(); + if !t.starts_with('@') { + break; + } + head.push_str(t); + head.push('\n'); + } + let annotated = has_anno && spring_listener_re().is_match(&head); + let is_app = app_listener.is_some() && n.name() == "onApplicationEvent"; + if !annotated && !is_app { + continue; + } + let ty = n + .signature() + .and_then(first_param_type) + .or_else(|| spring_anno_type_re().captures(&head).map(|c| c[1].to_owned())) + .or_else(|| app_listener.clone().filter(|_| is_app)); + if let Some(ty) = ty { + out.sites.push(site(PASS_SPRING, ROLE_SPRING_LISTENER, ty, "", Some(row as u32), n.start_line(), "")); + } + } + if src.contains(".publishEvent(") { + let safe = cg_extract::post::strip_js_comments(src); + let li = LineIndex::new(&safe); + for c in spring_publish_re().captures_iter(&safe) { + let line = li.line(c.get(0).unwrap().start()); + let Some(row) = enclosing_row(ctx, line, &[NodeKind::Method, NodeKind::Function]) else { continue }; + out.sites.push(site(PASS_SPRING, ROLE_SPRING_PUBLISH, &c[1], "", Some(row), line, "")); + } + } + } +} + +pub(crate) fn spring_events(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + let mut listeners: FxHashMap<&str, Vec> = FxHashMap::default(); + let mut publishes = Vec::new(); + for f in view.files() { + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_SPRING) { + if s.role == ROLE_SPRING_LISTENER { + listeners.entry(text(view, s.key_a)).or_default().push(s.node); + } else { + publishes.push((f, s.node, s.line, text(view, s.key_a))); + } + } + } + let mut seen: FxHashSet<(NodeIdx, NodeIdx)> = FxHashSet::default(); + let mut per_file: FxHashMap = FxHashMap::default(); + for (f, disp, line, ty) in publishes { + let Some(targets) = listeners.get(ty) else { continue }; + for &t in targets { + let n = per_file.entry(f).or_default(); + if t == disp || *n >= SPRING_FANOUT_CAP || !seen.insert((disp, t)) { + continue; + } + *n += 1; + let meta = serde_json::json!({ "synthesizedBy": "spring-event", "via": ty, "registeredAt": registered(view, f, line) }); + out.push_meta(EdgeKind::Calls, disp, t, line, NONE, meta, order_of("spring-event")); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn http_paths() { + let routes = vec![ + HttpRoute { node: NodeIdx(1), method: "GET".into(), segs: vec!["api".into(), "users".into()] }, + HttpRoute { node: NodeIdx(2), method: "GET".into(), segs: vec!["api".into(), "users".into(), ":id".into()] }, + HttpRoute { node: NodeIdx(3), method: "GET".into(), segs: vec!["api".into(), "users".into(), "top".into()] }, + HttpRoute { node: NodeIdx(4), method: "POST".into(), segs: vec!["api".into(), "users".into()] }, + ]; + let m = |method: &str, raw: &str, base: Option<&str>| { + client_path(raw, base).and_then(|(s, suffix)| match_http(method, &s, suffix, &routes)).map(|r| r.node) + }; + assert_eq!(m("GET", "/api/users", None), Some(NodeIdx(1))); + assert_eq!(m("POST", "/api/users", None), Some(NodeIdx(4))); + assert_eq!(m("GET", &format!("/api/users/{HOLE}"), None), Some(NodeIdx(2))); + assert_eq!(m("GET", "/api/users/top", None), Some(NodeIdx(3))); + assert_eq!(m("GET", "users", Some("/api")), Some(NodeIdx(1))); + assert_eq!(m("GET", &format!("{HOLE}/users/7"), None), Some(NodeIdx(2))); + assert_eq!(m("GET", "https://x.io/api/users?x=1", None), Some(NodeIdx(1))); + assert_eq!(m("DELETE", "/api/users", None), None); + assert_eq!(m("GET", "users", None), None); + } + + #[test] + fn events_and_params() { + assert!(event_matches("user.*", "user.created")); + assert!(!event_matches("user.*", "user.created.x")); + assert!(event_matches("user.**", "user.created.x")); + assert!(!event_matches("order.*", "user.created")); + assert_eq!(first_param_type("void (final UserCreated e)").as_deref(), Some("UserCreated")); + assert_eq!(first_param_type("void ()"), None); + assert_eq!(shape_of("this.eventEmitter"), Some("bus")); + assert_eq!(shape_of("this.server.to(room)"), Some("socket")); + assert_eq!(shape_of("foo"), None); + assert_eq!(registry_entries(" create: createUser, remove: removeUser, x: 5, y: () => 1 "), vec!["createUser", "removeUser"]); + } +} diff --git a/crates/cg-resolve/src/synth/go.rs b/crates/cg-resolve/src/synth/go.rs new file mode 100644 index 0000000..7c0141a --- /dev/null +++ b/crates/cg-resolve/src/synth/go.rs @@ -0,0 +1,321 @@ +//! Go structural interfaces (upstream `goImplementsEdges`, with #3185's type check): a type +//! implements an interface when its method set, embedded types' methods included, has every +//! method the interface asks for with the same parameter and result counts and types. An +//! unexported method name only matches within its package. A type is compared by its name with +//! the package qualifier left off (the two declarations may sit in different packages); a true +//! alias, a type parameter and a `func`/`struct`/`interface` literal type rule nothing out. + +use super::sites::{PASS_GO_TYPES, ROLE_GO_ALIAS, ROLE_GO_GENERIC}; +use super::{Out, MAX_GO_IMPLEMENTERS}; +use crate::engine::Engine; +use crate::project::parent_dir; +use cg_core::{EdgeKind, Lang, NodeIdx, NodeKind}; +use rustc_hash::{FxHashMap, FxHashSet}; + +/// A method's parameter and result types, each comparable or `None` (open). +type Sig = Vec>; + +/// The top-level items of the bracketed list opening at `text[open]`, and where it closes. +fn list_items(text: &str, open: usize) -> Option<(Vec<&str>, usize)> { + let b = text.as_bytes(); + let mut items = Vec::new(); + let (mut depth, mut start) = (0i32, open + 1); + let mut i = open; + while i < b.len() { + match b[i] { + b'"' | b'`' | b'\'' => { + let q = b[i]; + i += 1; + while i < b.len() && b[i] != q { + if q != b'`' && b[i] == b'\\' { + i += 1; + } + i += 1; + } + } + b'(' | b'[' | b'{' => depth += 1, + b')' | b']' | b'}' => { + depth -= 1; + if depth == 0 { + let last = text[start..i].trim(); + if !last.is_empty() { + items.push(last); + } + return Some((items, i)); + } + } + b',' if depth == 1 => { + let item = text[start..i].trim(); + if !item.is_empty() { + items.push(item); + } + start = i + 1; + } + _ => {} + } + i += 1; + } + None +} + +/// `(ctx context.Context, a, b int) (int, error)` -> parameters and results. +fn signature_parts(sig: &str) -> Option<(Vec<&str>, Vec<&str>)> { + let sig = sig.trim(); + if !sig.starts_with('(') { + return None; + } + let (params, end) = list_items(sig, 0)?; + let rest = sig[end + 1..].trim(); + if !rest.starts_with('(') { + return Some((params, if rest.is_empty() { vec![] } else { vec![rest] })); + } + let (results, end) = list_items(rest, 0)?; + rest[end + 1..].trim().is_empty().then_some((params, results)) +} + +const TYPE_WORDS: &[&str] = &["func", "chan", "map", "struct", "interface"]; + +/// The types of a list's items: `a, b int` gives `a` the type `int`; a list with no names is +/// a list of types. +fn item_types<'a>(items: &[&'a str]) -> Vec<&'a str> { + let named: Vec> = items + .iter() + .map(|item| { + let (name, ty) = item.split_once(char::is_whitespace)?; + let ok = name.chars().next().is_some_and(|c| c.is_alphabetic() || c == '_') && name.chars().all(|c| c.is_alphanumeric() || c == '_'); + (ok && !TYPE_WORDS.contains(&name)).then(|| ty.trim()) + }) + .collect(); + if named.iter().all(Option::is_none) { + return items.to_vec(); + } + let mut out = vec![""; items.len()]; + let mut ty = ""; + for i in (0..items.len()).rev() { + if let Some(t) = named[i] { + ty = t; + } + out[i] = ty; + } + out +} + +/// A type as two declarations can compare it, or `None` for one that holds a list of its own. +fn comparable(written: &str) -> Option { + let flat: String = written.chars().filter(|c| !c.is_whitespace()).collect(); + let flat = flat.replace("interface{}", "any"); + if flat.contains("func(") || flat.contains("struct{") || flat.contains("interface{") { + return None; + } + // drop package qualifiers: `*http.Request` -> `*Request` + let mut out = String::with_capacity(flat.len()); + let mut word = String::new(); + let flush = |word: &mut String, out: &mut String, next: Option| { + if next == Some('.') { + word.clear(); + } else { + out.push_str(match word.as_str() { + "uint8" => "byte", + "int32" => "rune", + w => w, + }); + word.clear(); + } + }; + let chars: Vec = flat.chars().collect(); + let mut i = 0; + while i < chars.len() { + let c = chars[i]; + if c.is_alphanumeric() || c == '_' { + word.push(c); + } else { + let qualifier = c == '.' && !word.is_empty(); + flush(&mut word, &mut out, Some(c)); + if !qualifier { + out.push(c); + } + } + i += 1; + } + flush(&mut word, &mut out, None); + Some(out) +} + +fn method_types(sig: &str) -> Option<(usize, usize, Sig)> { + let (params, results) = signature_parts(sig)?; + let types = item_types(¶ms).into_iter().chain(item_types(&results)).map(comparable).collect(); + Some((params.len(), results.len(), types)) +} + +/// What a method matches an interface method by: its name, plus its package when unexported. +fn method_key(view: &cg_store::View, m: NodeIdx) -> String { + let node = view.node(m); + let name = node.name(); + if name.chars().next().is_some_and(char::is_uppercase) { + name.to_owned() + } else { + format!("{}\0{name}", parent_dir(view.file(node.file()).path())) + } +} + +pub(super) fn go_implements(e: &Engine<'_>, out: &mut Out) -> Vec<(NodeIdx, NodeIdx)> { + let view = e.view; + let mut implementers: Vec = Vec::new(); + let mut interfaces: Vec = Vec::new(); + let mut is_go_type: FxHashSet = FxHashSet::default(); + // type name -> declaring nodes, per package directory, for receiver methods of other files + let mut types_in_dir: FxHashMap<(&str, &str), NodeIdx> = FxHashMap::default(); + let mut methods_of: FxHashMap> = FxHashMap::default(); + // names standing for some other type: true aliases and type parameters + let mut open_names: FxHashSet<&str> = FxHashSet::default(); + let mut receiver_methods: Vec = Vec::new(); + for f in view.files() { + if view.file(f).lang() != Lang::Go { + continue; + } + let dir = parent_dir(view.file(f).path()); + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_GO_TYPES) { + match s.role { + ROLE_GO_ALIAS => { + open_names.insert(view.text_str(cg_core::StrId(s.key_a))); + } + ROLE_GO_GENERIC => open_names.extend(view.text_str(s.payload).split(',').filter(|p| !p.is_empty())), + _ => {} + } + } + for n in view.nodes_in_file(f) { + let n = NodeIdx(n); + let node = view.node(n); + match node.kind() { + NodeKind::Interface => { + interfaces.push(n); + is_go_type.insert(n); + types_in_dir.entry((dir, node.name())).or_insert(n); + } + NodeKind::Struct | NodeKind::Class | NodeKind::TypeAlias => { + implementers.push(n); + is_go_type.insert(n); + types_in_dir.entry((dir, node.name())).or_insert(n); + } + NodeKind::Method | NodeKind::Function => { + if let Some((tq, name)) = node.qname().rsplit_once("::") { + if name == node.name() && !tq.contains("::") { + receiver_methods.push(n); + } + } + } + _ => {} + } + } + } + // a type's own methods: those in its body (interfaces) and the receiver methods its + // package declares anywhere + for &t in &interfaces { + let own: Vec = + view.out_edges(t).filter(|ed| ed.kind == EdgeKind::Contains && view.node(ed.dst).kind() == NodeKind::Method).map(|ed| ed.dst).collect(); + methods_of.insert(t, own); + } + for &m in &receiver_methods { + let node = view.node(m); + let tq = node.qname().rsplit_once("::").map_or("", |x| x.0); + let dir = parent_dir(view.file(node.file()).path()); + if let Some(&t) = types_in_dir.get(&(dir, tq)) { + if view.node(t).kind() != NodeKind::Interface { + methods_of.entry(t).or_default().push(m); + } + } + } + // embedding: Go records it as `extends` + let embeds = |t: NodeIdx| -> Vec { e.direct_supers(t).iter().copied().filter(|s| is_go_type.contains(s)).collect() }; + let closure = |t: NodeIdx| -> Vec { + let mut seen = vec![t]; + let mut i = 0; + while i < seen.len() && seen.len() < 64 { + for s in embeds(seen[i]) { + if !seen.contains(&s) { + seen.push(s); + } + } + i += 1; + } + seen + }; + // method set: key -> the signatures declared for it (None: a signature that doesn't read) + type MethodSet = FxHashMap>>; + let method_set = |t: NodeIdx| -> MethodSet { + let mut set: MethodSet = FxHashMap::default(); + for ty in closure(t) { + for &m in methods_of.get(&ty).map(Vec::as_slice).unwrap_or(&[]) { + set.entry(method_key(view, m)).or_default().push(view.node(m).signature().and_then(method_types)); + } + } + set + }; + let open = |t: &Option| -> bool { + match t { + None => true, + Some(t) => t + .split(|c: char| !(c.is_alphanumeric() || c == '_')) + .any(|w| open_names.contains(w) || (w.len() == 1 && w.chars().all(|c| c.is_ascii_uppercase()))), + } + }; + let fits = |have: &Option<(usize, usize, Sig)>, want: &Option<(usize, usize, Sig)>| -> bool { + let (Some(h), Some(w)) = (have, want) else { return true }; + h.0 == w.0 && h.1 == w.1 && h.2.iter().zip(&w.2).all(|(a, b)| a == b || open(a) || open(b)) + }; + let sets: Vec<(NodeIdx, MethodSet)> = implementers.iter().map(|&t| (t, method_set(t))).filter(|(_, s)| !s.is_empty()).collect(); + let mut pairs = Vec::new(); + for &iface in &interfaces { + let want = method_set(iface); + if want.is_empty() { + continue; // `any` would match everything + } + let mut direct = Vec::new(); + let mut promoted = Vec::new(); + for (t, have) in &sets { + let covers = want.iter().all(|(key, wanted)| have.get(key).is_some_and(|got| wanted.iter().any(|w| got.iter().any(|g| fits(g, w))))); + if !covers || embeds(*t).contains(&iface) { + continue; + } + let own_covers = want.keys().all(|key| methods_of.get(t).is_some_and(|ms| ms.iter().any(|m| &method_key(view, *m) == key))); + if own_covers { + direct.push(*t); + } else { + promoted.push(*t); + } + } + for t in direct.into_iter().chain(promoted).take(MAX_GO_IMPLEMENTERS) { + let node = view.node(t); + let at = format!("{}:{}", view.file(node.file()).path(), node.start_line()); + out.push( + EdgeKind::Implements, + t, + iface, + node.start_line(), + "go-implements", + Some(at), + Some(("via", view.node(iface).name())), + super::order_of("go-implements"), + ); + pairs.push((t, iface)); + } + } + pairs +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn signatures_compare_by_count_and_type() { + let (p, r, t) = method_types("(ctx context.Context, a, b int, rest ...string) (int, error)").unwrap(); + assert_eq!((p, r), (4, 2)); + assert_eq!(t, vec![Some("Context".into()), Some("int".into()), Some("int".into()), Some("...string".into()), Some("int".into()), Some("error".into())]); + assert_eq!(method_types("() error").unwrap().2, vec![Some("error".into())]); + assert_eq!(method_types("(*http.Request) bool").unwrap().2, vec![Some("*Request".into()), Some("bool".into())]); + assert_eq!(method_types("(f func(int) error)").unwrap().2, vec![None]); + assert_eq!(comparable("map[string]uint8").as_deref(), Some("map[string]byte")); + assert_eq!(comparable("interface{}").as_deref(), Some("any")); + } +} diff --git a/crates/cg-resolve/src/synth/mod.rs b/crates/cg-resolve/src/synth/mod.rs index 1002b09..77f674e 100644 --- a/crates/cg-resolve/src/synth/mod.rs +++ b/crates/cg-resolve/src/synth/mod.rs @@ -2,11 +2,14 @@ //! view (hierarchy, members, persisted sites), so the same code serves full builds and the //! incremental sync, which re-runs it and rewrites the files whose synthesised edges changed. +pub mod dynamic; +pub mod go; +pub mod scan; pub mod sites; use crate::engine::Engine; use crate::resolve::{Accept, NameRes, WorkerCx}; -use cg_core::{EdgeKind, FileId, Lang, NodeIdx, NodeKind, Provenance, NONE}; +use cg_core::{EdgeKind, FileId, Lang, LangFamily, NodeIdx, NodeKind, Provenance, NONE}; use cg_store::{outkind, EdgeBuild, View}; use rayon::prelude::*; use rustc_hash::{FxHashMap, FxHashSet}; @@ -20,16 +23,15 @@ pub struct SynthOutput { } /// Fan-out ceilings: a key with more targets is skipped entirely (precision first, design 7.2). -const MAX_IMPLS: usize = 12; +/// Overrides linked per (type, supertype) pair (upstream `MAX_CALLBACKS_PER_CHANNEL`). +const MAX_IMPLS_PER_PAIR: usize = 40; const MAX_EMITTERS: usize = 8; const MAX_HANDLERS: usize = 8; -const MAX_GO_IMPLEMENTERS: usize = 24; - -/// Go method names that nearly every type has: an interface made only of these says nothing. -const GENERIC_GO_METHODS: &[&str] = &["String", "Error", "Close", "Read", "Write", "Len", "Less", "Swap", "Reset", "Unwrap", "Is", "As", "Format", "Clone"]; +/// Implementers linked per Go interface, in file order (upstream `MAX_CALLBACKS_PER_CHANNEL`). +const MAX_GO_IMPLEMENTERS: usize = 40; #[derive(Default)] -struct Out { +pub(crate) struct Out { edges: Vec, emeta: Vec, meta_ids: FxHashMap, @@ -54,8 +56,32 @@ impl Out { edge })); } + /// An edge with its metadata object as is (the passes that carry more than one extra key). + #[allow(clippy::too_many_arguments)] + pub(crate) fn push_meta(&mut self, kind: EdgeKind, src: NodeIdx, dst: NodeIdx, line: u32, col: u32, meta: serde_json::Value, order: u16) { + if src == dst { + return; + } + let json = meta.to_string(); + let id = *self.meta_ids.entry(json.clone()).or_insert_with(|| { + self.emeta.push(json); + (self.emeta.len() - 1) as u32 + }); + self.edges.push(EdgeBuild { + src: src.0, + dst: dst.0, + kind: outkind::pack(kind, Provenance::Heuristic, outkind::CLASS_SYNTH), + line: if line == 0 { NONE } else { line }, + col, + res: 0, + ref_idx: NONE, + meta: id, + order, + }); + } + #[allow(clippy::too_many_arguments)] // one call site per synthesis pass; a builder would only add noise - fn push( + pub(crate) fn push( &mut self, kind: EdgeKind, src: NodeIdx, @@ -100,162 +126,124 @@ fn path_line(view: &View, f: FileId, line: u32) -> String { format!("{}:{}", view.file(f).path(), line) } -/// Invert the engine's hierarchy: type -> direct subtypes. -fn subtypes(e: &Engine<'_>, extra: &[(NodeIdx, NodeIdx)]) -> FxHashMap> { - let mut subs: FxHashMap> = FxHashMap::default(); - for (sub, sups) in &e.supers { - for s in sups { - subs.entry(*s).or_default().push(*sub); - } - } - for (sub, sup) in extra { - subs.entry(*sup).or_default().push(*sub); - } - for v in subs.values_mut() { - v.sort(); - v.dedup(); - } - subs +/// Languages whose supertype methods dispatch to a subtype's same-named methods. +fn overrides_lang(l: Lang) -> bool { + matches!(l.family(), LangFamily::Jvm | LangFamily::Js) || matches!(l, Lang::Go | Lang::Rust) } -fn descendants(subs: &FxHashMap>, t: NodeIdx) -> Vec { - let mut out = Vec::new(); - let mut seen: FxHashSet = FxHashSet::default(); - seen.insert(t); - let mut frontier = vec![t]; - for _ in 0..10 { - let mut next = Vec::new(); - for x in frontier { - for s in subs.get(&x).into_iter().flatten() { - if seen.insert(*s) { - out.push(*s); - next.push(*s); - } - } - } - if next.is_empty() { - break; - } - frontier = next; +/// The methods of `t` called `name`: its body's, or for Go / Rust the receiver and impl +/// methods declared outside it. +fn own_methods(e: &Engine<'_>, t: NodeIdx, name: &str) -> Vec { + let view = e.view; + let mut v: Vec = view + .out_edges(t) + .filter(|ed| ed.kind == EdgeKind::Contains) + .map(|ed| ed.dst) + .filter(|m| view.node(*m).kind() == NodeKind::Method && view.node(*m).name() == name) + .collect(); + if v.is_empty() && matches!(view.file(view.node(t).file()).lang(), Lang::Go | Lang::Rust) { + v.extend(e.direct_member(t, name).filter(|m| view.node(*m).kind().is_callable())); } - out + v } -/// Go has no `implements`: a struct implements an interface when it has every method (by name). -fn go_implements(e: &Engine<'_>, out: &mut Out) -> Vec<(NodeIdx, NodeIdx)> { +/// Of an overloaded name, the methods that can override `base`: Java, Kotlin and TypeScript +/// make an override repeat the parameters it overrides. Unreadable signatures stay in. +fn overrides_of(e: &Engine<'_>, base: NodeIdx, impls: &[NodeIdx]) -> Vec { let view = e.view; - // method name -> types that declare it (receiver methods are named `Type::method`) - let mut by_method: FxHashMap<&str, Vec> = FxHashMap::default(); - let mut interfaces: Vec<(NodeIdx, Vec<&str>)> = Vec::new(); - for f in view.files() { - if view.file(f).lang() != Lang::Go { - continue; - } - for n in view.nodes_in_file(f) { - let n = NodeIdx(n); - let node = view.node(n); - match node.kind() { - NodeKind::Interface => { - let mut names: Vec<&str> = view - .out_edges(n) - .filter(|ed| ed.kind == EdgeKind::Contains) - .map(|ed| view.node(ed.dst)) - .filter(|c| c.kind() == NodeKind::Method) - .map(|c| c.name()) - .collect(); - names.sort_unstable(); - names.dedup(); - if !names.is_empty() { - interfaces.push((n, names)); - } - } - NodeKind::Method | NodeKind::Function => { - if let Some((tq, name)) = node.qname().rsplit_once("::") { - if name == node.name() { - for t in view.by_qname(tq) { - if matches!(view.node(t).kind(), NodeKind::Struct | NodeKind::Class) && view.hot(t).file != FileId::NONE { - by_method.entry(view.name_str(view.hot(n).name)).or_default().push(t); - } - } - } - } + let params = |n: NodeIdx| -> Option> { + let lang = view.file(view.node(n).file()).lang(); + crate::overload::parse_params(lang, view.node(n).signature()?).map(|p| p.items.iter().map(|i| i.ty.clone()).collect()) + }; + let Some(want) = params(base) else { return impls.to_vec() }; + let (mut same, mut maybe) = (Vec::new(), Vec::new()); + for &m in impls { + match params(m) { + None => maybe.push(m), + Some(have) if have.len() != want.len() => {} + Some(have) => { + if have == want { + same.push(m); + } else if have.iter().zip(&want).all(|(a, b)| a == b || a.is_empty() || b.is_empty() || a.len() == 1 || b.len() == 1) { + maybe.push(m); // a type parameter on either side } - _ => {} } } } - let mut pairs: Vec<(NodeIdx, NodeIdx)> = Vec::new(); - for (iface, names) in &interfaces { - if names.iter().all(|n| GENERIC_GO_METHODS.contains(n)) { - continue; - } - // intersect candidate sets, rarest method first - let mut order: Vec<&str> = names.to_vec(); - order.sort_by_key(|n| by_method.get(*n).map_or(0, |v| v.len())); - let Some(first) = by_method.get(order[0]) else { continue }; - let mut cands: Vec = first.clone(); - cands.sort(); - cands.dedup(); - for n in &order[1..] { - let Some(set) = by_method.get(*n) else { - cands.clear(); - break; - }; - let set: FxHashSet = set.iter().copied().collect(); - cands.retain(|c| set.contains(c)); - if cands.is_empty() { - break; - } - } - if cands.is_empty() || cands.len() > MAX_GO_IMPLEMENTERS { - continue; - } - for c in cands { - if c != *iface { - out.push(EdgeKind::Implements, c, *iface, view.hot(c).start_line, "go-implements", None, None, 10); - pairs.push((c, *iface)); - } - } + if same.is_empty() { + maybe + } else { + same } - pairs } -/// A call to an interface / abstract method lands on its implementations: the hop -/// `Interface.m -> Impl.m` lets a flow continue through dynamic dispatch. -fn interface_impl(e: &Engine<'_>, subs: &FxHashMap>, out: &mut Out) { +/// A call through a supertype's method lands on what a subtype declares for it (upstream +/// `interfaceOverrideEdges`): per concrete type and each direct supertype, every base method +/// links to the type's own methods of that name (a Go unexported name: of that package), +/// overloads paired by their parameters, a JS static only to a static. Capped per (type, +/// supertype) pair. A Go struct's supertype that is no interface is an embedding, which +/// dispatches nothing. +fn interface_impl(e: &Engine<'_>, go_pairs: &[(NodeIdx, NodeIdx)], out: &mut Out) { let view = e.view; - let mut types: Vec = subs.keys().copied().collect(); + let mut sups: FxHashMap> = FxHashMap::default(); + for (sub, list) in &e.supers { + sups.entry(*sub).or_default().extend(list.iter().copied()); + } + for (sub, sup) in go_pairs { + sups.entry(*sub).or_default().push(*sup); + } + let mut types: Vec = sups.keys().copied().collect(); types.sort(); - for t in types { - let tn = view.node(t); - let abstract_type = matches!(tn.kind(), NodeKind::Interface | NodeKind::Trait | NodeKind::Protocol) || tn.is_abstract() == Some(true); - let descs = descendants(subs, t); - if descs.is_empty() { + for cls in types { + let cn = view.node(cls); + let lang = view.file(cn.file()).lang(); + let concrete = matches!(cn.kind(), NodeKind::Class | NodeKind::Struct | NodeKind::Union) + || (lang == Lang::Go && cn.kind() == NodeKind::TypeAlias) + || (lang == Lang::Rust && cn.kind() == NodeKind::Enum); + if !concrete || !overrides_lang(lang) { continue; } - let mut members: Vec = - view.out_edges(t).filter(|ed| ed.kind == EdgeKind::Contains).map(|ed| ed.dst).filter(|m| view.node(*m).kind() == NodeKind::Method).collect(); - // Go / Rust declare trait-like methods through qualified names as well - members.sort(); - for m in members { - let mn = view.node(m); - if !abstract_type && mn.is_abstract() != Some(true) { + let mut bases = sups[&cls].clone(); + bases.sort(); + bases.dedup(); + for base in bases { + let bn = view.node(base); + let blang = view.file(bn.file()).lang(); + if base == cls || !overrides_lang(blang) || (lang == Lang::Go && bn.kind() != NodeKind::Interface) { continue; } - let mut impls: Vec = Vec::new(); - for d in &descs { - if let Some(im) = e.direct_member(*d, mn.name()) { - if im != m && view.node(im).kind().is_callable() && !impls.contains(&im) { - impls.push(im); + let base_methods: Vec = + view.out_edges(base).filter(|ed| ed.kind == EdgeKind::Contains).map(|ed| ed.dst).filter(|m| view.node(*m).kind() == NodeKind::Method).collect(); + let mut added = 0; + for &bm in &base_methods { + if added >= MAX_IMPLS_PER_PAIR { + break; + } + let bmn = view.node(bm); + let mut impls = own_methods(e, cls, bmn.name()); + if lang == Lang::Go && !bmn.name().starts_with(|c: char| c.is_uppercase()) { + // an unexported method matches only in the interface's package + let dir = crate::project::parent_dir(view.file(bmn.file()).path()); + impls.retain(|m| crate::project::parent_dir(view.file(view.node(*m).file()).path()) == dir); + } + if impls.is_empty() { + continue; + } + let overloaded = impls.len() > 1 || base_methods.iter().filter(|o| view.node(**o).name() == bmn.name()).count() > 1; + let chosen = if overloaded { overrides_of(e, bm, &impls) } else { impls }; + for m in chosen { + if added >= MAX_IMPLS_PER_PAIR || m == bm { + continue; } + let mn = view.node(m); + if lang.family() == LangFamily::Js && (bmn.is_static() == Some(true)) != (mn.is_static() == Some(true)) { + continue; + } + let at = format!("{}:{}", view.file(mn.file()).path(), mn.start_line()); + out.push(EdgeKind::Calls, bm, m, bmn.start_line(), "interface-impl", Some(at), Some(("via", mn.name())), order_of("interface-impl")); + added += 1; } } - if impls.is_empty() || impls.len() > MAX_IMPLS { - continue; - } - for im in impls { - out.push(EdgeKind::Calls, m, im, mn.start_line(), "interface-impl", None, None, 20); - } } } } @@ -329,7 +317,7 @@ fn event_emitter<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e } for em in &k.emitters { for (t, line, f) in &targets { - out.push(EdgeKind::Calls, *em, *t, 0, "event-emitter", Some(path_line(view, *f, *line)), Some(("event", event)), 30); + out.push(EdgeKind::Calls, *em, *t, 0, "event-emitter", Some(path_line(view, *f, *line)), Some(("event", event)), order_of("event-emitter")); } } out @@ -373,7 +361,7 @@ fn react_render(e: &Engine<'_>, out: &mut Out, selected: Option<&FxHashSet> } if let Some(render) = e.direct_member(class, "render") { if render != m { - out.push(EdgeKind::Calls, m, render, 0, "react-render", Some(path_line(view, f, s.line)), None, 40); + out.push(EdgeKind::Calls, m, render, 0, "react-render", Some(path_line(view, f, s.line)), None, order_of("react-render")); } } } @@ -397,7 +385,7 @@ fn jsx_render<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, ' if let NameRes::Found(t, ..) = cx.resolve_name(wcx, tag, Accept::Callable, 0, u32::MAX, None, false) { let k = view.node(t).kind(); if matches!(k, NodeKind::Function | NodeKind::Class | NodeKind::Component | NodeKind::Variable | NodeKind::Constant) && t != s.node { - out.push(EdgeKind::Calls, s.node, t, 0, "jsx-render", Some(path_line(view, f, s.line)), Some(("via", tag)), 50); + out.push(EdgeKind::Calls, s.node, t, 0, "jsx-render", Some(path_line(view, f, s.line)), Some(("via", tag)), order_of("jsx-render")); } } } @@ -408,19 +396,99 @@ fn jsx_render<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, ' pub fn run_all(e: &Engine<'_>) -> SynthOutput { let mut out = Out { edges: Vec::new(), emeta: Vec::new(), meta_ids: FxHashMap::default() }; let mut wcx = WorkerCx::new(); - let go_pairs = go_implements(e, &mut out); - let subs = subtypes(e, &go_pairs); - interface_impl(e, &subs, &mut out); + let go_pairs = go::go_implements(e, &mut out); + interface_impl(e, &go_pairs, &mut out); event_emitter(e, &mut out, &mut wcx, None); react_render(e, &mut out, None); jsx_render(e, &mut out, &mut wcx, None); + run_global(e, &mut out, &mut wcx); SynthOutput { edges: out.edges, emeta: out.emeta } } +/// Passes whose keys span files (routes against navigations, a proxy against the routes it +/// covers, a channel's two ends): cheap over persisted sites, so an incremental run +/// recomputes them whole. +const GLOBAL_PASSES: &[&str] = &[ + "next-navigation", + "react-router-navigation", + "tanstack-navigation", + "next-link", + "react-router-link", + "tanstack-link", + "next-proxy", + "callback", + "object-registry", + "http-client", + "queue-job", + "event-bus", + "celery-dispatch", + "spring-event", +]; + +/// Merge order of a synthesis pass (upstream `SYNTH_PASSES`): of two synthesized edges +/// between the same two symbols the earlier pass's stays. Go's fixed steps and navigation +/// calls (resolved references upstream) stand outside the merge. +pub(crate) fn order_of(by: &str) -> u16 { + match by { + "go-implements" | "go-method-contains" => 10, + "callback" => 100, + "http-client" | "queue-job" | "event-bus" => 120, + "event-emitter" => 150, + "react-render" => 160, + "jsx-render" => 170, + "interface-impl" => 260, + "object-registry" => 390, + "celery-dispatch" => 430, + "spring-event" => 440, + "next-link" => 510, + "next-proxy" => 520, + "react-router-link" => 530, + "tanstack-link" => 540, + _ => 600, + } +} + +/// Keep the first synthesized edge per (source, target) in merge order. +fn dedupe_pairs(out: &mut Out) { + let in_merge = |o: u16| (100..600).contains(&o); + let mut idx: Vec = (0..out.edges.len()).filter(|i| in_merge(out.edges[*i].order)).collect(); + idx.sort_by_key(|i| { + let e = &out.edges[*i]; + (e.order, e.src, e.dst, e.line, e.col, out.emeta.get(e.meta as usize).cloned().unwrap_or_default()) + }); + let mut seen: FxHashSet<(u32, u32)> = FxHashSet::default(); + let mut drop = vec![false; out.edges.len()]; + for i in idx { + let e = &out.edges[i]; + if !seen.insert((e.src, e.dst)) { + drop[i] = true; + } + } + let mut k = 0; + out.edges.retain(|_| { + let keep = !drop[k]; + k += 1; + keep + }); +} + +fn run_global<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, 'v>) { + dynamic::callbacks(e, out, wcx); + dynamic::cross_tier(e, out, wcx); + dynamic::registries(e, out, wcx); + dynamic::celery(e, out); + dynamic::spring_events(e, out); + crate::frameworks::nav::navigation(e, out); + crate::frameworks::nav::next_proxy(e, out); + dedupe_pairs(out); +} + /// Reconnect only changed event keys / per-file render sites when the definition and type /// tables did not change. Structural changes deliberately use the full pass set. pub fn run_incremental(e: &Engine<'_>, previous: &View, changed: &FxHashSet, remap: &FxHashMap, structural: bool) -> SynthOutput { - if structural { + // Go aliases and type parameters change which implementers fit without changing a node + let go_types = |view: &View| view.files().filter(|f| changed.contains(&f.0)).any(|f| view.sites_of(f).iter().any(|s| s.pass == sites::PASS_GO_TYPES)); + if structural || go_types(previous) || go_types(e.view) { return run_all(e); } let mut keys = FxHashSet::default(); @@ -443,7 +511,9 @@ pub fn run_incremental(e: &Engine<'_>, previous: &View, changed: &FxHashSet let recompute = match by { "event-emitter" => value.get("event").and_then(|v| v.as_str()).is_some_and(|key| keys.contains(key)), "react-render" | "jsx-render" => changed.contains(&f.0), - _ => false, + // registration sites name lines of other files: recomputed whole, as cheap + "interface-impl" | "go-implements" => true, + by => GLOBAL_PASSES.contains(&by), }; if recompute { continue; @@ -470,15 +540,18 @@ pub fn run_incremental(e: &Engine<'_>, previous: &View, changed: &FxHashSet res: edge.res, ref_idx: NONE, meta: mid, - order: 0, + order: order_of(by), }); } } } let mut wcx = WorkerCx::new(); + let go_pairs = go::go_implements(e, &mut out); + interface_impl(e, &go_pairs, &mut out); event_emitter(e, &mut out, &mut wcx, Some(&keys)); react_render(e, &mut out, Some(changed)); jsx_render(e, &mut out, &mut wcx, Some(changed)); + run_global(e, &mut out, &mut wcx); SynthOutput { edges: out.edges, emeta: out.emeta } } diff --git a/crates/cg-resolve/src/synth/scan.rs b/crates/cg-resolve/src/synth/scan.rs new file mode 100644 index 0000000..a691dcb --- /dev/null +++ b/crates/cg-resolve/src/synth/scan.rs @@ -0,0 +1,183 @@ +//! Helpers shared by the site scanners: line / UTF-16 column of an offset, the function a +//! line sits in, and call arguments read off comment-stripped source. + +use crate::frameworks::href::{index_at_depth0, match_bracket}; +use cg_core::NodeKind; +use cg_extract::HookCtx; + +/// Line starts of a source text, for offset -> (line, column) lookups. +pub struct LineIndex<'a> { + src: &'a str, + starts: Vec, +} + +impl<'a> LineIndex<'a> { + pub fn new(src: &'a str) -> Self { + let mut starts = vec![0]; + starts.extend(src.bytes().enumerate().filter(|(_, b)| *b == b'\n').map(|(i, _)| i + 1)); + LineIndex { src, starts } + } + + /// 1-based line of a byte offset. + pub fn line(&self, off: usize) -> u32 { + self.starts.partition_point(|s| *s <= off) as u32 + } + + /// UTF-16 column of a byte offset on its line. + pub fn col(&self, off: usize) -> u32 { + let i = self.starts.partition_point(|s| *s <= off) - 1; + let start = self.starts[i]; + let end = self.starts.get(i + 1).map_or(self.src.len(), |e| e - 1); + cg_core::text::col16(&self.src[start..end.max(start)], off - start) + } + + /// Text of lines `from..=to` (1-based, clamped). + pub fn lines(&self, from: u32, to: u32) -> &'a str { + let from = (from.max(1) as usize - 1).min(self.starts.len() - 1); + let to = (to as usize).min(self.starts.len()); + let end = self.starts.get(to).copied().unwrap_or(self.src.len()); + &self.src[self.starts[from]..end.max(self.starts[from])] + } +} + +/// A bundle or minified file: lines far longer than written code. Its pervasive `x[k](…)`, +/// `.get(…)` and one-letter names are a false-positive minefield, and its one giant line +/// makes per-match position lookups quadratic, so the site scanners skip it. +pub fn is_minified(src: &str) -> bool { + let lines = src.bytes().filter(|b| *b == b'\n').count() + 1; + src.len() / lines > 200 || (src.len() > 20_000 && src.split('\n').any(|l| l.len() > 5_000)) +} + +/// Kinds a call site is attributed to (upstream `FN_KINDS`). +pub const FN_KINDS: &[NodeKind] = &[NodeKind::Method, NodeKind::Function, NodeKind::Component]; + +/// Row of the tightest node of `kinds` whose lines contain `line`. +pub fn enclosing_row(ctx: &HookCtx<'_>, line: u32, kinds: &[NodeKind]) -> Option { + let mut best: Option<(u32, u32, u32)> = None; // (span, start, row) + for (row, n) in ctx.rows.nodes().enumerate() { + if !kinds.contains(&n.kind()) { + continue; + } + let (s, e) = (n.start_line(), n.end_line().max(n.start_line())); + if s <= line && line <= e { + let span = e - s; + if best.is_none_or(|(b, bs, _)| span < b || (span == b && s > bs)) { + best = Some((span, s, row as u32)); + } + } + } + best.map(|b| b.2) +} + +/// Top-level arguments of the call whose `(` is at `open`, trimmed. +pub fn call_args(src: &str, open: usize) -> Option> { + let close = match_bracket(src, open)?; + let inner = &src[open + 1..close]; + let mut out = Vec::new(); + let mut start = 0; + while let Some(c) = index_at_depth0(inner, b',', start) { + out.push(inner[start..c].trim()); + start = c + 1; + } + let last = inner[start..].trim(); + if !last.is_empty() || !out.is_empty() { + out.push(last); + } + Some(out) +} + +/// Whether the byte before `at` may end an identifier or member chain (`x.` / `x$`), so a +/// match starting at `at` would begin mid-chain. +pub fn mid_chain(src: &str, at: usize) -> bool { + at > 0 && (src.as_bytes()[at - 1].is_ascii_alphanumeric() || matches!(src.as_bytes()[at - 1], b'_' | b'$' | b'.')) +} + +/// The initializer of the nearest `const|let|var name = …` above `line` and not above `floor` +/// (a reassignment in between makes it unknown): what a bare identifier argument holds. +pub fn local_initializer<'a>(lines: &LineIndex<'a>, name: &str, line: u32, floor: u32) -> Option<&'a str> { + if name.is_empty() || !name.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '$') { + return None; + } + let src = lines.src; + let mut l = line.saturating_sub(1); + while l >= floor.max(1) { + let text = lines.lines(l, l); + let line_off = text.as_ptr() as usize - src.as_ptr() as usize; + for kw in ["const ", "let ", "var "] { + let Some(i) = text.find(&format!("{kw}{name}")) else { continue }; + let after_name = i + kw.len() + name.len(); + if text[after_name..].starts_with(|c: char| c.is_alphanumeric() || c == '_' || c == '$') { + continue; + } + let rest = text[after_name..].trim_start(); + let rest = if let Some(r) = rest.strip_prefix(':') { r.find('=').map_or("", |e| &r[e..]) } else { rest }; + if let Some(init) = rest.strip_prefix('=').filter(|r| !r.starts_with(['=', '>'])) { + // the initializer may continue onto the lines before the call + let init_off = line_off + (text.len() - init.len()); + let limit = lines.lines(line, line).as_ptr() as usize - src.as_ptr() as usize; + let init_full = &src[init_off..limit.max(init_off)]; + let end = statement_end(init_full); + return Some(init_full[..end].trim()); + } + } + let reassigned = text.match_indices(name).any(|(i, _)| { + let before = text[..i].chars().next_back(); + let after = text[i + name.len()..].trim_start(); + !before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') + && after.starts_with('=') + && !after.starts_with("==") + && !after.starts_with("=>") + }); + if reassigned { + return None; + } + if l == 0 { + break; + } + l -= 1; + } + None +} + +/// End of the expression statement starting the text: the first `;` or line break at depth 0. +fn statement_end(s: &str) -> usize { + let b = s.as_bytes(); + let mut depth = 0i32; + let mut i = 0; + while i < b.len() { + match b[i] { + b'"' | b'\'' => i = crate::frameworks::href::string_end(s, i).unwrap_or(b.len()), + b'`' => i = crate::frameworks::href::template_end(s, i).unwrap_or(b.len()), + b'(' | b'[' | b'{' => depth += 1, + b')' | b']' | b'}' => depth -= 1, + b';' if depth <= 0 => return i, + b'\n' if depth <= 0 => { + // a continuation line (`? a : b`, `+ x`) belongs to the expression + let next = s[i + 1..].trim_start(); + if !next.starts_with(['?', ':', '.', '+', '&', '|']) { + return i; + } + } + _ => {} + } + i += 1; + } + s.len() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn lines_and_locals() { + let src = "function f() {\n const href = ok\n ? '/a'\n : '/b';\n go(href);\n}\n"; + let li = LineIndex::new(src); + assert_eq!(li.line(src.find("go(").unwrap()), 5); + assert_eq!(li.col(src.find("go(").unwrap()), 2); + assert_eq!(local_initializer(&li, "href", 5, 1), Some("ok\n ? '/a'\n : '/b'")); + let src2 = "let x = '/a';\nx = other;\ngo(x);\n"; + assert_eq!(local_initializer(&LineIndex::new(src2), "x", 3, 1), None); + assert_eq!(call_args("f(a, {b: 1, c}, `x${y}`)", 1).unwrap(), vec!["a", "{b: 1, c}", "`x${y}`"]); + } +} diff --git a/crates/cg-resolve/src/synth/sites.rs b/crates/cg-resolve/src/synth/sites.rs index a097c40..57a4c17 100644 --- a/crates/cg-resolve/src/synth/sites.rs +++ b/crates/cg-resolve/src/synth/sites.rs @@ -10,9 +10,57 @@ use std::sync::OnceLock; pub const PASS_EMITTER: u16 = 1; pub const PASS_REACT_RENDER: u16 = 2; pub const PASS_JSX: u16 = 3; +/// Go type names that stand for other types (`go_implements`). +pub const PASS_GO_TYPES: u16 = 4; +/// Navigation calls and link markup (`frameworks::nav`). +pub const PASS_NAV: u16 = 5; +/// A Next.js proxy's entry and matcher (`frameworks::nav`). +pub const PASS_PROXY: u16 = 6; +/// Field-backed callback channels (`dynamic::callbacks`). +pub const PASS_CALLBACK: u16 = 7; +/// Object-literal registries and their dispatch (`dynamic::registries`). +pub const PASS_REGISTRY: u16 = 8; +/// HTTP client calls and client instances (`dynamic::cross_tier`). +pub const PASS_HTTP: u16 = 9; +/// Queue bindings, producers and consumers (`dynamic::cross_tier`). +pub const PASS_QUEUE: u16 = 10; +/// Event-bus / socket dispatches and handlers (`dynamic::cross_tier`). +pub const PASS_BUS: u16 = 11; +/// Celery tasks and their dispatch (`dynamic::celery`). +pub const PASS_CELERY: u16 = 12; +/// Spring application events (`dynamic::spring_events`). +pub const PASS_SPRING: u16 = 13; pub const ROLE_EMIT: u8 = 0; pub const ROLE_ON: u8 = 1; +pub const ROLE_GO_ALIAS: u8 = 0; +pub const ROLE_GO_GENERIC: u8 = 1; + +/// Go true aliases and generic type parameters, from the facts of the file. +pub struct GoTypeSites; + +impl FileHook for GoTypeSites { + fn name(&self) -> &'static str { + "go-type-sites" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + lang == Lang::Go + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + for t in &ctx.facts.go_types { + out.sites.push(SiteFact { + pass: PASS_GO_TYPES, + role: if t.alias { ROLE_GO_ALIAS } else { ROLE_GO_GENERIC }, + key_kind: 0, + key_a: t.name.clone(), + key_b: String::new(), + node_row: cg_core::NONE, + line: 0, + payload: t.type_params.join(","), + }); + } + } +} /// Event names too generic to link on (design doc 7.3). const GENERIC_EVENTS: &[&str] = &[ diff --git a/crates/cg-resolve/tests/upstream_parity.rs b/crates/cg-resolve/tests/upstream_parity.rs new file mode 100644 index 0000000..1a5a3bb --- /dev/null +++ b/crates/cg-resolve/tests/upstream_parity.rs @@ -0,0 +1,393 @@ +//! Capabilities ported from upstream CodeGraph: overload selection by arguments, Go structural +//! interfaces by signature, framework routes and navigation, and dynamic / cross-tier edges. +//! Every positive case has a negative twin that must stay unlinked. + +use cg_core::{EdgeKind, NodeIdx}; +use cg_resolve::{build_seg, IndexOptions}; +use cg_store::{dump, Seg, View}; +use std::sync::Arc; + +fn project(files: &[(&str, &str)]) -> (tempfile::TempDir, View) { + let dir = tempfile::tempdir().unwrap(); + for (p, c) in files { + let path = dir.path().join(p); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, c).unwrap(); + } + let built = build_seg(dir.path(), &IndexOptions { threads: Some(2), ..Default::default() }).unwrap(); + let seg: Arc = Arc::new(built.seg); + let root = dir.path().to_path_buf(); + (dir, View::new(seg).with_root(root)) +} + +/// Edges as `from -kind-> to@line` where `to@line` is the target's start line, so overloads +/// sharing a qualified name stay apart. +fn edges_with_lines(v: &View) -> Vec { + let mut out = Vec::new(); + for f in v.files() { + for n in v.nodes_in_file(f) { + let n = NodeIdx(n); + for e in v.out_edges(n) { + if e.kind == EdgeKind::Contains { + continue; + } + out.push(format!("{} -{}-> {}@{}", dump::node_label(v, n), e.kind, dump::node_label(v, e.dst), v.node(e.dst).start_line())); + } + } + } + out.sort(); + out +} + +fn meta_of(v: &View, from: &str, to: &str) -> Vec { + let mut out = Vec::new(); + for f in v.files() { + for n in v.nodes_in_file(f) { + let n = NodeIdx(n); + if dump::node_label(v, n) != from { + continue; + } + for e in v.out_edges(n) { + if dump::node_label(v, e.dst) == to { + out.push(v.edge_meta(e.eid).unwrap_or("").to_owned()); + } + } + } + } + out +} + +#[test] +fn java_overloads_follow_argument_count_and_types() { + let (_d, v) = project(&[ + ("src/p/Node.java", "package p;\npublic class Node {}\n"), + ("src/p/Element.java", "package p;\npublic class Element extends Node {}\n"), + ( + "src/p/Api.java", + r#"package p; +import java.util.function.Function; +public class Api { + public void put(String key) {} + public void put(int key) {} + public void put(String key, int value) {} + public void add(Node n) {} + public void add(String s) {} + public void on(Runnable r) {} + public void on(Function f) {} + public void log(String fmt, Object... args) {} + public void log(String msg) {} + public void size(long n) {} + public void size(Object o) {} +} +"#, + ), + ( + "src/p/Use.java", + r#"package p; +public class Use { + private final Api api = new Api(); + private int count; + void run(Element el, String label) { + api.put("k"); + api.put(1); + api.put("k", 2); + api.add(el); + api.add(label); + api.on(() -> {}); + api.on(s -> s); + api.log("x", 1, 2); + api.log("x"); + api.size(count); + } +} +"#, + ), + ]); + let edges = edges_with_lines(&v); + let run = "src/p/Use.java::p::Use::run -calls-> src/p/Api.java::p::Api"; + for (method, line) in [("put", 4), ("put", 5), ("put", 6), ("add", 7), ("add", 8), ("on", 9), ("on", 10), ("log", 11), ("log", 12), ("size", 13)] { + let needle = format!("{run}::{method}@{line}"); + assert!(edges.contains(&needle), "missing {needle}: {edges:#?}"); + } + // the primitive field widens to `long` and is no `Object` only by boxing: `size(long)` wins + assert!(!edges.contains(&format!("{run}::size@14")), "{edges:#?}"); +} + +#[test] +fn kotlin_and_typescript_overloads() { + let (_d, v) = project(&[ + ( + "src/k/K.kt", + "package k\nclass K {\n fun f(s: String) {}\n fun f(n: Int, m: Long = 0L) {}\n fun g(cb: (Int) -> Unit) {}\n fun g(cb: (Int, Int) -> Unit) {}\n fun run() {\n f(\"x\")\n f(1)\n g { a -> }\n g { a, b -> }\n }\n}\n", + ), + ( + "src/t.ts", + "export class T {\n f(s: string): void;\n f(n: number, m?: number): void;\n f(a: any, b?: any): void {}\n run(n: number) {\n this.f('x');\n this.f(n, 2);\n }\n}\n", + ), + ]); + let edges = edges_with_lines(&v); + let k = "src/k/K.kt::k::K::run -calls-> src/k/K.kt::k::K"; + for needle in [format!("{k}::f@3"), format!("{k}::f@4"), format!("{k}::g@5"), format!("{k}::g@6")] { + assert!(edges.contains(&needle), "missing {needle}: {edges:#?}"); + } + let t = "src/t.ts::T::run -calls-> src/t.ts::T::f"; + assert!(edges.contains(&format!("{t}@2")), "{edges:#?}"); + assert!(edges.contains(&format!("{t}@3")), "{edges:#?}"); +} + +#[test] +fn go_interfaces_need_matching_signatures() { + let (_d, v) = project(&[ + ("go.mod", "module example.com/m\n\ngo 1.21\n"), + ( + "httpm/m.go", + "package httpm\n\nimport \"net/http\"\n\ntype RequestMatcher interface {\n\tMatch(r *http.Request) bool\n}\n\ntype PathMatcher struct{}\n\nfunc (m PathMatcher) Match(r *http.Request) bool { return true }\n", + ), + ( + "tlsm/m.go", + "package tlsm\n\nimport \"crypto/tls\"\n\ntype SNI struct{}\n\nfunc (m SNI) Match(hello *tls.ClientHelloInfo) bool { return true }\n\ntype Counter struct{}\n\nfunc (c Counter) Match(r *Request, n int) bool { return true }\n", + ), + ( + "cache/c.go", + "package cache\n\ntype Getter[K comparable, V any] interface {\n\tGet(k K) (V, bool)\n}\n\ntype Map[K comparable, V any] struct{ m map[K]V }\n\nfunc (c *Map[K, V]) Get(k K) (V, bool) { var v V; return v, false }\n\ntype Base struct{}\n\nfunc (b *Base) Close() error { return nil }\n\ntype Closer interface {\n\tClose() error\n}\n\ntype Wrapped struct {\n\tBase\n}\n\ntype refresher interface {\n\trefresh() error\n}\n\ntype local struct{}\n\nfunc (l local) refresh() error { return nil }\n", + ), + ("other/o.go", "package other\n\ntype remote struct{}\n\nfunc (r remote) refresh() error { return nil }\n"), + ]); + let edges = dump::dump_edges(&v); + let has = |needle: &str| edges.iter().any(|e| e == needle); + assert!(has("httpm/m.go::PathMatcher -implements-> httpm/m.go::RequestMatcher"), "{edges:#?}"); + assert!(!has("tlsm/m.go::SNI -implements-> httpm/m.go::RequestMatcher"), "another parameter type: {edges:#?}"); + assert!(!has("tlsm/m.go::Counter -implements-> httpm/m.go::RequestMatcher"), "another parameter count: {edges:#?}"); + assert!(has("cache/c.go::Map -implements-> cache/c.go::Getter"), "type parameters stay open: {edges:#?}"); + assert!(has("cache/c.go::Wrapped -implements-> cache/c.go::Closer"), "promoted through embedding: {edges:#?}"); + assert!(has("cache/c.go::local -implements-> cache/c.go::refresher"), "{edges:#?}"); + assert!(!has("other/o.go::remote -implements-> cache/c.go::refresher"), "an unexported method of another package: {edges:#?}"); +} + +#[test] +fn next_pages_navigation_links_and_proxy() { + let (_d, v) = project(&[ + ("package.json", r#"{"name":"web","dependencies":{"next":"15.0.0","react":"19"}}"#), + ("app/page.tsx", "export default function Home() { return about }\n"), + ("app/about/page.tsx", "export default function About() { return null }\n"), + ("app/users/[id]/page.tsx", "import Link from 'next/link';\nexport default function User({ id }) {\n return x;\n}\n"), + ("app/users/[id]/settings/page.tsx", "export default function Settings() { return null }\n"), + ("app/dashboard/page.tsx", "export default function Dashboard() { return null }\n"), + ("app/api/users/route.ts", "export async function GET() { return Response.json([]) }\nexport async function POST(req: Request) { return Response.json({}) }\n"), + ( + "app/login/actions.ts", + "import { redirect } from 'next/navigation';\nexport async function login(ok: boolean) {\n const next = ok ? '/dashboard' : '/about';\n redirect(next);\n}\n", + ), + ( + "components/Nav.tsx", + "'use client';\nimport { useRouter } from 'next/navigation';\nexport function Nav() {\n const router = useRouter();\n const go = () => router.push('/users/42');\n const nowhere = () => router.push('/missing');\n const list = [];\n list.push('/about');\n return null;\n}\n", + ), + ( + "middleware.ts", + "import { NextResponse } from 'next/server';\nexport function middleware(req) { return NextResponse.next(); }\nexport const config = {\n matcher: ['/dashboard/:path*', '/((?!api|_next/static).*)'],\n};\n", + ), + ]); + let edges = dump::dump_edges(&v); + let has = |needle: &str| edges.iter().any(|e| e == needle); + // pages are named by their path and render their default export + assert!(has("app/about/page.tsx::app/about/page.tsx::route:/about -calls-> app/about/page.tsx::About"), "{edges:#?}"); + assert!(has("app/api/users/route.ts::app/api/users/route.ts::GET:/api/users -references-> app/api/users/route.ts::GET"), "{edges:#?}"); + // links and navigation calls reach the page + assert!(has("app/page.tsx::Home -navigates-> app/about/page.tsx::app/about/page.tsx::route:/about"), "{edges:#?}"); + assert!( + has("app/users/[id]/page.tsx::User -navigates-> app/users/[id]/settings/page.tsx::app/users/[id]/settings/page.tsx::route:/users/:id/settings"), + "{edges:#?}" + ); + assert!(has("components/Nav.tsx::Nav::go -navigates-> app/users/[id]/page.tsx::app/users/[id]/page.tsx::route:/users/:id"), "{edges:#?}"); + assert!( + has("app/login/actions.ts::login -navigates-> app/dashboard/page.tsx::app/dashboard/page.tsx::route:/dashboard"), + "both arms of the local: {edges:#?}" + ); + assert!(has("app/login/actions.ts::login -navigates-> app/about/page.tsx::app/about/page.tsx::route:/about"), "{edges:#?}"); + assert!(!edges.iter().any(|e| e.starts_with("components/Nav.tsx::Nav::nowhere -navigates->")), "no page serves /missing: {edges:#?}"); + assert!(!edges.iter().any(|e| e.starts_with("components/Nav.tsx::Nav -navigates->")), "an array push is no navigation: {edges:#?}"); + // the proxy runs before every route its matcher covers + assert!(has("app/dashboard/page.tsx::app/dashboard/page.tsx::route:/dashboard -calls-> middleware.ts::middleware"), "{edges:#?}"); + assert!(has("app/about/page.tsx::app/about/page.tsx::route:/about -calls-> middleware.ts::middleware"), "{edges:#?}"); + assert!(!has("app/api/users/route.ts::app/api/users/route.ts::GET:/api/users -calls-> middleware.ts::middleware"), "excluded by the matcher: {edges:#?}"); + let meta = meta_of(&v, "components/Nav.tsx::Nav::go", "app/users/[id]/page.tsx::app/users/[id]/page.tsx::route:/users/:id"); + assert!(meta.iter().any(|m| m.contains("\"next-navigation\"") && m.contains("\"href\":\"/users/42\"")), "{meta:?}"); +} + +#[test] +fn react_router_routes_links_and_navigate() { + let (_d, v) = project(&[ + ("package.json", r#"{"name":"spa","dependencies":{"react":"18","react-router-dom":"6"}}"#), + ( + "src/App.tsx", + r#"import { BrowserRouter, Routes, Route } from 'react-router-dom'; +import { Home, Profile, Team, Login } from './pages'; +export function App() { + return ( + + + } /> + } /> + }> + } /> + + } /> + + + ); +} +function Layout() { return null; } +"#, + ), + ( + "src/pages.tsx", + r#"import { Link, useNavigate } from 'react-router-dom'; +export function Home() { + const navigate = useNavigate(); + const onLogout = () => navigate('/login'); + return me; +} +export function Profile() { return t; } +export function Team() { return null; } +export function Login() { return x; } +"#, + ), + ]); + let edges = dump::dump_edges(&v); + let has = |needle: &str| edges.iter().any(|e| e == needle); + assert!(has("src/App.tsx::src/App.tsx::route:/profile/:id -references-> src/pages.tsx::Profile"), "{edges:#?}"); + assert!(has("src/App.tsx::src/App.tsx::route:/teams/:teamId -references-> src/pages.tsx::Team"), "nested path: {edges:#?}"); + assert!(has("src/pages.tsx::Home -navigates-> src/App.tsx::src/App.tsx::route:/profile/:id"), "{edges:#?}"); + assert!(has("src/pages.tsx::Home::onLogout -navigates-> src/App.tsx::src/App.tsx::route:/login"), "{edges:#?}"); + assert!(has("src/pages.tsx::Profile -navigates-> src/App.tsx::src/App.tsx::route:/teams/:teamId"), "{edges:#?}"); + assert!(!edges.iter().any(|e| e.starts_with("src/pages.tsx::Login -navigates->")), "{edges:#?}"); +} + +#[test] +fn tanstack_routes_and_links() { + let (_d, v) = project(&[ + ("package.json", r#"{"name":"spa","dependencies":{"react":"18","@tanstack/react-router":"1"}}"#), + ("src/routes/posts.$postId.tsx", "import { createFileRoute } from '@tanstack/react-router';\nexport const Route = createFileRoute('/posts/$postId')({ component: PostComponent });\nfunction PostComponent() { return null; }\n"), + ( + "src/routes/index.tsx", + "import { createFileRoute, Link, useNavigate } from '@tanstack/react-router';\nexport const Route = createFileRoute('/')({ component: Index });\nfunction Index() {\n const navigate = useNavigate();\n const open = () => navigate({ to: '/posts/$postId', params: { postId: '1' } });\n return p;\n}\n", + ), + ]); + let edges = dump::dump_edges(&v); + let has = |needle: &str| edges.iter().any(|e| e == needle); + let post = "src/routes/posts.$postId.tsx::src/routes/posts.$postId.tsx::route:/posts/:postId"; + assert!(has(&format!("{post} -calls-> src/routes/posts.$postId.tsx::PostComponent")), "{edges:#?}"); + assert!(has(&format!("src/routes/index.tsx::Index -navigates-> {post}")), "{edges:#?}"); + assert!(has(&format!("src/routes/index.tsx::Index::open -navigates-> {post}")), "{edges:#?}"); +} + +#[test] +fn nestjs_routes_and_injected_services() { + let (_d, v) = project(&[ + ("package.json", r#"{"name":"api","dependencies":{"@nestjs/core":"10","@nestjs/common":"10"}}"#), + ("src/users/users.service.ts", "import { Injectable } from '@nestjs/common';\n@Injectable()\nexport class UsersService {\n findAll() { return []; }\n findOne(id: string) { return id; }\n}\n"), + ( + "src/users/users.controller.ts", + "import { Controller, Get, Param } from '@nestjs/common';\nimport { UsersService } from './users.service';\n@Controller('users')\nexport class UsersController {\n constructor(private readonly users: UsersService) {}\n @Get()\n list() { return this.users.findAll(); }\n @Get(':id')\n one(@Param('id') id: string) { return this.users.findOne(id); }\n}\n", + ), + ]); + let edges = dump::dump_edges(&v); + let has = |needle: &str| edges.iter().any(|e| e == needle); + let ctl = "src/users/users.controller.ts"; + assert!(has(&format!("{ctl}::{ctl}::GET:/users -references-> {ctl}::UsersController::list")), "{edges:#?}"); + assert!(has(&format!("{ctl}::{ctl}::GET:/users/:id -references-> {ctl}::UsersController::one")), "{edges:#?}"); + assert!(has(&format!("{ctl}::UsersController::list -calls-> src/users/users.service.ts::UsersService::findAll")), "{edges:#?}"); +} + +#[test] +fn callback_collections_and_object_registries() { + let (_d, v) = project(&[ + ( + "src/store.ts", + "export class Store {\n private listeners: Array<() => void> = [];\n onChange(cb: () => void) {\n this.listeners.push(cb);\n }\n notify() {\n this.listeners.forEach((l) => l());\n }\n}\n", + ), + ( + "src/view.ts", + "import { Store } from './store';\nexport class View {\n constructor(private store: Store) {}\n mount() {\n this.store.onChange(this.render);\n }\n render() {}\n}\n", + ), + ( + "src/commands.ts", + "export function createUser() {}\nexport function removeUser() {}\nexport function audit() {}\nconst handlers = {\n create: createUser,\n remove: removeUser,\n};\nconst config = { width: 5, height: 6 };\nexport function run(action: string) {\n handlers[action]();\n config[action];\n}\n", + ), + ]); + let edges = dump::dump_edges(&v); + let has = |needle: &str| edges.iter().any(|e| e == needle); + assert!(has("src/store.ts::Store::notify -calls-> src/view.ts::View::render"), "callback: {edges:#?}"); + let meta = meta_of(&v, "src/store.ts::Store::notify", "src/view.ts::View::render"); + assert!(meta.iter().any(|m| m.contains("\"callback\"") && m.contains("\"field\":\"listeners\"")), "{meta:?}"); + assert!(has("src/commands.ts::run -calls-> src/commands.ts::createUser"), "registry: {edges:#?}"); + assert!(has("src/commands.ts::run -calls-> src/commands.ts::removeUser"), "{edges:#?}"); + assert!(!has("src/commands.ts::run -calls-> src/commands.ts::audit"), "{edges:#?}"); +} + +#[test] +fn http_clients_reach_their_routes() { + let (_d, v) = project(&[ + ("package.json", r#"{"name":"full","dependencies":{"express":"4","axios":"1","react":"18"}}"#), + ( + "server/app.js", + "const express = require('express');\nconst app = express();\nfunction listUsers(req, res) {}\nfunction getUser(req, res) {}\nfunction createUser(req, res) {}\napp.get('/api/users', listUsers);\napp.get('/api/users/:id', getUser);\napp.post('/api/users', createUser);\n", + ), + ("web/api.ts", "import axios from 'axios';\nexport const api = axios.create({ baseURL: '/api' });\n"), + ( + "web/users.ts", + "import { api } from './api';\nexport async function loadUsers() {\n return fetch('/api/users');\n}\nexport async function loadUser(id: string) {\n return api.get(`/users/${id}`);\n}\nexport async function addUser(u: object) {\n return fetch('/api/users', { method: 'POST', body: JSON.stringify(u) });\n}\nexport async function missing() {\n return fetch('/api/nothing');\n}\n", + ), + ]); + let edges = dump::dump_edges(&v); + let has = |needle: &str| edges.iter().any(|e| e == needle); + assert!(has("web/users.ts::loadUsers -calls-> server/app.js::server/app.js::GET:/api/users"), "{edges:#?}"); + assert!(has("web/users.ts::loadUser -calls-> server/app.js::server/app.js::GET:/api/users/:id"), "base URL of the imported client: {edges:#?}"); + assert!(has("web/users.ts::addUser -calls-> server/app.js::server/app.js::POST:/api/users"), "method option: {edges:#?}"); + assert!(!edges.iter().any(|e| e.starts_with("web/users.ts::missing -calls->")), "{edges:#?}"); + // the route still reaches its handler, so the flow runs client -> route -> handler + assert!(has("server/app.js::server/app.js::GET:/api/users -references-> server/app.js::listUsers"), "{edges:#?}"); +} + +#[test] +fn queues_buses_celery_and_spring_events() { + let (_d, v) = project(&[ + ("package.json", r#"{"name":"svc","dependencies":{"@nestjs/core":"10","@nestjs/bull":"10"}}"#), + ( + "src/mail.producer.ts", + "import { InjectQueue } from '@nestjs/bull';\nimport { Queue } from 'bull';\nexport class MailProducer {\n constructor(@InjectQueue('email') private readonly mailQueue: Queue) {}\n async welcome() {\n await this.mailQueue.add('welcome', {});\n }\n}\n", + ), + ( + "src/mail.processor.ts", + "import { Processor, Process } from '@nestjs/bull';\n@Processor('email')\nexport class MailProcessor {\n @Process('welcome')\n async sendWelcome(job) {}\n @Process('reset')\n async sendReset(job) {}\n}\n", + ), + ( + "src/users.service.ts", + "import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';\nexport class UsersService {\n constructor(private eventEmitter: EventEmitter2) {}\n create() {\n this.eventEmitter.emit('user.created', {});\n }\n}\nexport class Audit {\n @OnEvent('user.*')\n onUser(payload) {}\n}\n", + ), + ("app/tasks.py", "from celery import shared_task\n\n@shared_task\ndef send_email(to):\n pass\n\ndef not_a_task():\n pass\n"), + ("app/views.py", "from .tasks import send_email, not_a_task\n\ndef signup(request):\n send_email.delay('a@b.c')\n not_a_task.delay()\n"), + ("src/main/java/p/UserCreated.java", "package p;\npublic class UserCreated {}\n"), + ( + "src/main/java/p/UserService.java", + "package p;\npublic class UserService {\n private final Publisher publisher;\n public void create() {\n publisher.publishEvent(new UserCreated());\n }\n}\n", + ), + ( + "src/main/java/p/Welcome.java", + "package p;\npublic class Welcome {\n @EventListener\n public void on(UserCreated e) {}\n @EventListener\n public void other(OtherEvent e) {}\n}\n", + ), + ]); + let edges = dump::dump_edges(&v); + let has = |needle: &str| edges.iter().any(|e| e == needle); + assert!(has("src/mail.producer.ts::MailProducer::welcome -calls-> src/mail.processor.ts::MailProcessor::sendWelcome"), "queue: {edges:#?}"); + assert!(!has("src/mail.producer.ts::MailProducer::welcome -calls-> src/mail.processor.ts::MailProcessor::sendReset"), "{edges:#?}"); + assert!(has("src/users.service.ts::UsersService::create -calls-> src/users.service.ts::Audit::onUser"), "event bus glob: {edges:#?}"); + assert!(has("app/views.py::signup -calls-> app/tasks.py::send_email"), "celery: {edges:#?}"); + assert!( + !edges.iter().any(|e| e.starts_with("app/views.py::signup -calls-> app/tasks.py::not_a_task") + && meta_of(&v, "app/views.py::signup", "app/tasks.py::not_a_task").iter().any(|m| m.contains("celery"))), + "{edges:#?}" + ); + assert!(has("src/main/java/p/UserService.java::p::UserService::create -calls-> src/main/java/p/Welcome.java::p::Welcome::on"), "spring: {edges:#?}"); + assert!(!has("src/main/java/p/UserService.java::p::UserService::create -calls-> src/main/java/p/Welcome.java::p::Welcome::other"), "{edges:#?}"); +} diff --git a/crates/cg-store/src/build.rs b/crates/cg-store/src/build.rs index fda3ae2..89ae9b5 100644 --- a/crates/cg-store/src/build.rs +++ b/crates/cg-store/src/build.rs @@ -448,13 +448,35 @@ pub fn build_draft_at(batches: &[FileBatch], delta: Option<(&[FileId], BuildBase .or_insert(Some(call.name.as_str())); } } + // Argument shapes ride on the call reference as its candidate list (the kernel gives + // calls none). Two calls at one position with one name are told apart by nothing. + let mut call_args: rustc_hash::FxHashMap<(u32, u32, &str), Option<&cg_extract::facts::CallArgsFact>> = rustc_hash::FxHashMap::default(); + for call in &b.facts.call_args { + call_args + .entry((call.line, call.col, call.tail.as_str())) + .and_modify(|prev| { + if prev.is_some_and(|p| p.args != call.args) { + *prev = None; + } + }) + .or_insert(Some(call)); + } for r in rows.refs() { let from_row = r.from_row().filter(|x| *x < kcount as u32).or_else(|| row_by_id(&mut id_to_row, &rows, r.from_id()?)); let from = nstart + from_row.unwrap_or(0); - let name = if r.kind() == RefKind::Edge(EdgeKind::Calls) { - call_names.get(&(r.line(), r.col(), r.name())).copied().flatten().unwrap_or_else(|| r.name()) + let is_call = r.kind() == RefKind::Edge(EdgeKind::Calls); + let name = if is_call { call_names.get(&(r.line(), r.col(), r.name())).copied().flatten().unwrap_or_else(|| r.name()) } else { r.name() }; + let args = if is_call && r.candidates().is_none() { + call_args.get(&(r.line(), r.col(), tail_of(name))).copied().flatten().map(|call| { + let mut joined = String::from(cg_extract::facts::args::ARGS_MARK); + for a in &call.args { + joined.push('\0'); + joined.push_str(a); + } + arena.alloc(joined) + }) } else { - r.name() + None }; let tail_src: &str = if r.kind().edge_kind() == EdgeKind::Imports && import_specs.contains(name) { arena.alloc(format!("module:{name}")) } else { tail_of(name) }; @@ -467,7 +489,7 @@ pub fn build_draft_at(batches: &[FileBatch], delta: Option<(&[FileId], BuildBase by: 0, line: r.line(), col: r.col(), - candidates: r.candidates().map(|c| lists.push_nul_joined(c, &mut text)).unwrap_or(ListId::NONE), + candidates: r.candidates().or(args).map(|c| lists.push_nul_joined(c, &mut text)).unwrap_or(ListId::NONE), target: NodeIdx::NONE, tail: StrId(names.intern(tail_src)), }); diff --git a/crates/cg-sync/src/sync.rs b/crates/cg-sync/src/sync.rs index c9c3df0..eeaf457 100644 --- a/crates/cg-sync/src/sync.rs +++ b/crates/cg-sync/src/sync.rs @@ -539,7 +539,9 @@ pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOp export_changed.push(*f); } for (k, details) in &new { - if (old.get(k) != Some(details)) && !old.contains_key(k) { + // a changed signature can move a call to another overload of the name, even one + // whose target kept its identity + if old.get(k) != Some(details) { d_names.insert(k.name.clone()); } } diff --git a/crates/cg-sync/tests/incremental_parity.rs b/crates/cg-sync/tests/incremental_parity.rs new file mode 100644 index 0000000..d979bca --- /dev/null +++ b/crates/cg-sync/tests/incremental_parity.rs @@ -0,0 +1,274 @@ +//! "Incremental equals full" for the capabilities ported from upstream: overloads picked by +//! arguments, Go interfaces by signature, framework routes and navigation, and the dynamic / +//! cross-tier passes. Each targeted edit changes one side of a link those passes draw; the +//! random edits mix them. After every step the synced view must equal a fresh index. + +use cg_resolve::{index_project, IndexOptions}; +use cg_store::{dump, Store}; +use cg_sync::{sync, SyncOptions}; +use std::fs; +use std::path::Path; +use std::time::{Duration, SystemTime}; + +const FILES: &[(&str, &str)] = &[ + ( + "package.json", + r#"{"name":"parity","dependencies":{"next":"15.0.0","react":"19","react-router-dom":"6","@tanstack/react-router":"1","@nestjs/core":"10","express":"4","axios":"1"}}"#, + ), + ("app/page.tsx", "import Link from 'next/link';\nexport default function Home() {\n return about;\n}\n"), + ("app/about/page.tsx", "export default function About() { return null }\n"), + ("app/users/[id]/page.tsx", "export default function User() { return null }\n"), + ("app/api/items/route.ts", "export async function GET() { return Response.json([]) }\n"), + ("middleware.ts", "export function middleware(req) { return req; }\nexport const config = { matcher: ['/about/:path*', '/users/:id'] };\n"), + ("web/Nav.tsx", "import { useRouter } from 'next/navigation';\nexport function Nav() {\n const router = useRouter();\n const go = () => router.push('/users/7');\n return null;\n}\n"), + ( + "spa/src/App.tsx", + "import { Routes, Route } from 'react-router-dom';\nimport { Home, Team } from './pages';\nexport function App() {\n return (\n \n } />\n } />\n \n );\n}\n", + ), + ("spa/src/pages.tsx", "import { Link, useNavigate } from 'react-router-dom';\nexport function Home() {\n const navigate = useNavigate();\n const open = () => navigate('/teams/1');\n return t;\n}\nexport function Team() { return null; }\n"), + ("nest/items.controller.ts", "import { Controller, Get } from '@nestjs/common';\n@Controller('items')\nexport class ItemsController {\n @Get(':id')\n one() { return 1; }\n}\n"), + ("server/app.js", "const app = require('express')();\nfunction listItems(req, res) {}\napp.get('/api/list', listItems);\n"), + ("client/http.ts", "import axios from 'axios';\nexport const api = axios.create({ baseURL: '/api' });\n"), + ("client/items.ts", "import { api } from './http';\nexport function load() {\n return fetch('/api/list');\n}\nexport function one() {\n return api.get('/items');\n}\n"), + ( + "store/store.ts", + "export class Store {\n private listeners: Array<() => void> = [];\n onChange(cb: () => void) {\n this.listeners.push(cb);\n }\n notify() {\n this.listeners.forEach((l) => l());\n }\n}\n", + ), + ("store/view.ts", "import { Store } from './store';\nexport class View {\n constructor(private store: Store) {}\n mount() {\n this.store.onChange(this.paint);\n }\n paint() {}\n}\n"), + ("reg/commands.ts", "export function createOne() {}\nexport function removeOne() {}\nconst handlers = { create: createOne, remove: removeOne };\nexport function run(a: string) {\n handlers[a]();\n}\n"), + ("q/producer.ts", "export class Producer {\n constructor(private readonly mailQueue: any) {}\n async welcome() {\n await this.mailQueue.add('welcome', {});\n }\n}\n"), + ("q/processor.ts", "@Processor('email')\nexport class MailProcessor {\n @Process('welcome')\n async sendWelcome(job) {}\n}\n"), + ("bus/svc.ts", "export class Svc {\n constructor(private eventEmitter: any) {}\n create() {\n this.eventEmitter.emit('order.created', {});\n }\n}\nexport class Audit {\n @OnEvent('order.created')\n onOrder(p) {}\n}\n"), + ( + "java/p/Api.java", + "package p;\npublic class Api {\n public void put(String key) {}\n public void put(int key) {}\n public void put(String key, int value) {}\n}\n", + ), + ("java/p/Use.java", "package p;\npublic class Use {\n private final Api api = new Api();\n void run() {\n api.put(\"k\");\n api.put(1);\n api.put(\"k\", 2);\n }\n}\n"), + ("java/p/Created.java", "package p;\npublic class Created {}\n"), + ("java/p/Pub.java", "package p;\npublic class Pub {\n private Publisher publisher;\n public void fire() {\n publisher.publishEvent(new Created());\n }\n}\n"), + ("java/p/Listener.java", "package p;\npublic class Listener {\n @EventListener\n public void on(Created e) {}\n}\n"), + ("go/go.mod", "module example.com/m\n\ngo 1.21\n"), + ( + "go/x/x.go", + "package x\n\ntype Matcher interface {\n\tMatch(path string) bool\n}\n\ntype Prefix struct{}\n\nfunc (p Prefix) Match(path string) bool { return true }\n\ntype Count struct{}\n\nfunc (c Count) Match(n int) bool { return false }\n", + ), + ("py/tasks.py", "from celery import shared_task\n\n@shared_task\ndef send(to):\n pass\n"), + ("py/views.py", "from .tasks import send\n\ndef signup():\n send.delay('a')\n"), +]; + +/// One-sided edits of the links above: (file, from, to). +const EDITS: &[(&str, &str, &str)] = &[ + ("app/page.tsx", "href=\"/about\"", "href=\"/users/3\""), + ("app/page.tsx", "href=\"/users/3\"", "href=\"/about\""), + ("middleware.ts", "'/about/:path*', ", ""), + ("middleware.ts", "matcher: [", "matcher: ['/about/:path*', "), + ("web/Nav.tsx", "'/users/7'", "'/about'"), + ("spa/src/pages.tsx", "'/teams/1'", "'/nowhere'"), + ("spa/src/App.tsx", "/teams/:id", "/team/:id"), + ("nest/items.controller.ts", "@Controller('items')", "@Controller('things')"), + ("server/app.js", "'/api/list'", "'/api/all'"), + ("client/items.ts", "fetch('/api/list')", "fetch('/api/all')"), + ("store/store.ts", "this.listeners.push(cb)", "this.others.push(cb)"), + ("store/view.ts", "this.paint", "this.repaint"), + ("reg/commands.ts", "remove: removeOne", "remove: createOne"), + ("q/processor.ts", "@Process('welcome')", "@Process('reset')"), + ("bus/svc.ts", "@OnEvent('order.created')", "@OnEvent('order.*')"), + ("java/p/Api.java", "public void put(int key) {}", "public void put(long key) {}"), + ("java/p/Api.java", "public void put(String key) {}\n", ""), + ("java/p/Listener.java", "on(Created e)", "on(Object e)"), + ("go/x/x.go", "Match(n int) bool", "Match(path string) bool"), + ("go/x/x.go", "func (p Prefix) Match(path string)", "func (p Prefix) Match(path []byte)"), + ("py/tasks.py", "@shared_task\n", ""), + ("app/about/page.tsx", "About", "AboutPage"), +]; + +struct Rng(u64); +impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + fn below(&mut self, n: usize) -> usize { + (self.next() % n as u64) as usize + } +} + +fn write(root: &Path, rel: &str, content: &str, step: u64) { + let p = root.join(rel); + fs::create_dir_all(p.parent().unwrap()).unwrap(); + fs::write(&p, content).unwrap(); + let t = SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000 + step * 7) + Duration::from_nanos(step * 1013); + fs::OpenOptions::new().write(true).open(&p).unwrap().set_modified(t).unwrap(); +} + +fn copy_tree(src: &Path, dst: &Path) { + for e in fs::read_dir(src).unwrap().flatten() { + if e.file_name() == ".cgr" { + continue; + } + let (s, d) = (e.path(), dst.join(e.file_name())); + if s.is_dir() { + fs::create_dir_all(&d).unwrap(); + copy_tree(&s, &d); + } else { + fs::copy(&s, &d).unwrap(); + } + } +} + +fn opts() -> IndexOptions { + IndexOptions { threads: Some(2), ..Default::default() } +} + +fn canonical_of(root: &Path) -> Vec { + let store = Store::locate(root).expect("index"); + let snap = store.open_snapshot().expect("snapshot"); + dump::canonical(&snap.view) +} + +fn full_canonical(src: &Path) -> Vec { + let oracle = tempfile::tempdir().unwrap(); + copy_tree(src, oracle.path()); + index_project(oracle.path(), &opts()).unwrap(); + canonical_of(oracle.path()) +} + +fn first_diff(a: &[String], b: &[String]) -> String { + let (sa, sb): (std::collections::BTreeSet<&String>, std::collections::BTreeSet<&String>) = (a.iter().collect(), b.iter().collect()); + let mut out = format!("{} vs {} lines\n-- only incremental:\n", a.len(), b.len()); + for l in sa.difference(&sb).take(8) { + out.push_str(&format!(" {l}\n")); + } + out.push_str("-- only full:\n"); + for l in sb.difference(&sa).take(8) { + out.push_str(&format!(" {l}\n")); + } + out +} + +fn setup() -> tempfile::TempDir { + let dir = tempfile::tempdir().unwrap(); + for (i, (f, c)) in FILES.iter().enumerate() { + write(dir.path(), f, c, i as u64); + } + index_project(dir.path(), &opts()).unwrap(); + dir +} + +#[test] +fn fixture_draws_every_ported_link() { + let dir = setup(); + let lines = canonical_of(dir.path()); + let text = lines.join("\n"); + for by in [ + "next-link", + "next-navigation", + "next-proxy", + "react-router-link", + "react-router-navigation", + "http-client", + "callback", + "object-registry", + "queue-job", + "event-bus", + "spring-event", + "celery-dispatch", + "go-implements", + ] { + assert!(text.contains(by), "no {by} edge in the fixture:\n{text}"); + } +} + +#[test] +fn targeted_edits_match_full_rebuild() { + let dir = setup(); + let root = dir.path(); + for (step, (f, from, to)) in EDITS.iter().enumerate() { + let cur = fs::read_to_string(root.join(f)).unwrap(); + if !cur.contains(from) { + continue; + } + write(root, f, &cur.replacen(from, to, 1), 200 + step as u64); + let rep = sync(root, &SyncOptions { index: opts(), ..Default::default() }).unwrap(); + assert!(rep.full_rebuild.is_none(), "edit {step} fell back to a full rebuild: {:?}", rep.full_rebuild); + let (inc, full) = (canonical_of(root), full_canonical(root)); + assert!(inc == full, "edit {step} ({f}: {from:?} -> {to:?}) diverged:\n{}", first_diff(&inc, &full)); + } +} + +fn random_sequence(seed: u64, steps: usize) { + let dir = setup(); + let root = dir.path(); + let mut rng = Rng(seed.wrapping_mul(0x9E3779B97F4A7C15) | 1); + let mut log = Vec::new(); + let mut deltas = 0; + for step in 1..=steps as u64 { + let stamp = 500 + step; + match rng.below(6) { + 0 | 1 => { + let (f, from, to) = EDITS[rng.below(EDITS.len())]; + if let Ok(cur) = fs::read_to_string(root.join(f)) { + if cur.contains(from) { + log.push(format!("edit {f}: {from:?} -> {to:?}")); + write(root, f, &cur.replacen(from, to, 1), stamp); + } + } + } + 2 => { + let (f, c) = FILES[rng.below(FILES.len())]; + if f != "package.json" && f != "go/go.mod" { + log.push(format!("restore {f}")); + write(root, f, c, stamp); + } + } + 3 => { + let (f, _) = FILES[rng.below(FILES.len())]; + if f != "package.json" && f != "go/go.mod" && root.join(f).exists() { + log.push(format!("remove {f}")); + fs::remove_file(root.join(f)).unwrap(); + } + } + 4 => { + let (f, _) = FILES[rng.below(FILES.len())]; + if let Ok(cur) = fs::read_to_string(root.join(f)) { + if f.ends_with(".ts") || f.ends_with(".tsx") || f.ends_with(".java") || f.ends_with(".go") || f.ends_with(".js") { + log.push(format!("comment {f}")); + write(root, f, &format!("// note\n{cur}"), stamp); + } + } + } + _ => { + let n = rng.below(3); + let (rel, content) = match n { + 0 => ( + format!("app/gen{step}/page.tsx"), + format!("import Link from 'next/link';\nexport default function Gen{step}() {{ return a; }}\n"), + ), + 1 => (format!("client/gen{step}.ts"), "export function gen() { return fetch('/api/list'); }\n".to_owned()), + _ => (format!("java/p/Gen{step}.java"), format!("package p;\npublic class Gen{step} {{\n void go(Api a) {{ a.put(\"x\", 1); }}\n}}\n")), + }; + log.push(format!("add {rel}")); + write(root, &rel, &content, stamp); + } + } + let rep = sync(root, &SyncOptions { index: opts(), ..Default::default() }).unwrap_or_else(|e| panic!("sync failed at step {step}: {e}\n{log:#?}")); + if rep.full_rebuild.is_none() && !rep.up_to_date { + deltas += 1; + } + let (inc, full) = (canonical_of(root), full_canonical(root)); + assert!(inc == full, "seed {seed} step {step} diverged:\n{}\nlog: {log:#?}", first_diff(&inc, &full)); + } + assert!(deltas > steps / 3, "most steps should have used a delta layer ({deltas}/{steps})"); +} + +#[test] +fn random_edits_match_full_rebuild_a() { + random_sequence(11, 40); +} + +#[test] +fn random_edits_match_full_rebuild_b() { + random_sequence(29, 40); +} From a2c6c30e1e2eb1582d1eb480ae6ae8efc5b467c1 Mon Sep 17 00:00:00 2001 From: NianJiuZst <3235467914@qq.com> Date: Sun, 1 Nov 2026 09:36:37 -0600 Subject: [PATCH 2/2] fix: satisfy clippy in client path resolution --- crates/cg-resolve/src/synth/dynamic.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/cg-resolve/src/synth/dynamic.rs b/crates/cg-resolve/src/synth/dynamic.rs index 445f655..8d6d981 100644 --- a/crates/cg-resolve/src/synth/dynamic.rs +++ b/crates/cg-resolve/src/synth/dynamic.rs @@ -915,7 +915,8 @@ fn client_path(raw: &str, base: Option<&str>) -> Option<(Vec, bool)> { } p = rest; suffix = true; - } else if let Some(base) = base { + } else { + let base = base?; match client_path(base, None) { Some((segs, s)) => { suffix = s; @@ -929,8 +930,6 @@ fn client_path(raw: &str, base: Option<&str>) -> Option<(Vec, bool)> { } None => return None, } - } else { - return None; } } else if let Some(base) = base { match client_path(base, None) {