Skip to content

Commit a481253

Browse files
tasheecpcallen
andauthored
refactor(VariableMap): Stop using deprecated wrapper methods (#9340)
* refactor(VariableMap): Stop using deprecated wrapper methods * fix format * fix: Apply review suggestions Co-authored-by: Christopher Allen <[email protected]> * fix: restore blank line --------- Co-authored-by: Christopher Allen <[email protected]>
1 parent bf0043d commit a481253

File tree

8 files changed

+152
-139
lines changed

8 files changed

+152
-139
lines changed

blocks/procedures.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -726,21 +726,21 @@ const PROCEDURES_MUTATORARGUMENT = {
726726
if (sourceBlock.isInFlyout) {
727727
return varName;
728728
}
729-
730-
const model = outerWs.getVariable(varName, '');
729+
const variableMap = outerWs.getVariableMap();
730+
const model = variableMap.getVariable(varName, '');
731731
if (model && model.getName() !== varName) {
732732
// Rename the variable (case change)
733-
outerWs.renameVariableById(model.getId(), varName);
733+
variableMap.renameVariable(model, varName);
734734
}
735735
if (!model) {
736736
if (this.editingInteractively) {
737737
if (!this.editingVariable) {
738-
this.editingVariable = outerWs.createVariable(varName, '');
738+
this.editingVariable = variableMap.createVariable(varName, '');
739739
} else {
740-
outerWs.renameVariableById(this.editingVariable.getId(), varName);
740+
variableMap.renameVariable(this.editingVariable, varName);
741741
}
742742
} else {
743-
outerWs.createVariable(varName, '');
743+
variableMap.createVariable(varName, '');
744744
}
745745
}
746746
return varName;

tests/mocha/block_test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,7 @@ suite('Blocks', function () {
20882088
],
20892089
},
20902090
]);
2091+
this.variableMap = this.workspace.getVariableMap();
20912092
});
20922093
teardown(function () {
20932094
eventUtils.enable();
@@ -2426,13 +2427,14 @@ suite('Blocks', function () {
24262427
assertCollapsed(blockA);
24272428
});
24282429
});
2430+
24292431
suite('Renaming Vars', function () {
24302432
test('Simple Rename', function () {
24312433
const blockA = createRenderedBlock(this.workspace, 'variable_block');
24322434

24332435
blockA.setCollapsed(true);
24342436
const variable = this.workspace.getVariable('x', '');
2435-
this.workspace.renameVariableById(variable.getId(), 'y');
2437+
this.variableMap.renameVariable(variable, 'y');
24362438

24372439
this.clock.runAll();
24382440
assertCollapsed(blockA, 'y');
@@ -2441,8 +2443,8 @@ suite('Blocks', function () {
24412443
const blockA = createRenderedBlock(this.workspace, 'variable_block');
24422444

24432445
blockA.setCollapsed(true);
2444-
const variable = this.workspace.createVariable('y');
2445-
this.workspace.renameVariableById(variable.getId(), 'X');
2446+
const variable = this.variableMap.createVariable('y');
2447+
this.variableMap.renameVariable(variable, 'X');
24462448

24472449
this.clock.runAll();
24482450
assertCollapsed(blockA, 'X');

tests/mocha/blocks/procedures_test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ suite('Procedures', function () {
3333
'preCreatedTypedVarId',
3434
);
3535
defineRowBlock();
36+
this.variableMap = this.workspace.getVariableMap();
3637
});
3738

3839
teardown(function () {
@@ -453,7 +454,7 @@ suite('Procedures', function () {
453454
mutatorIcon.setBubbleVisible(false);
454455

455456
const variable = this.workspace.getVariable('param1', '');
456-
this.workspace.renameVariableById(variable.getId(), 'new name');
457+
this.variableMap.renameVariable(variable, 'new name');
457458

458459
assert.isNotNull(
459460
defBlock.getField('PARAMS'),
@@ -480,7 +481,7 @@ suite('Procedures', function () {
480481
this.clock.runAll();
481482

482483
const variable = this.workspace.getVariable('param1', '');
483-
this.workspace.renameVariableById(variable.getId(), 'new name');
484+
this.variableMap.renameVariable(variable, 'new name');
484485

485486
assert.equal(
486487
paramBlock.getFieldValue('NAME'),
@@ -506,7 +507,7 @@ suite('Procedures', function () {
506507
mutatorIcon.setBubbleVisible(false);
507508

508509
const variable = this.workspace.getVariable('param1', '');
509-
this.workspace.renameVariableById(variable.getId(), 'new name');
510+
this.variableMap.renameVariable(variable, 'new name');
510511

511512
assert.isNotNull(
512513
callBlock.getInput('ARG0'),
@@ -535,7 +536,7 @@ suite('Procedures', function () {
535536
mutatorIcon.setBubbleVisible(false);
536537

537538
const variable = this.workspace.getVariable('param1', '');
538-
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar');
539+
this.variableMap.renameVariable(variable, 'preCreatedVar');
539540

540541
assert.isNotNull(
541542
defBlock.getField('PARAMS'),
@@ -562,7 +563,7 @@ suite('Procedures', function () {
562563
this.clock.runAll();
563564

564565
const variable = this.workspace.getVariable('param1', '');
565-
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar');
566+
this.variableMap.renameVariable(variable, 'preCreatedVar');
566567

567568
assert.equal(
568569
paramBlock.getFieldValue('NAME'),
@@ -588,7 +589,7 @@ suite('Procedures', function () {
588589
mutatorIcon.setBubbleVisible(false);
589590

590591
const variable = this.workspace.getVariable('param1', '');
591-
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar');
592+
this.variableMap.renameVariable(variable, 'preCreatedVar');
592593

593594
assert.isNotNull(
594595
callBlock.getInput('ARG0'),

tests/mocha/blocks/variables_test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ suite('Variables', function () {
3232
],
3333
},
3434
]);
35-
this.workspace.createVariable('foo', 'type1', '1');
36-
this.workspace.createVariable('bar', 'type1', '2');
37-
this.workspace.createVariable('baz', 'type1', '3');
35+
this.variableMap = this.workspace.getVariableMap();
36+
this.variableMap.createVariable('foo', 'type1', '1');
37+
this.variableMap.createVariable('bar', 'type1', '2');
38+
this.variableMap.createVariable('baz', 'type1', '3');
3839
});
3940

4041
teardown(function () {
@@ -116,12 +117,11 @@ suite('Variables', function () {
116117
);
117118
});
118119
});
119-
120120
suite('getVariable', function () {
121121
test('By ID', function () {
122-
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
123-
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
124-
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
122+
const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
123+
const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
124+
const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
125125
const result1 = Blockly.Variables.getVariable(this.workspace, 'id1');
126126
const result2 = Blockly.Variables.getVariable(this.workspace, 'id2');
127127
const result3 = Blockly.Variables.getVariable(this.workspace, 'id3');
@@ -132,9 +132,9 @@ suite('Variables', function () {
132132
});
133133

134134
test('By name and type', function () {
135-
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
136-
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
137-
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
135+
const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
136+
const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
137+
const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
138138
const result1 = Blockly.Variables.getVariable(
139139
this.workspace,
140140
null,
@@ -161,9 +161,9 @@ suite('Variables', function () {
161161
});
162162

163163
test('Bad ID with name and type fallback', function () {
164-
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
165-
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
166-
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
164+
const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
165+
const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
166+
const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
167167
const result1 = Blockly.Variables.getVariable(
168168
this.workspace,
169169
'badId',

tests/mocha/field_variable_test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,15 @@ suite('Variable Fields', function () {
488488
this.variableField = this.variableBlock.getField('VAR');
489489
});
490490
test('Rename & Keep Old ID', function () {
491-
this.workspace.renameVariableById('id1', 'name2');
491+
const variableMap = this.workspace.getVariableMap();
492+
variableMap.renameVariable(variableMap.getVariableById('id1'), 'name2');
492493
assert.equal(this.variableField.getText(), 'name2');
493494
assert.equal(this.variableField.getValue(), 'id1');
494495
});
495496
test('Rename & Get New ID', function () {
496-
this.workspace.createVariable('name2', null, 'id2');
497-
this.workspace.renameVariableById('id1', 'name2');
497+
const variableMap = this.workspace.getVariableMap();
498+
variableMap.createVariable('name2', null, 'id2');
499+
variableMap.renameVariable(variableMap.getVariableById('id1'), 'name2');
498500
assert.equal(this.variableField.getText(), 'name2');
499501
assert.equal(this.variableField.getValue(), 'id2');
500502
});

tests/mocha/jso_serialization_test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ suite('JSO Serialization', function () {
3333
defineStatementBlock();
3434

3535
createGenUidStubWithReturns(new Array(10).fill().map((_, i) => 'id' + i));
36+
this.variableMap = this.workspace.getVariableMap();
3637
});
3738

3839
teardown(function () {
@@ -834,7 +835,7 @@ suite('JSO Serialization', function () {
834835

835836
suite('Variables', function () {
836837
test('Without type', function () {
837-
this.workspace.createVariable('testVar', '', 'testId');
838+
this.variableMap.createVariable('testVar', '', 'testId');
838839
const jso = Blockly.serialization.workspaces.save(this.workspace);
839840
const variable = jso['variables'][0];
840841
assertProperty(variable, 'name', 'testVar');
@@ -843,7 +844,7 @@ suite('JSO Serialization', function () {
843844
});
844845

845846
test('With type', function () {
846-
this.workspace.createVariable('testVar', 'testType', 'testId');
847+
this.variableMap.createVariable('testVar', 'testType', 'testId');
847848
const jso = Blockly.serialization.workspaces.save(this.workspace);
848849
const variable = jso['variables'][0];
849850
assertProperty(variable, 'name', 'testVar');

0 commit comments

Comments
 (0)