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
This commit is contained in:
Aaron Dodson
2026-01-12 15:57:00 -08:00
committed by GitHub
parent 5ae74e166f
commit 52d935c815
3 changed files with 40 additions and 1 deletions

View File

@@ -291,7 +291,10 @@ export class Connection {
}
let event;
if (eventUtils.isEnabled()) {
if (
eventUtils.isEnabled() &&
!childConnection.getSourceBlock().isDeadOrDying()
) {
event = new (eventUtils.get(EventType.BLOCK_MOVE))(
childConnection.getSourceBlock(),
) as BlockMove;

View File

@@ -315,6 +315,8 @@ export class VariableMap
}
try {
for (let i = 0; i < uses.length; i++) {
if (uses[i].isDeadOrDying()) continue;
uses[i].dispose(true);
}
const variables = this.variableMap.get(variable.getType());

View File

@@ -30,6 +30,25 @@ suite('Variables', function () {
'variableTypes': ['', 'type1', 'type2'],
},
],
'output': null,
},
// Block for variable setter.
{
'type': 'set_var_block',
'message0': '%{BKY_VARIABLES_SET}',
'args0': [
{
'type': 'field_variable',
'name': 'VAR',
'variableTypes': ['', 'type1', 'type2'],
},
{
'type': 'input_value',
'name': 'VALUE',
},
],
'previousStatement': null,
'nextStatement': null,
},
]);
this.variableMap = this.workspace.getVariableMap();
@@ -59,6 +78,21 @@ suite('Variables', function () {
return block;
}
test('can be deleted when two connected blocks reference the same variable', function () {
const getter = new Blockly.Block(this.workspace, 'get_var_block');
getter.getField('VAR').setValue('1');
const setter = new Blockly.Block(this.workspace, 'set_var_block');
setter.getField('VAR').setValue('1');
setter.getInput('VALUE').connection.connect(getter.outputConnection);
this.variableMap.deleteVariable(this.variableMap.getVariableById('1'));
// Both blocks should have been deleted.
assert.equal(0, this.workspace.getAllBlocks(false).length);
// The variable itself should have been deleted.
assert.equal(this.variableMap.getVariableById('1'), undefined);
});
suite('allUsedVarModels', function () {
test('All used', function () {
createTestVarBlock(this.workspace, '1');