Skip to content

Implement AST unparser and update extract_method_source (BT-977)#1017

Merged
jamesc merged 1 commit intomainfrom
worktree-BT-977
Feb 28, 2026
Merged

Implement AST unparser and update extract_method_source (BT-977)#1017
jamesc merged 1 commit intomainfrom
worktree-BT-977

Conversation

@jamesc
Copy link
Owner

@jamesc jamesc commented Feb 28, 2026

Summary

Implements ADR 0044 Phase 4: a full AST-to-source unparser using the Document/docvec! API, and updates extract_method_source to use it instead of raw byte-range slicing.

Linear issue: https://linear.app/beamtalk/issue/BT-977

Key changes

  • New module crates/beamtalk-core/src/unparse/mod.rs (~850 lines of unparse logic + ~450 lines of tests)

    • Covers all 19 Expression variants, ClassDefinition, MethodDefinition, StateDeclaration, ExpressionStatement
    • Emits leading/trailing comments from CommentAttachment
    • Single-expression methods render inline; multi-expression on new lines
    • 26 unit tests including round-trip parse→unparse→parse verification
  • Updated extract_method_source in gen_server/methods.rs

    • Replaces byte-range slicing with unparse_method() call
    • Fixes: leading comments no longer silently dropped
    • Fixes: synthesized methods now produce full source instead of just selector name
  • 20 codegen snapshots updatedmethodSource entries now contain full method text

Test plan

  • 26 unparse unit tests (including round-trip tests)
  • 1471 Rust tests pass
  • 327 snapshot tests pass
  • 645 BUnit tests pass (including CompiledMethodTest)
  • 2208 Erlang runtime tests pass
  • E2E tests pass
  • Full just ci passes

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • New Features

    • Added comprehensive AST unparsing system to convert internal code representations back to readable source code, with support for comments, expressions, blocks, pattern matching, and type annotations.
  • Refactor

    • Enhanced method source extraction to use new unparsing system, improving consistency and comment preservation across file-based and synthesized methods.

- Add crates/beamtalk-core/src/unparse/mod.rs with full AST-to-source
  unparser using the Document/docvec! API (ADR 0044 Phase 4)
- Cover all 19 Expression variants, ClassDefinition, MethodDefinition,
  StateDeclaration, ExpressionStatement with comment attachment
- Single-expression methods render inline; multi-expression on new lines
- Replace byte-range slicing in extract_method_source with unparser call,
  fixing missing leading comments and synthesized method fallback
- 26 unit tests including round-trip parse→unparse→parse verification
- Update 20 codegen snapshots for full method source in CompiledMethod

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Feb 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between f5ab820 and 38b55d0.

⛔ Files ignored due to path filters (20)
  • test-package-compiler/tests/snapshots/compiler_tests__abstract_class_spawn_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__class_definition_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__class_methods_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__empty_method_body_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__error_message_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__intrinsic_keyword_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__method_lookup_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__sealed_class_violation_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__sealed_method_override_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__stdlib_class_dictionary_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__stdlib_class_list_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__stdlib_class_set_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__stdlib_class_tuple_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__typed_class_warnings_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__typed_methods_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__typed_value_type_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__value_type_multi_expr_method_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__value_type_param_collision_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__workspace_binding_cascade_codegen.snap is excluded by !**/*.snap
  • test-package-compiler/tests/snapshots/compiler_tests__workspace_binding_send_codegen.snap is excluded by !**/*.snap
📒 Files selected for processing (3)
  • crates/beamtalk-core/src/codegen/core_erlang/gen_server/methods.rs
  • crates/beamtalk-core/src/lib.rs
  • crates/beamtalk-core/src/unparse/mod.rs

📝 Walkthrough

Walkthrough

This PR introduces a new comprehensive AST unparser module that converts Beamtalk AST nodes back to source text using a Document-based rendering pipeline. It updates the Core Erlang method source extraction to call the unparser instead of slicing source by byte indices, eliminating reliance on source_text and spans. The unparser handles comments, method signatures, class definitions, expressions, literals, and type annotations.

Changes

Cohort / File(s) Summary
Module Declaration
crates/beamtalk-core/src/lib.rs
Adds public module unparse to crate root, exposing the unparser API.
AST Unparser Implementation
crates/beamtalk-core/src/unparse/mod.rs
New module implementing comprehensive AST-to-source conversion via Document-based rendering pipeline. Provides public API (unparse_module, unparse_method, unparse_class) and extensive internal builders for all AST node types, including comment handling, method signatures, type annotations, and literal rendering.
Core Erlang Code Generation
crates/beamtalk-core/src/codegen/core_erlang/gen_server/methods.rs
Replaces byte-index slicing of source text with call to unparse_method, removing dependency on source spans and source_text while enabling unparsed output including comments for both file-based and synthesized methods.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main changes: implementing an AST unparser and updating the extract_method_source function, with the tracking issue reference BT-977.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch worktree-BT-977

Comment @coderabbitai help to get the list of available commands and usage tips.

@jamesc jamesc merged commit 30efa39 into main Feb 28, 2026
5 checks passed
@jamesc jamesc deleted the worktree-BT-977 branch February 28, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant