mirror of
https://github.com/google/blockly.git
synced 2026-01-04 23:50:12 +01:00
chore: update php block generators to const and let (#5663)
This commit is contained in:
@@ -18,28 +18,28 @@ Blockly.PHP['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.PHP.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.PHP.nameDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
||||
}
|
||||
globals = globals.length ?
|
||||
const globalStr = globals.length ?
|
||||
Blockly.PHP.INDENT + 'global ' + globals.join(', ') + ';\n' : '';
|
||||
|
||||
var funcName = Blockly.PHP.nameDB_.getName(
|
||||
const funcName = Blockly.PHP.nameDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
|
||||
var xfix1 = '';
|
||||
let xfix1 = '';
|
||||
if (Blockly.PHP.STATEMENT_PREFIX) {
|
||||
xfix1 += Blockly.PHP.injectId(Blockly.PHP.STATEMENT_PREFIX, block);
|
||||
}
|
||||
@@ -49,16 +49,16 @@ Blockly.PHP['procedures_defreturn'] = function(block) {
|
||||
if (xfix1) {
|
||||
xfix1 = Blockly.PHP.prefixLines(xfix1, Blockly.PHP.INDENT);
|
||||
}
|
||||
var loopTrap = '';
|
||||
let loopTrap = '';
|
||||
if (Blockly.PHP.INFINITE_LOOP_TRAP) {
|
||||
loopTrap = Blockly.PHP.prefixLines(
|
||||
Blockly.PHP.injectId(Blockly.PHP.INFINITE_LOOP_TRAP, block),
|
||||
Blockly.PHP.INDENT);
|
||||
}
|
||||
var branch = Blockly.PHP.statementToCode(block, 'STACK');
|
||||
var returnValue = Blockly.PHP.valueToCode(block, 'RETURN',
|
||||
const branch = Blockly.PHP.statementToCode(block, 'STACK');
|
||||
let returnValue = Blockly.PHP.valueToCode(block, 'RETURN',
|
||||
Blockly.PHP.ORDER_NONE) || '';
|
||||
var xfix2 = '';
|
||||
let xfix2 = '';
|
||||
if (branch && returnValue) {
|
||||
// After executing the function body, revisit this block for the return.
|
||||
xfix2 = xfix1;
|
||||
@@ -66,14 +66,14 @@ Blockly.PHP['procedures_defreturn'] = function(block) {
|
||||
if (returnValue) {
|
||||
returnValue = Blockly.PHP.INDENT + 'return ' + returnValue + ';\n';
|
||||
}
|
||||
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.PHP.nameDB_.getName(variables[i],
|
||||
Blockly.VARIABLE_CATEGORY_NAME);
|
||||
}
|
||||
var code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||
globals + xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
|
||||
let code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||
globalStr + xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
|
||||
code = Blockly.PHP.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Blockly.PHP.definitions_['%' + funcName] = code;
|
||||
@@ -87,15 +87,15 @@ Blockly.PHP['procedures_defnoreturn'] =
|
||||
|
||||
Blockly.PHP['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
var funcName = Blockly.PHP.nameDB_.getName(
|
||||
const funcName = Blockly.PHP.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.PHP.valueToCode(block, 'ARG' + i,
|
||||
Blockly.PHP.ORDER_NONE) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')';
|
||||
const code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
@@ -103,15 +103,15 @@ Blockly.PHP['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.PHP['procedures_callreturn'](block);
|
||||
const tuple = Blockly.PHP['procedures_callreturn'](block);
|
||||
return tuple[0] + ';\n';
|
||||
};
|
||||
|
||||
Blockly.PHP['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
var condition = Blockly.PHP.valueToCode(block, 'CONDITION',
|
||||
const condition = Blockly.PHP.valueToCode(block, 'CONDITION',
|
||||
Blockly.PHP.ORDER_NONE) || 'false';
|
||||
var code = 'if (' + condition + ') {\n';
|
||||
let code = 'if (' + condition + ') {\n';
|
||||
if (Blockly.PHP.STATEMENT_SUFFIX) {
|
||||
// Inject any statement suffix here since the regular one at the end
|
||||
// will not get executed if the return is triggered.
|
||||
@@ -120,7 +120,7 @@ Blockly.PHP['procedures_ifreturn'] = function(block) {
|
||||
Blockly.PHP.INDENT);
|
||||
}
|
||||
if (block.hasReturnValue_) {
|
||||
var value = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
const value = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'null';
|
||||
code += Blockly.PHP.INDENT + 'return ' + value + ';\n';
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user