List variable manipulation on cloud vars differently in block listing

This commit is contained in:
Andrew Sliwinski 2019-03-13 15:57:04 -04:00
parent 2967e71261
commit 7dd3399d52
5 changed files with 116 additions and 3 deletions

View file

@ -92,6 +92,24 @@ const blocks = function (project) {
// Storage objects
const result = [];
/**
* Determine if a argument is the name of a known cloud variable.
* @param {string} arg Argument (variable name)
* @return {boolean} Is cloud variable?
*/
const isArgCloudVar = function (arg) {
// Validate argument
// @note "Hacked" inputs here could be objects (arrays)
if (typeof arg !== 'string') return false;
// Iterate over global variables and check to see if arg matches
for (let i in project.variables) {
const variable = project.variables[i];
if (variable.name === arg && variable.isPersistent) return true;
}
return false;
};
/**
* Walk scripts array(s) and build block list.
* @param {array} stack Stack of blocks
@ -108,11 +126,20 @@ const blocks = function (project) {
continue;
}
// Get opcode and check variable manipulation for the presence of
// cloud variables
let opcode = stack[i][0];
if (opcode === 'setVar:to:' || opcode === 'changeVar:by:') {
if (isArgCloudVar(stack[i][1])) {
opcode += 'cloud:';
}
}
// Add to block list
result.push(stack[i][0]);
result.push(opcode);
// Don't pull in params from procedures
if (stack[i][0] === 'procDef') continue;
if (opcode === 'procDef') continue;
// Move to next item and walk
walk(stack[i].slice(1));

View file

@ -72,10 +72,37 @@ const blocks = function (targets) {
// Storage object
let result = [];
/**
* Determine if a argument is the name of a known cloud variable.
* @param {string} arg Argument (variable name)
* @return {boolean} Is cloud variable?
*/
const isArgCloudVar = function (arg) {
// Validate argument
if (typeof arg !== 'string') return false;
// Check first target (stage) to determine if arg is a cloud variable id
const stage = targets[0];
if (typeof stage.variables[arg] !== 'undefined') {
return stage.variables[arg].length === 3 && stage.variables[arg][2];
}
};
// Iterate over all targets and push block opcodes to storage object
for (let t in targets) {
for (let a in targets[t].blocks) {
const block = targets[t].blocks[a];
if (!block.shadow) result.push(block.opcode);
// Get opcode and check variable manipulation for the presence of
// cloud variables
let opcode = block.opcode;
if (opcode === 'data_setvariableto' || opcode === 'data_changevariableby') {
if (isArgCloudVar(block.fields.VARIABLE[1])) {
opcode += '_cloud';
}
}
if (!block.shadow) result.push(opcode);
}
}