Migrate core/variable_map.js to ES6 const/let

This commit is contained in:
Aaron Dodson
2021-07-29 12:30:44 -07:00
parent c9a0333bfe
commit 1d9c9359f9

View File

@@ -66,9 +66,9 @@ Blockly.VariableMap.prototype.clear = function() {
* @package
*/
Blockly.VariableMap.prototype.renameVariable = function(variable, newName) {
var type = variable.type;
var conflictVar = this.getVariable(newName, type);
var blocks = this.workspace.getAllBlocks(false);
const type = variable.type;
const conflictVar = this.getVariable(newName, type);
const blocks = this.workspace.getAllBlocks(false);
Blockly.Events.setGroup(true);
try {
// The IDs may match if the rename is a simple case change (name1 -> Name1).
@@ -89,7 +89,7 @@ Blockly.VariableMap.prototype.renameVariable = function(variable, newName) {
* @param {string} newName New variable name.
*/
Blockly.VariableMap.prototype.renameVariableById = function(id, newName) {
var variable = this.getVariableById(id);
const variable = this.getVariableById(id);
if (!variable) {
throw Error('Tried to rename a variable that didn\'t exist. ID: ' + id);
}
@@ -111,7 +111,7 @@ Blockly.VariableMap.prototype.renameVariableAndUses_ = function(variable,
Blockly.Events.fire(new (Blockly.Events.get(Blockly.Events.VAR_RENAME))(
variable, newName));
variable.name = newName;
for (var i = 0; i < blocks.length; i++) {
for (let i = 0; i < blocks.length; i++) {
blocks[i].updateVarName(variable);
}
};
@@ -131,8 +131,8 @@ Blockly.VariableMap.prototype.renameVariableAndUses_ = function(variable,
*/
Blockly.VariableMap.prototype.renameVariableWithConflict_ = function(variable,
newName, conflictVar, blocks) {
var type = variable.type;
var oldCase = conflictVar.name;
const type = variable.type;
const oldCase = conflictVar.name;
if (newName != oldCase) {
// Simple rename to change the case and update references.
@@ -141,7 +141,7 @@ Blockly.VariableMap.prototype.renameVariableWithConflict_ = function(variable,
// These blocks now refer to a different variable.
// These will fire change events.
for (var i = 0; i < blocks.length; i++) {
for (let i = 0; i < blocks.length; i++) {
blocks[i].renameVarById(variable.getId(), conflictVar.getId());
}
@@ -149,8 +149,8 @@ Blockly.VariableMap.prototype.renameVariableWithConflict_ = function(variable,
Blockly.Events.fire(new (Blockly.Events.get(Blockly.Events.VAR_DELETE))(
variable));
// And remove it from the list.
var variableList = this.getVariablesOfType(type);
var variableIndex = variableList.indexOf(variable);
const variableList = this.getVariablesOfType(type);
const variableIndex = variableList.indexOf(variable);
this.variableMap_[type].splice(variableIndex, 1);
};
@@ -170,7 +170,7 @@ Blockly.VariableMap.prototype.renameVariableWithConflict_ = function(variable,
*/
Blockly.VariableMap.prototype.createVariable = function(name,
opt_type, opt_id) {
var variable = this.getVariable(name, opt_type);
let variable = this.getVariable(name, opt_type);
if (variable) {
if (opt_id && variable.getId() != opt_id) {
throw Error('Variable "' + name + '" is already in use and its id is "' +
@@ -183,11 +183,11 @@ Blockly.VariableMap.prototype.createVariable = function(name,
if (opt_id && this.getVariableById(opt_id)) {
throw Error('Variable id, "' + opt_id + '", is already in use.');
}
var id = opt_id || Blockly.utils.genUid();
var type = opt_type || '';
const id = opt_id || Blockly.utils.genUid();
const type = opt_type || '';
variable = new Blockly.VariableModel(this.workspace, name, type, id);
var variables = this.variableMap_[type] || [];
const variables = this.variableMap_[type] || [];
variables.push(variable);
// Delete the list of variables of this type, and re-add it so that
// the most recent addition is at the end.
@@ -205,8 +205,8 @@ Blockly.VariableMap.prototype.createVariable = function(name,
* @param {!Blockly.VariableModel} variable Variable to delete.
*/
Blockly.VariableMap.prototype.deleteVariable = function(variable) {
var variableList = this.variableMap_[variable.type];
for (var i = 0, tempVar; (tempVar = variableList[i]); i++) {
const variableList = this.variableMap_[variable.type];
for (let i = 0, tempVar; (tempVar = variableList[i]); i++) {
if (tempVar.getId() == variable.getId()) {
variableList.splice(i, 1);
Blockly.Events.fire(new (Blockly.Events.get(Blockly.Events.VAR_DELETE))(
@@ -222,16 +222,16 @@ Blockly.VariableMap.prototype.deleteVariable = function(variable) {
* @param {string} id ID of variable to delete.
*/
Blockly.VariableMap.prototype.deleteVariableById = function(id) {
var variable = this.getVariableById(id);
const variable = this.getVariableById(id);
if (variable) {
// Check whether this variable is a function parameter before deleting.
var variableName = variable.name;
var uses = this.getVariableUsesById(id);
for (var i = 0, block; (block = uses[i]); i++) {
const variableName = variable.name;
const uses = this.getVariableUsesById(id);
for (let i = 0, block; (block = uses[i]); i++) {
if (block.type == 'procedures_defnoreturn' ||
block.type == 'procedures_defreturn') {
var procedureName = block.getFieldValue('NAME');
var deleteText = Blockly.Msg['CANNOT_DELETE_VARIABLE_PROCEDURE'].
const procedureName = block.getFieldValue('NAME');
const deleteText = Blockly.Msg['CANNOT_DELETE_VARIABLE_PROCEDURE'].
replace('%1', variableName).
replace('%2', procedureName);
Blockly.alert(deleteText);
@@ -239,10 +239,10 @@ Blockly.VariableMap.prototype.deleteVariableById = function(id) {
}
}
var map = this;
const map = this;
if (uses.length > 1) {
// Confirm before deleting multiple blocks.
var confirmText = Blockly.Msg['DELETE_VARIABLE_CONFIRMATION'].
const confirmText = Blockly.Msg['DELETE_VARIABLE_CONFIRMATION'].
replace('%1', String(uses.length)).
replace('%2', variableName);
Blockly.confirm(confirmText,
@@ -269,12 +269,12 @@ Blockly.VariableMap.prototype.deleteVariableById = function(id) {
*/
Blockly.VariableMap.prototype.deleteVariableInternal = function(variable,
uses) {
var existingGroup = Blockly.Events.getGroup();
const existingGroup = Blockly.Events.getGroup();
if (!existingGroup) {
Blockly.Events.setGroup(true);
}
try {
for (var i = 0; i < uses.length; i++) {
for (let i = 0; i < uses.length; i++) {
uses[i].dispose(true);
}
this.deleteVariable(variable);
@@ -297,10 +297,10 @@ Blockly.VariableMap.prototype.deleteVariableInternal = function(variable,
* it was not found.
*/
Blockly.VariableMap.prototype.getVariable = function(name, opt_type) {
var type = opt_type || '';
var list = this.variableMap_[type];
const type = opt_type || '';
const list = this.variableMap_[type];
if (list) {
for (var j = 0, variable; (variable = list[j]); j++) {
for (let j = 0, variable; (variable = list[j]); j++) {
if (Blockly.Names.equals(variable.name, name)) {
return variable;
}
@@ -315,10 +315,10 @@ Blockly.VariableMap.prototype.getVariable = function(name, opt_type) {
* @return {?Blockly.VariableModel} The variable with the given ID.
*/
Blockly.VariableMap.prototype.getVariableById = function(id) {
var keys = Object.keys(this.variableMap_);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
for (var j = 0, variable; (variable = this.variableMap_[key][j]); j++) {
const keys = Object.keys(this.variableMap_);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
for (let j = 0, variable; (variable = this.variableMap_[key][j]); j++) {
if (variable.getId() == id) {
return variable;
}
@@ -336,7 +336,7 @@ Blockly.VariableMap.prototype.getVariableById = function(id) {
*/
Blockly.VariableMap.prototype.getVariablesOfType = function(type) {
type = type || '';
var variable_list = this.variableMap_[type];
const variable_list = this.variableMap_[type];
if (variable_list) {
return variable_list.slice();
}
@@ -353,15 +353,15 @@ Blockly.VariableMap.prototype.getVariablesOfType = function(type) {
* @package
*/
Blockly.VariableMap.prototype.getVariableTypes = function(ws) {
var variableMap = {};
const variableMap = {};
Blockly.utils.object.mixin(variableMap, this.variableMap_);
if (ws && ws.getPotentialVariableMap()) {
Blockly.utils.object.mixin(variableMap,
ws.getPotentialVariableMap().variableMap_);
}
var types = Object.keys(variableMap);
var hasEmpty = false;
for (var i = 0; i < types.length; i++) {
const types = Object.keys(variableMap);
let hasEmpty = false;
for (let i = 0; i < types.length; i++) {
if (types[i] == '') {
hasEmpty = true;
}
@@ -377,8 +377,8 @@ Blockly.VariableMap.prototype.getVariableTypes = function(ws) {
* @return {!Array<!Blockly.VariableModel>} List of variable models.
*/
Blockly.VariableMap.prototype.getAllVariables = function() {
var all_variables = [];
for (var key in this.variableMap_) {
let all_variables = [];
for (const key in this.variableMap_) {
all_variables = all_variables.concat(this.variableMap_[key]);
}
return all_variables;
@@ -389,10 +389,10 @@ Blockly.VariableMap.prototype.getAllVariables = function() {
* @return {!Array<string>} All of the variable names of all types.
*/
Blockly.VariableMap.prototype.getAllVariableNames = function() {
var allNames = [];
for (var key in this.variableMap_) {
var variables = this.variableMap_[key];
for (var i = 0, variable; (variable = variables[i]); i++) {
const allNames = [];
for (const key in this.variableMap_) {
const variables = this.variableMap_[key];
for (let i = 0, variable; (variable = variables[i]); i++) {
allNames.push(variable.name);
}
}
@@ -405,13 +405,13 @@ Blockly.VariableMap.prototype.getAllVariableNames = function() {
* @return {!Array<!Blockly.Block>} Array of block usages.
*/
Blockly.VariableMap.prototype.getVariableUsesById = function(id) {
var uses = [];
var blocks = this.workspace.getAllBlocks(false);
const uses = [];
const blocks = this.workspace.getAllBlocks(false);
// Iterate through every block and check the name.
for (var i = 0; i < blocks.length; i++) {
var blockVariables = blocks[i].getVarModels();
for (let i = 0; i < blocks.length; i++) {
const blockVariables = blocks[i].getVarModels();
if (blockVariables) {
for (var j = 0; j < blockVariables.length; j++) {
for (let j = 0; j < blockVariables.length; j++) {
if (blockVariables[j].getId() == id) {
uses.push(blocks[i]);
}