Skip to content

Commit 294ef74

Browse files
authored
refactor: Use IVariableModel instead of VariableModel. (#8400)
* refactor: Use IVariableModel methods instead of directly accessing properties. * refactor: replace references to VariableModel with IVariableModel.
1 parent 02e64be commit 294ef74

22 files changed

+248
-141
lines changed

blocks/loops.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ const CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN = {
269269
}
270270
const varField = this.getField('VAR') as FieldVariable;
271271
const variable = varField.getVariable()!;
272-
const varName = variable.name;
272+
const varName = variable.getName();
273273
if (!this.isCollapsed() && varName !== null) {
274274
const getVarBlockState = {
275275
type: 'variables_get',

blocks/procedures.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import {FieldTextInput} from '../core/field_textinput.js';
3232
import {Msg} from '../core/msg.js';
3333
import {MutatorIcon as Mutator} from '../core/icons/mutator_icon.js';
3434
import {Names} from '../core/names.js';
35-
import type {VariableModel} from '../core/variable_model.js';
35+
import type {
36+
IVariableModel,
37+
IVariableState,
38+
} from '../core/interfaces/i_variable_model.js';
3639
import type {Workspace} from '../core/workspace.js';
3740
import type {WorkspaceSvg} from '../core/workspace_svg.js';
3841
import {config} from '../core/config.js';
@@ -48,7 +51,7 @@ export const blocks: {[key: string]: BlockDefinition} = {};
4851
type ProcedureBlock = Block & ProcedureMixin;
4952
interface ProcedureMixin extends ProcedureMixinType {
5053
arguments_: string[];
51-
argumentVarModels_: VariableModel[];
54+
argumentVarModels_: IVariableModel<IVariableState>[];
5255
callType_: string;
5356
paramIds_: string[];
5457
hasStatements_: boolean;
@@ -128,7 +131,7 @@ const PROCEDURE_DEF_COMMON = {
128131
for (let i = 0; i < this.argumentVarModels_.length; i++) {
129132
const parameter = xmlUtils.createElement('arg');
130133
const argModel = this.argumentVarModels_[i];
131-
parameter.setAttribute('name', argModel.name);
134+
parameter.setAttribute('name', argModel.getName());
132135
parameter.setAttribute('varid', argModel.getId());
133136
if (opt_paramIds && this.paramIds_) {
134137
parameter.setAttribute('paramId', this.paramIds_[i]);
@@ -196,7 +199,7 @@ const PROCEDURE_DEF_COMMON = {
196199
state['params'].push({
197200
// We don't need to serialize the name, but just in case we decide
198201
// to separate params from variables.
199-
'name': this.argumentVarModels_[i].name,
202+
'name': this.argumentVarModels_[i].getName(),
200203
'id': this.argumentVarModels_[i].getId(),
201204
});
202205
}
@@ -224,7 +227,7 @@ const PROCEDURE_DEF_COMMON = {
224227
param['name'],
225228
'',
226229
);
227-
this.arguments_.push(variable.name);
230+
this.arguments_.push(variable.getName());
228231
this.argumentVarModels_.push(variable);
229232
}
230233
}
@@ -352,7 +355,9 @@ const PROCEDURE_DEF_COMMON = {
352355
*
353356
* @returns List of variable models.
354357
*/
355-
getVarModels: function (this: ProcedureBlock): VariableModel[] {
358+
getVarModels: function (
359+
this: ProcedureBlock,
360+
): IVariableModel<IVariableState>[] {
356361
return this.argumentVarModels_;
357362
},
358363
/**
@@ -370,23 +375,23 @@ const PROCEDURE_DEF_COMMON = {
370375
newId: string,
371376
) {
372377
const oldVariable = this.workspace.getVariableById(oldId)!;
373-
if (oldVariable.type !== '') {
378+
if (oldVariable.getType() !== '') {
374379
// Procedure arguments always have the empty type.
375380
return;
376381
}
377-
const oldName = oldVariable.name;
382+
const oldName = oldVariable.getName();
378383
const newVar = this.workspace.getVariableById(newId)!;
379384

380385
let change = false;
381386
for (let i = 0; i < this.argumentVarModels_.length; i++) {
382387
if (this.argumentVarModels_[i].getId() === oldId) {
383-
this.arguments_[i] = newVar.name;
388+
this.arguments_[i] = newVar.getName();
384389
this.argumentVarModels_[i] = newVar;
385390
change = true;
386391
}
387392
}
388393
if (change) {
389-
this.displayRenamedVar_(oldName, newVar.name);
394+
this.displayRenamedVar_(oldName, newVar.getName());
390395
Procedures.mutateCallers(this);
391396
}
392397
},
@@ -398,9 +403,9 @@ const PROCEDURE_DEF_COMMON = {
398403
*/
399404
updateVarName: function (
400405
this: ProcedureBlock & BlockSvg,
401-
variable: VariableModel,
406+
variable: IVariableModel<IVariableState>,
402407
) {
403-
const newName = variable.name;
408+
const newName = variable.getName();
404409
let change = false;
405410
let oldName;
406411
for (let i = 0; i < this.argumentVarModels_.length; i++) {
@@ -473,12 +478,16 @@ const PROCEDURE_DEF_COMMON = {
473478
const getVarBlockState = {
474479
type: 'variables_get',
475480
fields: {
476-
VAR: {name: argVar.name, id: argVar.getId(), type: argVar.type},
481+
VAR: {
482+
name: argVar.getName(),
483+
id: argVar.getId(),
484+
type: argVar.getType(),
485+
},
477486
},
478487
};
479488
options.push({
480489
enabled: true,
481-
text: Msg['VARIABLES_SET_CREATE_GET'].replace('%1', argVar.name),
490+
text: Msg['VARIABLES_SET_CREATE_GET'].replace('%1', argVar.getName()),
482491
callback: ContextMenu.callbackFactory(this, getVarBlockState),
483492
});
484493
}
@@ -623,7 +632,7 @@ type ArgumentMixinType = typeof PROCEDURES_MUTATORARGUMENT;
623632
// TODO(#6920): This is kludgy.
624633
type FieldTextInputForArgument = FieldTextInput & {
625634
oldShowEditorFn_(_e?: Event, quietInput?: boolean): void;
626-
createdVariables_: VariableModel[];
635+
createdVariables_: IVariableModel<IVariableState>[];
627636
};
628637

629638
const PROCEDURES_MUTATORARGUMENT = {
@@ -708,7 +717,7 @@ const PROCEDURES_MUTATORARGUMENT = {
708717
}
709718

710719
let model = outerWs.getVariable(varName, '');
711-
if (model && model.name !== varName) {
720+
if (model && model.getName() !== varName) {
712721
// Rename the variable (case change)
713722
outerWs.renameVariableById(model.getId(), varName);
714723
}
@@ -739,7 +748,7 @@ const PROCEDURES_MUTATORARGUMENT = {
739748
}
740749
for (let i = 0; i < this.createdVariables_.length; i++) {
741750
const model = this.createdVariables_[i];
742-
if (model.name !== newText) {
751+
if (model.getName() !== newText) {
743752
outerWs.deleteVariableById(model.getId());
744753
}
745754
}
@@ -750,7 +759,7 @@ blocks['procedures_mutatorarg'] = PROCEDURES_MUTATORARGUMENT;
750759
/** Type of a block using the PROCEDURE_CALL_COMMON mixin. */
751760
type CallBlock = Block & CallMixin;
752761
interface CallMixin extends CallMixinType {
753-
argumentVarModels_: VariableModel[];
762+
argumentVarModels_: IVariableModel<IVariableState>[];
754763
arguments_: string[];
755764
defType_: string;
756765
quarkIds_: string[] | null;
@@ -1029,7 +1038,7 @@ const PROCEDURE_CALL_COMMON = {
10291038
*
10301039
* @returns List of variable models.
10311040
*/
1032-
getVarModels: function (this: CallBlock): VariableModel[] {
1041+
getVarModels: function (this: CallBlock): IVariableModel<IVariableState>[] {
10331042
return this.argumentVarModels_;
10341043
},
10351044
/**

blocks/variables_dynamic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ const CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
144144
const id = this.getFieldValue('VAR');
145145
const variableModel = Variables.getVariable(this.workspace, id)!;
146146
if (this.type === 'variables_get_dynamic') {
147-
this.outputConnection!.setCheck(variableModel.type);
147+
this.outputConnection!.setCheck(variableModel.getType());
148148
} else {
149-
this.getInput('VALUE')!.connection!.setCheck(variableModel.type);
149+
this.getInput('VALUE')!.connection!.setCheck(variableModel.getType());
150150
}
151151
},
152152
};

core/block.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ import * as idGenerator from './utils/idgenerator.js';
4545
import * as parsing from './utils/parsing.js';
4646
import * as registry from './registry.js';
4747
import {Size} from './utils/size.js';
48-
import type {VariableModel} from './variable_model.js';
48+
import type {
49+
IVariableModel,
50+
IVariableState,
51+
} from './interfaces/i_variable_model.js';
4952
import type {Workspace} from './workspace.js';
5053
import {DummyInput} from './inputs/dummy_input.js';
5154
import {EndRowInput} from './inputs/end_row_input.js';
@@ -1133,7 +1136,7 @@ export class Block implements IASTNodeLocation {
11331136
* @returns List of variable models.
11341137
* @internal
11351138
*/
1136-
getVarModels(): VariableModel[] {
1139+
getVarModels(): IVariableModel<IVariableState>[] {
11371140
const vars = [];
11381141
for (let i = 0, input; (input = this.inputList[i]); i++) {
11391142
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
@@ -1159,7 +1162,7 @@ export class Block implements IASTNodeLocation {
11591162
* @param variable The variable being renamed.
11601163
* @internal
11611164
*/
1162-
updateVarName(variable: VariableModel) {
1165+
updateVarName(variable: IVariableModel<IVariableState>) {
11631166
for (let i = 0, input; (input = this.inputList[i]); i++) {
11641167
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
11651168
if (

core/events/events_var_base.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
*/
1212
// Former goog.module ID: Blockly.Events.VarBase
1313

14-
import type {VariableModel} from '../variable_model.js';
14+
import type {
15+
IVariableModel,
16+
IVariableState,
17+
} from '../interfaces/i_variable_model.js';
1518

1619
import {
1720
Abstract as AbstractEvent,
@@ -31,13 +34,13 @@ export class VarBase extends AbstractEvent {
3134
* @param opt_variable The variable this event corresponds to. Undefined for
3235
* a blank event.
3336
*/
34-
constructor(opt_variable?: VariableModel) {
37+
constructor(opt_variable?: IVariableModel<IVariableState>) {
3538
super();
3639
this.isBlank = typeof opt_variable === 'undefined';
3740
if (!opt_variable) return;
3841

3942
this.varId = opt_variable.getId();
40-
this.workspaceId = opt_variable.workspace.id;
43+
this.workspaceId = opt_variable.getWorkspace().id;
4144
}
4245

4346
/**

core/events/events_var_create.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
// Former goog.module ID: Blockly.Events.VarCreate
1313

1414
import * as registry from '../registry.js';
15-
import type {VariableModel} from '../variable_model.js';
15+
import type {
16+
IVariableModel,
17+
IVariableState,
18+
} from '../interfaces/i_variable_model.js';
1619

1720
import {VarBase, VarBaseJson} from './events_var_base.js';
1821
import * as eventUtils from './utils.js';
@@ -33,14 +36,14 @@ export class VarCreate extends VarBase {
3336
/**
3437
* @param opt_variable The created variable. Undefined for a blank event.
3538
*/
36-
constructor(opt_variable?: VariableModel) {
39+
constructor(opt_variable?: IVariableModel<IVariableState>) {
3740
super(opt_variable);
3841

3942
if (!opt_variable) {
4043
return; // Blank event to be populated by fromJson.
4144
}
42-
this.varType = opt_variable.type;
43-
this.varName = opt_variable.name;
45+
this.varType = opt_variable.getType();
46+
this.varName = opt_variable.getName();
4447
}
4548

4649
/**

core/events/events_var_delete.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
// Former goog.module ID: Blockly.Events.VarDelete
88

99
import * as registry from '../registry.js';
10-
import type {VariableModel} from '../variable_model.js';
10+
import type {
11+
IVariableModel,
12+
IVariableState,
13+
} from '../interfaces/i_variable_model.js';
1114

1215
import {VarBase, VarBaseJson} from './events_var_base.js';
1316
import * as eventUtils from './utils.js';
@@ -28,14 +31,14 @@ export class VarDelete extends VarBase {
2831
/**
2932
* @param opt_variable The deleted variable. Undefined for a blank event.
3033
*/
31-
constructor(opt_variable?: VariableModel) {
34+
constructor(opt_variable?: IVariableModel<IVariableState>) {
3235
super(opt_variable);
3336

3437
if (!opt_variable) {
3538
return; // Blank event to be populated by fromJson.
3639
}
37-
this.varType = opt_variable.type;
38-
this.varName = opt_variable.name;
40+
this.varType = opt_variable.getType();
41+
this.varName = opt_variable.getName();
3942
}
4043

4144
/**

core/events/events_var_rename.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
// Former goog.module ID: Blockly.Events.VarRename
88

99
import * as registry from '../registry.js';
10-
import type {VariableModel} from '../variable_model.js';
10+
import type {
11+
IVariableModel,
12+
IVariableState,
13+
} from '../interfaces/i_variable_model.js';
1114

1215
import {VarBase, VarBaseJson} from './events_var_base.js';
1316
import * as eventUtils from './utils.js';
@@ -31,13 +34,13 @@ export class VarRename extends VarBase {
3134
* @param opt_variable The renamed variable. Undefined for a blank event.
3235
* @param newName The new name the variable will be changed to.
3336
*/
34-
constructor(opt_variable?: VariableModel, newName?: string) {
37+
constructor(opt_variable?: IVariableModel<IVariableState>, newName?: string) {
3538
super(opt_variable);
3639

3740
if (!opt_variable) {
3841
return; // Blank event to be populated by fromJson.
3942
}
40-
this.oldName = opt_variable.name;
43+
this.oldName = opt_variable.getName();
4144
this.newName = typeof newName === 'undefined' ? '' : newName;
4245
}
4346

0 commit comments

Comments
 (0)