Skip to content

Commit 52d935c

Browse files
authored
fix: Fix bug that prevented deleting a variable referenced by two connected blocks (#9563)
* fix: Fix bug that prevented deleting a variable referenced by two connected blocks * fix: Remove inadvertent .only
1 parent 5ae74e1 commit 52d935c

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

core/connection.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ export class Connection {
291291
}
292292

293293
let event;
294-
if (eventUtils.isEnabled()) {
294+
if (
295+
eventUtils.isEnabled() &&
296+
!childConnection.getSourceBlock().isDeadOrDying()
297+
) {
295298
event = new (eventUtils.get(EventType.BLOCK_MOVE))(
296299
childConnection.getSourceBlock(),
297300
) as BlockMove;

core/variable_map.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ export class VariableMap
315315
}
316316
try {
317317
for (let i = 0; i < uses.length; i++) {
318+
if (uses[i].isDeadOrDying()) continue;
319+
318320
uses[i].dispose(true);
319321
}
320322
const variables = this.variableMap.get(variable.getType());

tests/mocha/blocks/variables_test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ suite('Variables', function () {
3030
'variableTypes': ['', 'type1', 'type2'],
3131
},
3232
],
33+
'output': null,
34+
},
35+
// Block for variable setter.
36+
{
37+
'type': 'set_var_block',
38+
'message0': '%{BKY_VARIABLES_SET}',
39+
'args0': [
40+
{
41+
'type': 'field_variable',
42+
'name': 'VAR',
43+
'variableTypes': ['', 'type1', 'type2'],
44+
},
45+
{
46+
'type': 'input_value',
47+
'name': 'VALUE',
48+
},
49+
],
50+
'previousStatement': null,
51+
'nextStatement': null,
3352
},
3453
]);
3554
this.variableMap = this.workspace.getVariableMap();
@@ -59,6 +78,21 @@ suite('Variables', function () {
5978
return block;
6079
}
6180

81+
test('can be deleted when two connected blocks reference the same variable', function () {
82+
const getter = new Blockly.Block(this.workspace, 'get_var_block');
83+
getter.getField('VAR').setValue('1');
84+
85+
const setter = new Blockly.Block(this.workspace, 'set_var_block');
86+
setter.getField('VAR').setValue('1');
87+
setter.getInput('VALUE').connection.connect(getter.outputConnection);
88+
89+
this.variableMap.deleteVariable(this.variableMap.getVariableById('1'));
90+
// Both blocks should have been deleted.
91+
assert.equal(0, this.workspace.getAllBlocks(false).length);
92+
// The variable itself should have been deleted.
93+
assert.equal(this.variableMap.getVariableById('1'), undefined);
94+
});
95+
6296
suite('allUsedVarModels', function () {
6397
test('All used', function () {
6498
createTestVarBlock(this.workspace, '1');

0 commit comments

Comments
 (0)