blockly-samples icon indicating copy to clipboard operation
blockly-samples copied to clipboard

0 not taken in account

Open ascazzola opened this issue 4 years ago • 3 comments

on https://blocklycodelabs.dev/codelabs/custom-generator/index.html?index=..%2F..index#6

The array block generator example does not take in account the value 0 for integers. The example code is: const values = []; for (var i = 0; i < block.itemCount_; i++) { let valueCode = codelabGenerator.valueToCode(block, 'ADD' + i, codelabGenerator.PRECEDENCE); if (valueCode) { values.push(valueCode); } }

and mentions that null values are not taken in account but 0 return false on the if and is not taken in account too.

I replaced it with values.push([undefined, null, ""].indexOf(valueCode) > -1 ? 'null' : valueCode);

But still not working to 0. I debugged a bit the code and the file generator.js on line 259 has the next code

if (!code) { return ''; }

I think here appear the same issue, but this is inside the library code, do you have any workaround to create my custom code generator fixing it, or am I doing / understanding something wrong?

ascazzola avatar Aug 20 '21 19:08 ascazzola

I changed the math_number generator parsing it to string and returning the string value and finally the example works, maybe it can be changed on codelabs

ascazzola avatar Aug 20 '21 19:08 ascazzola

Hello! So I think the reason this normally works is because the scrub_ function in this section ends up converting the 0 into a string, which will fix your problem. However, I think you are correct that the math block should return a string since the scrub_ method says it should take in a string. I think to fix this we should update this codelab and also update our example block in core.

alschmiedt avatar Aug 21 '21 00:08 alschmiedt

Yes, I agree completely. sorry for sending the information so messy

ascazzola avatar Aug 21 '21 00:08 ascazzola