Skip to content

Commit 7411675

Browse files
authored
fix: Remove references to deprecated variable-related methods (#9572)
1 parent 8e9b95f commit 7411675

16 files changed

Lines changed: 162 additions & 239 deletions

blocks/procedures.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ const PROCEDURE_DEF_COMMON = {
308308
while (paramBlock && !paramBlock.isInsertionMarker()) {
309309
const varName = paramBlock.getFieldValue('NAME');
310310
this.arguments_.push(varName);
311-
const variable = this.workspace.getVariable(varName, '')!;
311+
const variable = this.workspace
312+
.getVariableMap()
313+
.getVariable(varName, '')!;
312314
this.argumentVarModels_.push(variable);
313315

314316
this.paramIds_.push(paramBlock.id);
@@ -374,13 +376,13 @@ const PROCEDURE_DEF_COMMON = {
374376
oldId: string,
375377
newId: string,
376378
) {
377-
const oldVariable = this.workspace.getVariableById(oldId)!;
379+
const oldVariable = this.workspace.getVariableMap().getVariableById(oldId)!;
378380
if (oldVariable.getType() !== '') {
379381
// Procedure arguments always have the empty type.
380382
return;
381383
}
382384
const oldName = oldVariable.getName();
383-
const newVar = this.workspace.getVariableById(newId)!;
385+
const newVar = this.workspace.getVariableMap().getVariableById(newId)!;
384386

385387
let change = false;
386388
for (let i = 0; i < this.argumentVarModels_.length; i++) {

core/block.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,9 +1161,9 @@ export class Block {
11611161
const vars = [];
11621162
for (const field of this.getFields()) {
11631163
if (field.referencesVariables()) {
1164-
const model = this.workspace.getVariableById(
1165-
field.getValue() as string,
1166-
);
1164+
const model = this.workspace
1165+
.getVariableMap()
1166+
.getVariableById(field.getValue() as string);
11671167
// Check if the variable actually exists (and isn't just a potential
11681168
// variable).
11691169
if (model) {

core/flyout_base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,9 @@ export abstract class Flyout
813813
createBlock(originalBlock: BlockSvg): BlockSvg {
814814
let newBlock = null;
815815
eventUtils.disable();
816-
const variablesBeforeCreation = this.targetWorkspace.getAllVariables();
816+
const variablesBeforeCreation = this.targetWorkspace
817+
.getVariableMap()
818+
.getAllVariables();
817819
this.targetWorkspace.setResizesEnabled(false);
818820
try {
819821
newBlock = this.placeNewBlock(originalBlock);

core/serialization/blocks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export function appendInternal(
391391
}
392392
eventUtils.disable();
393393

394-
const variablesBeforeCreation = workspace.getAllVariables();
394+
const variablesBeforeCreation = workspace.getVariableMap().getAllVariables();
395395
let block;
396396
try {
397397
block = appendPrivate(state, workspace, {parentConnection, isShadow});

core/serialization/variables.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export class VariableSerializer implements ISerializer {
3232
* variables.
3333
*/
3434
save(workspace: Workspace): IVariableState[] | null {
35-
const variableStates = workspace.getAllVariables().map((v) => v.save());
35+
const variableStates = workspace
36+
.getVariableMap()
37+
.getAllVariables()
38+
.map((v) => v.save());
3639
return variableStates.length ? variableStates : null;
3740
}
3841

core/variables.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ export function getVariable(
727727
// Try to just get the variable, by ID if possible.
728728
if (id) {
729729
// Look in the real variable map before checking the potential variable map.
730-
variable = workspace.getVariableById(id);
730+
variable = workspace.getVariableMap().getVariableById(id);
731731
if (!variable && potentialVariableMap) {
732732
variable = potentialVariableMap.getVariableById(id);
733733
}
@@ -742,7 +742,7 @@ export function getVariable(
742742
throw Error('Tried to look up a variable by name without a type');
743743
}
744744
// Otherwise look up by name and type.
745-
variable = workspace.getVariable(opt_name, opt_type);
745+
variable = workspace.getVariableMap().getVariable(opt_name, opt_type);
746746
if (!variable && potentialVariableMap) {
747747
variable = potentialVariableMap.getVariable(opt_name, opt_type);
748748
}
@@ -809,7 +809,7 @@ export function getAddedVariables(
809809
workspace: Workspace,
810810
originalVariables: IVariableModel<IVariableState>[],
811811
): IVariableModel<IVariableState>[] {
812-
const allCurrentVariables = workspace.getAllVariables();
812+
const allCurrentVariables = workspace.getVariableMap().getAllVariables();
813813
const addedVariables = [];
814814
if (originalVariables.length !== allCurrentVariables.length) {
815815
for (let i = 0; i < allCurrentVariables.length; i++) {

core/xml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ export function domToBlockInternal(
635635
): Block {
636636
// Create top-level block.
637637
eventUtils.disable();
638-
const variablesBeforeCreation = workspace.getAllVariables();
638+
const variablesBeforeCreation = workspace.getVariableMap().getAllVariables();
639639
let topBlock;
640640
try {
641641
topBlock = domToBlockHeadless(xmlBlock, workspace);

tests/mocha/block_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2450,7 +2450,7 @@ suite('Blocks', function () {
24502450
const blockA = createRenderedBlock(this.workspace, 'variable_block');
24512451

24522452
blockA.setCollapsed(true);
2453-
const variable = this.workspace.getVariable('x', '');
2453+
const variable = this.workspace.getVariableMap().getVariable('x', '');
24542454
this.variableMap.renameVariable(variable, 'y');
24552455

24562456
this.clock.runAll();

tests/mocha/blocks/procedures_test.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ suite('Procedures', function () {
2626
setup(function () {
2727
sharedTestSetup.call(this, {fireEventsNow: false});
2828
this.workspace = Blockly.inject('blocklyDiv', {});
29-
this.workspace.createVariable('preCreatedVar', '', 'preCreatedVarId');
30-
this.workspace.createVariable(
31-
'preCreatedTypedVar',
32-
'type',
33-
'preCreatedTypedVarId',
34-
);
29+
this.workspace
30+
.getVariableMap()
31+
.createVariable('preCreatedVar', '', 'preCreatedVarId');
32+
this.workspace
33+
.getVariableMap()
34+
.createVariable('preCreatedTypedVar', 'type', 'preCreatedTypedVarId');
3535
defineRowBlock();
3636
this.variableMap = this.workspace.getVariableMap();
3737
});
@@ -433,7 +433,7 @@ suite('Procedures', function () {
433433
this.clock.runAll();
434434

435435
assert.isNotNull(
436-
this.workspace.getVariable('param1', ''),
436+
this.workspace.getVariableMap().getVariable('param1', ''),
437437
'Expected the old variable to continue to exist',
438438
);
439439
});
@@ -453,7 +453,9 @@ suite('Procedures', function () {
453453
this.clock.runAll();
454454
mutatorIcon.setBubbleVisible(false);
455455

456-
const variable = this.workspace.getVariable('param1', '');
456+
const variable = this.workspace
457+
.getVariableMap()
458+
.getVariable('param1', '');
457459
this.variableMap.renameVariable(variable, 'new name');
458460

459461
assert.isNotNull(
@@ -480,7 +482,9 @@ suite('Procedures', function () {
480482
.connection.connect(paramBlock.previousConnection);
481483
this.clock.runAll();
482484

483-
const variable = this.workspace.getVariable('param1', '');
485+
const variable = this.workspace
486+
.getVariableMap()
487+
.getVariable('param1', '');
484488
this.variableMap.renameVariable(variable, 'new name');
485489

486490
assert.equal(
@@ -506,7 +510,9 @@ suite('Procedures', function () {
506510
this.clock.runAll();
507511
mutatorIcon.setBubbleVisible(false);
508512

509-
const variable = this.workspace.getVariable('param1', '');
513+
const variable = this.workspace
514+
.getVariableMap()
515+
.getVariable('param1', '');
510516
this.variableMap.renameVariable(variable, 'new name');
511517

512518
assert.isNotNull(
@@ -535,7 +541,9 @@ suite('Procedures', function () {
535541
this.clock.runAll();
536542
mutatorIcon.setBubbleVisible(false);
537543

538-
const variable = this.workspace.getVariable('param1', '');
544+
const variable = this.workspace
545+
.getVariableMap()
546+
.getVariable('param1', '');
539547
this.variableMap.renameVariable(variable, 'preCreatedVar');
540548

541549
assert.isNotNull(
@@ -562,7 +570,9 @@ suite('Procedures', function () {
562570
.connection.connect(paramBlock.previousConnection);
563571
this.clock.runAll();
564572

565-
const variable = this.workspace.getVariable('param1', '');
573+
const variable = this.workspace
574+
.getVariableMap()
575+
.getVariable('param1', '');
566576
this.variableMap.renameVariable(variable, 'preCreatedVar');
567577

568578
assert.equal(
@@ -588,7 +598,9 @@ suite('Procedures', function () {
588598
this.clock.runAll();
589599
mutatorIcon.setBubbleVisible(false);
590600

591-
const variable = this.workspace.getVariable('param1', '');
601+
const variable = this.workspace
602+
.getVariableMap()
603+
.getVariable('param1', '');
592604
this.variableMap.renameVariable(variable, 'preCreatedVar');
593605

594606
assert.isNotNull(

tests/mocha/event_test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,9 @@ suite('Events', function () {
833833
title: 'Variable events',
834834
testCases: variableEventTestCases,
835835
setup: (thisObj) => {
836-
thisObj.variable = thisObj.workspace.createVariable(
837-
'name1',
838-
'type1',
839-
'id1',
840-
);
836+
thisObj.variable = thisObj.workspace
837+
.getVariableMap()
838+
.createVariable('name1', 'type1', 'id1');
841839
},
842840
},
843841
{
@@ -1550,7 +1548,9 @@ suite('Events', function () {
15501548
);
15511549

15521550
// Expect the workspace to have a variable with ID 'test_var_id'.
1553-
assert.isNotNull(this.workspace.getVariableById(TEST_VAR_ID));
1551+
assert.isNotNull(
1552+
this.workspace.getVariableMap().getVariableById(TEST_VAR_ID),
1553+
);
15541554
});
15551555
});
15561556
suite('Disable orphans', function () {

0 commit comments

Comments
 (0)