MCPcopy
hub / github.com/RaspberryPiFoundation/blockly / blockToCode

Method blockToCode

packages/blockly/core/generator.ts:234–288  ·  view source on GitHub ↗

* Generate code for the specified block (and attached blocks). * The generator must be initialized before calling this function. * * @param block The block to generate code for. * @param opt_thisOnly True to generate code for only this statement. * @returns For statement blocks, the g

(
    block: Block | null,
    opt_thisOnly?: boolean,
  )

Source from the content-addressed store, hash-verified

232 * operator order value. Returns '' if block is null.
233 */
234 blockToCode(
235 block: Block | null,
236 opt_thisOnly?: boolean,
237 ): string | [string, number] {
238 if (this.isInitialized === false) {
239 console.warn(
240 'CodeGenerator init was not called before blockToCode was called.',
241 );
242 }
243 if (!block) {
244 return '';
245 }
246 if (!block.isEnabled()) {
247 // Skip past this block if it is disabled.
248 return opt_thisOnly ? '' : this.blockToCode(block.getNextBlock());
249 }
250 if (block.isInsertionMarker()) {
251 // Skip past insertion markers.
252 return opt_thisOnly ? '' : this.blockToCode(block.getChildren(false)[0]);
253 }
254
255 // Look up block generator function in dictionary.
256 const func = this.forBlock[block.type];
257 if (typeof func !== 'function') {
258 throw Error(
259 `${this.name_} generator does not know how to generate code ` +
260 `for block type "${block.type}".`,
261 );
262 }
263 // First argument to func.call is the value of 'this' in the generator.
264 // Prior to 24 September 2013 'this' was the only way to access the block.
265 // The current preferred method of accessing the block is through the second
266 // argument to func.call, which becomes the first parameter to the
267 // generator.
268 let code = func.call(block, block, this);
269 if (Array.isArray(code)) {
270 // Value blocks return tuples of code and operator order.
271 if (!block.outputConnection) {
272 throw TypeError('Expecting string from statement block: ' + block.type);
273 }
274 return [this.scrub_(block, code[0], opt_thisOnly), code[1]];
275 } else if (typeof code === 'string') {
276 if (this.STATEMENT_PREFIX && !block.suppressPrefixSuffix) {
277 code = this.injectId(this.STATEMENT_PREFIX, block) + code;
278 }
279 if (this.STATEMENT_SUFFIX && !block.suppressPrefixSuffix) {
280 code = code + this.injectId(this.STATEMENT_SUFFIX, block);
281 }
282 return this.scrub_(block, code, opt_thisOnly);
283 } else if (code === null) {
284 // Block has handled code generation itself.
285 return '';
286 }
287 throw SyntaxError('Invalid code generated: ' + code);
288 }
289
290 /**
291 * Generate code representing the specified value input.

Callers 11

workspaceToCodeMethod · 0.95
valueToCodeMethod · 0.95
statementToCodeMethod · 0.95
generator_test.jsFile · 0.80
scrub_Method · 0.80
scrub_Method · 0.80
scrub_Method · 0.80
scrub_Method · 0.80
scrub_Method · 0.80
json.jsFile · 0.80

Calls 6

scrub_Method · 0.95
injectIdMethod · 0.95
isInsertionMarkerMethod · 0.80
isEnabledMethod · 0.45
getNextBlockMethod · 0.45
getChildrenMethod · 0.45

Tested by

no test coverage detected