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

@@ -16,8 +16,8 @@ goog.require('Blockly.Python');
Blockly.Python['controls_if'] = function(block) {
// If/elseif/else condition.
var n = 0;
var code = '', branchCode, conditionCode;
let n = 0;
let code = '', branchCode, conditionCode;
if (Blockly.Python.STATEMENT_PREFIX) {
// Automatic prefix insertion is switched off for this block. Add manually.
code += Blockly.Python.injectId(Blockly.Python.STATEMENT_PREFIX, block);
@@ -53,7 +53,7 @@ Blockly.Python['controls_ifelse'] = Blockly.Python['controls_if'];
Blockly.Python['logic_compare'] = function(block) {
// Comparison operator.
var OPERATORS = {
const OPERATORS = {
'EQ': '==',
'NEQ': '!=',
'LT': '<',
@@ -61,28 +61,28 @@ Blockly.Python['logic_compare'] = function(block) {
'GT': '>',
'GTE': '>='
};
var operator = OPERATORS[block.getFieldValue('OP')];
var order = Blockly.Python.ORDER_RELATIONAL;
var argument0 = Blockly.Python.valueToCode(block, 'A', order) || '0';
var argument1 = Blockly.Python.valueToCode(block, 'B', order) || '0';
var code = argument0 + ' ' + operator + ' ' + argument1;
const operator = OPERATORS[block.getFieldValue('OP')];
const order = Blockly.Python.ORDER_RELATIONAL;
const argument0 = Blockly.Python.valueToCode(block, 'A', order) || '0';
const argument1 = Blockly.Python.valueToCode(block, 'B', order) || '0';
const code = argument0 + ' ' + operator + ' ' + argument1;
return [code, order];
};
Blockly.Python['logic_operation'] = function(block) {
// Operations 'and', 'or'.
var operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
var order = (operator === 'and') ? Blockly.Python.ORDER_LOGICAL_AND :
const operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
const order = (operator === 'and') ? Blockly.Python.ORDER_LOGICAL_AND :
Blockly.Python.ORDER_LOGICAL_OR;
var argument0 = Blockly.Python.valueToCode(block, 'A', order);
var argument1 = Blockly.Python.valueToCode(block, 'B', order);
let argument0 = Blockly.Python.valueToCode(block, 'A', order);
let argument1 = Blockly.Python.valueToCode(block, 'B', order);
if (!argument0 && !argument1) {
// If there are no arguments, then the return value is false.
argument0 = 'False';
argument1 = 'False';
} else {
// Single missing arguments have no effect on the return value.
var defaultArgument = (operator === 'and') ? 'True' : 'False';
const defaultArgument = (operator === 'and') ? 'True' : 'False';
if (!argument0) {
argument0 = defaultArgument;
}
@@ -90,21 +90,21 @@ Blockly.Python['logic_operation'] = function(block) {
argument1 = defaultArgument;
}
}
var code = argument0 + ' ' + operator + ' ' + argument1;
const code = argument0 + ' ' + operator + ' ' + argument1;
return [code, order];
};
Blockly.Python['logic_negate'] = function(block) {
// Negation.
var argument0 = Blockly.Python.valueToCode(block, 'BOOL',
const argument0 = Blockly.Python.valueToCode(block, 'BOOL',
Blockly.Python.ORDER_LOGICAL_NOT) || 'True';
var code = 'not ' + argument0;
const code = 'not ' + argument0;
return [code, Blockly.Python.ORDER_LOGICAL_NOT];
};
Blockly.Python['logic_boolean'] = function(block) {
// Boolean values true and false.
var code = (block.getFieldValue('BOOL') === 'TRUE') ? 'True' : 'False';
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'True' : 'False';
return [code, Blockly.Python.ORDER_ATOMIC];
};
@@ -115,12 +115,12 @@ Blockly.Python['logic_null'] = function(block) {
Blockly.Python['logic_ternary'] = function(block) {
// Ternary operator.
var value_if = Blockly.Python.valueToCode(block, 'IF',
const value_if = Blockly.Python.valueToCode(block, 'IF',
Blockly.Python.ORDER_CONDITIONAL) || 'False';
var value_then = Blockly.Python.valueToCode(block, 'THEN',
const value_then = Blockly.Python.valueToCode(block, 'THEN',
Blockly.Python.ORDER_CONDITIONAL) || 'None';
var value_else = Blockly.Python.valueToCode(block, 'ELSE',
const value_else = Blockly.Python.valueToCode(block, 'ELSE',
Blockly.Python.ORDER_CONDITIONAL) || 'None';
var code = value_then + ' if ' + value_if + ' else ' + value_else;
const code = value_then + ' if ' + value_if + ' else ' + value_else;
return [code, Blockly.Python.ORDER_CONDITIONAL];
};