From 3746688ebd680ce1856202a3304aaa8892c6205e Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Fri, 5 Nov 2021 09:57:59 -0700 Subject: [PATCH] chore: update lua block generators to const and let (#5662) --- generators/lua/colour.js | 24 +++--- generators/lua/lists.js | 122 ++++++++++++++-------------- generators/lua/logic.js | 46 +++++------ generators/lua/loops.js | 47 +++++------ generators/lua/math.js | 74 ++++++++--------- generators/lua/procedures.js | 38 ++++----- generators/lua/text.js | 150 ++++++++++++++++++----------------- generators/lua/variables.js | 6 +- tests/deps.mocha.js | 16 ++-- 9 files changed, 267 insertions(+), 256 deletions(-) diff --git a/generators/lua/colour.js b/generators/lua/colour.js index aefc3818f..996497036 100644 --- a/generators/lua/colour.js +++ b/generators/lua/colour.js @@ -16,19 +16,19 @@ 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)', @@ -36,19 +36,19 @@ Blockly.Lua['colour_rgb'] = function(block) { ' 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)', @@ -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]; }; diff --git a/generators/lua/lists.js b/generators/lua/lists.js index aca5c339e..3c16c75a0 100644 --- a/generators/lua/lists.js +++ b/generators/lua/lists.js @@ -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 = {}', @@ -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', @@ -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', @@ -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]; }; @@ -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. @@ -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 @@ -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 @@ -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 : '') + @@ -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' @@ -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; @@ -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 @@ -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 : '') + @@ -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)', @@ -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 = '\'\''; @@ -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 = {}', @@ -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]; }; diff --git a/generators/lua/logic.js b/generators/lua/logic.js index 510866f37..df8c460f1 100644 --- a/generators/lua/logic.js +++ b/generators/lua/logic.js @@ -16,16 +16,16 @@ goog.require('Blockly.Lua'); Blockly.Lua['controls_if'] = function(block) { // If/elseif/else condition. - var n = 0; - var code = '', branchCode, conditionCode; + let n = 0; + let code = ''; if (Blockly.Lua.STATEMENT_PREFIX) { // Automatic prefix insertion is switched off for this block. Add manually. code += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block); } do { - conditionCode = Blockly.Lua.valueToCode(block, 'IF' + n, + const conditionCode = Blockly.Lua.valueToCode(block, 'IF' + n, Blockly.Lua.ORDER_NONE) || 'false'; - branchCode = Blockly.Lua.statementToCode(block, 'DO' + n); + let branchCode = Blockly.Lua.statementToCode(block, 'DO' + n); if (Blockly.Lua.STATEMENT_SUFFIX) { branchCode = Blockly.Lua.prefixLines( Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block), @@ -37,7 +37,7 @@ Blockly.Lua['controls_if'] = function(block) { } while (block.getInput('IF' + n)); if (block.getInput('ELSE') || Blockly.Lua.STATEMENT_SUFFIX) { - branchCode = Blockly.Lua.statementToCode(block, 'ELSE'); + let branchCode = Blockly.Lua.statementToCode(block, 'ELSE'); if (Blockly.Lua.STATEMENT_SUFFIX) { branchCode = Blockly.Lua.prefixLines( Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block), @@ -52,7 +52,7 @@ Blockly.Lua['controls_ifelse'] = Blockly.Lua['controls_if']; Blockly.Lua['logic_compare'] = function(block) { // Comparison operator. - var OPERATORS = { + const OPERATORS = { 'EQ': '==', 'NEQ': '~=', 'LT': '<', @@ -60,29 +60,29 @@ Blockly.Lua['logic_compare'] = function(block) { 'GT': '>', 'GTE': '>=' }; - var operator = OPERATORS[block.getFieldValue('OP')]; - var argument0 = Blockly.Lua.valueToCode(block, 'A', + const operator = OPERATORS[block.getFieldValue('OP')]; + const argument0 = Blockly.Lua.valueToCode(block, 'A', Blockly.Lua.ORDER_RELATIONAL) || '0'; - var argument1 = Blockly.Lua.valueToCode(block, 'B', + const argument1 = Blockly.Lua.valueToCode(block, 'B', Blockly.Lua.ORDER_RELATIONAL) || '0'; - var code = argument0 + ' ' + operator + ' ' + argument1; + const code = argument0 + ' ' + operator + ' ' + argument1; return [code, Blockly.Lua.ORDER_RELATIONAL]; }; Blockly.Lua['logic_operation'] = function(block) { // Operations 'and', 'or'. - var operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or'; - var order = (operator === 'and') ? Blockly.Lua.ORDER_AND : + const operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or'; + const order = (operator === 'and') ? Blockly.Lua.ORDER_AND : Blockly.Lua.ORDER_OR; - var argument0 = Blockly.Lua.valueToCode(block, 'A', order); - var argument1 = Blockly.Lua.valueToCode(block, 'B', order); + let argument0 = Blockly.Lua.valueToCode(block, 'A', order); + let argument1 = Blockly.Lua.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.Lua['logic_operation'] = function(block) { argument1 = defaultArgument; } } - var code = argument0 + ' ' + operator + ' ' + argument1; + const code = argument0 + ' ' + operator + ' ' + argument1; return [code, order]; }; Blockly.Lua['logic_negate'] = function(block) { // Negation. - var argument0 = Blockly.Lua.valueToCode(block, 'BOOL', + const argument0 = Blockly.Lua.valueToCode(block, 'BOOL', Blockly.Lua.ORDER_UNARY) || 'true'; - var code = 'not ' + argument0; + const code = 'not ' + argument0; return [code, Blockly.Lua.ORDER_UNARY]; }; Blockly.Lua['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.Lua.ORDER_ATOMIC]; }; @@ -115,12 +115,12 @@ Blockly.Lua['logic_null'] = function(block) { Blockly.Lua['logic_ternary'] = function(block) { // Ternary operator. - var value_if = Blockly.Lua.valueToCode(block, 'IF', + const value_if = Blockly.Lua.valueToCode(block, 'IF', Blockly.Lua.ORDER_AND) || 'false'; - var value_then = Blockly.Lua.valueToCode(block, 'THEN', + const value_then = Blockly.Lua.valueToCode(block, 'THEN', Blockly.Lua.ORDER_AND) || 'nil'; - var value_else = Blockly.Lua.valueToCode(block, 'ELSE', + const value_else = Blockly.Lua.valueToCode(block, 'ELSE', Blockly.Lua.ORDER_OR) || 'nil'; - var code = value_if + ' and ' + value_then + ' or ' + value_else; + const code = value_if + ' and ' + value_then + ' or ' + value_else; return [code, Blockly.Lua.ORDER_OR]; }; diff --git a/generators/lua/loops.js b/generators/lua/loops.js index abc2996c3..2cc9696c7 100644 --- a/generators/lua/loops.js +++ b/generators/lua/loops.js @@ -43,12 +43,13 @@ Blockly.Lua.addContinueLabel_ = function(branch) { Blockly.Lua['controls_repeat_ext'] = function(block) { // Repeat n times. + let repeats; if (block.getField('TIMES')) { // Internal number. - var repeats = String(Number(block.getFieldValue('TIMES'))); + repeats = String(Number(block.getFieldValue('TIMES'))); } else { // External number. - var repeats = Blockly.Lua.valueToCode(block, 'TIMES', + repeats = Blockly.Lua.valueToCode(block, 'TIMES', Blockly.Lua.ORDER_NONE) || '0'; } if (Blockly.isNumber(repeats)) { @@ -56,12 +57,12 @@ Blockly.Lua['controls_repeat_ext'] = function(block) { } else { repeats = 'math.floor(' + repeats + ')'; } - var branch = Blockly.Lua.statementToCode(block, 'DO'); + let branch = Blockly.Lua.statementToCode(block, 'DO'); branch = Blockly.Lua.addLoopTrap(branch, block); branch = Blockly.Lua.addContinueLabel_(branch); - var loopVar = Blockly.Lua.nameDB_.getDistinctName( + const loopVar = Blockly.Lua.nameDB_.getDistinctName( 'count', Blockly.VARIABLE_CATEGORY_NAME); - var code = 'for ' + loopVar + ' = 1, ' + repeats + ' do\n' + + const code = 'for ' + loopVar + ' = 1, ' + repeats + ' do\n' + branch + 'end\n'; return code; }; @@ -70,11 +71,11 @@ Blockly.Lua['controls_repeat'] = Blockly.Lua['controls_repeat_ext']; Blockly.Lua['controls_whileUntil'] = function(block) { // Do while/until loop. - var until = block.getFieldValue('MODE') === 'UNTIL'; - var argument0 = Blockly.Lua.valueToCode(block, 'BOOL', + const until = block.getFieldValue('MODE') === 'UNTIL'; + let argument0 = Blockly.Lua.valueToCode(block, 'BOOL', until ? Blockly.Lua.ORDER_UNARY : Blockly.Lua.ORDER_NONE) || 'false'; - var branch = Blockly.Lua.statementToCode(block, 'DO'); + let branch = Blockly.Lua.statementToCode(block, 'DO'); branch = Blockly.Lua.addLoopTrap(branch, block); branch = Blockly.Lua.addContinueLabel_(branch); if (until) { @@ -85,24 +86,24 @@ Blockly.Lua['controls_whileUntil'] = function(block) { Blockly.Lua['controls_for'] = function(block) { // For loop. - var variable0 = Blockly.Lua.nameDB_.getName( + const variable0 = Blockly.Lua.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - var startVar = Blockly.Lua.valueToCode(block, 'FROM', + const startVar = Blockly.Lua.valueToCode(block, 'FROM', Blockly.Lua.ORDER_NONE) || '0'; - var endVar = Blockly.Lua.valueToCode(block, 'TO', + const endVar = Blockly.Lua.valueToCode(block, 'TO', Blockly.Lua.ORDER_NONE) || '0'; - var increment = Blockly.Lua.valueToCode(block, 'BY', + const increment = Blockly.Lua.valueToCode(block, 'BY', Blockly.Lua.ORDER_NONE) || '1'; - var branch = Blockly.Lua.statementToCode(block, 'DO'); + let branch = Blockly.Lua.statementToCode(block, 'DO'); branch = Blockly.Lua.addLoopTrap(branch, block); branch = Blockly.Lua.addContinueLabel_(branch); - var code = ''; - var incValue; + let code = ''; + let incValue; if (Blockly.isNumber(startVar) && Blockly.isNumber(endVar) && Blockly.isNumber(increment)) { // All arguments are simple numbers. - var up = Number(startVar) <= Number(endVar); - var step = Math.abs(Number(increment)); + const up = Number(startVar) <= Number(endVar); + const step = Math.abs(Number(increment)); incValue = (up ? '' : '-') + step; } else { code = ''; @@ -128,21 +129,21 @@ Blockly.Lua['controls_for'] = function(block) { Blockly.Lua['controls_forEach'] = function(block) { // For each loop. - var variable0 = Blockly.Lua.nameDB_.getName( + const variable0 = Blockly.Lua.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - var argument0 = Blockly.Lua.valueToCode(block, 'LIST', + const argument0 = Blockly.Lua.valueToCode(block, 'LIST', Blockly.Lua.ORDER_NONE) || '{}'; - var branch = Blockly.Lua.statementToCode(block, 'DO'); + let branch = Blockly.Lua.statementToCode(block, 'DO'); branch = Blockly.Lua.addLoopTrap(branch, block); branch = Blockly.Lua.addContinueLabel_(branch); - var code = 'for _, ' + variable0 + ' in ipairs(' + argument0 + ') do \n' + + const code = 'for _, ' + variable0 + ' in ipairs(' + argument0 + ') do \n' + branch + 'end\n'; return code; }; Blockly.Lua['controls_flow_statements'] = function(block) { // Flow statements: continue, break. - var xfix = ''; + let xfix = ''; if (Blockly.Lua.STATEMENT_PREFIX) { // Automatic prefix insertion is switched off for this block. Add manually. xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block); @@ -153,7 +154,7 @@ Blockly.Lua['controls_flow_statements'] = function(block) { xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block); } if (Blockly.Lua.STATEMENT_PREFIX) { - var loop = Blockly.Constants.Loops + const loop = Blockly.Constants.Loops .CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block); if (loop && !loop.suppressPrefixSuffix) { // Inject loop's statement prefix here since the regular one at the end diff --git a/generators/lua/math.js b/generators/lua/math.js index 9d450ff4f..aab654b86 100644 --- a/generators/lua/math.js +++ b/generators/lua/math.js @@ -16,35 +16,34 @@ goog.require('Blockly.Lua'); Blockly.Lua['math_number'] = function(block) { // Numeric value. - var code = Number(block.getFieldValue('NUM')); - var order = code < 0 ? Blockly.Lua.ORDER_UNARY : + const code = Number(block.getFieldValue('NUM')); + const order = code < 0 ? Blockly.Lua.ORDER_UNARY : Blockly.Lua.ORDER_ATOMIC; return [code, order]; }; Blockly.Lua['math_arithmetic'] = function(block) { // Basic arithmetic operators, and power. - var OPERATORS = { + const OPERATORS = { ADD: [' + ', Blockly.Lua.ORDER_ADDITIVE], MINUS: [' - ', Blockly.Lua.ORDER_ADDITIVE], MULTIPLY: [' * ', Blockly.Lua.ORDER_MULTIPLICATIVE], DIVIDE: [' / ', Blockly.Lua.ORDER_MULTIPLICATIVE], POWER: [' ^ ', Blockly.Lua.ORDER_EXPONENTIATION] }; - var tuple = OPERATORS[block.getFieldValue('OP')]; - var operator = tuple[0]; - var order = tuple[1]; - var argument0 = Blockly.Lua.valueToCode(block, 'A', order) || '0'; - var argument1 = Blockly.Lua.valueToCode(block, 'B', order) || '0'; - var code = argument0 + operator + argument1; + const tuple = OPERATORS[block.getFieldValue('OP')]; + const operator = tuple[0]; + const order = tuple[1]; + const argument0 = Blockly.Lua.valueToCode(block, 'A', order) || '0'; + const argument1 = Blockly.Lua.valueToCode(block, 'B', order) || '0'; + const code = argument0 + operator + argument1; return [code, order]; }; Blockly.Lua['math_single'] = function(block) { // Math operators with single operand. - var operator = block.getFieldValue('OP'); - var code; - var arg; + const operator = block.getFieldValue('OP'); + let arg; if (operator === 'NEG') { // Negation is a special case given its different operator precedence. arg = Blockly.Lua.valueToCode(block, 'NUM', @@ -63,6 +62,8 @@ Blockly.Lua['math_single'] = function(block) { arg = Blockly.Lua.valueToCode(block, 'NUM', Blockly.Lua.ORDER_NONE) || '0'; } + + let code; switch (operator) { case 'ABS': code = 'math.abs(' + arg + ')'; @@ -115,7 +116,7 @@ Blockly.Lua['math_single'] = function(block) { Blockly.Lua['math_constant'] = function(block) { // Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY. - var CONSTANTS = { + const CONSTANTS = { PI: ['math.pi', Blockly.Lua.ORDER_HIGH], E: ['math.exp(1)', Blockly.Lua.ORDER_HIGH], GOLDEN_RATIO: ['(1 + math.sqrt(5)) / 2', Blockly.Lua.ORDER_MULTIPLICATIVE], @@ -129,13 +130,13 @@ Blockly.Lua['math_constant'] = function(block) { Blockly.Lua['math_number_property'] = function(block) { // Check if a number is even, odd, prime, whole, positive, or negative // or if it is divisible by certain number. Returns true or false. - var number_to_check = Blockly.Lua.valueToCode(block, 'NUMBER_TO_CHECK', + const number_to_check = Blockly.Lua.valueToCode(block, 'NUMBER_TO_CHECK', Blockly.Lua.ORDER_MULTIPLICATIVE) || '0'; - var dropdown_property = block.getFieldValue('PROPERTY'); - var code; + const dropdown_property = block.getFieldValue('PROPERTY'); + let code; if (dropdown_property === 'PRIME') { // Prime is a special case as it is not a one-liner test. - var functionName = Blockly.Lua.provideFunction_( + const functionName = Blockly.Lua.provideFunction_( 'math_isPrime', ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(n)', ' -- https://en.wikipedia.org/wiki/Primality_test#Naive_methods', @@ -174,8 +175,8 @@ Blockly.Lua['math_number_property'] = function(block) { case 'NEGATIVE': code = number_to_check + ' < 0'; break; - case 'DIVISIBLE_BY': - var divisor = Blockly.Lua.valueToCode(block, 'DIVISOR', + case 'DIVISIBLE_BY': { + const divisor = Blockly.Lua.valueToCode(block, 'DIVISOR', Blockly.Lua.ORDER_MULTIPLICATIVE); // If 'divisor' is some code that evals to 0, Lua will produce a nan. // Let's produce nil if we can determine this at compile-time. @@ -187,15 +188,16 @@ Blockly.Lua['math_number_property'] = function(block) { // because nil is false, so allow a runtime failure. :-( code = number_to_check + ' % ' + divisor + ' == 0'; break; + } } return [code, Blockly.Lua.ORDER_RELATIONAL]; }; Blockly.Lua['math_change'] = function(block) { // Add to a variable in place. - var argument0 = Blockly.Lua.valueToCode(block, 'DELTA', + const argument0 = Blockly.Lua.valueToCode(block, 'DELTA', Blockly.Lua.ORDER_ADDITIVE) || '0'; - var varName = Blockly.Lua.nameDB_.getName( + const varName = Blockly.Lua.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); return varName + ' = ' + varName + ' + ' + argument0 + '\n'; }; @@ -207,10 +209,10 @@ Blockly.Lua['math_trig'] = Blockly.Lua['math_single']; Blockly.Lua['math_on_list'] = function(block) { // Math functions for lists. - var func = block.getFieldValue('OP'); - var list = Blockly.Lua.valueToCode(block, 'LIST', + const func = block.getFieldValue('OP'); + const list = Blockly.Lua.valueToCode(block, 'LIST', Blockly.Lua.ORDER_NONE) || '{}'; - var functionName; + let functionName; // Functions needed in more than one case. function provideSum() { @@ -375,34 +377,34 @@ Blockly.Lua['math_on_list'] = function(block) { Blockly.Lua['math_modulo'] = function(block) { // Remainder computation. - var argument0 = Blockly.Lua.valueToCode(block, 'DIVIDEND', + const argument0 = Blockly.Lua.valueToCode(block, 'DIVIDEND', Blockly.Lua.ORDER_MULTIPLICATIVE) || '0'; - var argument1 = Blockly.Lua.valueToCode(block, 'DIVISOR', + const argument1 = Blockly.Lua.valueToCode(block, 'DIVISOR', Blockly.Lua.ORDER_MULTIPLICATIVE) || '0'; - var code = argument0 + ' % ' + argument1; + const code = argument0 + ' % ' + argument1; return [code, Blockly.Lua.ORDER_MULTIPLICATIVE]; }; Blockly.Lua['math_constrain'] = function(block) { // Constrain a number between two limits. - var argument0 = Blockly.Lua.valueToCode(block, 'VALUE', + const argument0 = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_NONE) || '0'; - var argument1 = Blockly.Lua.valueToCode(block, 'LOW', + const argument1 = Blockly.Lua.valueToCode(block, 'LOW', Blockly.Lua.ORDER_NONE) || '-math.huge'; - var argument2 = Blockly.Lua.valueToCode(block, 'HIGH', + const argument2 = Blockly.Lua.valueToCode(block, 'HIGH', Blockly.Lua.ORDER_NONE) || 'math.huge'; - var code = 'math.min(math.max(' + argument0 + ', ' + argument1 + '), ' + + const code = 'math.min(math.max(' + argument0 + ', ' + argument1 + '), ' + argument2 + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['math_random_int'] = function(block) { // Random integer between [X] and [Y]. - var argument0 = Blockly.Lua.valueToCode(block, 'FROM', + const argument0 = Blockly.Lua.valueToCode(block, 'FROM', Blockly.Lua.ORDER_NONE) || '0'; - var argument1 = Blockly.Lua.valueToCode(block, 'TO', + const argument1 = Blockly.Lua.valueToCode(block, 'TO', Blockly.Lua.ORDER_NONE) || '0'; - var code = 'math.random(' + argument0 + ', ' + argument1 + ')'; + const code = 'math.random(' + argument0 + ', ' + argument1 + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; @@ -413,9 +415,9 @@ Blockly.Lua['math_random_float'] = function(block) { Blockly.Lua['math_atan2'] = function(block) { // Arctangent of point (X, Y) in degrees from -180 to 180. - var argument0 = Blockly.Lua.valueToCode(block, 'X', + const argument0 = Blockly.Lua.valueToCode(block, 'X', Blockly.Lua.ORDER_NONE) || '0'; - var argument1 = Blockly.Lua.valueToCode(block, 'Y', + const argument1 = Blockly.Lua.valueToCode(block, 'Y', Blockly.Lua.ORDER_NONE) || '0'; return ['math.deg(math.atan2(' + argument1 + ', ' + argument0 + '))', Blockly.Lua.ORDER_HIGH]; diff --git a/generators/lua/procedures.js b/generators/lua/procedures.js index c89ea72b7..70dfdd631 100644 --- a/generators/lua/procedures.js +++ b/generators/lua/procedures.js @@ -16,9 +16,9 @@ goog.require('Blockly.Lua'); Blockly.Lua['procedures_defreturn'] = function(block) { // Define a procedure with a return value. - var funcName = Blockly.Lua.nameDB_.getName( + const funcName = Blockly.Lua.nameDB_.getName( block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME); - var xfix1 = ''; + let xfix1 = ''; if (Blockly.Lua.STATEMENT_PREFIX) { xfix1 += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block); } @@ -28,16 +28,16 @@ Blockly.Lua['procedures_defreturn'] = function(block) { if (xfix1) { xfix1 = Blockly.Lua.prefixLines(xfix1, Blockly.Lua.INDENT); } - var loopTrap = ''; + let loopTrap = ''; if (Blockly.Lua.INFINITE_LOOP_TRAP) { loopTrap = Blockly.Lua.prefixLines( Blockly.Lua.injectId(Blockly.Lua.INFINITE_LOOP_TRAP, block), Blockly.Lua.INDENT); } - var branch = Blockly.Lua.statementToCode(block, 'STACK'); - var returnValue = Blockly.Lua.valueToCode(block, 'RETURN', + let branch = Blockly.Lua.statementToCode(block, 'STACK'); + let returnValue = Blockly.Lua.valueToCode(block, 'RETURN', Blockly.Lua.ORDER_NONE) || ''; - var xfix2 = ''; + let xfix2 = ''; if (branch && returnValue) { // After executing the function body, revisit this block for the return. xfix2 = xfix1; @@ -47,13 +47,13 @@ Blockly.Lua['procedures_defreturn'] = function(block) { } else if (!branch) { branch = ''; } - 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.Lua.nameDB_.getName(variables[i], Blockly.VARIABLE_CATEGORY_NAME); } - var code = 'function ' + funcName + '(' + args.join(', ') + ')\n' + + let code = 'function ' + funcName + '(' + args.join(', ') + ')\n' + xfix1 + loopTrap + branch + xfix2 + returnValue + 'end\n'; code = Blockly.Lua.scrub_(block, code); // Add % so as not to collide with helper functions in definitions list. @@ -68,15 +68,15 @@ Blockly.Lua['procedures_defnoreturn'] = Blockly.Lua['procedures_callreturn'] = function(block) { // Call a procedure with a return value. - var funcName = Blockly.Lua.nameDB_.getName( + const funcName = Blockly.Lua.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.Lua.valueToCode(block, 'ARG' + i, Blockly.Lua.ORDER_NONE) || 'nil'; } - var code = funcName + '(' + args.join(', ') + ')'; + const code = funcName + '(' + args.join(', ') + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; @@ -84,15 +84,15 @@ Blockly.Lua['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.Lua['procedures_callreturn'](block); + const tuple = Blockly.Lua['procedures_callreturn'](block); return tuple[0] + '\n'; }; Blockly.Lua['procedures_ifreturn'] = function(block) { // Conditionally return value from a procedure. - var condition = Blockly.Lua.valueToCode(block, 'CONDITION', + const condition = Blockly.Lua.valueToCode(block, 'CONDITION', Blockly.Lua.ORDER_NONE) || 'false'; - var code = 'if ' + condition + ' then\n'; + let code = 'if ' + condition + ' then\n'; if (Blockly.Lua.STATEMENT_SUFFIX) { // Inject any statement suffix here since the regular one at the end // will not get executed if the return is triggered. @@ -101,7 +101,7 @@ Blockly.Lua['procedures_ifreturn'] = function(block) { Blockly.Lua.INDENT); } if (block.hasReturnValue_) { - var value = Blockly.Lua.valueToCode(block, 'VALUE', + const value = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_NONE) || 'nil'; code += Blockly.Lua.INDENT + 'return ' + value + '\n'; } else { diff --git a/generators/lua/text.js b/generators/lua/text.js index ce47ffc7f..035df30fc 100644 --- a/generators/lua/text.js +++ b/generators/lua/text.js @@ -16,14 +16,14 @@ goog.require('Blockly.Lua'); Blockly.Lua['text'] = function(block) { // Text value. - var code = Blockly.Lua.quote_(block.getFieldValue('TEXT')); + const code = Blockly.Lua.quote_(block.getFieldValue('TEXT')); return [code, Blockly.Lua.ORDER_ATOMIC]; }; Blockly.Lua['text_multiline'] = function(block) { // Text value. - var code = Blockly.Lua.multiline_quote_(block.getFieldValue('TEXT')); - var order = code.indexOf('..') !== -1 ? Blockly.Lua.ORDER_CONCATENATION : + const code = Blockly.Lua.multiline_quote_(block.getFieldValue('TEXT')); + const order = code.indexOf('..') !== -1 ? Blockly.Lua.ORDER_CONCATENATION : Blockly.Lua.ORDER_ATOMIC; return [code, order]; }; @@ -33,59 +33,60 @@ Blockly.Lua['text_join'] = function(block) { if (block.itemCount_ === 0) { return ['\'\'', Blockly.Lua.ORDER_ATOMIC]; } else if (block.itemCount_ === 1) { - var element = Blockly.Lua.valueToCode(block, 'ADD0', + const element = Blockly.Lua.valueToCode(block, 'ADD0', Blockly.Lua.ORDER_NONE) || '\'\''; - var code = 'tostring(' + element + ')'; + const code = 'tostring(' + element + ')'; return [code, Blockly.Lua.ORDER_HIGH]; } else if (block.itemCount_ === 2) { - var element0 = Blockly.Lua.valueToCode(block, 'ADD0', + const element0 = Blockly.Lua.valueToCode(block, 'ADD0', Blockly.Lua.ORDER_CONCATENATION) || '\'\''; - var element1 = Blockly.Lua.valueToCode(block, 'ADD1', + const element1 = Blockly.Lua.valueToCode(block, 'ADD1', Blockly.Lua.ORDER_CONCATENATION) || '\'\''; - var code = element0 + ' .. ' + element1; + const code = element0 + ' .. ' + element1; return [code, Blockly.Lua.ORDER_CONCATENATION]; } else { - var elements = []; - for (var i = 0; i < block.itemCount_; i++) { + const elements = []; + for (let i = 0; i < block.itemCount_; i++) { elements[i] = Blockly.Lua.valueToCode(block, 'ADD' + i, Blockly.Lua.ORDER_NONE) || '\'\''; } - var code = 'table.concat({' + elements.join(', ') + '})'; + const code = 'table.concat({' + elements.join(', ') + '})'; return [code, Blockly.Lua.ORDER_HIGH]; } }; Blockly.Lua['text_append'] = function(block) { // Append to a variable in place. - var varName = Blockly.Lua.nameDB_.getName( + const varName = Blockly.Lua.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - var value = Blockly.Lua.valueToCode(block, 'TEXT', + const value = Blockly.Lua.valueToCode(block, 'TEXT', Blockly.Lua.ORDER_CONCATENATION) || '\'\''; return varName + ' = ' + varName + ' .. ' + value + '\n'; }; Blockly.Lua['text_length'] = function(block) { // String or array length. - var text = Blockly.Lua.valueToCode(block, 'VALUE', + const text = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_UNARY) || '\'\''; return ['#' + text, Blockly.Lua.ORDER_UNARY]; }; Blockly.Lua['text_isEmpty'] = function(block) { // Is the string null or array empty? - var text = Blockly.Lua.valueToCode(block, 'VALUE', + const text = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_UNARY) || '\'\''; return ['#' + text + ' == 0', Blockly.Lua.ORDER_RELATIONAL]; }; Blockly.Lua['text_indexOf'] = function(block) { // Search the text for a substring. - var substring = Blockly.Lua.valueToCode(block, 'FIND', + const substring = Blockly.Lua.valueToCode(block, 'FIND', Blockly.Lua.ORDER_NONE) || '\'\''; - var text = Blockly.Lua.valueToCode(block, 'VALUE', + const text = 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_( 'firstIndexOf', ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str, substr) ', @@ -97,7 +98,7 @@ Blockly.Lua['text_indexOf'] = function(block) { ' end', 'end']); } else { - var functionName = Blockly.Lua.provideFunction_( + functionName = Blockly.Lua.provideFunction_( 'lastIndexOf', ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str, substr)', @@ -109,22 +110,22 @@ Blockly.Lua['text_indexOf'] = function(block) { ' return 0', 'end']); } - var code = functionName + '(' + text + ', ' + substring + ')'; + const code = functionName + '(' + text + ', ' + substring + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['text_charAt'] = function(block) { // Get letter at index. // Note: Until January 2013 this block did not have the WHERE input. - var where = block.getFieldValue('WHERE') || 'FROM_START'; - var atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_UNARY : + const where = block.getFieldValue('WHERE') || 'FROM_START'; + const atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_UNARY : Blockly.Lua.ORDER_NONE; - var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1'; - var text = Blockly.Lua.valueToCode(block, 'VALUE', + const at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1'; + const text = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_NONE) || '\'\''; - var code; + let code; if (where === 'RANDOM') { - var functionName = Blockly.Lua.provideFunction_( + const functionName = Blockly.Lua.provideFunction_( 'text_random_letter', ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str)', ' local index = math.random(string.len(str))', @@ -132,15 +133,16 @@ Blockly.Lua['text_charAt'] = function(block) { 'end']); code = functionName + '(' + text + ')'; } else { + let start; if (where === 'FIRST') { - var start = '1'; + start = '1'; } else if (where === 'LAST') { - var start = '-1'; + start = '-1'; } else { if (where === 'FROM_START') { - var start = at; + start = at; } else if (where === 'FROM_END') { - var start = '-' + at; + start = '-' + at; } else { throw Error('Unhandled option (text_charAt).'); } @@ -149,7 +151,7 @@ Blockly.Lua['text_charAt'] = function(block) { code = 'string.sub(' + text + ', ' + start + ', ' + start + ')'; } else { // use function to avoid reevaluation - var functionName = Blockly.Lua.provideFunction_( + const functionName = Blockly.Lua.provideFunction_( 'text_char_at', ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str, index)', @@ -163,53 +165,56 @@ Blockly.Lua['text_charAt'] = function(block) { Blockly.Lua['text_getSubstring'] = function(block) { // Get substring. - var text = Blockly.Lua.valueToCode(block, 'STRING', + const text = Blockly.Lua.valueToCode(block, 'STRING', Blockly.Lua.ORDER_NONE) || '\'\''; // Get start index. - var where1 = block.getFieldValue('WHERE1'); - var at1Order = (where1 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY : + const where1 = block.getFieldValue('WHERE1'); + const at1Order = (where1 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY : Blockly.Lua.ORDER_NONE; - var at1 = Blockly.Lua.valueToCode(block, 'AT1', at1Order) || '1'; + const at1 = Blockly.Lua.valueToCode(block, 'AT1', at1Order) || '1'; + let start; if (where1 === 'FIRST') { - var start = 1; + start = 1; } else if (where1 === 'FROM_START') { - var start = at1; + start = at1; } else if (where1 === 'FROM_END') { - var start = '-' + at1; + start = '-' + at1; } else { throw Error('Unhandled option (text_getSubstring)'); } // Get end index. - var where2 = block.getFieldValue('WHERE2'); - var at2Order = (where2 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY : + const where2 = block.getFieldValue('WHERE2'); + const at2Order = (where2 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY : Blockly.Lua.ORDER_NONE; - var at2 = Blockly.Lua.valueToCode(block, 'AT2', at2Order) || '1'; + const at2 = Blockly.Lua.valueToCode(block, 'AT2', at2Order) || '1'; + let end; if (where2 === 'LAST') { - var end = -1; + end = -1; } else if (where2 === 'FROM_START') { - var end = at2; + end = at2; } else if (where2 === 'FROM_END') { - var end = '-' + at2; + end = '-' + at2; } else { throw Error('Unhandled option (text_getSubstring)'); } - var code = 'string.sub(' + text + ', ' + start + ', ' + end + ')'; + const code = 'string.sub(' + text + ', ' + start + ', ' + end + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['text_changeCase'] = function(block) { // Change capitalization. - var operator = block.getFieldValue('CASE'); - var text = Blockly.Lua.valueToCode(block, 'TEXT', + const operator = block.getFieldValue('CASE'); + const text = Blockly.Lua.valueToCode(block, 'TEXT', Blockly.Lua.ORDER_NONE) || '\'\''; + let functionName; if (operator === 'UPPERCASE') { - var functionName = 'string.upper'; + functionName = 'string.upper'; } else if (operator === 'LOWERCASE') { - var functionName = 'string.lower'; + functionName = 'string.lower'; } else if (operator === 'TITLECASE') { - var functionName = Blockly.Lua.provideFunction_( + functionName = Blockly.Lua.provideFunction_( 'text_titlecase', // There are shorter versions at // http://lua-users.org/wiki/SciteTitleCase @@ -232,52 +237,53 @@ Blockly.Lua['text_changeCase'] = function(block) { ' return table.concat(buf)', 'end']); } - var code = functionName + '(' + text + ')'; + const code = functionName + '(' + text + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['text_trim'] = function(block) { // Trim spaces. - var OPERATORS = { + const OPERATORS = { LEFT: '^%s*(,-)', RIGHT: '(.-)%s*$', BOTH: '^%s*(.-)%s*$' }; - var operator = OPERATORS[block.getFieldValue('MODE')]; - var text = Blockly.Lua.valueToCode(block, 'TEXT', + const operator = OPERATORS[block.getFieldValue('MODE')]; + const text = Blockly.Lua.valueToCode(block, 'TEXT', Blockly.Lua.ORDER_NONE) || '\'\''; - var code = 'string.gsub(' + text + ', "' + operator + '", "%1")'; + const code = 'string.gsub(' + text + ', "' + operator + '", "%1")'; return [code, Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['text_print'] = function(block) { // Print statement. - var msg = Blockly.Lua.valueToCode(block, 'TEXT', + const msg = Blockly.Lua.valueToCode(block, 'TEXT', Blockly.Lua.ORDER_NONE) || '\'\''; return 'print(' + msg + ')\n'; }; Blockly.Lua['text_prompt_ext'] = function(block) { // Prompt function. + let msg; if (block.getField('TEXT')) { // Internal message. - var msg = Blockly.Lua.quote_(block.getFieldValue('TEXT')); + msg = Blockly.Lua.quote_(block.getFieldValue('TEXT')); } else { // External message. - var msg = Blockly.Lua.valueToCode(block, 'TEXT', + msg = Blockly.Lua.valueToCode(block, 'TEXT', Blockly.Lua.ORDER_NONE) || '\'\''; } - var functionName = Blockly.Lua.provideFunction_( + const functionName = Blockly.Lua.provideFunction_( 'text_prompt', ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(msg)', ' io.write(msg)', ' io.flush()', ' return io.read()', 'end']); - var code = functionName + '(' + msg + ')'; + let code = functionName + '(' + msg + ')'; - var toNumber = block.getFieldValue('TYPE') === 'NUMBER'; + const toNumber = block.getFieldValue('TYPE') === 'NUMBER'; if (toNumber) { code = 'tonumber(' + code + ', 10)'; } @@ -287,11 +293,11 @@ Blockly.Lua['text_prompt_ext'] = function(block) { Blockly.Lua['text_prompt'] = Blockly.Lua['text_prompt_ext']; Blockly.Lua['text_count'] = function(block) { - var text = Blockly.Lua.valueToCode(block, 'TEXT', + const text = Blockly.Lua.valueToCode(block, 'TEXT', Blockly.Lua.ORDER_NONE) || '\'\''; - var sub = Blockly.Lua.valueToCode(block, 'SUB', + const sub = Blockly.Lua.valueToCode(block, 'SUB', Blockly.Lua.ORDER_NONE) || '\'\''; - var functionName = Blockly.Lua.provideFunction_( + const functionName = Blockly.Lua.provideFunction_( 'text_count', ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(haystack, needle)', @@ -311,18 +317,18 @@ Blockly.Lua['text_count'] = function(block) { ' return count', 'end', ]); - var code = functionName + '(' + text + ', ' + sub + ')'; + const code = functionName + '(' + text + ', ' + sub + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['text_replace'] = function(block) { - var text = Blockly.Lua.valueToCode(block, 'TEXT', + const text = Blockly.Lua.valueToCode(block, 'TEXT', Blockly.Lua.ORDER_NONE) || '\'\''; - var from = Blockly.Lua.valueToCode(block, 'FROM', + const from = Blockly.Lua.valueToCode(block, 'FROM', Blockly.Lua.ORDER_NONE) || '\'\''; - var to = Blockly.Lua.valueToCode(block, 'TO', + const to = Blockly.Lua.valueToCode(block, 'TO', Blockly.Lua.ORDER_NONE) || '\'\''; - var functionName = Blockly.Lua.provideFunction_( + const functionName = Blockly.Lua.provideFunction_( 'text_replace', ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(haystack, needle, replacement)', @@ -342,13 +348,13 @@ Blockly.Lua['text_replace'] = function(block) { ' return table.concat(buf)', 'end', ]); - var code = functionName + '(' + text + ', ' + from + ', ' + to + ')'; + const code = functionName + '(' + text + ', ' + from + ', ' + to + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['text_reverse'] = function(block) { - var text = Blockly.Lua.valueToCode(block, 'TEXT', + const text = Blockly.Lua.valueToCode(block, 'TEXT', Blockly.Lua.ORDER_NONE) || '\'\''; - var code = 'string.reverse(' + text + ')'; + const code = 'string.reverse(' + text + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; diff --git a/generators/lua/variables.js b/generators/lua/variables.js index 558117489..c623984ee 100644 --- a/generators/lua/variables.js +++ b/generators/lua/variables.js @@ -16,16 +16,16 @@ goog.require('Blockly.Lua'); Blockly.Lua['variables_get'] = function(block) { // Variable getter. - var code = Blockly.Lua.nameDB_.getName(block.getFieldValue('VAR'), + const code = Blockly.Lua.nameDB_.getName(block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); return [code, Blockly.Lua.ORDER_ATOMIC]; }; Blockly.Lua['variables_set'] = function(block) { // Variable setter. - var argument0 = Blockly.Lua.valueToCode(block, 'VALUE', + const argument0 = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_NONE) || '0'; - var varName = Blockly.Lua.nameDB_.getName( + const varName = Blockly.Lua.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); return varName + ' = ' + argument0 + '\n'; }; diff --git a/tests/deps.mocha.js b/tests/deps.mocha.js index 26fe5538f..f2849275a 100644 --- a/tests/deps.mocha.js +++ b/tests/deps.mocha.js @@ -282,14 +282,14 @@ goog.addDependency('../../generators/javascript/text.js', ['Blockly.JavaScript.t goog.addDependency('../../generators/javascript/variables.js', ['Blockly.JavaScript.variables'], ['Blockly.JavaScript'], {'lang': 'es6'}); goog.addDependency('../../generators/javascript/variables_dynamic.js', ['Blockly.JavaScript.variablesDynamic'], ['Blockly.JavaScript', 'Blockly.JavaScript.variables']); goog.addDependency('../../generators/lua.js', ['Blockly.Lua'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'}); -goog.addDependency('../../generators/lua/colour.js', ['Blockly.Lua.colour'], ['Blockly.Lua']); -goog.addDependency('../../generators/lua/lists.js', ['Blockly.Lua.lists'], ['Blockly.Lua']); -goog.addDependency('../../generators/lua/logic.js', ['Blockly.Lua.logic'], ['Blockly.Lua']); -goog.addDependency('../../generators/lua/loops.js', ['Blockly.Lua.loops'], ['Blockly.Lua']); -goog.addDependency('../../generators/lua/math.js', ['Blockly.Lua.math'], ['Blockly.Lua']); -goog.addDependency('../../generators/lua/procedures.js', ['Blockly.Lua.procedures'], ['Blockly.Lua']); -goog.addDependency('../../generators/lua/text.js', ['Blockly.Lua.texts'], ['Blockly.Lua'], {'lang': 'es5'}); -goog.addDependency('../../generators/lua/variables.js', ['Blockly.Lua.variables'], ['Blockly.Lua']); +goog.addDependency('../../generators/lua/colour.js', ['Blockly.Lua.colour'], ['Blockly.Lua'], {'lang': 'es6'}); +goog.addDependency('../../generators/lua/lists.js', ['Blockly.Lua.lists'], ['Blockly.Lua'], {'lang': 'es6'}); +goog.addDependency('../../generators/lua/logic.js', ['Blockly.Lua.logic'], ['Blockly.Lua'], {'lang': 'es6'}); +goog.addDependency('../../generators/lua/loops.js', ['Blockly.Lua.loops'], ['Blockly.Lua'], {'lang': 'es6'}); +goog.addDependency('../../generators/lua/math.js', ['Blockly.Lua.math'], ['Blockly.Lua'], {'lang': 'es6'}); +goog.addDependency('../../generators/lua/procedures.js', ['Blockly.Lua.procedures'], ['Blockly.Lua'], {'lang': 'es6'}); +goog.addDependency('../../generators/lua/text.js', ['Blockly.Lua.texts'], ['Blockly.Lua'], {'lang': 'es6'}); +goog.addDependency('../../generators/lua/variables.js', ['Blockly.Lua.variables'], ['Blockly.Lua'], {'lang': 'es6'}); goog.addDependency('../../generators/lua/variables_dynamic.js', ['Blockly.Lua.variablesDynamic'], ['Blockly.Lua', 'Blockly.Lua.variables']); goog.addDependency('../../generators/php.js', ['Blockly.PHP'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'}); goog.addDependency('../../generators/php/colour.js', ['Blockly.PHP.colour'], ['Blockly.PHP'], {'lang': 'es6'});