Skip to content

Commit 4c79ea1

Browse files
authored
refactor!: Deprecate Block.getVars() (#9574)
* refactor!: Deprecate `Block.getVars()` * fix: Fix import path * fix: Simplify test assertions
1 parent 2678f58 commit 4c79ea1

10 files changed

Lines changed: 59 additions & 38 deletions

File tree

blocks/procedures.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type {
3838
import {Msg} from '../core/msg.js';
3939
import {Names} from '../core/names.js';
4040
import * as Procedures from '../core/procedures.js';
41+
import * as deprecation from '../core/utils/deprecation.js';
4142
import * as xmlUtils from '../core/utils/xml.js';
4243
import * as Variables from '../core/variables.js';
4344
import type {Workspace} from '../core/workspace.js';
@@ -345,9 +346,17 @@ const PROCEDURE_DEF_COMMON = {
345346
/**
346347
* Return all variables referenced by this block.
347348
*
349+
* @deprecated v13: Use Blockly.libraryBlocks.procedures.getVarModels()
350+
* .map(m => m.getName())
348351
* @returns List of variable names.
349352
*/
350353
getVars: function (this: ProcedureBlock): string[] {
354+
deprecation.warn(
355+
'Blockly.libraryBlocks.procedures.getVars()',
356+
'v13',
357+
'v14',
358+
'Blockly.libraryBlocks.procedures.getVarModels().map(model => model.getName())',
359+
);
351360
return this.arguments_;
352361
},
353362
/**
@@ -1020,9 +1029,17 @@ const PROCEDURE_CALL_COMMON = {
10201029
/**
10211030
* Return all variables referenced by this block.
10221031
*
1032+
* @deprecated v13: Use Blockly.libraryBlocks.procedures.getVarModels()
1033+
* .map(m => m.getName())
10231034
* @returns List of variable names.
10241035
*/
10251036
getVars: function (this: CallBlock): string[] {
1037+
deprecation.warn(
1038+
'Blockly.libraryBlocks.procedures.getVars()',
1039+
'v13',
1040+
'v14',
1041+
'Blockly.libraryBlocks.procedures.getVarModels().map(model => model.getName())',
1042+
);
10261043
return this.arguments_;
10271044
},
10281045
/**
@@ -1060,7 +1077,8 @@ const PROCEDURE_CALL_COMMON = {
10601077
if (
10611078
def &&
10621079
(def.type !== this.defType_ ||
1063-
JSON.stringify(def.getVars()) !== JSON.stringify(this.arguments_))
1080+
JSON.stringify(def.getVarModels().map((model) => model.getName())) !==
1081+
JSON.stringify(this.arguments_))
10641082
) {
10651083
// The signatures don't match.
10661084
def = null;

core/block.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import * as registry from './registry.js';
5050
import * as Tooltip from './tooltip.js';
5151
import * as arrayUtils from './utils/array.js';
5252
import {Coordinate} from './utils/coordinate.js';
53+
import * as deprecation from './utils/deprecation.js';
5354
import * as idGenerator from './utils/idgenerator.js';
5455
import * as parsing from './utils/parsing.js';
5556
import {Size} from './utils/size.js';
@@ -1139,9 +1140,16 @@ export class Block {
11391140
/**
11401141
* Return all variables referenced by this block.
11411142
*
1143+
* @deprecated v13: Use Blockly.Block.getVarModels().map(m => m.getId())
11421144
* @returns List of variable ids.
11431145
*/
11441146
getVars(): string[] {
1147+
deprecation.warn(
1148+
'Blockly.Block.getVars()',
1149+
'v13',
1150+
'v14',
1151+
'Blockly.Block.getVarModels().map(model => model.getId())',
1152+
);
11451153
const vars: string[] = [];
11461154
for (const field of this.getFields()) {
11471155
if (field.referencesVariables()) {
@@ -1155,7 +1163,6 @@ export class Block {
11551163
* Return all variables referenced by this block.
11561164
*
11571165
* @returns List of variable models.
1158-
* @internal
11591166
*/
11601167
getVarModels(): IVariableModel<IVariableState>[] {
11611168
const vars = [];

demos/blockfactory/factory_utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ FactoryUtils.hasVariableField = function(block) {
964964
if (!block) {
965965
return false;
966966
}
967-
return block.getVars().length > 0;
967+
return block.getVarModels().length > 0;
968968
};
969969

970970
/**

generators/dart/procedures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ export function procedures_defreturn(block: Block, generator: DartGenerator) {
5656
}
5757
const returnType = returnValue ? 'dynamic' : 'void';
5858
const args = [];
59-
const variables = block.getVars();
59+
const variables = block.getVarModels();
6060
for (let i = 0; i < variables.length; i++) {
61-
args[i] = generator.getVariableName(variables[i]);
61+
args[i] = generator.getVariableName(variables[i].getId());
6262
}
6363
let code =
6464
returnType +
@@ -92,7 +92,7 @@ export function procedures_callreturn(
9292
// Call a procedure with a return value.
9393
const funcName = generator.getProcedureName(block.getFieldValue('NAME'));
9494
const args = [];
95-
const variables = block.getVars();
95+
const variables = block.getVarModels();
9696
for (let i = 0; i < variables.length; i++) {
9797
args[i] = generator.valueToCode(block, 'ARG' + i, Order.NONE) || 'null';
9898
}

generators/javascript/procedures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ export function procedures_defreturn(
5858
returnValue = generator.INDENT + 'return ' + returnValue + ';\n';
5959
}
6060
const args = [];
61-
const variables = block.getVars();
61+
const variables = block.getVarModels();
6262
for (let i = 0; i < variables.length; i++) {
63-
args[i] = generator.getVariableName(variables[i]);
63+
args[i] = generator.getVariableName(variables[i].getId());
6464
}
6565
let code =
6666
'function ' +
@@ -93,7 +93,7 @@ export function procedures_callreturn(
9393
// Call a procedure with a return value.
9494
const funcName = generator.getProcedureName(block.getFieldValue('NAME'));
9595
const args = [];
96-
const variables = block.getVars();
96+
const variables = block.getVarModels();
9797
for (let i = 0; i < variables.length; i++) {
9898
args[i] = generator.valueToCode(block, 'ARG' + i, Order.NONE) || 'null';
9999
}

generators/lua/procedures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export function procedures_defreturn(
6060
branch = '';
6161
}
6262
const args = [];
63-
const variables = block.getVars();
63+
const variables = block.getVarModels();
6464
for (let i = 0; i < variables.length; i++) {
65-
args[i] = generator.getVariableName(variables[i]);
65+
args[i] = generator.getVariableName(variables[i].getId());
6666
}
6767
let code =
6868
'function ' +
@@ -95,7 +95,7 @@ export function procedures_callreturn(
9595
// Call a procedure with a return value.
9696
const funcName = generator.getProcedureName(block.getFieldValue('NAME'));
9797
const args = [];
98-
const variables = block.getVars();
98+
const variables = block.getVarModels();
9999
for (let i = 0; i < variables.length; i++) {
100100
args[i] = generator.valueToCode(block, 'ARG' + i, Order.NONE) || 'nil';
101101
}

generators/php/procedures.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ export function procedures_defreturn(block: Block, generator: PhpGenerator) {
2626
const usedVariables = Variables.allUsedVarModels(workspace) || [];
2727
for (const variable of usedVariables) {
2828
const varName = variable.getName();
29-
// getVars returns parameter names, not ids, for procedure blocks
30-
if (!block.getVars().includes(varName)) {
29+
if (!block.getVarModels().includes(variable)) {
3130
globals.push(generator.getVariableName(varName));
3231
}
3332
}
@@ -80,9 +79,9 @@ export function procedures_defreturn(block: Block, generator: PhpGenerator) {
8079
returnValue = generator.INDENT + 'return ' + returnValue + ';\n';
8180
}
8281
const args = [];
83-
const variables = block.getVars();
82+
const variables = block.getVarModels();
8483
for (let i = 0; i < variables.length; i++) {
85-
args[i] = generator.getVariableName(variables[i]);
84+
args[i] = generator.getVariableName(variables[i].getId());
8685
}
8786
let code =
8887
'function ' +
@@ -116,7 +115,7 @@ export function procedures_callreturn(
116115
// Call a procedure with a return value.
117116
const funcName = generator.getProcedureName(block.getFieldValue('NAME'));
118117
const args = [];
119-
const variables = block.getVars();
118+
const variables = block.getVarModels();
120119
for (let i = 0; i < variables.length; i++) {
121120
args[i] = generator.valueToCode(block, 'ARG' + i, Order.NONE) || 'null';
122121
}

generators/python/procedures.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ export function procedures_defreturn(block: Block, generator: PythonGenerator) {
2626
const usedVariables = Variables.allUsedVarModels(workspace) || [];
2727
for (const variable of usedVariables) {
2828
const varName = variable.getName();
29-
// getVars returns parameter names, not ids, for procedure blocks
30-
if (!block.getVars().includes(varName)) {
29+
if (!block.getVarModels().includes(variable)) {
3130
globals.push(generator.getVariableName(varName));
3231
}
3332
}
@@ -82,9 +81,9 @@ export function procedures_defreturn(block: Block, generator: PythonGenerator) {
8281
branch = generator.PASS;
8382
}
8483
const args = [];
85-
const variables = block.getVars();
84+
const variables = block.getVarModels();
8685
for (let i = 0; i < variables.length; i++) {
87-
args[i] = generator.getVariableName(variables[i]);
86+
args[i] = generator.getVariableName(variables[i].getId());
8887
}
8988
let code =
9089
'def ' +
@@ -117,7 +116,7 @@ export function procedures_callreturn(
117116
// Call a procedure with a return value.
118117
const funcName = generator.getProcedureName(block.getFieldValue('NAME'));
119118
const args = [];
120-
const variables = block.getVars();
119+
const variables = block.getVarModels();
121120
for (let i = 0; i < variables.length; i++) {
122121
args[i] = generator.valueToCode(block, 'ARG' + i, Order.NONE) || 'None';
123122
}

tests/mocha/blocks/procedures_test.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,22 +1972,14 @@ suite('Procedures', function () {
19721972
this.clock.runAll();
19731973
}
19741974
function assertArgs(argArray) {
1975-
assert.equal(
1976-
this.defBlock.getVars().length,
1977-
argArray.length,
1978-
'Expected the def to have the right number of arguments',
1975+
assert.deepEqual(
1976+
this.defBlock.getVarModels().map((m) => m.getName()),
1977+
argArray,
19791978
);
1980-
for (let i = 0; i < argArray.length; i++) {
1981-
assert.equal(this.defBlock.getVars()[i], argArray[i]);
1982-
}
1983-
assert.equal(
1984-
this.callBlock.getVars().length,
1985-
argArray.length,
1986-
'Expected the call to have the right number of arguments',
1979+
assert.deepEqual(
1980+
this.callBlock.getVarModels().map((m) => m.getName()),
1981+
argArray,
19871982
);
1988-
for (let i = 0; i < argArray.length; i++) {
1989-
assert.equal(this.callBlock.getVars()[i], argArray[i]);
1990-
}
19911983
}
19921984
test('Simple Add Arg', async function () {
19931985
const args = ['arg1'];

tests/mocha/test_helpers/procedures.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ function assertCallBlockArgsStructure(callBlock, args) {
4747
'Call block consts did not match expected.',
4848
);
4949
}
50-
assert.sameOrderedMembers(callBlock.getVars(), args);
50+
assert.sameOrderedMembers(
51+
callBlock.getVarModels().map((model) => model.getName()),
52+
args,
53+
);
5154
}
5255

5356
/**
@@ -104,7 +107,10 @@ export function assertDefBlockStructure(
104107
);
105108
}
106109

107-
assert.sameOrderedMembers(defBlock.getVars(), args);
110+
assert.sameOrderedMembers(
111+
defBlock.getVarModels().map((model) => model.getName()),
112+
args,
113+
);
108114
assertBlockVarModels(defBlock, varIds);
109115
}
110116

0 commit comments

Comments
 (0)