From 7bb085db1215e78d4d95563c8932149c8e8d7832 Mon Sep 17 00:00:00 2001 From: MCR-3 Date: Tue, 22 Sep 2026 09:49:33 +0200 Subject: [PATCH] feat: resolve JSX elements as call edges in the TypeScript/JavaScript plugin Mounting a function component (, ..., ) is now resolved as a call, through the same identifier/member resolution _CallCollector already applies to call_expression - imports, local top-level functions, and the existing one-hop Foo.Bar limit. JSX desugars to a call, so this fits the existing Call edge concept. A lowercase intrinsic element (
) and a fragment (<>...) are never calls. A tag naming a class stays unresolved: a class has no single id in this plugin, only its methods do, so wiring it to the constructor would mean something the runtime doesn't (render() via the reconciler, not new). Closes #19 --- backend/languages/typescript.py | 48 +++++++++++++++++++----- sample_typescript_project/List.tsx | 20 ++++++++++ sample_typescript_project/formatting.jsx | 5 +++ 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 sample_typescript_project/List.tsx diff --git a/backend/languages/typescript.py b/backend/languages/typescript.py index eb5ce02..0cb0f6d 100644 --- a/backend/languages/typescript.py +++ b/backend/languages/typescript.py @@ -30,14 +30,22 @@ Calls resolved: `foo()`, `new Foo()`, `this.m()`, `Class.staticM()`, `ns.f()` and `variable.m()` when the variable's class is known from a parameter annotation, `const x: Foo` or `const x = new Foo()` - so annotated code -yields more edges than untyped code, but through the same path. +yields more edges than untyped code, but through the same path. ``, +`...` and `` resolve the same way, anchored to the +opening tag's name - JSX desugars to a call, so mounting a component is a +call from the function that renders it. A lowercase tag (`
`) is a DOM +element, not a call, by JSX's own capitalization convention; a fragment +(`<>...`) has no name and is never a call. A tag naming a class resolves +to nothing (see the member-resolution limit below: a class has no single id +in this plugin, only its methods do). Real limits, in the spirit of the other plugins' resolvers: one object and -one member only (no `this.a.b()`); no inheritance (`super`, inherited -methods); no dynamic calls (`obj[key]()`, `.call`, `.apply`); a function -passed as a value (`items.map(fn)`) isn't a call; JSX elements (``) -aren't calls; a getter or setter runs on property access, which isn't a call; -`export * as ns` isn't followed; and top-level statements belong to no Function. +one member only (no `this.a.b()`, no ``); no inheritance +(`super`, inherited methods); no dynamic calls (`obj[key]()`, `.call`, +`.apply`); a function passed as a value (`items.map(fn)`) isn't a call; a +getter or setter runs on property access, which isn't a call; `export * as +ns` isn't followed; hand-written `React.createElement(...)` isn't recognized +as a JSX-shaped call; and top-level statements belong to no Function. """ from __future__ import annotations @@ -284,6 +292,24 @@ def _extract_params(fn_node: Node, source: bytes) -> list[dict]: return params +# jsx_opening_element (the `` half of a paired `...`) and +# jsx_self_closing_element (``) both carry a `name` field; a fragment's +# opening tag (`<>`) has none. +_JSX_TAG_NODE_TYPES = frozenset({"jsx_opening_element", "jsx_self_closing_element"}) + + +def _is_intrinsic_jsx_tag(name_node: Node) -> bool: + """`
` (lowercase first letter) is a DOM element, not a call; `` / `<_Foo />` are.""" + first = (name_node.text or b"")[:1] + return first.islower() + + +def _anchor_line(node: Node) -> int: + """A method name on a later line than its object (a chain) is where the wire should leave from.""" + anchor = node.child_by_field_name("property") if node.type == "member_expression" else None + return (anchor or node).start_point[0] + 1 + + class _CallCollector: """Collects (lineno, callee-id) pairs inside one function body. @@ -302,9 +328,13 @@ def visit(self, node: Node) -> None: if func is not None: target = self.resolver(func, is_new) if target: - # a method name on a later line than its object (a chain) is where the wire should leave from - anchor = func.child_by_field_name("property") if func.type == "member_expression" else None - self.found.append(((anchor or func).start_point[0] + 1, target)) + self.found.append((_anchor_line(func), target)) + elif node.type in _JSX_TAG_NODE_TYPES: + name = node.child_by_field_name("name") + if name is not None and not (name.type == "identifier" and _is_intrinsic_jsx_tag(name)): + target = self.resolver(name, False) + if target: + self.found.append((_anchor_line(name), target)) for child in node.children: self.visit(child) diff --git a/sample_typescript_project/List.tsx b/sample_typescript_project/List.tsx new file mode 100644 index 0000000..bc9ba87 --- /dev/null +++ b/sample_typescript_project/List.tsx @@ -0,0 +1,20 @@ +import { Row } from "./formatting"; +import { Item } from "./models"; + +/** + * A function component renders another named component (a Wire to Row), a + * lowercase host element and a fragment (neither is a Wire), and a class + * tag (Item has no single id in this plugin, so it stays unresolved too). + */ +export function List({ items }: { items: Item[] }) { + return ( + <> +
+ {items.map((item) => ( + + ))} +
+ + + ); +} diff --git a/sample_typescript_project/formatting.jsx b/sample_typescript_project/formatting.jsx index 72c1e55..0d14268 100644 --- a/sample_typescript_project/formatting.jsx +++ b/sample_typescript_project/formatting.jsx @@ -6,3 +6,8 @@ function label(result) { export function renderRow(result) { return
  • {label(result)}
  • ; } + +/** The JSX-mounted counterpart to renderRow: same content, called as a component. */ +export function Row({ item }) { + return
  • {item.name}
  • ; +}