Skip to content

Commit 0b9570e

Browse files
Merge pull request #76 from MichalPawlicki/fix-missing-variable-field-error
Fix error when checking for cloud variables
2 parents d105fdd + 115bfe9 commit 0b9570e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

lib/sb3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const blocks = function (targets) {
134134
// Get opcode and check variable manipulation for the presence of
135135
// cloud variables
136136
if (opcode === 'data_setvariableto' || opcode === 'data_changevariableby') {
137-
if (isArgCloudVar(block.fields.VARIABLE[1])) {
137+
if (isArgCloudVar(block?.fields?.VARIABLE?.[1])) {
138138
opcode += '_cloud';
139139
}
140140
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"targets": [
3+
{
4+
"isStage": true,
5+
"name": "Stage",
6+
"variables": {},
7+
"blocks": {
8+
"a": {
9+
"opcode": "data_setvariableto",
10+
"fields": {}
11+
}
12+
},
13+
"costumes": [
14+
{
15+
"assetId": "cd21514d0531fdffb22204e0ec5ed84a",
16+
"name": "backdrop1",
17+
"md5ext": "cd21514d0531fdffb22204e0ec5ed84a.svg",
18+
"dataFormat": "svg",
19+
"rotationCenterX": 240,
20+
"rotationCenterY": 180
21+
}
22+
],
23+
"sounds": []
24+
}
25+
],
26+
"monitors": [],
27+
"extensions": [],
28+
"meta": {
29+
"semver": "3.0.0"
30+
}
31+
}

test/unit/sb3.js

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const primitiveVariableAndListBlocks = fs.readFileSync(
2525
path.resolve(__dirname, '../fixtures/sb3/primitiveVariableAndListBlocks.json')
2626
);
2727

28+
const missingVariableField = fs.readFileSync(
29+
path.resolve(__dirname, '../fixtures/sb3/missingVariableField.json')
30+
);
31+
2832
test('default (object)', t => {
2933
analysis(defaultObject, (err, result) => {
3034
t.ok(typeof err === 'undefined' || err === null);
@@ -472,3 +476,12 @@ test('correctly handling primitve reporter blocks: list and variable', t => {
472476
t.end();
473477
});
474478
});
479+
480+
test('missing VARIABLE field in a block does not break the library', t => {
481+
analysis(missingVariableField, (err, result) => {
482+
t.ok(typeof err === 'undefined' || err === null);
483+
t.type(result, 'object');
484+
485+
t.end();
486+
});
487+
});

0 commit comments

Comments
 (0)