Skip to content
This repository was archived by the owner on May 18, 2026. It is now read-only.

Commit 318c064

Browse files
committed
fix: adjust comment Y position and minimize state
- Updated _onBegin and targetCodeToBlocks to align comment Y-coordinate with block Y-coordinate - Incremented Y-coordinate for sequential statement blocks to prevent overlap - Relaxed test helpers to allow x/y coordinates in blocks
1 parent 3936746 commit 318c064

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,24 @@ class RubyToBlocksConverter {
188188
if (!_.isArray(blocks)) {
189189
blocks = [blocks];
190190
}
191+
let currentY = 0;
191192
blocks.forEach(block => {
192193
if (this._isBlock(block)) {
194+
if (this._isStatementBlock(block)) {
195+
if (block.x === void 0) {
196+
block.x = 0;
197+
}
198+
if (block.y === void 0) {
199+
block.y = currentY;
200+
}
201+
if (block.comment) {
202+
const comment = this._context.comments[block.comment];
203+
if (comment) {
204+
comment.y = block.y;
205+
}
206+
}
207+
currentY += 48;
208+
}
193209
block.topLevel = true;
194210
} else if (block instanceof Primitive) {
195211
throw new RubyToBlocksConverterError(
@@ -1452,21 +1468,22 @@ class RubyToBlocksConverter {
14521468
blocks.forEach(block => {
14531469
switch (this._getBlockType(block)) {
14541470
case 'statement':
1471+
if (block.comment) {
1472+
const comment = this._context.comments[block.comment];
1473+
if (comment) {
1474+
comment.y = currentY;
1475+
}
1476+
}
14551477
if (prevBlock) {
14561478
prevBlock.next = block.id;
14571479
block.parent = prevBlock.id;
14581480
} else {
14591481
result.push(block);
14601482
block.x = 0;
14611483
block.y = currentY;
1462-
if (block.comment) {
1463-
const comment = this._context.comments[block.comment];
1464-
if (comment) {
1465-
comment.y = block.y;
1466-
}
1467-
}
1468-
currentY += 48;
14691484
}
1485+
currentY += 48;
1486+
14701487
if (block.next) {
14711488
const b = this._lastBlock(block);
14721489
if (this._getBlockType(b) === 'statement') {

test/helpers/expect-to-equal-blocks.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ const expectToEqualBlock = function (context, parent, actualBlock, expectedBlock
182182
expect(blocks.getOpcode(block)).toEqual(expected.opcode);
183183
expect(block.parent).toEqual(parent);
184184
expect(block.shadow).toEqual(expected.shadow === true);
185-
expect(block.x).toEqual(void 0);
186-
expect(block.y).toEqual(void 0);
185+
if (expected.x !== void 0) {
186+
expect(block.x).toEqual(expected.x);
187+
}
188+
if (expected.y !== void 0) {
189+
expect(block.y).toEqual(expected.y);
190+
}
187191

188192
expectToEqualFields(context, blocks.getFields(block), expected.fields);
189193

0 commit comments

Comments
 (0)