Build a custom generator
9. Generating a stack
The scrub_ function
The scrub_ function is called on every block from blockToCode. It takes in three arguments:
blockis the current block.codeis the code generated for this block, which includes code from all attached value blocks.opt_thisOnlyis an optionalboolean. If true, code should be generated for this block but no subsequent blocks.
By default, scrub_ simply returns the passed-in code. A common pattern is to override the function to also generate code for any blocks that follow the current block in a stack. In this case, the code will add commas and newlines between object members:
jsonGenerator.scrub_ = function (block, code, thisOnly) {
const nextBlock = block.nextConnection && block.nextConnection.targetBlock();
if (nextBlock && !thisOnly) {
return code + ',\n' + jsonGenerator.blockToCode(nextBlock);
}
return code;
};
Testing scrub_
Create a stack of member blocks on the workspace. There should be generated code for all of the blocks, not just the first one.
Next, add an object block and drag the member blocks into it. This case tests statementToCode, and should generate code for all of of the blocks.