chore: replace var with const and let in python block generators (#5650)

* chore: replace var with const and let in python block generators

* chore: update test deps

* chore: update bracket usage in switch statements
This commit is contained in:
Rachel Fenichel
2021-10-29 11:02:58 -07:00
committed by GitHub
parent f0a874ca27
commit db78d9f280
9 changed files with 342 additions and 312 deletions

View File

@@ -18,28 +18,28 @@ Blockly.Python['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
// First, add a 'global' statement for every variable that is not shadowed by
// a local parameter.
var globals = [];
var workspace = block.workspace;
var variables = Blockly.Variables.allUsedVarModels(workspace) || [];
for (var i = 0, variable; (variable = variables[i]); i++) {
var varName = variable.name;
const globals = [];
const workspace = block.workspace;
const usedVariables = Blockly.Variables.allUsedVarModels(workspace) || [];
for (let i = 0, variable; (variable = usedVariables[i]); i++) {
const varName = variable.name;
if (block.getVars().indexOf(varName) === -1) {
globals.push(Blockly.Python.nameDB_.getName(varName,
Blockly.VARIABLE_CATEGORY_NAME));
}
}
// Add developer variables.
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (var i = 0; i < devVarList.length; i++) {
const devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (let i = 0; i < devVarList.length; i++) {
globals.push(Blockly.Python.nameDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
}
globals = globals.length ?
const globalString = globals.length ?
Blockly.Python.INDENT + 'global ' + globals.join(', ') + '\n' : '';
var funcName = Blockly.Python.nameDB_.getName(
const funcName = Blockly.Python.nameDB_.getName(
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
var xfix1 = '';
let xfix1 = '';
if (Blockly.Python.STATEMENT_PREFIX) {
xfix1 += Blockly.Python.injectId(Blockly.Python.STATEMENT_PREFIX, block);
}
@@ -49,16 +49,16 @@ Blockly.Python['procedures_defreturn'] = function(block) {
if (xfix1) {
xfix1 = Blockly.Python.prefixLines(xfix1, Blockly.Python.INDENT);
}
var loopTrap = '';
let loopTrap = '';
if (Blockly.Python.INFINITE_LOOP_TRAP) {
loopTrap = Blockly.Python.prefixLines(
Blockly.Python.injectId(Blockly.Python.INFINITE_LOOP_TRAP, block),
Blockly.Python.INDENT);
}
var branch = Blockly.Python.statementToCode(block, 'STACK');
var returnValue = Blockly.Python.valueToCode(block, 'RETURN',
let branch = Blockly.Python.statementToCode(block, 'STACK');
let returnValue = Blockly.Python.valueToCode(block, 'RETURN',
Blockly.Python.ORDER_NONE) || '';
var xfix2 = '';
let xfix2 = '';
if (branch && returnValue) {
// After executing the function body, revisit this block for the return.
xfix2 = xfix1;
@@ -68,14 +68,14 @@ Blockly.Python['procedures_defreturn'] = function(block) {
} else if (!branch) {
branch = Blockly.Python.PASS;
}
var args = [];
var variables = block.getVars();
for (var i = 0; i < variables.length; i++) {
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = Blockly.Python.nameDB_.getName(variables[i],
Blockly.VARIABLE_CATEGORY_NAME);
}
var code = 'def ' + funcName + '(' + args.join(', ') + '):\n' +
globals + xfix1 + loopTrap + branch + xfix2 + returnValue;
let code = 'def ' + funcName + '(' + args.join(', ') + '):\n' + globalString +
xfix1 + loopTrap + branch + xfix2 + returnValue;
code = Blockly.Python.scrub_(block, code);
// Add % so as not to collide with helper functions in definitions list.
Blockly.Python.definitions_['%' + funcName] = code;
@@ -89,15 +89,15 @@ Blockly.Python['procedures_defnoreturn'] =
Blockly.Python['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
var funcName = Blockly.Python.nameDB_.getName(block.getFieldValue('NAME'),
const funcName = Blockly.Python.nameDB_.getName(block.getFieldValue('NAME'),
Blockly.PROCEDURE_CATEGORY_NAME);
var args = [];
var variables = block.getVars();
for (var i = 0; i < variables.length; i++) {
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = Blockly.Python.valueToCode(block, 'ARG' + i,
Blockly.Python.ORDER_NONE) || 'None';
}
var code = funcName + '(' + args.join(', ') + ')';
const code = funcName + '(' + args.join(', ') + ')';
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};
@@ -105,15 +105,15 @@ Blockly.Python['procedures_callnoreturn'] = function(block) {
// Call a procedure with no return value.
// Generated code is for a function call as a statement is the same as a
// function call as a value, with the addition of line ending.
var tuple = Blockly.Python['procedures_callreturn'](block);
const tuple = Blockly.Python['procedures_callreturn'](block);
return tuple[0] + '\n';
};
Blockly.Python['procedures_ifreturn'] = function(block) {
// Conditionally return value from a procedure.
var condition = Blockly.Python.valueToCode(block, 'CONDITION',
const condition = Blockly.Python.valueToCode(block, 'CONDITION',
Blockly.Python.ORDER_NONE) || 'False';
var code = 'if ' + condition + ':\n';
let code = 'if ' + condition + ':\n';
if (Blockly.Python.STATEMENT_SUFFIX) {
// Inject any statement suffix here since the regular one at the end
// will not get executed if the return is triggered.
@@ -122,7 +122,7 @@ Blockly.Python['procedures_ifreturn'] = function(block) {
Blockly.Python.INDENT);
}
if (block.hasReturnValue_) {
var value = Blockly.Python.valueToCode(block, 'VALUE',
const value = Blockly.Python.valueToCode(block, 'VALUE',
Blockly.Python.ORDER_NONE) || 'None';
code += Blockly.Python.INDENT + 'return ' + value + '\n';
} else {