Allow variables of different types to share the same name.

This commit is contained in:
Rachel Fenichel
2017-11-29 14:37:34 -08:00
parent 0cb5dfc437
commit c825e60813
10 changed files with 214 additions and 111 deletions

View File

@@ -62,15 +62,15 @@ Blockly.VariableMap.prototype.clear = function() {
* Rename the given variable by updating its name in the variable map.
* @param {Blockly.VariableModel} variable Variable to rename.
* @param {string} newName New variable name.
* @param {string=} opt_type The type of the variable to create if variable was
* null.
*/
Blockly.VariableMap.prototype.renameVariable = function(variable, newName) {
var newVariable = this.getVariable(newName);
Blockly.VariableMap.prototype.renameVariable = function(variable, newName,
opt_type) {
var type = variable ? variable.type : (opt_type || '');
var newVariable = this.getVariable(newName, type);
var variableIndex = -1;
var newVariableIndex = -1;
var type = '';
if (variable || newVariable) {
type = (variable || newVariable).type;
}
var variableList = this.getVariablesOfType(type);
if (variable) {
@@ -116,19 +116,14 @@ Blockly.VariableMap.prototype.renameVariable = function(variable, newName) {
*/
Blockly.VariableMap.prototype.createVariable = function(name,
opt_type, opt_id) {
var variable = this.getVariable(name);
var variable = this.getVariable(name, opt_type);
if (variable) {
if (opt_type && variable.type != opt_type) {
throw Error('Variable "' + name + '" is already in use and its type is "'
+ variable.type + '" which conflicts with the passed in ' +
'type, "' + opt_type + '".');
}
if (opt_id && variable.getId() != opt_id) {
throw Error('Variable "' + name + '" is already in use and its id is "' +
variable.getId() + '" which conflicts with the passed in ' +
'id, "' + opt_id + '".');
}
// The variable already exists and has the same ID and type.
// The variable already exists and has the same ID.
return variable;
}
if (opt_id && this.getVariableById(opt_id)) {
@@ -164,17 +159,19 @@ Blockly.VariableMap.prototype.deleteVariable = function(variable) {
};
/**
* Find the variable by the given name and return it. Return null if it is not
* found.
* Find the variable by the given name and type and return it. Return null if
* it is not found.
* @param {string} name The name to check for.
* @param {string=} opt_type The type of the variable. If not provided it
* defaults to the empty string, which is a specific type.
* @return {Blockly.VariableModel} The variable with the given name, or null if
* it was not found.
*/
Blockly.VariableMap.prototype.getVariable = function(name) {
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++) {
Blockly.VariableMap.prototype.getVariable = function(name, opt_type) {
var type = opt_type || '';
var list = this.variableMap_[type];
if (list) {
for (var j = 0, variable; variable = list[j]; j++) {
if (Blockly.Names.equals(variable.name, name)) {
return variable;
}