From de95ffd13362a00b08c0a99317f5f634252f99b3 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Wed, 3 Nov 2021 10:27:13 -0700 Subject: [PATCH] chore: update javascript block generators to const and let (#5661) --- generators/javascript/colour.js | 26 ++-- generators/javascript/lists.js | 160 +++++++++++++------------ generators/javascript/logic.js | 50 ++++---- generators/javascript/loops.js | 57 ++++----- generators/javascript/math.js | 99 ++++++++------- generators/javascript/procedures.js | 38 +++--- generators/javascript/text.js | 179 +++++++++++++++------------- generators/javascript/variables.js | 9 +- tests/deps.mocha.js | 24 ++-- 9 files changed, 337 insertions(+), 305 deletions(-) diff --git a/generators/javascript/colour.js b/generators/javascript/colour.js index 894e66f4028..1d204c64319 100644 --- a/generators/javascript/colour.js +++ b/generators/javascript/colour.js @@ -16,31 +16,31 @@ goog.require('Blockly.JavaScript'); Blockly.JavaScript['colour_picker'] = function(block) { // Colour picker. - var code = Blockly.JavaScript.quote_(block.getFieldValue('COLOUR')); + const code = Blockly.JavaScript.quote_(block.getFieldValue('COLOUR')); return [code, Blockly.JavaScript.ORDER_ATOMIC]; }; Blockly.JavaScript['colour_random'] = function(block) { // Generate a random colour. - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'colourRandom', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '() {', ' var num = Math.floor(Math.random() * Math.pow(2, 24));', ' return \'#\' + (\'00000\' + num.toString(16)).substr(-6);', '}']); - var code = functionName + '()'; + const code = functionName + '()'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['colour_rgb'] = function(block) { // Compose a colour from RGB components expressed as percentages. - var red = Blockly.JavaScript.valueToCode(block, 'RED', + const red = Blockly.JavaScript.valueToCode(block, 'RED', Blockly.JavaScript.ORDER_NONE) || 0; - var green = Blockly.JavaScript.valueToCode(block, 'GREEN', + const green = Blockly.JavaScript.valueToCode(block, 'GREEN', Blockly.JavaScript.ORDER_NONE) || 0; - var blue = Blockly.JavaScript.valueToCode(block, 'BLUE', + const blue = Blockly.JavaScript.valueToCode(block, 'BLUE', Blockly.JavaScript.ORDER_NONE) || 0; - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'colourRgb', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b) {', @@ -52,19 +52,19 @@ Blockly.JavaScript['colour_rgb'] = function(block) { ' b = (\'0\' + (Math.round(b) || 0).toString(16)).slice(-2);', ' return \'#\' + r + g + b;', '}']); - var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')'; + const code = functionName + '(' + red + ', ' + green + ', ' + blue + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['colour_blend'] = function(block) { // Blend two colours together. - var c1 = Blockly.JavaScript.valueToCode(block, 'COLOUR1', + const c1 = Blockly.JavaScript.valueToCode(block, 'COLOUR1', Blockly.JavaScript.ORDER_NONE) || '\'#000000\''; - var c2 = Blockly.JavaScript.valueToCode(block, 'COLOUR2', + const c2 = Blockly.JavaScript.valueToCode(block, 'COLOUR2', Blockly.JavaScript.ORDER_NONE) || '\'#000000\''; - var ratio = Blockly.JavaScript.valueToCode(block, 'RATIO', + const ratio = Blockly.JavaScript.valueToCode(block, 'RATIO', Blockly.JavaScript.ORDER_NONE) || 0.5; - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'colourBlend', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(c1, c2, ratio) {', @@ -83,6 +83,6 @@ Blockly.JavaScript['colour_blend'] = function(block) { ' b = (\'0\' + (b || 0).toString(16)).slice(-2);', ' return \'#\' + r + g + b;', '}']); - var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')'; + const code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; diff --git a/generators/javascript/lists.js b/generators/javascript/lists.js index 238b0999870..c85a45968f4 100644 --- a/generators/javascript/lists.js +++ b/generators/javascript/lists.js @@ -21,18 +21,18 @@ Blockly.JavaScript['lists_create_empty'] = function(block) { Blockly.JavaScript['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.JavaScript.valueToCode(block, 'ADD' + i, Blockly.JavaScript.ORDER_NONE) || 'null'; } - var code = '[' + elements.join(', ') + ']'; + const code = '[' + elements.join(', ') + ']'; return [code, Blockly.JavaScript.ORDER_ATOMIC]; }; Blockly.JavaScript['lists_repeat'] = function(block) { // Create a list with one element repeated. - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'listsRepeat', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(value, n) {', @@ -42,37 +42,37 @@ Blockly.JavaScript['lists_repeat'] = function(block) { ' }', ' return array;', '}']); - var element = Blockly.JavaScript.valueToCode(block, 'ITEM', + const element = Blockly.JavaScript.valueToCode(block, 'ITEM', Blockly.JavaScript.ORDER_NONE) || 'null'; - var repeatCount = Blockly.JavaScript.valueToCode(block, 'NUM', + const repeatCount = Blockly.JavaScript.valueToCode(block, 'NUM', Blockly.JavaScript.ORDER_NONE) || '0'; - var code = functionName + '(' + element + ', ' + repeatCount + ')'; + const code = functionName + '(' + element + ', ' + repeatCount + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['lists_length'] = function(block) { // String or array length. - var list = Blockly.JavaScript.valueToCode(block, 'VALUE', + const list = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_MEMBER) || '[]'; return [list + '.length', Blockly.JavaScript.ORDER_MEMBER]; }; Blockly.JavaScript['lists_isEmpty'] = function(block) { // Is the string null or array empty? - var list = Blockly.JavaScript.valueToCode(block, 'VALUE', + const list = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_MEMBER) || '[]'; return ['!' + list + '.length', Blockly.JavaScript.ORDER_LOGICAL_NOT]; }; Blockly.JavaScript['lists_indexOf'] = function(block) { // Find an item in the list. - var operator = block.getFieldValue('END') === 'FIRST' ? + const operator = block.getFieldValue('END') === 'FIRST' ? 'indexOf' : 'lastIndexOf'; - var item = Blockly.JavaScript.valueToCode(block, 'FIND', + const item = Blockly.JavaScript.valueToCode(block, 'FIND', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var list = Blockly.JavaScript.valueToCode(block, 'VALUE', + const list = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_MEMBER) || '[]'; - var code = list + '.' + operator + '(' + item + ')'; + const code = list + '.' + operator + '(' + item + ')'; if (block.workspace.options.oneBasedIndex) { return [code + ' + 1', Blockly.JavaScript.ORDER_ADDITION]; } @@ -82,19 +82,19 @@ Blockly.JavaScript['lists_indexOf'] = function(block) { Blockly.JavaScript['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 listOrder = (where === 'RANDOM') ? Blockly.JavaScript.ORDER_NONE : + const mode = block.getFieldValue('MODE') || 'GET'; + const where = block.getFieldValue('WHERE') || 'FROM_START'; + const listOrder = (where === 'RANDOM') ? Blockly.JavaScript.ORDER_NONE : Blockly.JavaScript.ORDER_MEMBER; - var list = Blockly.JavaScript.valueToCode(block, 'VALUE', listOrder) || '[]'; + const list = Blockly.JavaScript.valueToCode(block, 'VALUE', listOrder) || '[]'; switch (where) { case ('FIRST'): if (mode === 'GET') { - var code = list + '[0]'; + const code = list + '[0]'; return [code, Blockly.JavaScript.ORDER_MEMBER]; } else if (mode === 'GET_REMOVE') { - var code = list + '.shift()'; + const code = list + '.shift()'; return [code, Blockly.JavaScript.ORDER_MEMBER]; } else if (mode === 'REMOVE') { return list + '.shift();\n'; @@ -102,41 +102,43 @@ Blockly.JavaScript['lists_getIndex'] = function(block) { break; case ('LAST'): if (mode === 'GET') { - var code = list + '.slice(-1)[0]'; + const code = list + '.slice(-1)[0]'; return [code, Blockly.JavaScript.ORDER_MEMBER]; } else if (mode === 'GET_REMOVE') { - var code = list + '.pop()'; + const code = list + '.pop()'; return [code, Blockly.JavaScript.ORDER_MEMBER]; } else if (mode === 'REMOVE') { return list + '.pop();\n'; } break; - case ('FROM_START'): - var at = Blockly.JavaScript.getAdjusted(block, 'AT'); + case ('FROM_START'): { + const at = Blockly.JavaScript.getAdjusted(block, 'AT'); if (mode === 'GET') { - var code = list + '[' + at + ']'; + const code = list + '[' + at + ']'; return [code, Blockly.JavaScript.ORDER_MEMBER]; } else if (mode === 'GET_REMOVE') { - var code = list + '.splice(' + at + ', 1)[0]'; + const code = list + '.splice(' + at + ', 1)[0]'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; } else if (mode === 'REMOVE') { return list + '.splice(' + at + ', 1);\n'; } break; - case ('FROM_END'): - var at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, true); + } + case ('FROM_END'): { + const at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, true); if (mode === 'GET') { - var code = list + '.slice(' + at + ')[0]'; + const code = list + '.slice(' + at + ')[0]'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; } else if (mode === 'GET_REMOVE') { - var code = list + '.splice(' + at + ', 1)[0]'; + const code = list + '.splice(' + at + ', 1)[0]'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; } else if (mode === 'REMOVE') { return list + '.splice(' + at + ', 1);'; } break; - case ('RANDOM'): - var functionName = Blockly.JavaScript.provideFunction_( + } + case ('RANDOM'): { + const functionName = Blockly.JavaScript.provideFunction_( 'listsGetRandomItem', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(list, remove) {', @@ -147,13 +149,14 @@ Blockly.JavaScript['lists_getIndex'] = function(block) { ' return list[x];', ' }', '}']); - code = functionName + '(' + list + ', ' + (mode !== 'GET') + ')'; + const code = functionName + '(' + list + ', ' + (mode !== 'GET') + ')'; if (mode === 'GET' || mode === 'GET_REMOVE') { return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; } else if (mode === 'REMOVE') { return code + ';\n'; } break; + } } throw Error('Unhandled combination (lists_getIndex).'); }; @@ -161,11 +164,11 @@ Blockly.JavaScript['lists_getIndex'] = function(block) { Blockly.JavaScript['lists_setIndex'] = function(block) { // Set element at index. // Note: Until February 2013 this block did not have MODE or WHERE inputs. - var list = Blockly.JavaScript.valueToCode(block, 'LIST', + let list = Blockly.JavaScript.valueToCode(block, 'LIST', Blockly.JavaScript.ORDER_MEMBER) || '[]'; - var mode = block.getFieldValue('MODE') || 'GET'; - var where = block.getFieldValue('WHERE') || 'FROM_START'; - var value = Blockly.JavaScript.valueToCode(block, 'TO', + const mode = block.getFieldValue('MODE') || 'GET'; + const where = block.getFieldValue('WHERE') || 'FROM_START'; + const value = Blockly.JavaScript.valueToCode(block, 'TO', Blockly.JavaScript.ORDER_ASSIGNMENT) || 'null'; // Cache non-trivial values to variables to prevent repeated look-ups. // Closure, which accesses and modifies 'list'. @@ -173,9 +176,9 @@ Blockly.JavaScript['lists_setIndex'] = function(block) { if (list.match(/^\w+$/)) { return ''; } - var listVar = Blockly.JavaScript.nameDB_.getDistinctName( + const listVar = Blockly.JavaScript.nameDB_.getDistinctName( 'tmpList', Blockly.VARIABLE_CATEGORY_NAME); - var code = 'var ' + listVar + ' = ' + list + ';\n'; + const code = 'var ' + listVar + ' = ' + list + ';\n'; list = listVar; return code; } @@ -189,25 +192,26 @@ Blockly.JavaScript['lists_setIndex'] = function(block) { break; case ('LAST'): if (mode === 'SET') { - var code = cacheList(); + let code = cacheList(); code += list + '[' + list + '.length - 1] = ' + value + ';\n'; return code; } else if (mode === 'INSERT') { return list + '.push(' + value + ');\n'; } break; - case ('FROM_START'): - var at = Blockly.JavaScript.getAdjusted(block, 'AT'); + case ('FROM_START'): { + const at = Blockly.JavaScript.getAdjusted(block, 'AT'); if (mode === 'SET') { return list + '[' + at + '] = ' + value + ';\n'; } else if (mode === 'INSERT') { return list + '.splice(' + at + ', 0, ' + value + ');\n'; } break; - case ('FROM_END'): - var at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, false, + } + case ('FROM_END'): { + const at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, false, Blockly.JavaScript.ORDER_SUBTRACTION); - var code = cacheList(); + let code = cacheList(); if (mode === 'SET') { code += list + '[' + list + '.length - ' + at + '] = ' + value + ';\n'; return code; @@ -217,9 +221,10 @@ Blockly.JavaScript['lists_setIndex'] = function(block) { return code; } break; - case ('RANDOM'): - var code = cacheList(); - var xVar = Blockly.JavaScript.nameDB_.getDistinctName( + } + case ('RANDOM'): { + let code = cacheList(); + const xVar = Blockly.JavaScript.nameDB_.getDistinctName( 'tmpX', Blockly.VARIABLE_CATEGORY_NAME); code += 'var ' + xVar + ' = Math.floor(Math.random() * ' + list + '.length);\n'; @@ -231,6 +236,7 @@ Blockly.JavaScript['lists_setIndex'] = function(block) { return code; } break; + } } throw Error('Unhandled combination (lists_setIndex).'); }; @@ -257,54 +263,57 @@ Blockly.JavaScript.lists.getIndex_ = function(listName, where, opt_at) { Blockly.JavaScript['lists_getSublist'] = function(block) { // Get sublist. - var list = Blockly.JavaScript.valueToCode(block, 'LIST', + const list = Blockly.JavaScript.valueToCode(block, 'LIST', Blockly.JavaScript.ORDER_MEMBER) || '[]'; - var where1 = block.getFieldValue('WHERE1'); - var where2 = block.getFieldValue('WHERE2'); + const where1 = block.getFieldValue('WHERE1'); + const where2 = block.getFieldValue('WHERE2'); + let code; if (where1 === 'FIRST' && where2 === 'LAST') { - var code = list + '.slice(0)'; + code = list + '.slice(0)'; } else if (list.match(/^\w+$/) || (where1 !== 'FROM_END' && where2 === 'FROM_START')) { // If the list is a variable or doesn't require a call for length, don't // generate a helper function. + let at1; switch (where1) { case 'FROM_START': - var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1'); + at1 = Blockly.JavaScript.getAdjusted(block, 'AT1'); break; case 'FROM_END': - var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1', 1, false, + at1 = Blockly.JavaScript.getAdjusted(block, 'AT1', 1, false, Blockly.JavaScript.ORDER_SUBTRACTION); at1 = list + '.length - ' + at1; break; case 'FIRST': - var at1 = '0'; + at1 = '0'; break; default: throw Error('Unhandled option (lists_getSublist).'); } + let at2; switch (where2) { case 'FROM_START': - var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 1); + at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 1); break; case 'FROM_END': - var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 0, false, + at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 0, false, Blockly.JavaScript.ORDER_SUBTRACTION); at2 = list + '.length - ' + at2; break; case 'LAST': - var at2 = list + '.length'; + at2 = list + '.length'; break; default: throw Error('Unhandled option (lists_getSublist).'); } code = list + '.slice(' + at1 + ', ' + at2 + ')'; } else { - var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1'); - var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2'); - var getIndex_ = Blockly.JavaScript.lists.getIndex_; - var wherePascalCase = {'FIRST': 'First', 'LAST': 'Last', + const at1 = Blockly.JavaScript.getAdjusted(block, 'AT1'); + const at2 = Blockly.JavaScript.getAdjusted(block, 'AT2'); + const getIndex_ = Blockly.JavaScript.lists.getIndex_; + const wherePascalCase = {'FIRST': 'First', 'LAST': 'Last', 'FROM_START': 'FromStart', 'FROM_END': 'FromEnd'}; - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'subsequence' + wherePascalCase[where1] + wherePascalCase[where2], ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(sequence' + @@ -317,7 +326,7 @@ Blockly.JavaScript['lists_getSublist'] = function(block) { ' var end = ' + getIndex_('sequence', where2, 'at2') + ' + 1;', ' return sequence.slice(start, end);', '}']); - var code = functionName + '(' + list + + 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 : '') + @@ -329,11 +338,11 @@ Blockly.JavaScript['lists_getSublist'] = function(block) { Blockly.JavaScript['lists_sort'] = function(block) { // Block for sorting a list. - var list = Blockly.JavaScript.valueToCode(block, 'LIST', + const list = Blockly.JavaScript.valueToCode(block, 'LIST', Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]'; - var direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1; - var type = block.getFieldValue('TYPE'); - var getCompareFunctionName = Blockly.JavaScript.provideFunction_( + const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1; + const type = block.getFieldValue('TYPE'); + const getCompareFunctionName = Blockly.JavaScript.provideFunction_( 'listsGetSortCompare', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(type, direction) {', @@ -356,32 +365,33 @@ Blockly.JavaScript['lists_sort'] = function(block) { Blockly.JavaScript['lists_split'] = function(block) { // Block for splitting text into a list, or joining a list into text. - var input = Blockly.JavaScript.valueToCode(block, 'INPUT', + let input = Blockly.JavaScript.valueToCode(block, 'INPUT', Blockly.JavaScript.ORDER_MEMBER); - var delimiter = Blockly.JavaScript.valueToCode(block, 'DELIM', + const delimiter = Blockly.JavaScript.valueToCode(block, 'DELIM', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var mode = block.getFieldValue('MODE'); + const mode = block.getFieldValue('MODE'); + let functionName; if (mode === 'SPLIT') { if (!input) { input = '\'\''; } - var functionName = 'split'; + functionName = 'split'; } else if (mode === 'JOIN') { if (!input) { input = '[]'; } - var functionName = 'join'; + functionName = 'join'; } else { throw Error('Unknown mode: ' + mode); } - var code = input + '.' + functionName + '(' + delimiter + ')'; + const code = input + '.' + functionName + '(' + delimiter + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['lists_reverse'] = function(block) { // Block for reversing a list. - var list = Blockly.JavaScript.valueToCode(block, 'LIST', + const list = Blockly.JavaScript.valueToCode(block, 'LIST', Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]'; - var code = list + '.slice().reverse()'; + const code = list + '.slice().reverse()'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; diff --git a/generators/javascript/logic.js b/generators/javascript/logic.js index 4162ea06ad0..a769a8709b4 100644 --- a/generators/javascript/logic.js +++ b/generators/javascript/logic.js @@ -16,17 +16,17 @@ goog.require('Blockly.JavaScript'); Blockly.JavaScript['controls_if'] = function(block) { // If/elseif/else condition. - var n = 0; - var code = '', branchCode, conditionCode; + let n = 0; + let code = ''; if (Blockly.JavaScript.STATEMENT_PREFIX) { // Automatic prefix insertion is switched off for this block. Add manually. code += Blockly.JavaScript.injectId(Blockly.JavaScript.STATEMENT_PREFIX, block); } do { - conditionCode = Blockly.JavaScript.valueToCode(block, 'IF' + n, + const conditionCode = Blockly.JavaScript.valueToCode(block, 'IF' + n, Blockly.JavaScript.ORDER_NONE) || 'false'; - branchCode = Blockly.JavaScript.statementToCode(block, 'DO' + n); + let branchCode = Blockly.JavaScript.statementToCode(block, 'DO' + n); if (Blockly.JavaScript.STATEMENT_SUFFIX) { branchCode = Blockly.JavaScript.prefixLines( Blockly.JavaScript.injectId(Blockly.JavaScript.STATEMENT_SUFFIX, @@ -38,7 +38,7 @@ Blockly.JavaScript['controls_if'] = function(block) { } while (block.getInput('IF' + n)); if (block.getInput('ELSE') || Blockly.JavaScript.STATEMENT_SUFFIX) { - branchCode = Blockly.JavaScript.statementToCode(block, 'ELSE'); + let branchCode = Blockly.JavaScript.statementToCode(block, 'ELSE'); if (Blockly.JavaScript.STATEMENT_SUFFIX) { branchCode = Blockly.JavaScript.prefixLines( Blockly.JavaScript.injectId(Blockly.JavaScript.STATEMENT_SUFFIX, @@ -53,7 +53,7 @@ Blockly.JavaScript['controls_ifelse'] = Blockly.JavaScript['controls_if']; Blockly.JavaScript['logic_compare'] = function(block) { // Comparison operator. - var OPERATORS = { + const OPERATORS = { 'EQ': '==', 'NEQ': '!=', 'LT': '<', @@ -61,29 +61,29 @@ Blockly.JavaScript['logic_compare'] = function(block) { 'GT': '>', 'GTE': '>=' }; - var operator = OPERATORS[block.getFieldValue('OP')]; - var order = (operator === '==' || operator === '!=') ? + const operator = OPERATORS[block.getFieldValue('OP')]; + const order = (operator === '==' || operator === '!=') ? Blockly.JavaScript.ORDER_EQUALITY : Blockly.JavaScript.ORDER_RELATIONAL; - var argument0 = Blockly.JavaScript.valueToCode(block, 'A', order) || '0'; - var argument1 = Blockly.JavaScript.valueToCode(block, 'B', order) || '0'; - var code = argument0 + ' ' + operator + ' ' + argument1; + const argument0 = Blockly.JavaScript.valueToCode(block, 'A', order) || '0'; + const argument1 = Blockly.JavaScript.valueToCode(block, 'B', order) || '0'; + const code = argument0 + ' ' + operator + ' ' + argument1; return [code, order]; }; Blockly.JavaScript['logic_operation'] = function(block) { // Operations 'and', 'or'. - var operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||'; - var order = (operator === '&&') ? Blockly.JavaScript.ORDER_LOGICAL_AND : + const operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||'; + const order = (operator === '&&') ? Blockly.JavaScript.ORDER_LOGICAL_AND : Blockly.JavaScript.ORDER_LOGICAL_OR; - var argument0 = Blockly.JavaScript.valueToCode(block, 'A', order); - var argument1 = Blockly.JavaScript.valueToCode(block, 'B', order); + let argument0 = Blockly.JavaScript.valueToCode(block, 'A', order); + let argument1 = Blockly.JavaScript.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 === '&&') ? 'true' : 'false'; + const defaultArgument = (operator === '&&') ? 'true' : 'false'; if (!argument0) { argument0 = defaultArgument; } @@ -91,22 +91,22 @@ Blockly.JavaScript['logic_operation'] = function(block) { argument1 = defaultArgument; } } - var code = argument0 + ' ' + operator + ' ' + argument1; + const code = argument0 + ' ' + operator + ' ' + argument1; return [code, order]; }; Blockly.JavaScript['logic_negate'] = function(block) { // Negation. - var order = Blockly.JavaScript.ORDER_LOGICAL_NOT; - var argument0 = Blockly.JavaScript.valueToCode(block, 'BOOL', order) || + const order = Blockly.JavaScript.ORDER_LOGICAL_NOT; + const argument0 = Blockly.JavaScript.valueToCode(block, 'BOOL', order) || 'true'; - var code = '!' + argument0; + const code = '!' + argument0; return [code, order]; }; Blockly.JavaScript['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.JavaScript.ORDER_ATOMIC]; }; @@ -117,12 +117,12 @@ Blockly.JavaScript['logic_null'] = function(block) { Blockly.JavaScript['logic_ternary'] = function(block) { // Ternary operator. - var value_if = Blockly.JavaScript.valueToCode(block, 'IF', + const value_if = Blockly.JavaScript.valueToCode(block, 'IF', Blockly.JavaScript.ORDER_CONDITIONAL) || 'false'; - var value_then = Blockly.JavaScript.valueToCode(block, 'THEN', + const value_then = Blockly.JavaScript.valueToCode(block, 'THEN', Blockly.JavaScript.ORDER_CONDITIONAL) || 'null'; - var value_else = Blockly.JavaScript.valueToCode(block, 'ELSE', + const value_else = Blockly.JavaScript.valueToCode(block, 'ELSE', Blockly.JavaScript.ORDER_CONDITIONAL) || 'null'; - var code = value_if + ' ? ' + value_then + ' : ' + value_else; + const code = value_if + ' ? ' + value_then + ' : ' + value_else; return [code, Blockly.JavaScript.ORDER_CONDITIONAL]; }; diff --git a/generators/javascript/loops.js b/generators/javascript/loops.js index 7da6ccde5ec..47975698e48 100644 --- a/generators/javascript/loops.js +++ b/generators/javascript/loops.js @@ -16,20 +16,21 @@ goog.require('Blockly.JavaScript'); Blockly.JavaScript['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.JavaScript.valueToCode(block, 'TIMES', + repeats = Blockly.JavaScript.valueToCode(block, 'TIMES', Blockly.JavaScript.ORDER_ASSIGNMENT) || '0'; } - var branch = Blockly.JavaScript.statementToCode(block, 'DO'); + let branch = Blockly.JavaScript.statementToCode(block, 'DO'); branch = Blockly.JavaScript.addLoopTrap(branch, block); - var code = ''; - var loopVar = Blockly.JavaScript.nameDB_.getDistinctName( + let code = ''; + const loopVar = Blockly.JavaScript.nameDB_.getDistinctName( 'count', Blockly.VARIABLE_CATEGORY_NAME); - var endVar = repeats; + let endVar = repeats; if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) { endVar = Blockly.JavaScript.nameDB_.getDistinctName( 'repeat_end', Blockly.VARIABLE_CATEGORY_NAME); @@ -47,11 +48,11 @@ Blockly.JavaScript['controls_repeat'] = Blockly.JavaScript['controls_whileUntil'] = function(block) { // Do while/until loop. - var until = block.getFieldValue('MODE') === 'UNTIL'; - var argument0 = Blockly.JavaScript.valueToCode(block, 'BOOL', + const until = block.getFieldValue('MODE') === 'UNTIL'; + let argument0 = Blockly.JavaScript.valueToCode(block, 'BOOL', until ? Blockly.JavaScript.ORDER_LOGICAL_NOT : Blockly.JavaScript.ORDER_NONE) || 'false'; - var branch = Blockly.JavaScript.statementToCode(block, 'DO'); + let branch = Blockly.JavaScript.statementToCode(block, 'DO'); branch = Blockly.JavaScript.addLoopTrap(branch, block); if (until) { argument0 = '!' + argument0; @@ -61,25 +62,25 @@ Blockly.JavaScript['controls_whileUntil'] = function(block) { Blockly.JavaScript['controls_for'] = function(block) { // For loop. - var variable0 = Blockly.JavaScript.nameDB_.getName( + const variable0 = Blockly.JavaScript.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - var argument0 = Blockly.JavaScript.valueToCode(block, 'FROM', + const argument0 = Blockly.JavaScript.valueToCode(block, 'FROM', Blockly.JavaScript.ORDER_ASSIGNMENT) || '0'; - var argument1 = Blockly.JavaScript.valueToCode(block, 'TO', + const argument1 = Blockly.JavaScript.valueToCode(block, 'TO', Blockly.JavaScript.ORDER_ASSIGNMENT) || '0'; - var increment = Blockly.JavaScript.valueToCode(block, 'BY', + const increment = Blockly.JavaScript.valueToCode(block, 'BY', Blockly.JavaScript.ORDER_ASSIGNMENT) || '1'; - var branch = Blockly.JavaScript.statementToCode(block, 'DO'); + let branch = Blockly.JavaScript.statementToCode(block, 'DO'); branch = Blockly.JavaScript.addLoopTrap(branch, block); - var code; + let code; if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) && Blockly.isNumber(increment)) { // All arguments are simple numbers. - var up = Number(argument0) <= Number(argument1); + const up = Number(argument0) <= Number(argument1); code = 'for (' + variable0 + ' = ' + argument0 + '; ' + variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' + variable0; - var step = Math.abs(Number(increment)); + const step = Math.abs(Number(increment)); if (step === 1) { code += up ? '++' : '--'; } else { @@ -89,13 +90,13 @@ Blockly.JavaScript['controls_for'] = function(block) { } else { code = ''; // Cache non-trivial values to variables to prevent repeated look-ups. - var startVar = argument0; + let startVar = argument0; if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) { startVar = Blockly.JavaScript.nameDB_.getDistinctName( variable0 + '_start', Blockly.VARIABLE_CATEGORY_NAME); code += 'var ' + startVar + ' = ' + argument0 + ';\n'; } - var endVar = argument1; + let endVar = argument1; if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) { endVar = Blockly.JavaScript.nameDB_.getDistinctName( variable0 + '_end', Blockly.VARIABLE_CATEGORY_NAME); @@ -103,7 +104,7 @@ Blockly.JavaScript['controls_for'] = function(block) { } // Determine loop direction at start, in case one of the bounds // changes during loop execution. - var incVar = Blockly.JavaScript.nameDB_.getDistinctName( + const incVar = Blockly.JavaScript.nameDB_.getDistinctName( variable0 + '_inc', Blockly.VARIABLE_CATEGORY_NAME); code += 'var ' + incVar + ' = '; if (Blockly.isNumber(increment)) { @@ -126,21 +127,21 @@ Blockly.JavaScript['controls_for'] = function(block) { Blockly.JavaScript['controls_forEach'] = function(block) { // For each loop. - var variable0 = Blockly.JavaScript.nameDB_.getName( + const variable0 = Blockly.JavaScript.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - var argument0 = Blockly.JavaScript.valueToCode(block, 'LIST', + const argument0 = Blockly.JavaScript.valueToCode(block, 'LIST', Blockly.JavaScript.ORDER_ASSIGNMENT) || '[]'; - var branch = Blockly.JavaScript.statementToCode(block, 'DO'); + let branch = Blockly.JavaScript.statementToCode(block, 'DO'); branch = Blockly.JavaScript.addLoopTrap(branch, block); - var code = ''; + let code = ''; // Cache non-trivial values to variables to prevent repeated look-ups. - var listVar = argument0; + let listVar = argument0; if (!argument0.match(/^\w+$/)) { listVar = Blockly.JavaScript.nameDB_.getDistinctName( variable0 + '_list', Blockly.VARIABLE_CATEGORY_NAME); code += 'var ' + listVar + ' = ' + argument0 + ';\n'; } - var indexVar = Blockly.JavaScript.nameDB_.getDistinctName( + const indexVar = Blockly.JavaScript.nameDB_.getDistinctName( variable0 + '_index', Blockly.VARIABLE_CATEGORY_NAME); branch = Blockly.JavaScript.INDENT + variable0 + ' = ' + listVar + '[' + indexVar + '];\n' + branch; @@ -150,7 +151,7 @@ Blockly.JavaScript['controls_forEach'] = function(block) { Blockly.JavaScript['controls_flow_statements'] = function(block) { // Flow statements: continue, break. - var xfix = ''; + let xfix = ''; if (Blockly.JavaScript.STATEMENT_PREFIX) { // Automatic prefix insertion is switched off for this block. Add manually. xfix += Blockly.JavaScript.injectId(Blockly.JavaScript.STATEMENT_PREFIX, @@ -163,7 +164,7 @@ Blockly.JavaScript['controls_flow_statements'] = function(block) { block); } if (Blockly.JavaScript.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/javascript/math.js b/generators/javascript/math.js index cc5c12d736b..a0c8cb42689 100644 --- a/generators/javascript/math.js +++ b/generators/javascript/math.js @@ -16,27 +16,27 @@ goog.require('Blockly.JavaScript'); Blockly.JavaScript['math_number'] = function(block) { // Numeric value. - var code = Number(block.getFieldValue('NUM')); - var order = code >= 0 ? Blockly.JavaScript.ORDER_ATOMIC : + const code = Number(block.getFieldValue('NUM')); + const order = code >= 0 ? Blockly.JavaScript.ORDER_ATOMIC : Blockly.JavaScript.ORDER_UNARY_NEGATION; return [code, order]; }; Blockly.JavaScript['math_arithmetic'] = function(block) { // Basic arithmetic operators, and power. - var OPERATORS = { + const OPERATORS = { 'ADD': [' + ', Blockly.JavaScript.ORDER_ADDITION], 'MINUS': [' - ', Blockly.JavaScript.ORDER_SUBTRACTION], 'MULTIPLY': [' * ', Blockly.JavaScript.ORDER_MULTIPLICATION], 'DIVIDE': [' / ', Blockly.JavaScript.ORDER_DIVISION], 'POWER': [null, Blockly.JavaScript.ORDER_NONE] // Handle power separately. }; - var tuple = OPERATORS[block.getFieldValue('OP')]; - var operator = tuple[0]; - var order = tuple[1]; - var argument0 = Blockly.JavaScript.valueToCode(block, 'A', order) || '0'; - var argument1 = Blockly.JavaScript.valueToCode(block, 'B', order) || '0'; - var code; + const tuple = OPERATORS[block.getFieldValue('OP')]; + const operator = tuple[0]; + const order = tuple[1]; + const argument0 = Blockly.JavaScript.valueToCode(block, 'A', order) || '0'; + const argument1 = Blockly.JavaScript.valueToCode(block, 'B', order) || '0'; + let code; // Power in JavaScript requires a special case since it has no operator. if (!operator) { code = 'Math.pow(' + argument0 + ', ' + argument1 + ')'; @@ -48,9 +48,9 @@ Blockly.JavaScript['math_arithmetic'] = function(block) { Blockly.JavaScript['math_single'] = function(block) { // Math operators with single operand. - var operator = block.getFieldValue('OP'); - var code; - var arg; + const operator = block.getFieldValue('OP'); + let code; + let arg; if (operator === 'NEG') { // Negation is a special case given its different operator precedence. arg = Blockly.JavaScript.valueToCode(block, 'NUM', @@ -132,7 +132,7 @@ Blockly.JavaScript['math_single'] = function(block) { Blockly.JavaScript['math_constant'] = function(block) { // Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY. - var CONSTANTS = { + const CONSTANTS = { 'PI': ['Math.PI', Blockly.JavaScript.ORDER_MEMBER], 'E': ['Math.E', Blockly.JavaScript.ORDER_MEMBER], 'GOLDEN_RATIO': @@ -147,13 +147,13 @@ Blockly.JavaScript['math_constant'] = function(block) { Blockly.JavaScript['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.JavaScript.valueToCode(block, 'NUMBER_TO_CHECK', + const number_to_check = Blockly.JavaScript.valueToCode(block, 'NUMBER_TO_CHECK', Blockly.JavaScript.ORDER_MODULUS) || '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.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'mathIsPrime', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(n) {', ' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods', @@ -193,20 +193,21 @@ Blockly.JavaScript['math_number_property'] = function(block) { case 'NEGATIVE': code = number_to_check + ' < 0'; break; - case 'DIVISIBLE_BY': - var divisor = Blockly.JavaScript.valueToCode(block, 'DIVISOR', + case 'DIVISIBLE_BY': { + const divisor = Blockly.JavaScript.valueToCode(block, 'DIVISOR', Blockly.JavaScript.ORDER_MODULUS) || '0'; code = number_to_check + ' % ' + divisor + ' === 0'; break; + } } return [code, Blockly.JavaScript.ORDER_EQUALITY]; }; Blockly.JavaScript['math_change'] = function(block) { // Add to a variable in place. - var argument0 = Blockly.JavaScript.valueToCode(block, 'DELTA', + const argument0 = Blockly.JavaScript.valueToCode(block, 'DELTA', Blockly.JavaScript.ORDER_ADDITION) || '0'; - var varName = Blockly.JavaScript.nameDB_.getName( + const varName = Blockly.JavaScript.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); return varName + ' = (typeof ' + varName + ' === \'number\' ? ' + varName + ' : 0) + ' + argument0 + ';\n'; @@ -219,8 +220,9 @@ Blockly.JavaScript['math_trig'] = Blockly.JavaScript['math_single']; Blockly.JavaScript['math_on_list'] = function(block) { // Math functions for lists. - var func = block.getFieldValue('OP'); - var list, code; + const func = block.getFieldValue('OP'); + let list; + let code; switch (func) { case 'SUM': list = Blockly.JavaScript.valueToCode(block, 'LIST', @@ -237,9 +239,9 @@ Blockly.JavaScript['math_on_list'] = function(block) { Blockly.JavaScript.ORDER_NONE) || '[]'; code = 'Math.max.apply(null, ' + list + ')'; break; - case 'AVERAGE': + case 'AVERAGE': { // mathMean([null,null,1,3]) === 2.0. - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'mathMean', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(myList) {', @@ -250,9 +252,10 @@ Blockly.JavaScript['math_on_list'] = function(block) { Blockly.JavaScript.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; - case 'MEDIAN': + } + case 'MEDIAN': { // mathMedian([null,null,1,3]) === 2.0. - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'mathMedian', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(myList) {', @@ -271,11 +274,12 @@ Blockly.JavaScript['math_on_list'] = function(block) { Blockly.JavaScript.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; - case 'MODE': + } + case 'MODE': { // As a list of numbers can contain more than one mode, // the returned result is provided as an array. // Mode of [3, 'x', 'x', 1, 1, 2, '3'] -> ['x', 1]. - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'mathModes', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(values) {', @@ -310,8 +314,9 @@ Blockly.JavaScript['math_on_list'] = function(block) { Blockly.JavaScript.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; - case 'STD_DEV': - var functionName = Blockly.JavaScript.provideFunction_( + } + case 'STD_DEV': { + const functionName = Blockly.JavaScript.provideFunction_( 'mathStandardDeviation', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(numbers) {', @@ -329,8 +334,9 @@ Blockly.JavaScript['math_on_list'] = function(block) { Blockly.JavaScript.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; - case 'RANDOM': - var functionName = Blockly.JavaScript.provideFunction_( + } + case 'RANDOM': { + const functionName = Blockly.JavaScript.provideFunction_( 'mathRandomList', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(list) {', @@ -341,6 +347,7 @@ Blockly.JavaScript['math_on_list'] = function(block) { Blockly.JavaScript.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; + } default: throw Error('Unknown operator: ' + func); } @@ -349,34 +356,34 @@ Blockly.JavaScript['math_on_list'] = function(block) { Blockly.JavaScript['math_modulo'] = function(block) { // Remainder computation. - var argument0 = Blockly.JavaScript.valueToCode(block, 'DIVIDEND', + const argument0 = Blockly.JavaScript.valueToCode(block, 'DIVIDEND', Blockly.JavaScript.ORDER_MODULUS) || '0'; - var argument1 = Blockly.JavaScript.valueToCode(block, 'DIVISOR', + const argument1 = Blockly.JavaScript.valueToCode(block, 'DIVISOR', Blockly.JavaScript.ORDER_MODULUS) || '0'; - var code = argument0 + ' % ' + argument1; + const code = argument0 + ' % ' + argument1; return [code, Blockly.JavaScript.ORDER_MODULUS]; }; Blockly.JavaScript['math_constrain'] = function(block) { // Constrain a number between two limits. - var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE', + const argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE) || '0'; - var argument1 = Blockly.JavaScript.valueToCode(block, 'LOW', + const argument1 = Blockly.JavaScript.valueToCode(block, 'LOW', Blockly.JavaScript.ORDER_NONE) || '0'; - var argument2 = Blockly.JavaScript.valueToCode(block, 'HIGH', + const argument2 = Blockly.JavaScript.valueToCode(block, 'HIGH', Blockly.JavaScript.ORDER_NONE) || 'Infinity'; - var code = 'Math.min(Math.max(' + argument0 + ', ' + argument1 + '), ' + + const code = 'Math.min(Math.max(' + argument0 + ', ' + argument1 + '), ' + argument2 + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['math_random_int'] = function(block) { // Random integer between [X] and [Y]. - var argument0 = Blockly.JavaScript.valueToCode(block, 'FROM', + const argument0 = Blockly.JavaScript.valueToCode(block, 'FROM', Blockly.JavaScript.ORDER_NONE) || '0'; - var argument1 = Blockly.JavaScript.valueToCode(block, 'TO', + const argument1 = Blockly.JavaScript.valueToCode(block, 'TO', Blockly.JavaScript.ORDER_NONE) || '0'; - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'mathRandomInt', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(a, b) {', @@ -388,7 +395,7 @@ Blockly.JavaScript['math_random_int'] = function(block) { ' }', ' return Math.floor(Math.random() * (b - a + 1) + a);', '}']); - var code = functionName + '(' + argument0 + ', ' + argument1 + ')'; + const code = functionName + '(' + argument0 + ', ' + argument1 + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; @@ -399,9 +406,9 @@ Blockly.JavaScript['math_random_float'] = function(block) { Blockly.JavaScript['math_atan2'] = function(block) { // Arctangent of point (X, Y) in degrees from -180 to 180. - var argument0 = Blockly.JavaScript.valueToCode(block, 'X', + const argument0 = Blockly.JavaScript.valueToCode(block, 'X', Blockly.JavaScript.ORDER_NONE) || '0'; - var argument1 = Blockly.JavaScript.valueToCode(block, 'Y', + const argument1 = Blockly.JavaScript.valueToCode(block, 'Y', Blockly.JavaScript.ORDER_NONE) || '0'; return ['Math.atan2(' + argument1 + ', ' + argument0 + ') / Math.PI * 180', Blockly.JavaScript.ORDER_DIVISION]; diff --git a/generators/javascript/procedures.js b/generators/javascript/procedures.js index f82df1f02c9..a95fe054698 100644 --- a/generators/javascript/procedures.js +++ b/generators/javascript/procedures.js @@ -16,9 +16,9 @@ goog.require('Blockly.JavaScript'); Blockly.JavaScript['procedures_defreturn'] = function(block) { // Define a procedure with a return value. - var funcName = Blockly.JavaScript.nameDB_.getName( + const funcName = Blockly.JavaScript.nameDB_.getName( block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME); - var xfix1 = ''; + let xfix1 = ''; if (Blockly.JavaScript.STATEMENT_PREFIX) { xfix1 += Blockly.JavaScript.injectId(Blockly.JavaScript.STATEMENT_PREFIX, block); @@ -30,16 +30,16 @@ Blockly.JavaScript['procedures_defreturn'] = function(block) { if (xfix1) { xfix1 = Blockly.JavaScript.prefixLines(xfix1, Blockly.JavaScript.INDENT); } - var loopTrap = ''; + let loopTrap = ''; if (Blockly.JavaScript.INFINITE_LOOP_TRAP) { loopTrap = Blockly.JavaScript.prefixLines( Blockly.JavaScript.injectId(Blockly.JavaScript.INFINITE_LOOP_TRAP, block), Blockly.JavaScript.INDENT); } - var branch = Blockly.JavaScript.statementToCode(block, 'STACK'); - var returnValue = Blockly.JavaScript.valueToCode(block, 'RETURN', + const branch = Blockly.JavaScript.statementToCode(block, 'STACK'); + let returnValue = Blockly.JavaScript.valueToCode(block, 'RETURN', Blockly.JavaScript.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.JavaScript['procedures_defreturn'] = function(block) { if (returnValue) { returnValue = Blockly.JavaScript.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.JavaScript.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 + '}'; code = Blockly.JavaScript.scrub_(block, code); // Add % so as not to collide with helper functions in definitions list. @@ -68,15 +68,15 @@ Blockly.JavaScript['procedures_defnoreturn'] = Blockly.JavaScript['procedures_callreturn'] = function(block) { // Call a procedure with a return value. - var funcName = Blockly.JavaScript.nameDB_.getName( + const funcName = Blockly.JavaScript.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.JavaScript.valueToCode(block, 'ARG' + i, Blockly.JavaScript.ORDER_NONE) || 'null'; } - var code = funcName + '(' + args.join(', ') + ')'; + const code = funcName + '(' + args.join(', ') + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; @@ -84,15 +84,15 @@ Blockly.JavaScript['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.JavaScript['procedures_callreturn'](block); + const tuple = Blockly.JavaScript['procedures_callreturn'](block); return tuple[0] + ';\n'; }; Blockly.JavaScript['procedures_ifreturn'] = function(block) { // Conditionally return value from a procedure. - var condition = Blockly.JavaScript.valueToCode(block, 'CONDITION', + const condition = Blockly.JavaScript.valueToCode(block, 'CONDITION', Blockly.JavaScript.ORDER_NONE) || 'false'; - var code = 'if (' + condition + ') {\n'; + let code = 'if (' + condition + ') {\n'; if (Blockly.JavaScript.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.JavaScript['procedures_ifreturn'] = function(block) { Blockly.JavaScript.INDENT); } if (block.hasReturnValue_) { - var value = Blockly.JavaScript.valueToCode(block, 'VALUE', + const value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE) || 'null'; code += Blockly.JavaScript.INDENT + 'return ' + value + ';\n'; } else { diff --git a/generators/javascript/text.js b/generators/javascript/text.js index 90192300665..f9e203fc254 100644 --- a/generators/javascript/text.js +++ b/generators/javascript/text.js @@ -16,14 +16,14 @@ goog.require('Blockly.JavaScript'); Blockly.JavaScript['text'] = function(block) { // Text value. - var code = Blockly.JavaScript.quote_(block.getFieldValue('TEXT')); + const code = Blockly.JavaScript.quote_(block.getFieldValue('TEXT')); return [code, Blockly.JavaScript.ORDER_ATOMIC]; }; Blockly.JavaScript['text_multiline'] = function(block) { // Text value. - var code = Blockly.JavaScript.multiline_quote_(block.getFieldValue('TEXT')); - var order = code.indexOf('+') !== -1 ? Blockly.JavaScript.ORDER_ADDITION : + const code = Blockly.JavaScript.multiline_quote_(block.getFieldValue('TEXT')); + const order = code.indexOf('+') !== -1 ? Blockly.JavaScript.ORDER_ADDITION : Blockly.JavaScript.ORDER_ATOMIC; return [code, order]; }; @@ -53,64 +53,67 @@ Blockly.JavaScript['text_join'] = function(block) { switch (block.itemCount_) { case 0: return ['\'\'', Blockly.JavaScript.ORDER_ATOMIC]; - case 1: - var element = Blockly.JavaScript.valueToCode(block, 'ADD0', + case 1: { + const element = Blockly.JavaScript.valueToCode(block, 'ADD0', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var codeAndOrder = Blockly.JavaScript.text.forceString_(element); + const codeAndOrder = Blockly.JavaScript.text.forceString_(element); return codeAndOrder; - case 2: - var element0 = Blockly.JavaScript.valueToCode(block, 'ADD0', + } + case 2: { + const element0 = Blockly.JavaScript.valueToCode(block, 'ADD0', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var element1 = Blockly.JavaScript.valueToCode(block, 'ADD1', + const element1 = Blockly.JavaScript.valueToCode(block, 'ADD1', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var code = Blockly.JavaScript.text.forceString_(element0)[0] + + const code = Blockly.JavaScript.text.forceString_(element0)[0] + ' + ' + Blockly.JavaScript.text.forceString_(element1)[0]; return [code, Blockly.JavaScript.ORDER_ADDITION]; - default: - var elements = new Array(block.itemCount_); - for (var i = 0; i < block.itemCount_; i++) { + } + default: { + const elements = new Array(block.itemCount_); + for (let i = 0; i < block.itemCount_; i++) { elements[i] = Blockly.JavaScript.valueToCode(block, 'ADD' + i, Blockly.JavaScript.ORDER_NONE) || '\'\''; } - var code = '[' + elements.join(',') + '].join(\'\')'; + const code = '[' + elements.join(',') + '].join(\'\')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; + } } }; Blockly.JavaScript['text_append'] = function(block) { // Append to a variable in place. - var varName = Blockly.JavaScript.nameDB_.getName( + const varName = Blockly.JavaScript.nameDB_.getName( block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - var value = Blockly.JavaScript.valueToCode(block, 'TEXT', + const value = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var code = varName + ' += ' + + const code = varName + ' += ' + Blockly.JavaScript.text.forceString_(value)[0] + ';\n'; return code; }; Blockly.JavaScript['text_length'] = function(block) { // String or array length. - var text = Blockly.JavaScript.valueToCode(block, 'VALUE', + const text = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_MEMBER) || '\'\''; return [text + '.length', Blockly.JavaScript.ORDER_MEMBER]; }; Blockly.JavaScript['text_isEmpty'] = function(block) { // Is the string null or array empty? - var text = Blockly.JavaScript.valueToCode(block, 'VALUE', + const text = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_MEMBER) || '\'\''; return ['!' + text + '.length', Blockly.JavaScript.ORDER_LOGICAL_NOT]; }; Blockly.JavaScript['text_indexOf'] = function(block) { // Search the text for a substring. - var operator = block.getFieldValue('END') === 'FIRST' ? + const operator = block.getFieldValue('END') === 'FIRST' ? 'indexOf' : 'lastIndexOf'; - var substring = Blockly.JavaScript.valueToCode(block, 'FIND', + const substring = Blockly.JavaScript.valueToCode(block, 'FIND', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var text = Blockly.JavaScript.valueToCode(block, 'VALUE', + const text = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_MEMBER) || '\'\''; - var code = text + '.' + operator + '(' + substring + ')'; + const code = text + '.' + operator + '(' + substring + ')'; // Adjust index if using one-based indices. if (block.workspace.options.oneBasedIndex) { return [code + ' + 1', Blockly.JavaScript.ORDER_ADDITION]; @@ -121,37 +124,42 @@ Blockly.JavaScript['text_indexOf'] = function(block) { Blockly.JavaScript['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 textOrder = (where === 'RANDOM') ? Blockly.JavaScript.ORDER_NONE : + const where = block.getFieldValue('WHERE') || 'FROM_START'; + const textOrder = (where === 'RANDOM') ? Blockly.JavaScript.ORDER_NONE : Blockly.JavaScript.ORDER_MEMBER; - var text = Blockly.JavaScript.valueToCode(block, 'VALUE', + const text = Blockly.JavaScript.valueToCode(block, 'VALUE', textOrder) || '\'\''; switch (where) { - case 'FIRST': - var code = text + '.charAt(0)'; + case 'FIRST': { + const code = text + '.charAt(0)'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; - case 'LAST': - var code = text + '.slice(-1)'; + } + case 'LAST': { + const code = text + '.slice(-1)'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; - case 'FROM_START': - var at = Blockly.JavaScript.getAdjusted(block, 'AT'); + } + case 'FROM_START': { + const at = Blockly.JavaScript.getAdjusted(block, 'AT'); // Adjust index if using one-based indices. - var code = text + '.charAt(' + at + ')'; + const code = text + '.charAt(' + at + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; - case 'FROM_END': - var at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, true); - var code = text + '.slice(' + at + ').charAt(0)'; + } + case 'FROM_END': { + const at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, true); + const code = text + '.slice(' + at + ').charAt(0)'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; - case 'RANDOM': - var functionName = Blockly.JavaScript.provideFunction_( + } + case 'RANDOM': { + const functionName = Blockly.JavaScript.provideFunction_( 'textRandomLetter', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(text) {', ' var x = Math.floor(Math.random() * text.length);', ' return text[x];', '}']); - var code = functionName + '(' + text + ')'; + const code = functionName + '(' + text + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; + } } throw Error('Unhandled option (text_charAt).'); }; @@ -178,58 +186,61 @@ Blockly.JavaScript.text.getIndex_ = function(stringName, where, opt_at) { Blockly.JavaScript['text_getSubstring'] = function(block) { // Get substring. - var where1 = block.getFieldValue('WHERE1'); - var where2 = block.getFieldValue('WHERE2'); - var requiresLengthCall = (where1 !== 'FROM_END' && where1 !== 'LAST' && + const where1 = block.getFieldValue('WHERE1'); + const where2 = block.getFieldValue('WHERE2'); + const requiresLengthCall = (where1 !== 'FROM_END' && where1 !== 'LAST' && where2 !== 'FROM_END' && where2 !== 'LAST'); - var textOrder = requiresLengthCall ? Blockly.JavaScript.ORDER_MEMBER : + const textOrder = requiresLengthCall ? Blockly.JavaScript.ORDER_MEMBER : Blockly.JavaScript.ORDER_NONE; - var text = Blockly.JavaScript.valueToCode(block, 'STRING', + const text = Blockly.JavaScript.valueToCode(block, 'STRING', textOrder) || '\'\''; + let code; if (where1 === 'FIRST' && where2 === 'LAST') { - var code = text; + code = text; return [code, Blockly.JavaScript.ORDER_NONE]; } else if (text.match(/^'?\w+'?$/) || requiresLengthCall) { // If the text is a variable or literal or doesn't require a call for // length, don't generate a helper function. + let at1; switch (where1) { case 'FROM_START': - var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1'); + at1 = Blockly.JavaScript.getAdjusted(block, 'AT1'); break; case 'FROM_END': - var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1', 1, false, + at1 = Blockly.JavaScript.getAdjusted(block, 'AT1', 1, false, Blockly.JavaScript.ORDER_SUBTRACTION); at1 = text + '.length - ' + at1; break; case 'FIRST': - var at1 = '0'; + at1 = '0'; break; default: throw Error('Unhandled option (text_getSubstring).'); } + let at2; switch (where2) { case 'FROM_START': - var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 1); + at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 1); break; case 'FROM_END': - var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 0, false, + at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 0, false, Blockly.JavaScript.ORDER_SUBTRACTION); at2 = text + '.length - ' + at2; break; case 'LAST': - var at2 = text + '.length'; + at2 = text + '.length'; break; default: throw Error('Unhandled option (text_getSubstring).'); } code = text + '.slice(' + at1 + ', ' + at2 + ')'; } else { - var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1'); - var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2'); - var getIndex_ = Blockly.JavaScript.text.getIndex_; - var wherePascalCase = {'FIRST': 'First', 'LAST': 'Last', + const at1 = Blockly.JavaScript.getAdjusted(block, 'AT1'); + const at2 = Blockly.JavaScript.getAdjusted(block, 'AT2'); + const getIndex_ = Blockly.JavaScript.text.getIndex_; + const wherePascalCase = {'FIRST': 'First', 'LAST': 'Last', 'FROM_START': 'FromStart', 'FROM_END': 'FromEnd'}; - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'subsequence' + wherePascalCase[where1] + wherePascalCase[where2], ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(sequence' + @@ -242,7 +253,7 @@ Blockly.JavaScript['text_getSubstring'] = function(block) { ' var end = ' + getIndex_('sequence', where2, 'at2') + ' + 1;', ' return sequence.slice(start, end);', '}']); - var code = functionName + '(' + text + + code = functionName + '(' + text + // The value for 'FROM_END' and 'FROM_START' depends on `at` so we // pass it. ((where1 === 'FROM_END' || where1 === 'FROM_START') ? ', ' + at1 : '') + @@ -254,22 +265,23 @@ Blockly.JavaScript['text_getSubstring'] = function(block) { Blockly.JavaScript['text_changeCase'] = function(block) { // Change capitalization. - var OPERATORS = { + const OPERATORS = { 'UPPERCASE': '.toUpperCase()', 'LOWERCASE': '.toLowerCase()', 'TITLECASE': null }; - var operator = OPERATORS[block.getFieldValue('CASE')]; - var textOrder = operator ? Blockly.JavaScript.ORDER_MEMBER : + const operator = OPERATORS[block.getFieldValue('CASE')]; + const textOrder = operator ? Blockly.JavaScript.ORDER_MEMBER : Blockly.JavaScript.ORDER_NONE; - var text = Blockly.JavaScript.valueToCode(block, 'TEXT', + const text = Blockly.JavaScript.valueToCode(block, 'TEXT', textOrder) || '\'\''; + let code; if (operator) { // Upper and lower case are functions built into JavaScript. - var code = text + operator; + code = text + operator; } else { // Title case is not a native JavaScript function. Define one. - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'textToTitleCase', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(str) {', @@ -277,43 +289,44 @@ Blockly.JavaScript['text_changeCase'] = function(block) { ' function(txt) {return txt[0].toUpperCase() + ' + 'txt.substring(1).toLowerCase();});', '}']); - var code = functionName + '(' + text + ')'; + code = functionName + '(' + text + ')'; } return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['text_trim'] = function(block) { // Trim spaces. - var OPERATORS = { + const OPERATORS = { 'LEFT': ".replace(/^[\\s\\xa0]+/, '')", 'RIGHT': ".replace(/[\\s\\xa0]+$/, '')", 'BOTH': '.trim()' }; - var operator = OPERATORS[block.getFieldValue('MODE')]; - var text = Blockly.JavaScript.valueToCode(block, 'TEXT', + const operator = OPERATORS[block.getFieldValue('MODE')]; + const text = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_MEMBER) || '\'\''; return [text + operator, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['text_print'] = function(block) { // Print statement. - var msg = Blockly.JavaScript.valueToCode(block, 'TEXT', + const msg = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE) || '\'\''; return 'window.alert(' + msg + ');\n'; }; Blockly.JavaScript['text_prompt_ext'] = function(block) { // Prompt function. + let msg; if (block.getField('TEXT')) { // Internal message. - var msg = Blockly.JavaScript.quote_(block.getFieldValue('TEXT')); + msg = Blockly.JavaScript.quote_(block.getFieldValue('TEXT')); } else { // External message. - var msg = Blockly.JavaScript.valueToCode(block, 'TEXT', + msg = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE) || '\'\''; } - var code = 'window.prompt(' + msg + ')'; - var toNumber = block.getFieldValue('TYPE') === 'NUMBER'; + let code = 'window.prompt(' + msg + ')'; + const toNumber = block.getFieldValue('TYPE') === 'NUMBER'; if (toNumber) { code = 'Number(' + code + ')'; } @@ -323,11 +336,11 @@ Blockly.JavaScript['text_prompt_ext'] = function(block) { Blockly.JavaScript['text_prompt'] = Blockly.JavaScript['text_prompt_ext']; Blockly.JavaScript['text_count'] = function(block) { - var text = Blockly.JavaScript.valueToCode(block, 'TEXT', + const text = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var sub = Blockly.JavaScript.valueToCode(block, 'SUB', + const sub = Blockly.JavaScript.valueToCode(block, 'SUB', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'textCount', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(haystack, needle) {', @@ -337,20 +350,20 @@ Blockly.JavaScript['text_count'] = function(block) { ' return haystack.split(needle).length - 1;', ' }', '}']); - var code = functionName + '(' + text + ', ' + sub + ')'; + const code = functionName + '(' + text + ', ' + sub + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['text_replace'] = function(block) { - var text = Blockly.JavaScript.valueToCode(block, 'TEXT', + const text = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var from = Blockly.JavaScript.valueToCode(block, 'FROM', + const from = Blockly.JavaScript.valueToCode(block, 'FROM', Blockly.JavaScript.ORDER_NONE) || '\'\''; - var to = Blockly.JavaScript.valueToCode(block, 'TO', + const to = Blockly.JavaScript.valueToCode(block, 'TO', Blockly.JavaScript.ORDER_NONE) || '\'\''; // The regex escaping code below is taken from the implementation of // goog.string.regExpEscape. - var functionName = Blockly.JavaScript.provideFunction_( + const functionName = Blockly.JavaScript.provideFunction_( 'textReplace', ['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(haystack, needle, replacement) {', @@ -359,13 +372,13 @@ Blockly.JavaScript['text_replace'] = function(block) { ' .replace(/\\x08/g,"\\\\x08");', ' return haystack.replace(new RegExp(needle, \'g\'), replacement);', '}']); - var code = functionName + '(' + text + ', ' + from + ', ' + to + ')'; + const code = functionName + '(' + text + ', ' + from + ', ' + to + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript['text_reverse'] = function(block) { - var text = Blockly.JavaScript.valueToCode(block, 'TEXT', + const text = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_MEMBER) || '\'\''; - var code = text + '.split(\'\').reverse().join(\'\')'; + const code = text + '.split(\'\').reverse().join(\'\')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; diff --git a/generators/javascript/variables.js b/generators/javascript/variables.js index d0ad8870912..0361709c1eb 100644 --- a/generators/javascript/variables.js +++ b/generators/javascript/variables.js @@ -16,16 +16,17 @@ goog.require('Blockly.JavaScript'); Blockly.JavaScript['variables_get'] = function(block) { // Variable getter. - var code = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('VAR'), + const code = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); return [code, Blockly.JavaScript.ORDER_ATOMIC]; }; Blockly.JavaScript['variables_set'] = function(block) { // Variable setter. - var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE', - Blockly.JavaScript.ORDER_ASSIGNMENT) || '0'; - var varName = Blockly.JavaScript.nameDB_.getName( + const argument0 = Blockly.JavaScript.valueToCode( + block, 'VALUE', Blockly.JavaScript.ORDER_ASSIGNMENT) || + '0'; + const varName = Blockly.JavaScript.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 2524297e7d3..88800de2ced 100644 --- a/tests/deps.mocha.js +++ b/tests/deps.mocha.js @@ -271,17 +271,17 @@ goog.addDependency('../../generators/dart/procedures.js', ['Blockly.Dart.procedu goog.addDependency('../../generators/dart/text.js', ['Blockly.Dart.texts'], ['Blockly.Dart'], {'lang': 'es6'}); goog.addDependency('../../generators/dart/variables.js', ['Blockly.Dart.variables'], ['Blockly.Dart'], {'lang': 'es6'}); goog.addDependency('../../generators/dart/variables_dynamic.js', ['Blockly.Dart.variablesDynamic'], ['Blockly.Dart', 'Blockly.Dart.variables']); -goog.addDependency('../../generators/javascript.js', ['Blockly.JavaScript'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.global', 'Blockly.utils.object', 'Blockly.utils.string']); -goog.addDependency('../../generators/javascript/colour.js', ['Blockly.JavaScript.colour'], ['Blockly.JavaScript']); -goog.addDependency('../../generators/javascript/lists.js', ['Blockly.JavaScript.lists'], ['Blockly.JavaScript']); -goog.addDependency('../../generators/javascript/logic.js', ['Blockly.JavaScript.logic'], ['Blockly.JavaScript']); -goog.addDependency('../../generators/javascript/loops.js', ['Blockly.JavaScript.loops'], ['Blockly.JavaScript']); -goog.addDependency('../../generators/javascript/math.js', ['Blockly.JavaScript.math'], ['Blockly.JavaScript']); -goog.addDependency('../../generators/javascript/procedures.js', ['Blockly.JavaScript.procedures'], ['Blockly.JavaScript']); -goog.addDependency('../../generators/javascript/text.js', ['Blockly.JavaScript.texts'], ['Blockly.JavaScript']); -goog.addDependency('../../generators/javascript/variables.js', ['Blockly.JavaScript.variables'], ['Blockly.JavaScript']); +goog.addDependency('../../generators/javascript.js', ['Blockly.JavaScript'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.global', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'}); +goog.addDependency('../../generators/javascript/colour.js', ['Blockly.JavaScript.colour'], ['Blockly.JavaScript'], {'lang': 'es6'}); +goog.addDependency('../../generators/javascript/lists.js', ['Blockly.JavaScript.lists'], ['Blockly.JavaScript'], {'lang': 'es6'}); +goog.addDependency('../../generators/javascript/logic.js', ['Blockly.JavaScript.logic'], ['Blockly.JavaScript'], {'lang': 'es6'}); +goog.addDependency('../../generators/javascript/loops.js', ['Blockly.JavaScript.loops'], ['Blockly.JavaScript'], {'lang': 'es6'}); +goog.addDependency('../../generators/javascript/math.js', ['Blockly.JavaScript.math'], ['Blockly.JavaScript'], {'lang': 'es6'}); +goog.addDependency('../../generators/javascript/procedures.js', ['Blockly.JavaScript.procedures'], ['Blockly.JavaScript'], {'lang': 'es6'}); +goog.addDependency('../../generators/javascript/text.js', ['Blockly.JavaScript.texts'], ['Blockly.JavaScript'], {'lang': 'es6'}); +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']); +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']); @@ -291,7 +291,7 @@ goog.addDependency('../../generators/lua/procedures.js', ['Blockly.Lua.procedure 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/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']); +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']); goog.addDependency('../../generators/php/lists.js', ['Blockly.PHP.lists'], ['Blockly.PHP']); goog.addDependency('../../generators/php/logic.js', ['Blockly.PHP.logic'], ['Blockly.PHP']); @@ -301,7 +301,7 @@ goog.addDependency('../../generators/php/procedures.js', ['Blockly.PHP.procedure goog.addDependency('../../generators/php/text.js', ['Blockly.PHP.texts'], ['Blockly.PHP']); goog.addDependency('../../generators/php/variables.js', ['Blockly.PHP.variables'], ['Blockly.PHP']); goog.addDependency('../../generators/php/variables_dynamic.js', ['Blockly.PHP.variablesDynamic'], ['Blockly.PHP', 'Blockly.PHP.variables']); -goog.addDependency('../../generators/python.js', ['Blockly.Python'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.string']); +goog.addDependency('../../generators/python.js', ['Blockly.Python'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.string'], {'lang': 'es6'}); goog.addDependency('../../generators/python/colour.js', ['Blockly.Python.colour'], ['Blockly.Python'], {'lang': 'es6'}); goog.addDependency('../../generators/python/lists.js', ['Blockly.Python.lists'], ['Blockly.Python'], {'lang': 'es6'}); goog.addDependency('../../generators/python/logic.js', ['Blockly.Python.logic'], ['Blockly.Python'], {'lang': 'es6'});