ml_switcheroo.utils.node_diff¶
AST Node Serialization for Visual Diffs.
This utility module provides mechanisms to convert arbitrary LibCST nodes textual source code representation “in vacuum”. This is essential for capturing states of the AST before and after transformations without requiring full file serialization.
Functions¶
|
Renders a LibCST node into its Python source code string representation. |
|
Compares two nodes and returns their source strings if they differ. |
Module Contents¶
- ml_switcheroo.utils.node_diff.capture_node_source(node: libcst.CSTNode) str¶
Renders a LibCST node into its Python source code string representation.
This handles both original nodes (which might carry whitespace info) and constructed nodes (detached from the original tree).
- Parameters:
node – The CST node to serialise.
- Returns:
The Python code string.
- Return type:
str
- ml_switcheroo.utils.node_diff.diff_nodes(original: libcst.CSTNode, modified: libcst.CSTNode) tuple[str, str, bool]¶
Compares two nodes and returns their source strings if they differ.
- Parameters:
original – The node before transformation.
modified – The node after transformation.
- Returns:
(source_before, source_after, has_changed)
- Return type:
tuple