Skip to main content

Module functions

Module functions 

Source
Expand description

Collect every function-unit (def/async def, method, nested def, lambda) from a parsed Python Module.

§Design

  • Build SpanIndex once per file; pass it by reference through the recursion.
  • Named units (def/async def): anchor via pointer-arithmetic on name.value (the libcst Name.value: &str borrows the original source buffer).
  • Lambda units: collect in pre-order; zip with lambda_anchors(src) by index — the k-th lambda keyword token (source order) corresponds to the k-th Lambda node encountered in pre-order. For this ordinal bijection to hold, the lambda-collection walk must be exhaustive: it visits EVERY Lambda node anywhere it can syntactically appear (a superset of the effect driver’s descent in detect/mod.rs — collection descends even into lazy generator-expression element bodies and decorator/param-default/with-item positions, because a lambda there is still its own tokenized unit). collect guards the bijection with a debug_assert_eq! against the tokenizer count, so any future drift fails loudly instead of silently mis-anchoring.

§Borrowed AST (lifetime)

Unlike syn/swc, libcst’s inflated tree borrows &'a str slices from the source buffer; it is not owned. So a FnUnit cannot retain an owned body — it borrows the body suite (or lambda body expression), parameters, and decorators from the live Module. Collection and analysis therefore run in a single borrowed pass (PythonFrontend::analyze keeps module + src alive while it iterates), and analyze_unit emits owned Hotspots so nothing borrowed outlives the pass.

Structs§

FnUnit
A single function-shaped scope collected from the source.

Enums§

FnBody
The body of a function-unit: a statement suite (def/method), a single expression (lambda), or a module’s top-level statement list (the synthetic <module> unit). Borrowed from the parsed Module.

Functions§

collect
Collect every def, async def, method, nested def, and lambda from module.
module_init_unit
Build a synthetic FnUnit representing a module’s top-level initialisation code — the statements that execute when the module is first imported.