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);
}
}

BIN
test/fixtures/sb2/cloud_opcodes.sb2 vendored Normal file

Binary file not shown.

BIN
test/fixtures/sb3/cloud_opcodes.sb3 vendored Normal file

Binary file not shown.

View file

@ -0,0 +1,59 @@
const fs = require('fs');
const path = require('path');
const test = require('tap').test;
const analysis = require('../../lib/index');
const sb2 = fs.readFileSync(
path.resolve(__dirname, '../fixtures/sb2/cloud_opcodes.sb2')
);
const sb3 = fs.readFileSync(
path.resolve(__dirname, '../fixtures/sb3/cloud_opcodes.sb3')
);
test('sb2', t => {
analysis(sb2, (err, result) => {
t.true(typeof err === 'undefined' || err === null);
t.type(result, 'object');
t.type(result.blocks, 'object');
t.type(result.blocks.id, 'object');
t.deepEquals(result.blocks.id, [
'whenGreenFlag',
'doForever',
'setVar:to:',
'randomFrom:to:',
'changeVar:by:',
'setVar:to:',
'randomFrom:to:',
'changeVar:by:',
'setVar:to:cloud:',
'randomFrom:to:',
'changeVar:by:cloud:',
'wait:elapsed:from:'
]);
t.end();
});
});
test('sb3', t => {
analysis(sb3, (err, result) => {
t.true(typeof err === 'undefined' || err === null);
t.type(result, 'object');
t.type(result.blocks, 'object');
t.type(result.blocks.id, 'object');
t.deepEquals(result.blocks.id, [
'event_whenflagclicked',
'control_forever',
'control_wait',
'data_setvariableto',
'data_setvariableto',
'data_setvariableto_cloud',
'operator_random',
'operator_random',
'operator_random',
'data_changevariableby',
'data_changevariableby',
'data_changevariableby_cloud'
]);
t.end();
});
});