Skip to content

fix: コメント付き module/def/メソッド呼び出しの round-trip 不具合#338

Merged
takaokouji merged 4 commits into
developfrom
fix/comment-roundtrip-bugs-336
Mar 18, 2026
Merged

fix: コメント付き module/def/メソッド呼び出しの round-trip 不具合#338
takaokouji merged 4 commits into
developfrom
fix/comment-roundtrip-bugs-336

Conversation

@takaokouji
Copy link
Copy Markdown

Summary

Ruby → Blocks → Ruby ラウンドトリップにおける3つのコメント関連不具合を修正。

  • Bug 3: findBlockForLine が input ブロック(data_variable)を優先してしまい、ユーザーコメントが @ruby:lvar: メタデータを破壊 → greet(name)greet(@_name_1_) に変化。statement ブロックを優先するよう修正
  • Bug 2: isBeforeContainerDefNode が含まれ、def 前のコメントが workspace コメントとしてクラス外に出力される → DefNode を除外し procedures_definition ブロックに関連付け
  • Bug 1: モジュール前のコメントが otherComments として module コードの後に配置される → @ruby:module:ModuleName workspace コメントを導入し、コメントをモジュールの前に配置

Changes Made

packages/scratch-gui/src/lib/ruby-to-blocks-converter/index.js

  • lineStartBlockMap 構築時に input ブロックより statement ブロックを優先(Bug 3)
  • isBeforeContainer から DefNode を除外(Bug 2)
  • ModuleNodecontainerNodeRanges にモジュール名を保存(Bug 1)
  • モジュール前コメントに @ruby:module:ModuleName メタデータを付加(Bug 1)

packages/scratch-gui/src/lib/ruby-generator/data.js

  • @ruby:lvar: メタデータ抽出を startsWithmatch() に変更(防御的修正)

packages/scratch-gui/src/lib/ruby-generator/procedure.js

  • @ruby:module_source: マッチングに multiline フラグ追加

packages/scratch-gui/src/lib/ruby-generator/code-finisher.js

  • @ruby:module: workspace コメント検出・モジュールコード前にユーザーコメント出力
  • finishTargets のモジュール抽出正規表現で preceding コメント行を含むよう修正

Implementation Steps

  • Phase 1: Bug 3 — findBlockForLine のブロック優先度修正 + @ruby:lvar: ロバスト化
  • Phase 2: Bug 2 — def コメントのブロック関連付け
  • Phase 3: Bug 1 — モジュールの workspace コメント
  • Phase Final: 完全なラウンドトリップ統合テスト

Test Coverage

  • 4 unit tests added in ruby-roundtrip-comment-bugs.test.js
  • Full round-trip test using exact reproduction case from issue
  • All existing tests pass (comment-round-trip, super, my_blocks, module, data, variables)

Related Issues

Fixes #336

takaokouji and others added 4 commits March 19, 2026 01:59
When multiple blocks start on the same line with equal range,
findBlockForLine now prefers statement blocks over input (value) blocks.
This prevents user comments from being attached to data_variable blocks
instead of the parent procedures_call, which corrupted @ruby:lvar:
metadata and caused variable names to become @_name_1_.

Also robustified data_variable's @ruby:lvar: extraction to use regex
match instead of startsWith, as a defensive measure.

Refs #336

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove DefNode from isBeforeContainer check so comments before method
definitions are attached to the procedures_definition block instead of
becoming workspace comments. This keeps method comments inside the class.

Also robustified @ruby:module_source: matching with multiline flag to
handle cases where user comments are prepended to metadata.

Refs #336

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Create @ruby:module:ModuleName workspace comments for comments before
module definitions, similar to @ruby:class for classes. The code
generator detects these comments and places them before the module code.

Also fixed finishTargets module deduplication regex to capture preceding
comment lines when extracting module...end blocks.

Refs #336

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Adds a comprehensive round-trip test using the exact reproduction case
from issue #336, verifying all three bugs are fixed together:
- Module comment before module definition
- Def comment inside class before method
- Variable reference preserved with comments

Refs #336

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

@takaokouji takaokouji merged commit bc9f7c5 into develop Mar 18, 2026
9 checks passed
@takaokouji takaokouji deleted the fix/comment-roundtrip-bugs-336 branch March 18, 2026 17:24
github-actions Bot pushed a commit that referenced this pull request Mar 18, 2026
…oundtrip-bugs-336

fix: コメント付き module/def/メソッド呼び出しの round-trip 不具合
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.

fix: コメント付き module/def/メソッド呼び出しの round-trip 不具合

1 participant