Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update lua block generators to const and let #5662

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions generators/lua/colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@ goog.require('Blockly.Lua');

Blockly.Lua['colour_picker'] = function(block) {
// Colour picker.
var code = Blockly.Lua.quote_(block.getFieldValue('COLOUR'));
const code = Blockly.Lua.quote_(block.getFieldValue('COLOUR'));
return [code, Blockly.Lua.ORDER_ATOMIC];
};

Blockly.Lua['colour_random'] = function(block) {
// Generate a random colour.
var code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
const code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['colour_rgb'] = function(block) {
// Compose a colour from RGB components expressed as percentages.
var functionName = Blockly.Lua.provideFunction_(
const functionName = Blockly.Lua.provideFunction_(
'colour_rgb',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b)',
' r = math.floor(math.min(100, math.max(0, r)) * 2.55 + .5)',
' g = math.floor(math.min(100, math.max(0, g)) * 2.55 + .5)',
' b = math.floor(math.min(100, math.max(0, b)) * 2.55 + .5)',
' return string.format("#%02x%02x%02x", r, g, b)',
'end']);
var r = Blockly.Lua.valueToCode(block, 'RED',
const r = Blockly.Lua.valueToCode(block, 'RED',
Blockly.Lua.ORDER_NONE) || 0;
var g = Blockly.Lua.valueToCode(block, 'GREEN',
const g = Blockly.Lua.valueToCode(block, 'GREEN',
Blockly.Lua.ORDER_NONE) || 0;
var b = Blockly.Lua.valueToCode(block, 'BLUE',
const b = Blockly.Lua.valueToCode(block, 'BLUE',
Blockly.Lua.ORDER_NONE) || 0;
var code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
const code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['colour_blend'] = function(block) {
// Blend two colours together.
var functionName = Blockly.Lua.provideFunction_(
const functionName = Blockly.Lua.provideFunction_(
'colour_blend',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
'(colour1, colour2, ratio)',
Expand All @@ -64,12 +64,12 @@ Blockly.Lua['colour_blend'] = function(block) {
' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
' return string.format("#%02x%02x%02x", r, g, b)',
'end']);
var colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
const colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
var colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
const colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
var ratio = Blockly.Lua.valueToCode(block, 'RATIO',
const ratio = Blockly.Lua.valueToCode(block, 'RATIO',
Blockly.Lua.ORDER_NONE) || 0;
var code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
const code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};
122 changes: 62 additions & 60 deletions generators/lua/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ Blockly.Lua['lists_create_empty'] = function(block) {

Blockly.Lua['lists_create_with'] = function(block) {
// Create a list with any number of elements of any type.
var elements = new Array(block.itemCount_);
for (var i = 0; i < block.itemCount_; i++) {
const elements = new Array(block.itemCount_);
for (let i = 0; i < block.itemCount_; i++) {
elements[i] = Blockly.Lua.valueToCode(block, 'ADD' + i,
Blockly.Lua.ORDER_NONE) || 'None';
}
var code = '{' + elements.join(', ') + '}';
const code = '{' + elements.join(', ') + '}';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['lists_repeat'] = function(block) {
// Create a list with one element repeated.
var functionName = Blockly.Lua.provideFunction_(
const functionName = Blockly.Lua.provideFunction_(
'create_list_repeated',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(item, count)',
' local t = {}',
Expand All @@ -41,37 +41,38 @@ Blockly.Lua['lists_repeat'] = function(block) {
' end',
' return t',
'end']);
var element = Blockly.Lua.valueToCode(block, 'ITEM',
const element = Blockly.Lua.valueToCode(block, 'ITEM',
Blockly.Lua.ORDER_NONE) || 'None';
var repeatCount = Blockly.Lua.valueToCode(block, 'NUM',
const repeatCount = Blockly.Lua.valueToCode(block, 'NUM',
Blockly.Lua.ORDER_NONE) || '0';
var code = functionName + '(' + element + ', ' + repeatCount + ')';
const code = functionName + '(' + element + ', ' + repeatCount + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['lists_length'] = function(block) {
// String or array length.
var list = Blockly.Lua.valueToCode(block, 'VALUE',
const list = Blockly.Lua.valueToCode(block, 'VALUE',
Blockly.Lua.ORDER_UNARY) || '{}';
return ['#' + list, Blockly.Lua.ORDER_UNARY];
};

Blockly.Lua['lists_isEmpty'] = function(block) {
// Is the string null or array empty?
var list = Blockly.Lua.valueToCode(block, 'VALUE',
const list = Blockly.Lua.valueToCode(block, 'VALUE',
Blockly.Lua.ORDER_UNARY) || '{}';
var code = '#' + list + ' == 0';
const code = '#' + list + ' == 0';
return [code, Blockly.Lua.ORDER_RELATIONAL];
};

Blockly.Lua['lists_indexOf'] = function(block) {
// Find an item in the list.
var item = Blockly.Lua.valueToCode(block, 'FIND',
const item = Blockly.Lua.valueToCode(block, 'FIND',
Blockly.Lua.ORDER_NONE) || '\'\'';
var list = Blockly.Lua.valueToCode(block, 'VALUE',
const list = Blockly.Lua.valueToCode(block, 'VALUE',
Blockly.Lua.ORDER_NONE) || '{}';
let functionName;
if (block.getFieldValue('END') === 'FIRST') {
var functionName = Blockly.Lua.provideFunction_(
functionName = Blockly.Lua.provideFunction_(
'first_index',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
' for k, v in ipairs(t) do',
Expand All @@ -82,7 +83,7 @@ Blockly.Lua['lists_indexOf'] = function(block) {
' return 0',
'end']);
} else {
var functionName = Blockly.Lua.provideFunction_(
functionName = Blockly.Lua.provideFunction_(
'last_index',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
' for i = #t, 1, -1 do',
Expand All @@ -93,7 +94,7 @@ Blockly.Lua['lists_indexOf'] = function(block) {
' return 0',
'end']);
}
var code = functionName + '(' + list + ', ' + item + ')';
const code = functionName + '(' + list + ', ' + item + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};

Expand Down Expand Up @@ -122,11 +123,11 @@ Blockly.Lua.lists.getIndex_ = function(listName, where, opt_at) {
Blockly.Lua['lists_getIndex'] = function(block) {
// Get element at index.
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
var mode = block.getFieldValue('MODE') || 'GET';
var where = block.getFieldValue('WHERE') || 'FROM_START';
var list = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_HIGH) ||
const mode = block.getFieldValue('MODE') || 'GET';
const where = block.getFieldValue('WHERE') || 'FROM_START';
const list = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_HIGH) ||
'({})';
var getIndex_ = Blockly.Lua.lists.getIndex_;
const getIndex_ = Blockly.Lua.lists.getIndex_;

// If `list` would be evaluated more than once (which is the case for LAST,
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
Expand All @@ -135,21 +136,22 @@ Blockly.Lua['lists_getIndex'] = function(block) {
// `list` is an expression, so we may not evaluate it more than once.
if (mode === 'REMOVE') {
// We can use multiple statements.
var atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_ADDITIVE :
const atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_ADDITIVE :
Blockly.Lua.ORDER_NONE;
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
var listVar = Blockly.Lua.nameDB_.getDistinctName(
let at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
const listVar = Blockly.Lua.nameDB_.getDistinctName(
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
at = getIndex_(listVar, where, at);
var code = listVar + ' = ' + list + '\n' +
const code = listVar + ' = ' + list + '\n' +
'table.remove(' + listVar + ', ' + at + ')\n';
return code;
} else {
// We need to create a procedure to avoid reevaluating values.
var at = Blockly.Lua.valueToCode(block, 'AT', Blockly.Lua.ORDER_NONE) ||
const at = Blockly.Lua.valueToCode(block, 'AT', Blockly.Lua.ORDER_NONE) ||
'1';
let functionName;
if (mode === 'GET') {
var functionName = Blockly.Lua.provideFunction_(
functionName = Blockly.Lua.provideFunction_(
'list_get_' + where.toLowerCase(),
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
// The value for 'FROM_END' and'FROM_START' depends on `at` so
Expand All @@ -159,7 +161,7 @@ Blockly.Lua['lists_getIndex'] = function(block) {
' return t[' + getIndex_('t', where, 'at') + ']',
'end']);
} else { // `mode` === 'GET_REMOVE'
var functionName = Blockly.Lua.provideFunction_(
functionName = Blockly.Lua.provideFunction_(
'list_remove_' + where.toLowerCase(),
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
// The value for 'FROM_END' and'FROM_START' depends on `at` so
Expand All @@ -169,7 +171,7 @@ Blockly.Lua['lists_getIndex'] = function(block) {
' return table.remove(t, ' + getIndex_('t', where, 'at') + ')',
'end']);
}
var code = functionName + '(' + list +
const code = functionName + '(' + list +
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
// pass it.
((where === 'FROM_END' || where === 'FROM_START') ? ', ' + at : '') +
Expand All @@ -179,15 +181,15 @@ Blockly.Lua['lists_getIndex'] = function(block) {
} else {
// Either `list` is a simple variable, or we only need to refer to `list`
// once.
var atOrder = (mode === 'GET' && where === 'FROM_END') ?
const atOrder = (mode === 'GET' && where === 'FROM_END') ?
Blockly.Lua.ORDER_ADDITIVE : Blockly.Lua.ORDER_NONE;
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
let at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
at = getIndex_(list, where, at);
if (mode === 'GET') {
var code = list + '[' + at + ']';
const code = list + '[' + at + ']';
return [code, Blockly.Lua.ORDER_HIGH];
} else {
var code = 'table.remove(' + list + ', ' + at + ')';
const code = 'table.remove(' + list + ', ' + at + ')';
if (mode === 'GET_REMOVE') {
return [code, Blockly.Lua.ORDER_HIGH];
} else { // `mode` === 'REMOVE'
Expand All @@ -200,24 +202,24 @@ Blockly.Lua['lists_getIndex'] = function(block) {
Blockly.Lua['lists_setIndex'] = function(block) {
// Set element at index.
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
var list = Blockly.Lua.valueToCode(block, 'LIST',
let list = Blockly.Lua.valueToCode(block, 'LIST',
Blockly.Lua.ORDER_HIGH) || '{}';
var mode = block.getFieldValue('MODE') || 'SET';
var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.Lua.valueToCode(block, 'AT',
const mode = block.getFieldValue('MODE') || 'SET';
const where = block.getFieldValue('WHERE') || 'FROM_START';
const at = Blockly.Lua.valueToCode(block, 'AT',
Blockly.Lua.ORDER_ADDITIVE) || '1';
var value = Blockly.Lua.valueToCode(block, 'TO',
const value = Blockly.Lua.valueToCode(block, 'TO',
Blockly.Lua.ORDER_NONE) || 'None';
var getIndex_ = Blockly.Lua.lists.getIndex_;
const getIndex_ = Blockly.Lua.lists.getIndex_;

var code = '';
let code = '';
// If `list` would be evaluated more than once (which is the case for LAST,
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
if ((where === 'LAST' || where === 'FROM_END' || where === 'RANDOM') &&
!list.match(/^\w+$/)) {
// `list` is an expression, so we may not evaluate it more than once.
// We can use multiple statements.
var listVar = Blockly.Lua.nameDB_.getDistinctName(
const listVar = Blockly.Lua.nameDB_.getDistinctName(
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
code = listVar + ' = ' + list + '\n';
list = listVar;
Expand All @@ -236,17 +238,17 @@ Blockly.Lua['lists_setIndex'] = function(block) {

Blockly.Lua['lists_getSublist'] = function(block) {
// Get sublist.
var list = Blockly.Lua.valueToCode(block, 'LIST',
const list = Blockly.Lua.valueToCode(block, 'LIST',
Blockly.Lua.ORDER_NONE) || '{}';
var where1 = block.getFieldValue('WHERE1');
var where2 = block.getFieldValue('WHERE2');
var at1 = Blockly.Lua.valueToCode(block, 'AT1',
const where1 = block.getFieldValue('WHERE1');
const where2 = block.getFieldValue('WHERE2');
const at1 = Blockly.Lua.valueToCode(block, 'AT1',
Blockly.Lua.ORDER_NONE) || '1';
var at2 = Blockly.Lua.valueToCode(block, 'AT2',
const at2 = Blockly.Lua.valueToCode(block, 'AT2',
Blockly.Lua.ORDER_NONE) || '1';
var getIndex_ = Blockly.Lua.lists.getIndex_;
const getIndex_ = Blockly.Lua.lists.getIndex_;

var functionName = Blockly.Lua.provideFunction_(
const functionName = Blockly.Lua.provideFunction_(
'list_sublist_' + where1.toLowerCase() + '_' + where2.toLowerCase(),
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(source' +
// The value for 'FROM_END' and'FROM_START' depends on `at` so
Expand All @@ -262,7 +264,7 @@ Blockly.Lua['lists_getSublist'] = function(block) {
' end',
' return t',
'end']);
var code = functionName + '(' + list +
const code = functionName + '(' + list +
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
// pass it.
((where1 === 'FROM_END' || where1 === 'FROM_START') ? ', ' + at1 : '') +
Expand All @@ -273,12 +275,12 @@ Blockly.Lua['lists_getSublist'] = function(block) {

Blockly.Lua['lists_sort'] = function(block) {
// Block for sorting a list.
var list = Blockly.Lua.valueToCode(
const list = Blockly.Lua.valueToCode(
block, 'LIST', Blockly.Lua.ORDER_NONE) || '{}';
var direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
var type = block.getFieldValue('TYPE');
const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
const type = block.getFieldValue('TYPE');

var functionName = Blockly.Lua.provideFunction_(
const functionName = Blockly.Lua.provideFunction_(
'list_sort',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
'(list, typev, direction)',
Expand All @@ -302,19 +304,19 @@ Blockly.Lua['lists_sort'] = function(block) {
' return t',
'end']);

var code = functionName +
const code = functionName +
'(' + list + ',"' + type + '", ' + direction + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['lists_split'] = function(block) {
// Block for splitting text into a list, or joining a list into text.
var input = Blockly.Lua.valueToCode(block, 'INPUT',
let input = Blockly.Lua.valueToCode(block, 'INPUT',
Blockly.Lua.ORDER_NONE);
var delimiter = Blockly.Lua.valueToCode(block, 'DELIM',
const delimiter = Blockly.Lua.valueToCode(block, 'DELIM',
Blockly.Lua.ORDER_NONE) || '\'\'';
var mode = block.getFieldValue('MODE');
var functionName;
const mode = block.getFieldValue('MODE');
let functionName;
if (mode === 'SPLIT') {
if (!input) {
input = '\'\'';
Expand Down Expand Up @@ -345,15 +347,15 @@ Blockly.Lua['lists_split'] = function(block) {
} else {
throw Error('Unknown mode: ' + mode);
}
var code = functionName + '(' + input + ', ' + delimiter + ')';
const code = functionName + '(' + input + ', ' + delimiter + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['lists_reverse'] = function(block) {
// Block for reversing a list.
var list = Blockly.Lua.valueToCode(block, 'LIST',
const list = Blockly.Lua.valueToCode(block, 'LIST',
Blockly.Lua.ORDER_NONE) || '{}';
var functionName = Blockly.Lua.provideFunction_(
const functionName = Blockly.Lua.provideFunction_(
'list_reverse',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(input)',
' local reversed = {}',
Expand All @@ -362,6 +364,6 @@ Blockly.Lua['lists_reverse'] = function(block) {
' end',
' return reversed',
'end']);
var code = functionName + '(' + list + ')';
const code = functionName + '(' + list + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};
Loading