vibe.static_analysis.visitors

Jinja AST Visitors for Static Analysis (Flask-Independent).

Contains all jinja2.visitor.NodeVisitor subclasses used to traverse the template's Abstract Syntax Tree during static analysis. Each visitor checks for specific potential errors or patterns (e.g., undefined variables, type mismatches, invalid function calls, cross-reference issues).

DependencyVisitor

Builds a map of conditional dependencies in the AST.

simple Names and attribute access (Getattr) for nested variables. Accepts known question keys (full paths) to filter dependencies.

visit_If

visit_If(node: If) -> None

Track if-condition variable as potential parent for nested questions.

visit_Name

visit_Name(node: Name) -> None

Record simple variable name as dependency if it's a known question.

visit_Getattr

visit_Getattr(node: Getattr) -> None

Record attribute path as dependency if it's a known question.

DisabledMetaNamespaceVisitor

Report usage of disabled meta namespaces (e.g., meta.assistant).

visit_Getattr

visit_Getattr(node: Getattr) -> None

Report error if meta.attr is used but that extension is disabled.

visit_Getitem

visit_Getitem(node: Getitem) -> None

Report error if meta['key'] is used but that extension is disabled.

generic_visit

generic_visit(node: Node, *args: object, **kwargs: object) -> object

Continue traversal for other node types.

UsageVisitor

Builds a usage map for known question variables.

and the full stack of conditional contexts active at that usage point.

Output format (usage_map): Dict[str, Set[UsageInfo]]

visit_If

visit_If(node: If) -> None

Push If context, visit body/else, pop context, then visit condition.

visit_For

visit_For(node: For) -> None

Push For context, visit body/else, pop context, then visit iterator.

visit_Macro

visit_Macro(node: Macro) -> None

Push Macro context, visit body, then pop context.

visit_Block

visit_Block(node: Block) -> None

Push Block context, visit body, then pop context.

visit_Name

visit_Name(node: Name) -> None

Record usage if simple variable name is a known question path.

visit_Getattr

visit_Getattr(node: Getattr) -> None

Record usage for dotted path (a.b.c), then recurse into base.

visit_Getitem

visit_Getitem(node: Getitem) -> None

Record usage for bracket path (a['b']), then recurse into base and key.

FindFirstVariablePathVisitor

Visits nodes to find the first variable Name or Getattr path encountered.

visit_Name

visit_Name(node: Name) -> None

Capture first variable name encountered in load context.

visit_Getattr

visit_Getattr(node: Getattr) -> None

Capture first dotted path encountered.

generic_visit

generic_visit(node: Node, *args: object, **kwargs: object) -> object

Continue visiting children only if path not yet found.

ValidateDefinedVisitor

Visits a Jinja AST to validate calls to the 'defined' function.

Checks that the argument is a string literal corresponding to a defined question path.

visit_Call

visit_Call(node: Call) -> None

Validate defined() calls have exactly one string literal argument.

CrossReferenceContextVisitor

Finds VIBE markers in the Jinja AST.

conditional contexts based on enclosing 'if' statements.

visit_If

visit_If(node: If) -> None

Push if-condition variables onto stack, visit body/else, then pop.

visit_For

visit_For(node: For) -> None

Push for-iterator variables onto stack, visit body/else, then pop.

visit_Comment

visit_Comment(node: object) -> None

Check comments for VIBE markers.

finalize_checks

finalize_checks() -> None

Report errors for missing targets and context mismatches after AST traversal.

ValidateShowCallsVisitor

Visits a Jinja AST to validate calls to the 'show' function.

Checks that the argument is a simple variable name corresponding to a defined block of type note, warning, or error.

visit_Call

visit_Call(node: Call) -> None

Validate show() calls reference defined message blocks (note/warning/error).

ValidateVariablePathsVisitor

Validates variable paths referenced in the AST.

valid block prefixes, and paths existing within the session_context structure.

visit_Name

visit_Name(node: Name) -> None

Check that a simple variable name is defined in questions/context/globals.

visit_Getattr

visit_Getattr(node: Getattr) -> None

Check attribute access (a.b) by validating the base path is known.

visit_Getitem

visit_Getitem(node: Getitem) -> None

Check subscript access (a['key']) by validating the base path.

visit_Block

visit_Block(node: Block) -> None

Push new scope for block variables, pop after visiting.

visit_For

visit_For(node: For) -> None

Handle for-loop scope: add loop variables, visit body, remove after.

visit_Macro

visit_Macro(node: Macro) -> None

Handle macro scope: add argument names, visit body, pop scope.

visit_Assign

visit_Assign(node: Assign) -> None

Add assigned variable to current scope, then visit RHS expression.

ValidateMultichoiceKeyAccessVisitor

Checks key access validity for multichoice variables.

visit_Getitem

visit_Getitem(node: Getitem) -> None

Validate key access on multichoice variables against allowed option values.

ValidateEnumComparisonVisitor

Checks comparisons (==, !=) of enum variables against string literals.

visit_Compare

visit_Compare(node: Compare) -> None

Check enum comparisons against string literals for valid option values.

ValidateTristateOperatorVisitor

Warns when tristate variables are used as generic truthy/falsy values.

visit_If

visit_If(node: If) -> None

Check if-condition for tristate misuse (truthy/falsy instead of explicit tests).

InsertComponentVisitor

Collect insert() call sites for component validation.

visit_Call

visit_Call(node: Call) -> None

Extract insert() calls, recording component paths and alias mappings.

ValidateExpressionVarsVisitor

Validate variable references inside expression blocks.

visit_Name

visit_Name(node: Name) -> None

Validate that referenced variable exists in host context or local scope.

visit_Getattr

visit_Getattr(node: Getattr) -> None

Visit attribute access by checking the base node in host context.

visit_Getitem

visit_Getitem(node: Getitem) -> None

Visit subscript access by checking both base and key in host context.

AppendixComponentVisitor

Visitor to find appendix() calls in templates during static analysis.

This visitor discovers appendix() calls and extracts the component IDs so they can be loaded during the static analysis phase, even though the appendix() calls may be conditional.

visit_Call

visit_Call(node: Call) -> None

Extract appendix() calls, recording component IDs and alias mappings.