fix(demos): Update BlockFactory generator stub generator (#7211)

Update the BlockFactory block generator function stub generator
to apply recent changes in generator function best practices:

- Use languageGenarator instead of Blockly.Language.
- Put generator functions in .forBlock dictionary.
- Accept (and use) a second argument that is the calling
  CodeGenerator object.
- User Order.ATOMIC enum instead of ORDER_ATOMIC.

Also:

- Prefix (e.g.) javascriptGenerator and Order with "javascript.".
- Use template literals where useful.
- DRY up all the non-special field stub code generation.
This commit is contained in:
Christopher Allen
2023-06-27 14:36:15 +01:00
committed by GitHub
parent b189b1989c
commit 5579098e35

View File

@@ -80,10 +80,10 @@ FactoryUtils.getGeneratorStub = function(block, generatorLanguage) {
return ' var ' + root + '_' + name; return ' var ' + root + '_' + name;
} }
// The makevar function lives in the original update generator. // The makevar function lives in the original update generator.
var language = generatorLanguage; var language = generatorLanguage.toLowerCase();
var code = []; var code = [];
code.push("Blockly." + language + "['" + block.type + code.push(`${language}.${language}Generator.forBlock['${block.type}'] = ` +
"'] = function(block) {"); 'function(block, generator) {');
// Generate getters for any fields or inputs. // Generate getters for any fields or inputs.
for (var i = 0, input; input = block.inputList[i]; i++) { for (var i = 0, input; input = block.inputList[i]; i++) {
@@ -93,42 +93,34 @@ FactoryUtils.getGeneratorStub = function(block, generatorLanguage) {
continue; continue;
} }
if (field instanceof Blockly.FieldVariable) { if (field instanceof Blockly.FieldVariable) {
// Subclass of Blockly.FieldDropdown, must test first. // FieldVariable is subclass of FieldDropdown; must test first.
code.push(makeVar('variable', name) + code.push(`${makeVar('variable', name)} = ` +
" = Blockly." + language + `generator.nameDB_.getName(block.getFieldValue('${name}'), ` +
".nameDB_.getName(block.getFieldValue('" + name + `Blockly.Variables.NAME_TYPE);`);
"'), Blockly.Variables.NAME_TYPE);");
} else if (field instanceof Blockly.FieldAngle) {
// Subclass of Blockly.FieldTextInput, must test first.
code.push(makeVar('angle', name) +
" = block.getFieldValue('" + name + "');");
} else if (field instanceof Blockly.FieldColour) {
code.push(makeVar('colour', name) +
" = block.getFieldValue('" + name + "');");
} else if (field instanceof Blockly.FieldCheckbox) { } else if (field instanceof Blockly.FieldCheckbox) {
code.push(makeVar('checkbox', name) + code.push(`${makeVar('checkbox', name)} = ` +
" = block.getFieldValue('" + name + "') === 'TRUE';"); `block.getFieldValue('${name}') === 'TRUE';`);
} else if (field instanceof Blockly.FieldDropdown) { } else {
code.push(makeVar('dropdown', name) + let prefix =
" = block.getFieldValue('" + name + "');"); // Angle is subclass of FieldTextInput; must test first.
} else if (field instanceof Blockly.FieldNumber) { field instanceof Blockly.FieldAngle ? 'angle' :
code.push(makeVar('number', name) + field instanceof Blockly.FieldColour ? 'colour' :
" = block.getFieldValue('" + name + "');"); field instanceof Blockly.FieldDropdown ? 'dropdown' :
} else if (field instanceof Blockly.FieldTextInput) { field instanceof Blockly.FieldNumber ? 'number' :
code.push(makeVar('text', name) + field instanceof Blockly.FieldTextInput ? 'text' :
" = block.getFieldValue('" + name + "');"); 'field'; // Default if subclass not found.
code.push(`${makeVar(prefix, name)} = block.getFieldValue('${name}');`);
} }
} }
var name = input.name; var name = input.name;
if (name) { if (name) {
if (input.type === Blockly.INPUT_VALUE) { if (input.type === Blockly.INPUT_VALUE) {
code.push(makeVar('value', name) + code.push(`${makeVar('value', name)} = ` +
" = Blockly." + language + ".valueToCode(block, '" + name + `generator.valueToCode(block, '${name}', ` +
"', Blockly." + language + ".ORDER_ATOMIC);"); `${language}.Order.ATOMIC);`);
} else if (input.type === Blockly.NEXT_STATEMENT) { } else if (input.type === Blockly.NEXT_STATEMENT) {
code.push(makeVar('statements', name) + code.push(`${makeVar('statements', name)} = ` +
" = Blockly." + language + ".statementToCode(block, '" + `generator.statementToCode(block, '${name}');`);
name + "');");
} }
} }
} }