diff --git a/generators/dart/colour.js b/generators/dart/colour.js index e1920292884..7f6f621ac0e 100644 --- a/generators/dart/colour.js +++ b/generators/dart/colour.js @@ -9,26 +9,26 @@ */ 'use strict'; -goog.provide('Blockly.Dart.colour'); +goog.module('Blockly.Dart.colour'); -goog.require('Blockly.Dart'); +const Dart = goog.require('Blockly.Dart'); -Blockly.Dart.addReservedWords('Math'); +Dart.addReservedWords('Math'); -Blockly.Dart['colour_picker'] = function(block) { +Dart['colour_picker'] = function(block) { // Colour picker. - const code = Blockly.Dart.quote_(block.getFieldValue('COLOUR')); - return [code, Blockly.Dart.ORDER_ATOMIC]; + const code = Dart.quote_(block.getFieldValue('COLOUR')); + return [code, Dart.ORDER_ATOMIC]; }; -Blockly.Dart['colour_random'] = function(block) { +Dart['colour_random'] = function(block) { // Generate a random colour. - Blockly.Dart.definitions_['import_dart_math'] = + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( + const functionName = Dart.provideFunction_( 'colour_random', - ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {', + ['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {', ' String hex = \'0123456789abcdef\';', ' var rnd = new Math.Random();', ' return \'#${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'', @@ -36,23 +36,23 @@ Blockly.Dart['colour_random'] = function(block) { ' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\';', '}']); const code = functionName + '()'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['colour_rgb'] = function(block) { +Dart['colour_rgb'] = function(block) { // Compose a colour from RGB components expressed as percentages. - const red = Blockly.Dart.valueToCode(block, 'RED', - Blockly.Dart.ORDER_NONE) || 0; - const green = Blockly.Dart.valueToCode(block, 'GREEN', - Blockly.Dart.ORDER_NONE) || 0; - const blue = Blockly.Dart.valueToCode(block, 'BLUE', - Blockly.Dart.ORDER_NONE) || 0; + const red = Dart.valueToCode(block, 'RED', + Dart.ORDER_NONE) || 0; + const green = Dart.valueToCode(block, 'GREEN', + Dart.ORDER_NONE) || 0; + const blue = Dart.valueToCode(block, 'BLUE', + Dart.ORDER_NONE) || 0; - Blockly.Dart.definitions_['import_dart_math'] = + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( + const functionName = Dart.provideFunction_( 'colour_rgb', - ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + + ['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(num r, num g, num b) {', ' num rn = (Math.max(Math.min(r, 100), 0) * 2.55).round();', ' String rs = rn.toInt().toRadixString(16);', @@ -69,23 +69,23 @@ Blockly.Dart['colour_rgb'] = function(block) { ' return \'#$rs$gs$bs\';', '}']); const code = functionName + '(' + red + ', ' + green + ', ' + blue + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['colour_blend'] = function(block) { +Dart['colour_blend'] = function(block) { // Blend two colours together. - const c1 = Blockly.Dart.valueToCode(block, 'COLOUR1', - Blockly.Dart.ORDER_NONE) || '\'#000000\''; - const c2 = Blockly.Dart.valueToCode(block, 'COLOUR2', - Blockly.Dart.ORDER_NONE) || '\'#000000\''; - const ratio = Blockly.Dart.valueToCode(block, 'RATIO', - Blockly.Dart.ORDER_NONE) || 0.5; + const c1 = Dart.valueToCode(block, 'COLOUR1', + Dart.ORDER_NONE) || '\'#000000\''; + const c2 = Dart.valueToCode(block, 'COLOUR2', + Dart.ORDER_NONE) || '\'#000000\''; + const ratio = Dart.valueToCode(block, 'RATIO', + Dart.ORDER_NONE) || 0.5; - Blockly.Dart.definitions_['import_dart_math'] = + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( + const functionName = Dart.provideFunction_( 'colour_blend', - ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + + ['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(String c1, String c2, num ratio) {', ' ratio = Math.max(Math.min(ratio, 1), 0);', ' int r1 = int.parse(\'0x${c1.substring(1, 3)}\');', @@ -109,5 +109,5 @@ Blockly.Dart['colour_blend'] = function(block) { ' return \'#$rs$gs$bs\';', '}']); const code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; diff --git a/generators/dart/lists.js b/generators/dart/lists.js index 5c0fff7e79d..5eefea91bb9 100644 --- a/generators/dart/lists.js +++ b/generators/dart/lists.js @@ -6,85 +6,81 @@ /** * @fileoverview Generating Dart for list blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.Dart.lists'); +goog.module('Blockly.Dart.lists'); -goog.require('Blockly.Dart'); +const Dart = goog.require('Blockly.Dart'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.Dart.addReservedWords('Math'); +Dart.addReservedWords('Math'); -Blockly.Dart['lists_create_empty'] = function(block) { +Dart['lists_create_empty'] = function(block) { // Create an empty list. - return ['[]', Blockly.Dart.ORDER_ATOMIC]; + return ['[]', Dart.ORDER_ATOMIC]; }; -Blockly.Dart['lists_create_with'] = function(block) { +Dart['lists_create_with'] = function(block) { // Create a list with any number of elements of any type. const elements = new Array(block.itemCount_); for (let i = 0; i < block.itemCount_; i++) { - elements[i] = Blockly.Dart.valueToCode(block, 'ADD' + i, - Blockly.Dart.ORDER_NONE) || 'null'; + elements[i] = Dart.valueToCode(block, 'ADD' + i, Dart.ORDER_NONE) || 'null'; } const code = '[' + elements.join(', ') + ']'; - return [code, Blockly.Dart.ORDER_ATOMIC]; + return [code, Dart.ORDER_ATOMIC]; }; -Blockly.Dart['lists_repeat'] = function(block) { +Dart['lists_repeat'] = function(block) { // Create a list with one element repeated. - const element = Blockly.Dart.valueToCode(block, 'ITEM', - Blockly.Dart.ORDER_NONE) || 'null'; - const repeatCount = Blockly.Dart.valueToCode(block, 'NUM', - Blockly.Dart.ORDER_NONE) || '0'; + const element = Dart.valueToCode(block, 'ITEM', Dart.ORDER_NONE) || 'null'; + const repeatCount = Dart.valueToCode(block, 'NUM', Dart.ORDER_NONE) || '0'; const code = 'new List.filled(' + repeatCount + ', ' + element + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['lists_length'] = function(block) { +Dart['lists_length'] = function(block) { // String or array length. - const list = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]'; - return [list + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX]; + const list = + Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '[]'; + return [list + '.length', Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['lists_isEmpty'] = function(block) { +Dart['lists_isEmpty'] = function(block) { // Is the string null or array empty? - const list = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]'; - return [list + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX]; + const list = + Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '[]'; + return [list + '.isEmpty', Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['lists_indexOf'] = function(block) { +Dart['lists_indexOf'] = function(block) { // Find an item in the list. - const operator = block.getFieldValue('END') === 'FIRST' ? - 'indexOf' : 'lastIndexOf'; - const item = Blockly.Dart.valueToCode(block, 'FIND', - Blockly.Dart.ORDER_NONE) || '\'\''; - const list = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]'; + const operator = + block.getFieldValue('END') === 'FIRST' ? 'indexOf' : 'lastIndexOf'; + const item = Dart.valueToCode(block, 'FIND', Dart.ORDER_NONE) || '\'\''; + const list = + Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '[]'; const code = list + '.' + operator + '(' + item + ')'; if (block.workspace.options.oneBasedIndex) { - return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE]; + return [code + ' + 1', Dart.ORDER_ADDITIVE]; } - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['lists_getIndex'] = function(block) { +Dart['lists_getIndex'] = function(block) { // Get element at index. // Note: Until January 2013 this block did not have MODE or WHERE inputs. const mode = block.getFieldValue('MODE') || 'GET'; const where = block.getFieldValue('WHERE') || 'FROM_START'; const listOrder = (where === 'RANDOM' || where === 'FROM_END') ? - Blockly.Dart.ORDER_NONE : Blockly.Dart.ORDER_UNARY_POSTFIX; - let list = Blockly.Dart.valueToCode(block, 'VALUE', listOrder) || '[]'; + Dart.ORDER_NONE : + Dart.ORDER_UNARY_POSTFIX; + let list = Dart.valueToCode(block, 'VALUE', listOrder) || '[]'; // Cache non-trivial values to variables to prevent repeated look-ups. // Closure, which accesses and modifies 'list'. function cacheList() { - const listVar = Blockly.Dart.nameDB_.getDistinctName( - 'tmp_list', Blockly.VARIABLE_CATEGORY_NAME); + const listVar = Dart.nameDB_.getDistinctName('tmp_list', NameType.VARIABLE); const code = 'List ' + listVar + ' = ' + list + ';\n'; list = listVar; return code; @@ -96,12 +92,10 @@ Blockly.Dart['lists_getIndex'] = function(block) { !list.match(/^\w+$/)) { // `list` is an expression, so we may not evaluate it more than once. if (where === 'RANDOM') { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; // We can use multiple statements. let code = cacheList(); - const xVar = Blockly.Dart.nameDB_.getDistinctName( - 'tmp_x', Blockly.VARIABLE_CATEGORY_NAME); + const xVar = Dart.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE); code += 'int ' + xVar + ' = new Math.Random().nextInt(' + list + '.length);\n'; code += list + '.removeAt(' + xVar + ');\n'; @@ -109,36 +103,32 @@ Blockly.Dart['lists_getIndex'] = function(block) { } else { // where === 'FROM_END' if (mode === 'REMOVE') { // We can use multiple statements. - const at = Blockly.Dart.getAdjusted(block, 'AT', 1, false, - Blockly.Dart.ORDER_ADDITIVE); + const at = Dart.getAdjusted(block, 'AT', 1, false, Dart.ORDER_ADDITIVE); let code = cacheList(); - code += list + '.removeAt(' + list + '.length' + ' - ' + at + ');\n'; + code += list + '.removeAt(' + list + '.length' + + ' - ' + at + ');\n'; return code; } else if (mode === 'GET') { - const at = Blockly.Dart.getAdjusted(block, 'AT', 1); + const at = Dart.getAdjusted(block, 'AT', 1); // We need to create a procedure to avoid reevaluating values. - const functionName = Blockly.Dart.provideFunction_( - 'lists_get_from_end', - ['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List my_list, num x) {', - ' x = my_list.length - x;', - ' return my_list[x];', - '}']); + const functionName = Dart.provideFunction_('lists_get_from_end', [ + 'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + + '(List my_list, num x) {', + ' x = my_list.length - x;', ' return my_list[x];', '}' + ]); const code = functionName + '(' + list + ', ' + at + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'GET_REMOVE') { - const at = Blockly.Dart.getAdjusted(block, 'AT', 1); + const at = Dart.getAdjusted(block, 'AT', 1); // We need to create a procedure to avoid reevaluating values. - const functionName = Blockly.Dart.provideFunction_( - 'lists_remove_from_end', - ['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List my_list, num x) {', - ' x = my_list.length - x;', - ' return my_list.removeAt(x);', - '}']); + const functionName = Dart.provideFunction_('lists_remove_from_end', [ + 'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + + '(List my_list, num x) {', + ' x = my_list.length - x;', ' return my_list.removeAt(x);', '}' + ]); const code = functionName + '(' + list + ', ' + at + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } } } else { @@ -148,10 +138,10 @@ Blockly.Dart['lists_getIndex'] = function(block) { case 'FIRST': if (mode === 'GET') { const code = list + '.first'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'GET_REMOVE') { const code = list + '.removeAt(0)'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'REMOVE') { return list + '.removeAt(0);\n'; } @@ -159,37 +149,36 @@ Blockly.Dart['lists_getIndex'] = function(block) { case 'LAST': if (mode === 'GET') { const code = list + '.last'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'GET_REMOVE') { const code = list + '.removeLast()'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'REMOVE') { return list + '.removeLast();\n'; } break; case 'FROM_START': { - const at = Blockly.Dart.getAdjusted(block, 'AT'); + const at = Dart.getAdjusted(block, 'AT'); if (mode === 'GET') { const code = list + '[' + at + ']'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'GET_REMOVE') { const code = list + '.removeAt(' + at + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'REMOVE') { return list + '.removeAt(' + at + ');\n'; } break; } case 'FROM_END': { - const at = Blockly.Dart.getAdjusted(block, 'AT', 1, false, - Blockly.Dart.ORDER_ADDITIVE); + const at = Dart.getAdjusted(block, 'AT', 1, false, Dart.ORDER_ADDITIVE); if (mode === 'GET') { const code = list + '[' + list + '.length - ' + at + ']'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'GET_REMOVE' || mode === 'REMOVE') { const code = list + '.removeAt(' + list + '.length - ' + at + ')'; if (mode === 'GET_REMOVE') { - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'REMOVE') { return code + ';\n'; } @@ -197,36 +186,32 @@ Blockly.Dart['lists_getIndex'] = function(block) { break; } case 'RANDOM': - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; if (mode === 'REMOVE') { // We can use multiple statements. - const xVar = Blockly.Dart.nameDB_.getDistinctName( - 'tmp_x', Blockly.VARIABLE_CATEGORY_NAME); + const xVar = Dart.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE); let code = 'int ' + xVar + ' = new Math.Random().nextInt(' + list + '.length);\n'; code += list + '.removeAt(' + xVar + ');\n'; return code; } else if (mode === 'GET') { - const functionName = - Blockly.Dart.provideFunction_('lists_get_random_item', [ - 'dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List my_list) {', - ' int x = new Math.Random().nextInt(my_list.length);', - ' return my_list[x];', '}' - ]); + const functionName = Dart.provideFunction_('lists_get_random_item', [ + 'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List my_list) {', + ' int x = new Math.Random().nextInt(my_list.length);', + ' return my_list[x];', '}' + ]); const code = functionName + '(' + list + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } else if (mode === 'GET_REMOVE') { const functionName = - Blockly.Dart.provideFunction_('lists_remove_random_item', [ - 'dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + + Dart.provideFunction_('lists_remove_random_item', [ + 'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List my_list) {', ' int x = new Math.Random().nextInt(my_list.length);', ' return my_list.removeAt(x);', '}' ]); const code = functionName + '(' + list + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } break; } @@ -234,23 +219,20 @@ Blockly.Dart['lists_getIndex'] = function(block) { throw Error('Unhandled combination (lists_getIndex).'); }; -Blockly.Dart['lists_setIndex'] = function(block) { +Dart['lists_setIndex'] = function(block) { // Set element at index. // Note: Until February 2013 this block did not have MODE or WHERE inputs. const mode = block.getFieldValue('MODE') || 'GET'; const where = block.getFieldValue('WHERE') || 'FROM_START'; - let list = Blockly.Dart.valueToCode(block, 'LIST', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]'; - const value = Blockly.Dart.valueToCode(block, 'TO', - Blockly.Dart.ORDER_ASSIGNMENT) || 'null'; + let list = Dart.valueToCode(block, 'LIST', Dart.ORDER_UNARY_POSTFIX) || '[]'; + const value = Dart.valueToCode(block, 'TO', Dart.ORDER_ASSIGNMENT) || 'null'; // Cache non-trivial values to variables to prevent repeated look-ups. // Closure, which accesses and modifies 'list'. function cacheList() { if (list.match(/^\w+$/)) { return ''; } - const listVar = Blockly.Dart.nameDB_.getDistinctName( - 'tmp_list', Blockly.VARIABLE_CATEGORY_NAME); + const listVar = Dart.nameDB_.getDistinctName('tmp_list', NameType.VARIABLE); const code = 'List ' + listVar + ' = ' + list + ';\n'; list = listVar; return code; @@ -273,7 +255,7 @@ Blockly.Dart['lists_setIndex'] = function(block) { } break; case 'FROM_START': { - const at = Blockly.Dart.getAdjusted(block, 'AT'); + const at = Dart.getAdjusted(block, 'AT'); if (mode === 'SET') { return list + '[' + at + '] = ' + value + ';\n'; } else if (mode === 'INSERT') { @@ -282,28 +264,24 @@ Blockly.Dart['lists_setIndex'] = function(block) { break; } case 'FROM_END': { - const at = Blockly.Dart.getAdjusted( - block, 'AT', 1, false, Blockly.Dart.ORDER_ADDITIVE); + const at = Dart.getAdjusted(block, 'AT', 1, false, Dart.ORDER_ADDITIVE); let code = cacheList(); if (mode === 'SET') { - code += list + '[' + list + '.length - ' + at + '] = ' + value + - ';\n'; + code += list + '[' + list + '.length - ' + at + '] = ' + value + ';\n'; return code; } else if (mode === 'INSERT') { - code += list + '.insert(' + list + '.length - ' + at + ', ' + - value + ');\n'; + code += list + '.insert(' + list + '.length - ' + at + ', ' + value + + ');\n'; return code; } break; } case 'RANDOM': { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; let code = cacheList(); - const xVar = Blockly.Dart.nameDB_.getDistinctName( - 'tmp_x', Blockly.VARIABLE_CATEGORY_NAME); - code += 'int ' + xVar + - ' = new Math.Random().nextInt(' + list + '.length);\n'; + const xVar = Dart.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE); + code += 'int ' + xVar + ' = new Math.Random().nextInt(' + list + + '.length);\n'; if (mode === 'SET') { code += list + '[' + xVar + '] = ' + value + ';\n'; return code; @@ -317,24 +295,24 @@ Blockly.Dart['lists_setIndex'] = function(block) { throw Error('Unhandled combination (lists_setIndex).'); }; -Blockly.Dart['lists_getSublist'] = function(block) { +Dart['lists_getSublist'] = function(block) { // Get sublist. - const list = Blockly.Dart.valueToCode(block, 'LIST', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]'; + const list = + Dart.valueToCode(block, 'LIST', Dart.ORDER_UNARY_POSTFIX) || '[]'; const where1 = block.getFieldValue('WHERE1'); const where2 = block.getFieldValue('WHERE2'); let code; - if (list.match(/^\w+$/) || (where1 !== 'FROM_END' && where2 === 'FROM_START')) { + if (list.match(/^\w+$/) || + (where1 !== 'FROM_END' && where2 === 'FROM_START')) { // If the list is a is a variable or doesn't require a call for length, // don't generate a helper function. let at1; switch (where1) { case 'FROM_START': - at1 = Blockly.Dart.getAdjusted(block, 'AT1'); + at1 = Dart.getAdjusted(block, 'AT1'); break; case 'FROM_END': - at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false, - Blockly.Dart.ORDER_ADDITIVE); + at1 = Dart.getAdjusted(block, 'AT1', 1, false, Dart.ORDER_ADDITIVE); at1 = list + '.length - ' + at1; break; case 'FIRST': @@ -346,11 +324,10 @@ Blockly.Dart['lists_getSublist'] = function(block) { let at2; switch (where2) { case 'FROM_START': - at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1); + at2 = Dart.getAdjusted(block, 'AT2', 1); break; case 'FROM_END': - at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false, - Blockly.Dart.ORDER_ADDITIVE); + at2 = Dart.getAdjusted(block, 'AT2', 0, false, Dart.ORDER_ADDITIVE); at2 = list + '.length - ' + at2; break; case 'LAST': @@ -365,68 +342,56 @@ Blockly.Dart['lists_getSublist'] = function(block) { code = list + '.sublist(' + at1 + ', ' + at2 + ')'; } } else { - const at1 = Blockly.Dart.getAdjusted(block, 'AT1'); - const at2 = Blockly.Dart.getAdjusted(block, 'AT2'); - const functionName = Blockly.Dart.provideFunction_( - 'lists_get_sublist', - ['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List list, String where1, num at1, String where2, num at2) {', - ' int getAt(String where, num at) {', - ' if (where == \'FROM_END\') {', - ' at = list.length - 1 - at;', - ' } else if (where == \'FIRST\') {', - ' at = 0;', - ' } else if (where == \'LAST\') {', - ' at = list.length - 1;', - ' } else if (where != \'FROM_START\') {', - ' throw \'Unhandled option (lists_getSublist).\';', - ' }', - ' return at;', - ' }', - ' at1 = getAt(where1, at1);', - ' at2 = getAt(where2, at2) + 1;', - ' return list.sublist(at1, at2);', - '}']); - code = functionName + '(' + list + ', \'' + where1 + '\', ' + at1 + - ', \'' + where2 + '\', ' + at2 + ')'; + const at1 = Dart.getAdjusted(block, 'AT1'); + const at2 = Dart.getAdjusted(block, 'AT2'); + const functionName = Dart.provideFunction_('lists_get_sublist', [ + 'List ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + + '(List list, String where1, num at1, String where2, num at2) {', + ' int getAt(String where, num at) {', ' if (where == \'FROM_END\') {', + ' at = list.length - 1 - at;', + ' } else if (where == \'FIRST\') {', ' at = 0;', + ' } else if (where == \'LAST\') {', ' at = list.length - 1;', + ' } else if (where != \'FROM_START\') {', + ' throw \'Unhandled option (lists_getSublist).\';', ' }', + ' return at;', ' }', ' at1 = getAt(where1, at1);', + ' at2 = getAt(where2, at2) + 1;', ' return list.sublist(at1, at2);', '}' + ]); + code = functionName + '(' + list + ', \'' + where1 + '\', ' + at1 + ', \'' + + where2 + '\', ' + at2 + ')'; } - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['lists_sort'] = function(block) { +Dart['lists_sort'] = function(block) { // Block for sorting a list. - const list = Blockly.Dart.valueToCode(block, 'LIST', - Blockly.Dart.ORDER_NONE) || '[]'; + const list = Dart.valueToCode(block, 'LIST', Dart.ORDER_NONE) || '[]'; const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1; const type = block.getFieldValue('TYPE'); - const sortFunctionName = Blockly.Dart.provideFunction_( - 'lists_sort', - ['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List list, String type, int direction) {', - ' var compareFuncs = {', - ' "NUMERIC": (a, b) => (direction * a.compareTo(b)).toInt(),', - ' "TEXT": (a, b) => direction * ' + - 'a.toString().compareTo(b.toString()),', - ' "IGNORE_CASE": ', - ' (a, b) => direction * ', - ' a.toString().toLowerCase().compareTo(b.toString().toLowerCase())', - ' };', - ' list = new List.from(list);', // Clone the list. - ' var compare = compareFuncs[type];', - ' list.sort(compare);', - ' return list;', - '}']); - return [sortFunctionName + '(' + list + ', ' + - '"' + type + '", ' + direction + ')', - Blockly.Dart.ORDER_UNARY_POSTFIX]; + const sortFunctionName = Dart.provideFunction_('lists_sort', [ + 'List ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + + '(List list, String type, int direction) {', + ' var compareFuncs = {', + ' "NUMERIC": (a, b) => (direction * a.compareTo(b)).toInt(),', + ' "TEXT": (a, b) => direction * ' + + 'a.toString().compareTo(b.toString()),', + ' "IGNORE_CASE": ', ' (a, b) => direction * ', + ' a.toString().toLowerCase().compareTo(b.toString().toLowerCase())', + ' };', + ' list = new List.from(list);', // Clone the list. + ' var compare = compareFuncs[type];', ' list.sort(compare);', + ' return list;', '}' + ]); + return [ + sortFunctionName + '(' + list + ', ' + + '"' + type + '", ' + direction + ')', + Dart.ORDER_UNARY_POSTFIX + ]; }; -Blockly.Dart['lists_split'] = function(block) { +Dart['lists_split'] = function(block) { // Block for splitting text into a list, or joining a list into text. - let input = Blockly.Dart.valueToCode(block, 'INPUT', - Blockly.Dart.ORDER_UNARY_POSTFIX); - const delimiter = Blockly.Dart.valueToCode(block, 'DELIM', - Blockly.Dart.ORDER_NONE) || '\'\''; + let input = Dart.valueToCode(block, 'INPUT', Dart.ORDER_UNARY_POSTFIX); + const delimiter = Dart.valueToCode(block, 'DELIM', Dart.ORDER_NONE) || '\'\''; const mode = block.getFieldValue('MODE'); let functionName; if (mode === 'SPLIT') { @@ -443,14 +408,13 @@ Blockly.Dart['lists_split'] = function(block) { throw Error('Unknown mode: ' + mode); } const code = input + '.' + functionName + '(' + delimiter + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['lists_reverse'] = function(block) { +Dart['lists_reverse'] = function(block) { // Block for reversing a list. - const list = Blockly.Dart.valueToCode(block, 'LIST', - Blockly.Dart.ORDER_NONE) || '[]'; + const list = Dart.valueToCode(block, 'LIST', Dart.ORDER_NONE) || '[]'; // XXX What should the operator precedence be for a `new`? const code = 'new List.from(' + list + '.reversed)'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; diff --git a/generators/dart/logic.js b/generators/dart/logic.js index ac493646e4e..56cdaef6ce2 100644 --- a/generators/dart/logic.js +++ b/generators/dart/logic.js @@ -9,73 +9,70 @@ */ 'use strict'; -goog.provide('Blockly.Dart.logic'); +goog.module('Blockly.Dart.logic'); -goog.require('Blockly.Dart'); +const Dart = goog.require('Blockly.Dart'); -Blockly.Dart['controls_if'] = function(block) { +Dart['controls_if'] = function(block) { // If/elseif/else condition. let n = 0; let code = '', branchCode, conditionCode; - if (Blockly.Dart.STATEMENT_PREFIX) { + if (Dart.STATEMENT_PREFIX) { // Automatic prefix insertion is switched off for this block. Add manually. - code += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_PREFIX, block); + code += Dart.injectId(Dart.STATEMENT_PREFIX, block); } do { - conditionCode = Blockly.Dart.valueToCode(block, 'IF' + n, - Blockly.Dart.ORDER_NONE) || 'false'; - branchCode = Blockly.Dart.statementToCode(block, 'DO' + n); - if (Blockly.Dart.STATEMENT_SUFFIX) { - branchCode = Blockly.Dart.prefixLines( - Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block), - Blockly.Dart.INDENT) + branchCode; + conditionCode = + Dart.valueToCode(block, 'IF' + n, Dart.ORDER_NONE) || 'false'; + branchCode = Dart.statementToCode(block, 'DO' + n); + if (Dart.STATEMENT_SUFFIX) { + branchCode = + Dart.prefixLines( + Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT) + + branchCode; } - code += (n > 0 ? 'else ' : '') + - 'if (' + conditionCode + ') {\n' + branchCode + '}'; + code += (n > 0 ? 'else ' : '') + 'if (' + conditionCode + ') {\n' + + branchCode + '}'; n++; } while (block.getInput('IF' + n)); - if (block.getInput('ELSE') || Blockly.Dart.STATEMENT_SUFFIX) { - branchCode = Blockly.Dart.statementToCode(block, 'ELSE'); - if (Blockly.Dart.STATEMENT_SUFFIX) { - branchCode = Blockly.Dart.prefixLines( - Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block), - Blockly.Dart.INDENT) + branchCode; + if (block.getInput('ELSE') || Dart.STATEMENT_SUFFIX) { + branchCode = Dart.statementToCode(block, 'ELSE'); + if (Dart.STATEMENT_SUFFIX) { + branchCode = + Dart.prefixLines( + Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT) + + branchCode; } code += ' else {\n' + branchCode + '}'; } return code + '\n'; }; -Blockly.Dart['controls_ifelse'] = Blockly.Dart['controls_if']; +Dart['controls_ifelse'] = Dart['controls_if']; -Blockly.Dart['logic_compare'] = function(block) { +Dart['logic_compare'] = function(block) { // Comparison operator. - const OPERATORS = { - 'EQ': '==', - 'NEQ': '!=', - 'LT': '<', - 'LTE': '<=', - 'GT': '>', - 'GTE': '>=' - }; + const OPERATORS = + {'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='}; const operator = OPERATORS[block.getFieldValue('OP')]; const order = (operator === '==' || operator === '!=') ? - Blockly.Dart.ORDER_EQUALITY : Blockly.Dart.ORDER_RELATIONAL; - const argument0 = Blockly.Dart.valueToCode(block, 'A', order) || '0'; - const argument1 = Blockly.Dart.valueToCode(block, 'B', order) || '0'; + Dart.ORDER_EQUALITY : + Dart.ORDER_RELATIONAL; + const argument0 = Dart.valueToCode(block, 'A', order) || '0'; + const argument1 = Dart.valueToCode(block, 'B', order) || '0'; const code = argument0 + ' ' + operator + ' ' + argument1; return [code, order]; }; -Blockly.Dart['logic_operation'] = function(block) { +Dart['logic_operation'] = function(block) { // Operations 'and', 'or'. const operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||'; - const order = (operator === '&&') ? Blockly.Dart.ORDER_LOGICAL_AND : - Blockly.Dart.ORDER_LOGICAL_OR; - let argument0 = Blockly.Dart.valueToCode(block, 'A', order); - let argument1 = Blockly.Dart.valueToCode(block, 'B', order); + const order = + (operator === '&&') ? Dart.ORDER_LOGICAL_AND : Dart.ORDER_LOGICAL_OR; + let argument0 = Dart.valueToCode(block, 'A', order); + let argument1 = Dart.valueToCode(block, 'B', order); if (!argument0 && !argument1) { // If there are no arguments, then the return value is false. argument0 = 'false'; @@ -94,33 +91,33 @@ Blockly.Dart['logic_operation'] = function(block) { return [code, order]; }; -Blockly.Dart['logic_negate'] = function(block) { +Dart['logic_negate'] = function(block) { // Negation. - const order = Blockly.Dart.ORDER_UNARY_PREFIX; - const argument0 = Blockly.Dart.valueToCode(block, 'BOOL', order) || 'true'; + const order = Dart.ORDER_UNARY_PREFIX; + const argument0 = Dart.valueToCode(block, 'BOOL', order) || 'true'; const code = '!' + argument0; return [code, order]; }; -Blockly.Dart['logic_boolean'] = function(block) { +Dart['logic_boolean'] = function(block) { // Boolean values true and false. const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false'; - return [code, Blockly.Dart.ORDER_ATOMIC]; + return [code, Dart.ORDER_ATOMIC]; }; -Blockly.Dart['logic_null'] = function(block) { +Dart['logic_null'] = function(block) { // Null data type. - return ['null', Blockly.Dart.ORDER_ATOMIC]; + return ['null', Dart.ORDER_ATOMIC]; }; -Blockly.Dart['logic_ternary'] = function(block) { +Dart['logic_ternary'] = function(block) { // Ternary operator. - const value_if = Blockly.Dart.valueToCode(block, 'IF', - Blockly.Dart.ORDER_CONDITIONAL) || 'false'; - const value_then = Blockly.Dart.valueToCode(block, 'THEN', - Blockly.Dart.ORDER_CONDITIONAL) || 'null'; - const value_else = Blockly.Dart.valueToCode(block, 'ELSE', - Blockly.Dart.ORDER_CONDITIONAL) || 'null'; + const value_if = + Dart.valueToCode(block, 'IF', Dart.ORDER_CONDITIONAL) || 'false'; + const value_then = + Dart.valueToCode(block, 'THEN', Dart.ORDER_CONDITIONAL) || 'null'; + const value_else = + Dart.valueToCode(block, 'ELSE', Dart.ORDER_CONDITIONAL) || 'null'; const code = value_if + ' ? ' + value_then + ' : ' + value_else; - return [code, Blockly.Dart.ORDER_CONDITIONAL]; + return [code, Dart.ORDER_CONDITIONAL]; }; diff --git a/generators/dart/loops.js b/generators/dart/loops.js index 63a7762bb16..0360737b8f3 100644 --- a/generators/dart/loops.js +++ b/generators/dart/loops.js @@ -6,17 +6,17 @@ /** * @fileoverview Generating Dart for loop blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.Dart.loops'); +goog.module('Blockly.Dart.loops'); -goog.require('Blockly.Dart'); -goog.require('Blockly.utils.string'); +const Dart = goog.require('Blockly.Dart'); +const stringUtils = goog.require('Blockly.utils.string'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.Dart['controls_repeat_ext'] = function(block) { +Dart['controls_repeat_ext'] = function(block) { let repeats; // Repeat n times. if (block.getField('TIMES')) { @@ -24,63 +24,56 @@ Blockly.Dart['controls_repeat_ext'] = function(block) { repeats = String(Number(block.getFieldValue('TIMES'))); } else { // External number. - repeats = Blockly.Dart.valueToCode(block, 'TIMES', - Blockly.Dart.ORDER_ASSIGNMENT) || '0'; + repeats = Dart.valueToCode(block, 'TIMES', Dart.ORDER_ASSIGNMENT) || '0'; } - let branch = Blockly.Dart.statementToCode(block, 'DO'); - branch = Blockly.Dart.addLoopTrap(branch, block); + let branch = Dart.statementToCode(block, 'DO'); + branch = Dart.addLoopTrap(branch, block); let code = ''; - const loopVar = Blockly.Dart.nameDB_.getDistinctName( - 'count', Blockly.VARIABLE_CATEGORY_NAME); + const loopVar = Dart.nameDB_.getDistinctName('count', NameType.VARIABLE); let endVar = repeats; - if (!repeats.match(/^\w+$/) && !Blockly.utils.string.isNumber(repeats)) { - endVar = Blockly.Dart.nameDB_.getDistinctName( - 'repeat_end', Blockly.VARIABLE_CATEGORY_NAME); + if (!repeats.match(/^\w+$/) && !stringUtils.isNumber(repeats)) { + endVar = Dart.nameDB_.getDistinctName('repeat_end', NameType.VARIABLE); code += 'var ' + endVar + ' = ' + repeats + ';\n'; } - code += 'for (int ' + loopVar + ' = 0; ' + - loopVar + ' < ' + endVar + '; ' + - loopVar + '++) {\n' + - branch + '}\n'; + code += 'for (int ' + loopVar + ' = 0; ' + loopVar + ' < ' + endVar + '; ' + + loopVar + '++) {\n' + branch + '}\n'; return code; }; -Blockly.Dart['controls_repeat'] = Blockly.Dart['controls_repeat_ext']; +Dart['controls_repeat'] = Dart['controls_repeat_ext']; -Blockly.Dart['controls_whileUntil'] = function(block) { +Dart['controls_whileUntil'] = function(block) { // Do while/until loop. const until = block.getFieldValue('MODE') === 'UNTIL'; - let argument0 = Blockly.Dart.valueToCode(block, 'BOOL', - until ? Blockly.Dart.ORDER_UNARY_PREFIX : - Blockly.Dart.ORDER_NONE) || 'false'; - let branch = Blockly.Dart.statementToCode(block, 'DO'); - branch = Blockly.Dart.addLoopTrap(branch, block); + let argument0 = + Dart.valueToCode( + block, 'BOOL', until ? Dart.ORDER_UNARY_PREFIX : Dart.ORDER_NONE) || + 'false'; + let branch = Dart.statementToCode(block, 'DO'); + branch = Dart.addLoopTrap(branch, block); if (until) { argument0 = '!' + argument0; } return 'while (' + argument0 + ') {\n' + branch + '}\n'; }; -Blockly.Dart['controls_for'] = function(block) { +Dart['controls_for'] = function(block) { // For loop. - const variable0 = Blockly.Dart.nameDB_.getName( - block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - const argument0 = Blockly.Dart.valueToCode(block, 'FROM', - Blockly.Dart.ORDER_ASSIGNMENT) || '0'; - const argument1 = Blockly.Dart.valueToCode(block, 'TO', - Blockly.Dart.ORDER_ASSIGNMENT) || '0'; - const increment = Blockly.Dart.valueToCode(block, 'BY', - Blockly.Dart.ORDER_ASSIGNMENT) || '1'; - let branch = Blockly.Dart.statementToCode(block, 'DO'); - branch = Blockly.Dart.addLoopTrap(branch, block); + const variable0 = + Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + const argument0 = + Dart.valueToCode(block, 'FROM', Dart.ORDER_ASSIGNMENT) || '0'; + const argument1 = Dart.valueToCode(block, 'TO', Dart.ORDER_ASSIGNMENT) || '0'; + const increment = Dart.valueToCode(block, 'BY', Dart.ORDER_ASSIGNMENT) || '1'; + let branch = Dart.statementToCode(block, 'DO'); + branch = Dart.addLoopTrap(branch, block); let code; - if (Blockly.utils.string.isNumber(argument0) && Blockly.utils.string.isNumber(argument1) && - Blockly.utils.string.isNumber(increment)) { + if (stringUtils.isNumber(argument0) && stringUtils.isNumber(argument1) && + stringUtils.isNumber(increment)) { // All arguments are simple numbers. const up = Number(argument0) <= Number(argument1); - code = 'for (' + variable0 + ' = ' + argument0 + '; ' + - variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' + - variable0; + code = 'for (' + variable0 + ' = ' + argument0 + '; ' + variable0 + + (up ? ' <= ' : ' >= ') + argument1 + '; ' + variable0; const step = Math.abs(Number(increment)); if (step === 1) { code += up ? '++' : '--'; @@ -92,72 +85,70 @@ Blockly.Dart['controls_for'] = function(block) { code = ''; // Cache non-trivial values to variables to prevent repeated look-ups. let startVar = argument0; - if (!argument0.match(/^\w+$/) && !Blockly.utils.string.isNumber(argument0)) { - startVar = Blockly.Dart.nameDB_.getDistinctName( - variable0 + '_start', Blockly.VARIABLE_CATEGORY_NAME); + if (!argument0.match(/^\w+$/) && !stringUtils.isNumber(argument0)) { + startVar = + Dart.nameDB_.getDistinctName(variable0 + '_start', NameType.VARIABLE); code += 'var ' + startVar + ' = ' + argument0 + ';\n'; } let endVar = argument1; - if (!argument1.match(/^\w+$/) && !Blockly.utils.string.isNumber(argument1)) { - endVar = Blockly.Dart.nameDB_.getDistinctName( - variable0 + '_end', Blockly.VARIABLE_CATEGORY_NAME); + if (!argument1.match(/^\w+$/) && !stringUtils.isNumber(argument1)) { + endVar = + Dart.nameDB_.getDistinctName(variable0 + '_end', NameType.VARIABLE); code += 'var ' + endVar + ' = ' + argument1 + ';\n'; } // Determine loop direction at start, in case one of the bounds // changes during loop execution. - const incVar = Blockly.Dart.nameDB_.getDistinctName( - variable0 + '_inc', Blockly.VARIABLE_CATEGORY_NAME); + const incVar = + Dart.nameDB_.getDistinctName(variable0 + '_inc', NameType.VARIABLE); code += 'num ' + incVar + ' = '; - if (Blockly.utils.string.isNumber(increment)) { + if (stringUtils.isNumber(increment)) { code += Math.abs(increment) + ';\n'; } else { code += '(' + increment + ').abs();\n'; } code += 'if (' + startVar + ' > ' + endVar + ') {\n'; - code += Blockly.Dart.INDENT + incVar + ' = -' + incVar + ';\n'; + code += Dart.INDENT + incVar + ' = -' + incVar + ';\n'; code += '}\n'; - code += 'for (' + variable0 + ' = ' + startVar + '; ' + - incVar + ' >= 0 ? ' + - variable0 + ' <= ' + endVar + ' : ' + - variable0 + ' >= ' + endVar + '; ' + - variable0 + ' += ' + incVar + ') {\n' + + code += 'for (' + variable0 + ' = ' + startVar + '; ' + incVar + + ' >= 0 ? ' + variable0 + ' <= ' + endVar + ' : ' + variable0 + + ' >= ' + endVar + '; ' + variable0 + ' += ' + incVar + ') {\n' + branch + '}\n'; } return code; }; -Blockly.Dart['controls_forEach'] = function(block) { +Dart['controls_forEach'] = function(block) { // For each loop. - const variable0 = Blockly.Dart.nameDB_.getName( - block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - const argument0 = Blockly.Dart.valueToCode(block, 'LIST', - Blockly.Dart.ORDER_ASSIGNMENT) || '[]'; - let branch = Blockly.Dart.statementToCode(block, 'DO'); - branch = Blockly.Dart.addLoopTrap(branch, block); - const code = 'for (var ' + variable0 + ' in ' + argument0 + ') {\n' + - branch + '}\n'; + const variable0 = + Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + const argument0 = + Dart.valueToCode(block, 'LIST', Dart.ORDER_ASSIGNMENT) || '[]'; + let branch = Dart.statementToCode(block, 'DO'); + branch = Dart.addLoopTrap(branch, block); + const code = + 'for (var ' + variable0 + ' in ' + argument0 + ') {\n' + branch + '}\n'; return code; }; -Blockly.Dart['controls_flow_statements'] = function(block) { +Dart['controls_flow_statements'] = function(block) { // Flow statements: continue, break. let xfix = ''; - if (Blockly.Dart.STATEMENT_PREFIX) { + if (Dart.STATEMENT_PREFIX) { // Automatic prefix insertion is switched off for this block. Add manually. - xfix += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_PREFIX, block); + xfix += Dart.injectId(Dart.STATEMENT_PREFIX, block); } - if (Blockly.Dart.STATEMENT_SUFFIX) { + if (Dart.STATEMENT_SUFFIX) { // Inject any statement suffix here since the regular one at the end // will not get executed if the break/continue is triggered. - xfix += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block); + xfix += Dart.injectId(Dart.STATEMENT_SUFFIX, block); } - if (Blockly.Dart.STATEMENT_PREFIX) { + if (Dart.STATEMENT_PREFIX) { const loop = block.getSurroundLoop(); if (loop && !loop.suppressPrefixSuffix) { // Inject loop's statement prefix here since the regular one at the end // of the loop will not get executed if 'continue' is triggered. // In the case of 'break', a prefix is needed due to the loop's suffix. - xfix += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_PREFIX, loop); + xfix += Dart.injectId(Dart.STATEMENT_PREFIX, loop); } } switch (block.getFieldValue('FLOW')) { diff --git a/generators/dart/math.js b/generators/dart/math.js index c1b9c59e225..24b7e268e4f 100644 --- a/generators/dart/math.js +++ b/generators/dart/math.js @@ -6,89 +6,82 @@ /** * @fileoverview Generating Dart for math blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.Dart.math'); +goog.module('Blockly.Dart.math'); -goog.require('Blockly.Dart'); +const Dart = goog.require('Blockly.Dart'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.Dart.addReservedWords('Math'); +Dart.addReservedWords('Math'); -Blockly.Dart['math_number'] = function(block) { +Dart['math_number'] = function(block) { // Numeric value. let code = Number(block.getFieldValue('NUM')); let order; if (code === Infinity) { code = 'double.infinity'; - order = Blockly.Dart.ORDER_UNARY_POSTFIX; + order = Dart.ORDER_UNARY_POSTFIX; } else if (code === -Infinity) { code = '-double.infinity'; - order = Blockly.Dart.ORDER_UNARY_PREFIX; + order = Dart.ORDER_UNARY_PREFIX; } else { // -4.abs() returns -4 in Dart due to strange order of operation choices. // -4 is actually an operator and a number. Reflect this in the order. - order = code < 0 ? - Blockly.Dart.ORDER_UNARY_PREFIX : Blockly.Dart.ORDER_ATOMIC; + order = code < 0 ? Dart.ORDER_UNARY_PREFIX : Dart.ORDER_ATOMIC; } return [code, order]; }; -Blockly.Dart['math_arithmetic'] = function(block) { +Dart['math_arithmetic'] = function(block) { // Basic arithmetic operators, and power. const OPERATORS = { - 'ADD': [' + ', Blockly.Dart.ORDER_ADDITIVE], - 'MINUS': [' - ', Blockly.Dart.ORDER_ADDITIVE], - 'MULTIPLY': [' * ', Blockly.Dart.ORDER_MULTIPLICATIVE], - 'DIVIDE': [' / ', Blockly.Dart.ORDER_MULTIPLICATIVE], - 'POWER': [null, Blockly.Dart.ORDER_NONE] // Handle power separately. + 'ADD': [' + ', Dart.ORDER_ADDITIVE], + 'MINUS': [' - ', Dart.ORDER_ADDITIVE], + 'MULTIPLY': [' * ', Dart.ORDER_MULTIPLICATIVE], + 'DIVIDE': [' / ', Dart.ORDER_MULTIPLICATIVE], + 'POWER': [null, Dart.ORDER_NONE] // Handle power separately. }; const tuple = OPERATORS[block.getFieldValue('OP')]; const operator = tuple[0]; const order = tuple[1]; - const argument0 = Blockly.Dart.valueToCode(block, 'A', order) || '0'; - const argument1 = Blockly.Dart.valueToCode(block, 'B', order) || '0'; + const argument0 = Dart.valueToCode(block, 'A', order) || '0'; + const argument1 = Dart.valueToCode(block, 'B', order) || '0'; let code; // Power in Dart requires a special case since it has no operator. if (!operator) { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; code = 'Math.pow(' + argument0 + ', ' + argument1 + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } code = argument0 + operator + argument1; return [code, order]; }; -Blockly.Dart['math_single'] = function(block) { +Dart['math_single'] = function(block) { // Math operators with single operand. const operator = block.getFieldValue('OP'); let code; let arg; if (operator === 'NEG') { // Negation is a special case given its different operator precedence. - arg = Blockly.Dart.valueToCode(block, 'NUM', - Blockly.Dart.ORDER_UNARY_PREFIX) || '0'; + arg = Dart.valueToCode(block, 'NUM', Dart.ORDER_UNARY_PREFIX) || '0'; if (arg[0] === '-') { // --3 is not legal in Dart. arg = ' ' + arg; } code = '-' + arg; - return [code, Blockly.Dart.ORDER_UNARY_PREFIX]; + return [code, Dart.ORDER_UNARY_PREFIX]; } - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; if (operator === 'ABS' || operator.substring(0, 5) === 'ROUND') { - arg = Blockly.Dart.valueToCode(block, 'NUM', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '0'; + arg = Dart.valueToCode(block, 'NUM', Dart.ORDER_UNARY_POSTFIX) || '0'; } else if (operator === 'SIN' || operator === 'COS' || operator === 'TAN') { - arg = Blockly.Dart.valueToCode(block, 'NUM', - Blockly.Dart.ORDER_MULTIPLICATIVE) || '0'; + arg = Dart.valueToCode(block, 'NUM', Dart.ORDER_MULTIPLICATIVE) || '0'; } else { - arg = Blockly.Dart.valueToCode(block, 'NUM', - Blockly.Dart.ORDER_NONE) || '0'; + arg = Dart.valueToCode(block, 'NUM', Dart.ORDER_NONE) || '0'; } // First, handle cases which generate values that don't need parentheses // wrapping the code. @@ -128,7 +121,7 @@ Blockly.Dart['math_single'] = function(block) { break; } if (code) { - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } // Second, handle cases which generate values that may need parentheses // wrapping the code. @@ -148,65 +141,55 @@ Blockly.Dart['math_single'] = function(block) { default: throw Error('Unknown math operator: ' + operator); } - return [code, Blockly.Dart.ORDER_MULTIPLICATIVE]; + return [code, Dart.ORDER_MULTIPLICATIVE]; }; -Blockly.Dart['math_constant'] = function(block) { +Dart['math_constant'] = function(block) { // Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY. const CONSTANTS = { - 'PI': ['Math.pi', Blockly.Dart.ORDER_UNARY_POSTFIX], - 'E': ['Math.e', Blockly.Dart.ORDER_UNARY_POSTFIX], - 'GOLDEN_RATIO': - ['(1 + Math.sqrt(5)) / 2', Blockly.Dart.ORDER_MULTIPLICATIVE], - 'SQRT2': ['Math.sqrt2', Blockly.Dart.ORDER_UNARY_POSTFIX], - 'SQRT1_2': ['Math.sqrt1_2', Blockly.Dart.ORDER_UNARY_POSTFIX], - 'INFINITY': ['double.infinity', Blockly.Dart.ORDER_ATOMIC] + 'PI': ['Math.pi', Dart.ORDER_UNARY_POSTFIX], + 'E': ['Math.e', Dart.ORDER_UNARY_POSTFIX], + 'GOLDEN_RATIO': ['(1 + Math.sqrt(5)) / 2', Dart.ORDER_MULTIPLICATIVE], + 'SQRT2': ['Math.sqrt2', Dart.ORDER_UNARY_POSTFIX], + 'SQRT1_2': ['Math.sqrt1_2', Dart.ORDER_UNARY_POSTFIX], + 'INFINITY': ['double.infinity', Dart.ORDER_ATOMIC] }; const constant = block.getFieldValue('CONSTANT'); if (constant !== 'INFINITY') { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; } return CONSTANTS[constant]; }; -Blockly.Dart['math_number_property'] = function(block) { +Dart['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. - const number_to_check = Blockly.Dart.valueToCode(block, 'NUMBER_TO_CHECK', - Blockly.Dart.ORDER_MULTIPLICATIVE); + const number_to_check = + Dart.valueToCode(block, 'NUMBER_TO_CHECK', Dart.ORDER_MULTIPLICATIVE); if (!number_to_check) { - return ['false', Blockly.Dart.ORDER_ATOMIC]; + return ['false', Dart.ORDER_ATOMIC]; } 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. - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( - 'math_isPrime', - ['bool ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '(n) {', - ' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods', - ' if (n == 2 || n == 3) {', - ' return true;', - ' }', - ' // False if n is null, negative, is 1, or not whole.', - ' // And false if n is divisible by 2 or 3.', - ' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' + - ' n % 3 == 0) {', - ' return false;', - ' }', - ' // Check all the numbers of form 6k +/- 1, up to sqrt(n).', - ' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {', - ' if (n % (x - 1) == 0 || n % (x + 1) == 0) {', - ' return false;', - ' }', - ' }', - ' return true;', - '}']); + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const functionName = Dart.provideFunction_('math_isPrime', [ + 'bool ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(n) {', + ' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods', + ' if (n == 2 || n == 3) {', ' return true;', ' }', + ' // False if n is null, negative, is 1, or not whole.', + ' // And false if n is divisible by 2 or 3.', + ' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' + + ' n % 3 == 0) {', + ' return false;', ' }', + ' // Check all the numbers of form 6k +/- 1, up to sqrt(n).', + ' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {', + ' if (n % (x - 1) == 0 || n % (x + 1) == 0) {', ' return false;', + ' }', ' }', ' return true;', '}' + ]); code = functionName + '(' + number_to_check + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } switch (dropdown_property) { case 'EVEN': @@ -225,269 +208,227 @@ Blockly.Dart['math_number_property'] = function(block) { code = number_to_check + ' < 0'; break; case 'DIVISIBLE_BY': - const divisor = Blockly.Dart.valueToCode(block, 'DIVISOR', - Blockly.Dart.ORDER_MULTIPLICATIVE); + const divisor = + Dart.valueToCode(block, 'DIVISOR', Dart.ORDER_MULTIPLICATIVE); if (!divisor) { - return ['false', Blockly.Dart.ORDER_ATOMIC]; + return ['false', Dart.ORDER_ATOMIC]; } code = number_to_check + ' % ' + divisor + ' == 0'; break; } - return [code, Blockly.Dart.ORDER_EQUALITY]; + return [code, Dart.ORDER_EQUALITY]; }; -Blockly.Dart['math_change'] = function(block) { +Dart['math_change'] = function(block) { // Add to a variable in place. - const argument0 = Blockly.Dart.valueToCode(block, 'DELTA', - Blockly.Dart.ORDER_ADDITIVE) || '0'; - const varName = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'), - Blockly.VARIABLE_CATEGORY_NAME); + const argument0 = + Dart.valueToCode(block, 'DELTA', Dart.ORDER_ADDITIVE) || '0'; + const varName = + Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); return varName + ' = (' + varName + ' is num ? ' + varName + ' : 0) + ' + argument0 + ';\n'; }; // Rounding functions have a single operand. -Blockly.Dart['math_round'] = Blockly.Dart['math_single']; +Dart['math_round'] = Dart['math_single']; // Trigonometry functions have a single operand. -Blockly.Dart['math_trig'] = Blockly.Dart['math_single']; +Dart['math_trig'] = Dart['math_single']; -Blockly.Dart['math_on_list'] = function(block) { +Dart['math_on_list'] = function(block) { // Math functions for lists. const func = block.getFieldValue('OP'); - const list = Blockly.Dart.valueToCode(block, 'LIST', - Blockly.Dart.ORDER_NONE) || '[]'; + const list = Dart.valueToCode(block, 'LIST', Dart.ORDER_NONE) || '[]'; let code; switch (func) { case 'SUM': { - const functionName = Blockly.Dart.provideFunction_( - 'math_sum', - ['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List myList) {', - ' num sumVal = 0;', - ' myList.forEach((num entry) {sumVal += entry;});', - ' return sumVal;', - '}']); + const functionName = Dart.provideFunction_('math_sum', [ + 'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {', + ' num sumVal = 0;', + ' myList.forEach((num entry) {sumVal += entry;});', ' return sumVal;', + '}' + ]); code = functionName + '(' + list + ')'; break; } case 'MIN': { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( - 'math_min', - ['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List myList) {', - ' if (myList.isEmpty) return null;', - ' num minVal = myList[0];', - ' myList.forEach((num entry) ' + - '{minVal = Math.min(minVal, entry);});', - ' return minVal;', - '}']); + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const functionName = Dart.provideFunction_('math_min', [ + 'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {', + ' if (myList.isEmpty) return null;', ' num minVal = myList[0];', + ' myList.forEach((num entry) ' + + '{minVal = Math.min(minVal, entry);});', + ' return minVal;', '}' + ]); code = functionName + '(' + list + ')'; break; } case 'MAX': { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( - 'math_max', - ['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List myList) {', - ' if (myList.isEmpty) return null;', - ' num maxVal = myList[0];', - ' myList.forEach((num entry) ' + - '{maxVal = Math.max(maxVal, entry);});', - ' return maxVal;', - '}']); + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const functionName = Dart.provideFunction_('math_max', [ + 'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {', + ' if (myList.isEmpty) return null;', ' num maxVal = myList[0];', + ' myList.forEach((num entry) ' + + '{maxVal = Math.max(maxVal, entry);});', + ' return maxVal;', '}' + ]); code = functionName + '(' + list + ')'; break; } case 'AVERAGE': { // This operation exclude null and values that are not int or float: // math_mean([null,null,"aString",1,9]) -> 5.0 - const functionName = Blockly.Dart.provideFunction_( - 'math_mean', - ['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List myList) {', - ' // First filter list for numbers only.', - ' List localList = new List.from(myList);', - ' localList.removeWhere((a) => a is! num);', - ' if (localList.isEmpty) return null;', - ' num sumVal = 0;', - ' localList.forEach((var entry) {sumVal += entry;});', - ' return sumVal / localList.length;', - '}']); + const functionName = Dart.provideFunction_('math_mean', [ + 'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {', + ' // First filter list for numbers only.', + ' List localList = new List.from(myList);', + ' localList.removeWhere((a) => a is! num);', + ' if (localList.isEmpty) return null;', ' num sumVal = 0;', + ' localList.forEach((var entry) {sumVal += entry;});', + ' return sumVal / localList.length;', '}' + ]); code = functionName + '(' + list + ')'; break; } case 'MEDIAN': { - const functionName = Blockly.Dart.provideFunction_( - 'math_median', - ['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List myList) {', - ' // First filter list for numbers only, then sort, ' + - 'then return middle value', - ' // or the average of two middle values if list has an ' + - 'even number of elements.', - ' List localList = new List.from(myList);', - ' localList.removeWhere((a) => a is! num);', - ' if (localList.isEmpty) return null;', - ' localList.sort((a, b) => (a - b));', - ' int index = localList.length ~/ 2;', - ' if (localList.length % 2 == 1) {', - ' return localList[index];', - ' } else {', - ' return (localList[index - 1] + localList[index]) / 2;', - ' }', - '}']); + const functionName = Dart.provideFunction_('math_median', [ + 'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {', + ' // First filter list for numbers only, then sort, ' + + 'then return middle value', + ' // or the average of two middle values if list has an ' + + 'even number of elements.', + ' List localList = new List.from(myList);', + ' localList.removeWhere((a) => a is! num);', + ' if (localList.isEmpty) return null;', + ' localList.sort((a, b) => (a - b));', + ' int index = localList.length ~/ 2;', + ' if (localList.length % 2 == 1) {', ' return localList[index];', + ' } else {', + ' return (localList[index - 1] + localList[index]) / 2;', ' }', '}' + ]); code = functionName + '(' + list + ')'; break; } case 'MODE': { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; // 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] - const functionName = Blockly.Dart.provideFunction_( - 'math_modes', - ['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List values) {', - ' List modes = [];', - ' List counts = [];', - ' int maxCount = 0;', - ' for (int i = 0; i < values.length; i++) {', - ' var value = values[i];', - ' bool found = false;', - ' int thisCount;', - ' for (int j = 0; j < counts.length; j++) {', - ' if (counts[j][0] == value) {', - ' thisCount = ++counts[j][1];', - ' found = true;', - ' break;', - ' }', - ' }', - ' if (!found) {', - ' counts.add([value, 1]);', - ' thisCount = 1;', - ' }', - ' maxCount = Math.max(thisCount, maxCount);', - ' }', - ' for (int j = 0; j < counts.length; j++) {', - ' if (counts[j][1] == maxCount) {', - ' modes.add(counts[j][0]);', - ' }', - ' }', - ' return modes;', - '}']); + const functionName = Dart.provideFunction_('math_modes', [ + 'List ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List values) {', + ' List modes = [];', + ' List counts = [];', + ' int maxCount = 0;', + ' for (int i = 0; i < values.length; i++) {', + ' var value = values[i];', + ' bool found = false;', + ' int thisCount;', + ' for (int j = 0; j < counts.length; j++) {', + ' if (counts[j][0] == value) {', + ' thisCount = ++counts[j][1];', + ' found = true;', + ' break;', + ' }', + ' }', + ' if (!found) {', + ' counts.add([value, 1]);', + ' thisCount = 1;', + ' }', + ' maxCount = Math.max(thisCount, maxCount);', + ' }', + ' for (int j = 0; j < counts.length; j++) {', + ' if (counts[j][1] == maxCount) {', + ' modes.add(counts[j][0]);', + ' }', + ' }', + ' return modes;', + '}' + ]); code = functionName + '(' + list + ')'; break; } case 'STD_DEV': { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( - 'math_standard_deviation', - ['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List myList) {', - ' // First filter list for numbers only.', - ' List numbers = new List.from(myList);', - ' numbers.removeWhere((a) => a is! num);', - ' if (numbers.isEmpty) return null;', - ' num n = numbers.length;', - ' num sum = 0;', - ' numbers.forEach((x) => sum += x);', - ' num mean = sum / n;', - ' num sumSquare = 0;', - ' numbers.forEach((x) => sumSquare += ' + - 'Math.pow(x - mean, 2));', - ' return Math.sqrt(sumSquare / n);', - '}']); + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const functionName = Dart.provideFunction_('math_standard_deviation', [ + 'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {', + ' // First filter list for numbers only.', + ' List numbers = new List.from(myList);', + ' numbers.removeWhere((a) => a is! num);', + ' if (numbers.isEmpty) return null;', ' num n = numbers.length;', + ' num sum = 0;', ' numbers.forEach((x) => sum += x);', + ' num mean = sum / n;', ' num sumSquare = 0;', + ' numbers.forEach((x) => sumSquare += ' + + 'Math.pow(x - mean, 2));', + ' return Math.sqrt(sumSquare / n);', '}' + ]); code = functionName + '(' + list + ')'; break; } case 'RANDOM': { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( - 'math_random_item', - ['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(List myList) {', - ' int x = new Math.Random().nextInt(myList.length);', - ' return myList[x];', - '}']); + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const functionName = Dart.provideFunction_('math_random_item', [ + 'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {', + ' int x = new Math.Random().nextInt(myList.length);', + ' return myList[x];', '}' + ]); code = functionName + '(' + list + ')'; break; } default: throw Error('Unknown operator: ' + func); } - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['math_modulo'] = function(block) { +Dart['math_modulo'] = function(block) { // Remainder computation. - const argument0 = Blockly.Dart.valueToCode(block, 'DIVIDEND', - Blockly.Dart.ORDER_MULTIPLICATIVE) || '0'; - const argument1 = Blockly.Dart.valueToCode(block, 'DIVISOR', - Blockly.Dart.ORDER_MULTIPLICATIVE) || '0'; + const argument0 = + Dart.valueToCode(block, 'DIVIDEND', Dart.ORDER_MULTIPLICATIVE) || '0'; + const argument1 = + Dart.valueToCode(block, 'DIVISOR', Dart.ORDER_MULTIPLICATIVE) || '0'; const code = argument0 + ' % ' + argument1; - return [code, Blockly.Dart.ORDER_MULTIPLICATIVE]; + return [code, Dart.ORDER_MULTIPLICATIVE]; }; -Blockly.Dart['math_constrain'] = function(block) { +Dart['math_constrain'] = function(block) { // Constrain a number between two limits. - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const argument0 = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_NONE) || '0'; - const argument1 = Blockly.Dart.valueToCode(block, 'LOW', - Blockly.Dart.ORDER_NONE) || '0'; - const argument2 = Blockly.Dart.valueToCode(block, 'HIGH', - Blockly.Dart.ORDER_NONE) || 'double.infinity'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const argument0 = Dart.valueToCode(block, 'VALUE', Dart.ORDER_NONE) || '0'; + const argument1 = Dart.valueToCode(block, 'LOW', Dart.ORDER_NONE) || '0'; + const argument2 = + Dart.valueToCode(block, 'HIGH', Dart.ORDER_NONE) || 'double.infinity'; const code = 'Math.min(Math.max(' + argument0 + ', ' + argument1 + '), ' + argument2 + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['math_random_int'] = function(block) { +Dart['math_random_int'] = function(block) { // Random integer between [X] and [Y]. - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const argument0 = Blockly.Dart.valueToCode(block, 'FROM', - Blockly.Dart.ORDER_NONE) || '0'; - const argument1 = Blockly.Dart.valueToCode(block, 'TO', - Blockly.Dart.ORDER_NONE) || '0'; - const functionName = Blockly.Dart.provideFunction_( - 'math_random_int', - ['int ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '(num a, num b) {', - ' if (a > b) {', - ' // Swap a and b to ensure a is smaller.', - ' num c = a;', - ' a = b;', - ' b = c;', - ' }', - ' return new Math.Random().nextInt(b - a + 1) + a;', - '}']); + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const argument0 = Dart.valueToCode(block, 'FROM', Dart.ORDER_NONE) || '0'; + const argument1 = Dart.valueToCode(block, 'TO', Dart.ORDER_NONE) || '0'; + const functionName = Dart.provideFunction_('math_random_int', [ + 'int ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(num a, num b) {', + ' if (a > b) {', ' // Swap a and b to ensure a is smaller.', + ' num c = a;', ' a = b;', ' b = c;', ' }', + ' return new Math.Random().nextInt(b - a + 1) + a;', '}' + ]); const code = functionName + '(' + argument0 + ', ' + argument1 + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['math_random_float'] = function(block) { +Dart['math_random_float'] = function(block) { // Random fraction between 0 and 1. - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - return ['new Math.Random().nextDouble()', Blockly.Dart.ORDER_UNARY_POSTFIX]; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + return ['new Math.Random().nextDouble()', Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['math_atan2'] = function(block) { +Dart['math_atan2'] = function(block) { // Arctangent of point (X, Y) in degrees from -180 to 180. - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const argument0 = Blockly.Dart.valueToCode(block, 'X', - Blockly.Dart.ORDER_NONE) || '0'; - const argument1 = Blockly.Dart.valueToCode(block, 'Y', - Blockly.Dart.ORDER_NONE) || '0'; - return ['Math.atan2(' + argument1 + ', ' + argument0 + ') / Math.pi * 180', - Blockly.Dart.ORDER_MULTIPLICATIVE]; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const argument0 = Dart.valueToCode(block, 'X', Dart.ORDER_NONE) || '0'; + const argument1 = Dart.valueToCode(block, 'Y', Dart.ORDER_NONE) || '0'; + return [ + 'Math.atan2(' + argument1 + ', ' + argument0 + ') / Math.pi * 180', + Dart.ORDER_MULTIPLICATIVE + ]; }; diff --git a/generators/dart/procedures.js b/generators/dart/procedures.js index f5d17660fc2..1427c0c4e5a 100644 --- a/generators/dart/procedures.js +++ b/generators/dart/procedures.js @@ -6,105 +6,99 @@ /** * @fileoverview Generating Dart for procedure blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.Dart.procedures'); +goog.module('Blockly.Dart.procedures'); -goog.require('Blockly.Dart'); +const Dart = goog.require('Blockly.Dart'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.Dart['procedures_defreturn'] = function(block) { +Dart['procedures_defreturn'] = function(block) { // Define a procedure with a return value. - const funcName = Blockly.Dart.nameDB_.getName(block.getFieldValue('NAME'), - Blockly.PROCEDURE_CATEGORY_NAME); + const funcName = + Dart.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE); let xfix1 = ''; - if (Blockly.Dart.STATEMENT_PREFIX) { - xfix1 += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_PREFIX, block); + if (Dart.STATEMENT_PREFIX) { + xfix1 += Dart.injectId(Dart.STATEMENT_PREFIX, block); } - if (Blockly.Dart.STATEMENT_SUFFIX) { - xfix1 += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block); + if (Dart.STATEMENT_SUFFIX) { + xfix1 += Dart.injectId(Dart.STATEMENT_SUFFIX, block); } if (xfix1) { - xfix1 = Blockly.Dart.prefixLines(xfix1, Blockly.Dart.INDENT); + xfix1 = Dart.prefixLines(xfix1, Dart.INDENT); } let loopTrap = ''; - if (Blockly.Dart.INFINITE_LOOP_TRAP) { - loopTrap = Blockly.Dart.prefixLines( - Blockly.Dart.injectId(Blockly.Dart.INFINITE_LOOP_TRAP, block), - Blockly.Dart.INDENT); + if (Dart.INFINITE_LOOP_TRAP) { + loopTrap = Dart.prefixLines( + Dart.injectId(Dart.INFINITE_LOOP_TRAP, block), Dart.INDENT); } - const branch = Blockly.Dart.statementToCode(block, 'STACK'); - let returnValue = Blockly.Dart.valueToCode(block, 'RETURN', - Blockly.Dart.ORDER_NONE) || ''; + const branch = Dart.statementToCode(block, 'STACK'); + let returnValue = Dart.valueToCode(block, 'RETURN', Dart.ORDER_NONE) || ''; let xfix2 = ''; if (branch && returnValue) { // After executing the function body, revisit this block for the return. xfix2 = xfix1; } if (returnValue) { - returnValue = Blockly.Dart.INDENT + 'return ' + returnValue + ';\n'; + returnValue = Dart.INDENT + 'return ' + returnValue + ';\n'; } const returnType = returnValue ? 'dynamic' : 'void'; const args = []; const variables = block.getVars(); for (let i = 0; i < variables.length; i++) { - args[i] = Blockly.Dart.nameDB_.getName(variables[i], - Blockly.VARIABLE_CATEGORY_NAME); + args[i] = Dart.nameDB_.getName(variables[i], NameType.VARIABLE); } let code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' + xfix1 + loopTrap + branch + xfix2 + returnValue + '}'; - code = Blockly.Dart.scrub_(block, code); + code = Dart.scrub_(block, code); // Add % so as not to collide with helper functions in definitions list. - Blockly.Dart.definitions_['%' + funcName] = code; + Dart.definitions_['%' + funcName] = code; return null; }; // Defining a procedure without a return value uses the same generator as // a procedure with a return value. -Blockly.Dart['procedures_defnoreturn'] = Blockly.Dart['procedures_defreturn']; +Dart['procedures_defnoreturn'] = Dart['procedures_defreturn']; -Blockly.Dart['procedures_callreturn'] = function(block) { +Dart['procedures_callreturn'] = function(block) { // Call a procedure with a return value. - const funcName = Blockly.Dart.nameDB_.getName(block.getFieldValue('NAME'), - Blockly.PROCEDURE_CATEGORY_NAME); + const funcName = + Dart.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE); const args = []; const variables = block.getVars(); for (let i = 0; i < variables.length; i++) { - args[i] = Blockly.Dart.valueToCode(block, 'ARG' + i, - Blockly.Dart.ORDER_NONE) || 'null'; + args[i] = Dart.valueToCode(block, 'ARG' + i, Dart.ORDER_NONE) || 'null'; } let code = funcName + '(' + args.join(', ') + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['procedures_callnoreturn'] = function(block) { +Dart['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. - const tuple = Blockly.Dart['procedures_callreturn'](block); + const tuple = Dart['procedures_callreturn'](block); return tuple[0] + ';\n'; }; -Blockly.Dart['procedures_ifreturn'] = function(block) { +Dart['procedures_ifreturn'] = function(block) { // Conditionally return value from a procedure. - const condition = Blockly.Dart.valueToCode(block, 'CONDITION', - Blockly.Dart.ORDER_NONE) || 'false'; + const condition = + Dart.valueToCode(block, 'CONDITION', Dart.ORDER_NONE) || 'false'; let code = 'if (' + condition + ') {\n'; - if (Blockly.Dart.STATEMENT_SUFFIX) { + if (Dart.STATEMENT_SUFFIX) { // Inject any statement suffix here since the regular one at the end // will not get executed if the return is triggered. - code += Blockly.Dart.prefixLines( - Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block), - Blockly.Dart.INDENT); + code += Dart.prefixLines( + Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT); } if (block.hasReturnValue_) { - const value = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_NONE) || 'null'; - code += Blockly.Dart.INDENT + 'return ' + value + ';\n'; + const value = Dart.valueToCode(block, 'VALUE', Dart.ORDER_NONE) || 'null'; + code += Dart.INDENT + 'return ' + value + ';\n'; } else { - code += Blockly.Dart.INDENT + 'return;\n'; + code += Dart.INDENT + 'return;\n'; } code += '}\n'; return code; diff --git a/generators/dart/text.js b/generators/dart/text.js index 6c8a7b354e3..7078815ec22 100644 --- a/generators/dart/text.js +++ b/generators/dart/text.js @@ -6,164 +6,157 @@ /** * @fileoverview Generating Dart for text blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.Dart.texts'); +goog.module('Blockly.Dart.texts'); -goog.require('Blockly.Dart'); +const Dart = goog.require('Blockly.Dart'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.Dart.addReservedWords('Html,Math'); +Dart.addReservedWords('Html,Math'); -Blockly.Dart['text'] = function(block) { +Dart['text'] = function(block) { // Text value. - const code = Blockly.Dart.quote_(block.getFieldValue('TEXT')); - return [code, Blockly.Dart.ORDER_ATOMIC]; + const code = Dart.quote_(block.getFieldValue('TEXT')); + return [code, Dart.ORDER_ATOMIC]; }; -Blockly.Dart['text_multiline'] = function(block) { +Dart['text_multiline'] = function(block) { // Text value. - const code = Blockly.Dart.multiline_quote_(block.getFieldValue('TEXT')); - const order = code.indexOf('+') !== -1 ? Blockly.Dart.ORDER_ADDITIVE : - Blockly.Dart.ORDER_ATOMIC; + const code = Dart.multiline_quote_(block.getFieldValue('TEXT')); + const order = + code.indexOf('+') !== -1 ? Dart.ORDER_ADDITIVE : Dart.ORDER_ATOMIC; return [code, order]; }; -Blockly.Dart['text_join'] = function(block) { +Dart['text_join'] = function(block) { // Create a string made up of any number of elements of any type. switch (block.itemCount_) { case 0: - return ['\'\'', Blockly.Dart.ORDER_ATOMIC]; + return ['\'\'', Dart.ORDER_ATOMIC]; case 1: { - const element = Blockly.Dart.valueToCode(block, 'ADD0', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\''; + const element = + Dart.valueToCode(block, 'ADD0', Dart.ORDER_UNARY_POSTFIX) || '\'\''; const code = element + '.toString()'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } default: { const elements = new Array(block.itemCount_); for (let i = 0; i < block.itemCount_; i++) { - elements[i] = Blockly.Dart.valueToCode(block, 'ADD' + i, - Blockly.Dart.ORDER_NONE) || '\'\''; + elements[i] = + Dart.valueToCode(block, 'ADD' + i, Dart.ORDER_NONE) || '\'\''; } const code = '[' + elements.join(',') + '].join()'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } } }; -Blockly.Dart['text_append'] = function(block) { +Dart['text_append'] = function(block) { // Append to a variable in place. - const varName = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'), - Blockly.VARIABLE_CATEGORY_NAME); - const value = Blockly.Dart.valueToCode(block, 'TEXT', - Blockly.Dart.ORDER_NONE) || '\'\''; + const varName = + Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + const value = Dart.valueToCode(block, 'TEXT', Dart.ORDER_NONE) || '\'\''; return varName + ' = [' + varName + ', ' + value + '].join();\n'; }; -Blockly.Dart['text_length'] = function(block) { +Dart['text_length'] = function(block) { // String or array length. - const text = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\''; - return [text + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX]; + const text = + Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '\'\''; + return [text + '.length', Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_isEmpty'] = function(block) { +Dart['text_isEmpty'] = function(block) { // Is the string null or array empty? - const text = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\''; - return [text + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX]; + const text = + Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '\'\''; + return [text + '.isEmpty', Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_indexOf'] = function(block) { +Dart['text_indexOf'] = function(block) { // Search the text for a substring. - const operator = block.getFieldValue('END') === 'FIRST' ? - 'indexOf' : 'lastIndexOf'; - const substring = Blockly.Dart.valueToCode(block, 'FIND', - Blockly.Dart.ORDER_NONE) || '\'\''; - const text = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\''; + const operator = + block.getFieldValue('END') === 'FIRST' ? 'indexOf' : 'lastIndexOf'; + const substring = Dart.valueToCode(block, 'FIND', Dart.ORDER_NONE) || '\'\''; + const text = + Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '\'\''; const code = text + '.' + operator + '(' + substring + ')'; if (block.workspace.options.oneBasedIndex) { - return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE]; + return [code + ' + 1', Dart.ORDER_ADDITIVE]; } - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_charAt'] = function(block) { +Dart['text_charAt'] = function(block) { // Get letter at index. // Note: Until January 2013 this block did not have the WHERE input. const where = block.getFieldValue('WHERE') || 'FROM_START'; const textOrder = (where === 'FIRST' || where === 'FROM_START') ? - Blockly.Dart.ORDER_UNARY_POSTFIX : Blockly.Dart.ORDER_NONE; - const text = Blockly.Dart.valueToCode(block, 'VALUE', textOrder) || '\'\''; + Dart.ORDER_UNARY_POSTFIX : + Dart.ORDER_NONE; + const text = Dart.valueToCode(block, 'VALUE', textOrder) || '\'\''; let at; switch (where) { case 'FIRST': { const code = text + '[0]'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } case 'FROM_START': { - at = Blockly.Dart.getAdjusted(block, 'AT'); + at = Dart.getAdjusted(block, 'AT'); const code = text + '[' + at + ']'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } case 'LAST': at = 1; // Fall through. case 'FROM_END': { - at = Blockly.Dart.getAdjusted(block, 'AT', 1); - const functionName = Blockly.Dart.provideFunction_( - 'text_get_from_end', - ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(String text, num x) {', - ' return text[text.length - x];', - '}']); + at = Dart.getAdjusted(block, 'AT', 1); + const functionName = Dart.provideFunction_('text_get_from_end', [ + 'String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(String text, num x) {', + ' return text[text.length - x];', '}' + ]); const code = functionName + '(' + text + ', ' + at + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } case 'RANDOM': { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; - const functionName = Blockly.Dart.provideFunction_( - 'text_random_letter', - ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(String text) {', - ' int x = new Math.Random().nextInt(text.length);', - ' return text[x];', - '}']); + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; + const functionName = Dart.provideFunction_('text_random_letter', [ + 'String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(String text) {', + ' int x = new Math.Random().nextInt(text.length);', + ' return text[x];', '}' + ]); const code = functionName + '(' + text + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; } } throw Error('Unhandled option (text_charAt).'); }; -Blockly.Dart['text_getSubstring'] = function(block) { +Dart['text_getSubstring'] = function(block) { // Get substring. const where1 = block.getFieldValue('WHERE1'); const where2 = block.getFieldValue('WHERE2'); const requiresLengthCall = (where1 !== 'FROM_END' && where2 === 'FROM_START'); - const textOrder = requiresLengthCall ? Blockly.Dart.ORDER_UNARY_POSTFIX : - Blockly.Dart.ORDER_NONE; - const text = Blockly.Dart.valueToCode(block, 'STRING', textOrder) || '\'\''; + const textOrder = + requiresLengthCall ? Dart.ORDER_UNARY_POSTFIX : Dart.ORDER_NONE; + const text = Dart.valueToCode(block, 'STRING', textOrder) || '\'\''; let code; if (where1 === 'FIRST' && where2 === 'LAST') { code = text; - return [code, Blockly.Dart.ORDER_NONE]; + return [code, Dart.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': - at1 = Blockly.Dart.getAdjusted(block, 'AT1'); + at1 = Dart.getAdjusted(block, 'AT1'); break; case 'FROM_END': - at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false, - Blockly.Dart.ORDER_ADDITIVE); + at1 = Dart.getAdjusted(block, 'AT1', 1, false, Dart.ORDER_ADDITIVE); at1 = text + '.length - ' + at1; break; case 'FIRST': @@ -175,11 +168,10 @@ Blockly.Dart['text_getSubstring'] = function(block) { let at2; switch (where2) { case 'FROM_START': - at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1); + at2 = Dart.getAdjusted(block, 'AT2', 1); break; case 'FROM_END': - at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false, - Blockly.Dart.ORDER_ADDITIVE); + at2 = Dart.getAdjusted(block, 'AT2', 0, false, Dart.ORDER_ADDITIVE); at2 = text + '.length - ' + at2; break; case 'LAST': @@ -194,35 +186,28 @@ Blockly.Dart['text_getSubstring'] = function(block) { code = text + '.substring(' + at1 + ', ' + at2 + ')'; } } else { - const at1 = Blockly.Dart.getAdjusted(block, 'AT1'); - const at2 = Blockly.Dart.getAdjusted(block, 'AT2'); - const functionName = Blockly.Dart.provideFunction_( - 'text_get_substring', - ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(String text, String where1, num at1, String where2, num at2) {', - ' int getAt(String where, num at) {', - ' if (where == \'FROM_END\') {', - ' at = text.length - 1 - at;', - ' } else if (where == \'FIRST\') {', - ' at = 0;', - ' } else if (where == \'LAST\') {', - ' at = text.length - 1;', - ' } else if (where != \'FROM_START\') {', - ' throw \'Unhandled option (text_getSubstring).\';', - ' }', - ' return at;', - ' }', - ' at1 = getAt(where1, at1);', - ' at2 = getAt(where2, at2) + 1;', - ' return text.substring(at1, at2);', - '}']); - code = functionName + '(' + text + ', \'' + - where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')'; + const at1 = Dart.getAdjusted(block, 'AT1'); + const at2 = Dart.getAdjusted(block, 'AT2'); + const functionName = Dart.provideFunction_('text_get_substring', [ + 'String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + + '(String text, String where1, num at1, String where2, num at2) {', + ' int getAt(String where, num at) {', ' if (where == \'FROM_END\') {', + ' at = text.length - 1 - at;', + ' } else if (where == \'FIRST\') {', ' at = 0;', + ' } else if (where == \'LAST\') {', ' at = text.length - 1;', + ' } else if (where != \'FROM_START\') {', + ' throw \'Unhandled option (text_getSubstring).\';', ' }', + ' return at;', ' }', ' at1 = getAt(where1, at1);', + ' at2 = getAt(where2, at2) + 1;', ' return text.substring(at1, at2);', + '}' + ]); + code = functionName + '(' + text + ', \'' + where1 + '\', ' + at1 + ', \'' + + where2 + '\', ' + at2 + ')'; } - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_changeCase'] = function(block) { +Dart['text_changeCase'] = function(block) { // Change capitalization. const OPERATORS = { 'UPPERCASE': '.toUpperCase()', @@ -230,38 +215,30 @@ Blockly.Dart['text_changeCase'] = function(block) { 'TITLECASE': null }; const operator = OPERATORS[block.getFieldValue('CASE')]; - const textOrder = operator ? Blockly.Dart.ORDER_UNARY_POSTFIX : - Blockly.Dart.ORDER_NONE; - const text = Blockly.Dart.valueToCode(block, 'TEXT', textOrder) || '\'\''; + const textOrder = operator ? Dart.ORDER_UNARY_POSTFIX : Dart.ORDER_NONE; + const text = Dart.valueToCode(block, 'TEXT', textOrder) || '\'\''; let code; if (operator) { // Upper and lower case are functions built into Dart. code = text + operator; } else { // Title case is not a native Dart function. Define one. - const functionName = Blockly.Dart.provideFunction_( - 'text_toTitleCase', - ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + - '(String str) {', - ' RegExp exp = new RegExp(r\'\\b\');', - ' List list = str.split(exp);', - ' final title = new StringBuffer();', - ' for (String part in list) {', - ' if (part.length > 0) {', - ' title.write(part[0].toUpperCase());', - ' if (part.length > 0) {', - ' title.write(part.substring(1).toLowerCase());', - ' }', - ' }', - ' }', - ' return title.toString();', - '}']); + const functionName = Dart.provideFunction_('text_toTitleCase', [ + 'String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(String str) {', + ' RegExp exp = new RegExp(r\'\\b\');', + ' List list = str.split(exp);', + ' final title = new StringBuffer();', ' for (String part in list) {', + ' if (part.length > 0) {', ' title.write(part[0].toUpperCase());', + ' if (part.length > 0) {', + ' title.write(part.substring(1).toLowerCase());', ' }', + ' }', ' }', ' return title.toString();', '}' + ]); code = functionName + '(' + text + ')'; } - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_trim'] = function(block) { +Dart['text_trim'] = function(block) { // Trim spaces. const OPERATORS = { 'LEFT': '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')', @@ -269,88 +246,71 @@ Blockly.Dart['text_trim'] = function(block) { 'BOTH': '.trim()' }; const operator = OPERATORS[block.getFieldValue('MODE')]; - const text = Blockly.Dart.valueToCode(block, 'TEXT', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\''; - return [text + operator, Blockly.Dart.ORDER_UNARY_POSTFIX]; + const text = + Dart.valueToCode(block, 'TEXT', Dart.ORDER_UNARY_POSTFIX) || '\'\''; + return [text + operator, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_print'] = function(block) { +Dart['text_print'] = function(block) { // Print statement. - const msg = Blockly.Dart.valueToCode(block, 'TEXT', - Blockly.Dart.ORDER_NONE) || '\'\''; + const msg = Dart.valueToCode(block, 'TEXT', Dart.ORDER_NONE) || '\'\''; return 'print(' + msg + ');\n'; }; -Blockly.Dart['text_prompt_ext'] = function(block) { +Dart['text_prompt_ext'] = function(block) { // Prompt function. - Blockly.Dart.definitions_['import_dart_html'] = - 'import \'dart:html\' as Html;'; + Dart.definitions_['import_dart_html'] = 'import \'dart:html\' as Html;'; let msg; if (block.getField('TEXT')) { // Internal message. - msg = Blockly.Dart.quote_(block.getFieldValue('TEXT')); + msg = Dart.quote_(block.getFieldValue('TEXT')); } else { // External message. - msg = Blockly.Dart.valueToCode(block, 'TEXT', - Blockly.Dart.ORDER_NONE) || '\'\''; + msg = Dart.valueToCode(block, 'TEXT', Dart.ORDER_NONE) || '\'\''; } let code = 'Html.window.prompt(' + msg + ', \'\')'; const toNumber = block.getFieldValue('TYPE') === 'NUMBER'; if (toNumber) { - Blockly.Dart.definitions_['import_dart_math'] = - 'import \'dart:math\' as Math;'; + Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;'; code = 'Math.parseDouble(' + code + ')'; } - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_prompt'] = Blockly.Dart['text_prompt_ext']; +Dart['text_prompt'] = Dart['text_prompt_ext']; -Blockly.Dart['text_count'] = function(block) { - const text = Blockly.Dart.valueToCode(block, 'TEXT', - Blockly.Dart.ORDER_NONE) || '\'\''; - const sub = Blockly.Dart.valueToCode(block, 'SUB', - Blockly.Dart.ORDER_NONE) || '\'\''; +Dart['text_count'] = function(block) { + const text = Dart.valueToCode(block, 'TEXT', Dart.ORDER_NONE) || '\'\''; + const sub = Dart.valueToCode(block, 'SUB', Dart.ORDER_NONE) || '\'\''; // Substring count is not a native Dart function. Define one. - const functionName = Blockly.Dart.provideFunction_( - 'text_count', - ['int ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + + const functionName = Dart.provideFunction_('text_count', [ + 'int ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(String haystack, String needle) {', - ' if (needle.length == 0) {', - ' return haystack.length + 1;', - ' }', - ' int index = 0;', - ' int count = 0;', - ' while (index != -1) {', - ' index = haystack.indexOf(needle, index);', - ' if (index != -1) {', - ' count++;', - ' index += needle.length;', - ' }', - ' }', - ' return count;', - '}']); + ' if (needle.length == 0) {', ' return haystack.length + 1;', ' }', + ' int index = 0;', ' int count = 0;', ' while (index != -1) {', + ' index = haystack.indexOf(needle, index);', ' if (index != -1) {', + ' count++;', ' index += needle.length;', ' }', ' }', + ' return count;', '}' + ]); const code = functionName + '(' + text + ', ' + sub + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_replace'] = function(block) { - const text = Blockly.Dart.valueToCode(block, 'TEXT', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\''; - const from = Blockly.Dart.valueToCode(block, 'FROM', - Blockly.Dart.ORDER_NONE) || '\'\''; - const to = Blockly.Dart.valueToCode(block, 'TO', - Blockly.Dart.ORDER_NONE) || '\'\''; +Dart['text_replace'] = function(block) { + const text = + Dart.valueToCode(block, 'TEXT', Dart.ORDER_UNARY_POSTFIX) || '\'\''; + const from = Dart.valueToCode(block, 'FROM', Dart.ORDER_NONE) || '\'\''; + const to = Dart.valueToCode(block, 'TO', Dart.ORDER_NONE) || '\'\''; const code = text + '.replaceAll(' + from + ', ' + to + ')'; - return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; + return [code, Dart.ORDER_UNARY_POSTFIX]; }; -Blockly.Dart['text_reverse'] = function(block) { +Dart['text_reverse'] = function(block) { // There isn't a sensible way to do this in Dart. See: // http://stackoverflow.com/a/21613700/3529104 // Implementing something is possibly better than not implementing anything? - const text = Blockly.Dart.valueToCode(block, 'TEXT', - Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\''; + const text = + Dart.valueToCode(block, 'TEXT', Dart.ORDER_UNARY_POSTFIX) || '\'\''; const code = 'new String.fromCharCodes(' + text + '.runes.toList().reversed)'; - return [code, Blockly.Dart.ORDER_UNARY_PREFIX]; + return [code, Dart.ORDER_UNARY_PREFIX]; }; diff --git a/generators/dart/variables.js b/generators/dart/variables.js index 90848525d55..38ab39bb467 100644 --- a/generators/dart/variables.js +++ b/generators/dart/variables.js @@ -6,27 +6,27 @@ /** * @fileoverview Generating Dart for variable blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.Dart.variables'); +goog.module('Blockly.Dart.variables'); -goog.require('Blockly.Dart'); +const Dart = goog.require('Blockly.Dart'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.Dart['variables_get'] = function(block) { +Dart['variables_get'] = function(block) { // Variable getter. - const code = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'), - Blockly.VARIABLE_CATEGORY_NAME); - return [code, Blockly.Dart.ORDER_ATOMIC]; + const code = + Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + return [code, Dart.ORDER_ATOMIC]; }; -Blockly.Dart['variables_set'] = function(block) { +Dart['variables_set'] = function(block) { // Variable setter. - const argument0 = Blockly.Dart.valueToCode(block, 'VALUE', - Blockly.Dart.ORDER_ASSIGNMENT) || '0'; - const varName = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'), - Blockly.VARIABLE_CATEGORY_NAME); + const argument0 = + Dart.valueToCode(block, 'VALUE', Dart.ORDER_ASSIGNMENT) || '0'; + const varName = + Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); return varName + ' = ' + argument0 + ';\n'; }; diff --git a/generators/dart/variables_dynamic.js b/generators/dart/variables_dynamic.js index 82dd6436434..1eac6ed6719 100644 --- a/generators/dart/variables_dynamic.js +++ b/generators/dart/variables_dynamic.js @@ -6,16 +6,16 @@ /** * @fileoverview Generating Dart for dynamic variable blocks. - * @suppress {extraRequire} */ 'use strict'; -goog.provide('Blockly.Dart.variablesDynamic'); +goog.module('Blockly.Dart.variablesDynamic'); -goog.require('Blockly.Dart'); +const Dart = goog.require('Blockly.Dart'); +/** @suppress {extraRequire} */ goog.require('Blockly.Dart.variables'); // Dart is dynamically typed. -Blockly.Dart['variables_get_dynamic'] = Blockly.Dart['variables_get']; -Blockly.Dart['variables_set_dynamic'] = Blockly.Dart['variables_set']; +Dart['variables_get_dynamic'] = Dart['variables_get']; +Dart['variables_set_dynamic'] = Dart['variables_set']; diff --git a/generators/javascript.js b/generators/javascript.js index ea8db287d5a..00d1b0b5acd 100644 --- a/generators/javascript.js +++ b/generators/javascript.js @@ -6,28 +6,29 @@ /** * @fileoverview Helper functions for generating JavaScript for blocks. - * @suppress {missingRequire|checkTypes|globalThis} + * @suppress {checkTypes|globalThis} */ 'use strict'; -goog.provide('Blockly.JavaScript'); +goog.module('Blockly.JavaScript'); +goog.module.declareLegacyNamespace(); -goog.require('Blockly.Generator'); -goog.require('Blockly.Variables'); -goog.require('Blockly.Names'); -goog.require('Blockly.inputTypes'); -goog.require('Blockly.utils.global'); -goog.require('Blockly.utils.object'); -goog.require('Blockly.utils.string'); -goog.requireType('Blockly.Block'); -goog.requireType('Blockly.Workspace'); +const Variables = goog.require('Blockly.Variables'); +const objectUtils = goog.require('Blockly.utils.object'); +const stringUtils = goog.require('Blockly.utils.string'); +const {Block} = goog.requireType('Blockly.Block'); +const {Generator} = goog.require('Blockly.Generator'); +const {globalThis} = goog.require('Blockly.utils.global'); +const {inputTypes} = goog.require('Blockly.inputTypes'); +const {Names, NameType} = goog.require('Blockly.Names'); +const {Workspace} = goog.requireType('Blockly.Workspace'); /** * JavaScript code generator. - * @type {!Blockly.Generator} + * @type {!Generator} */ -Blockly.JavaScript = new Blockly.Generator('JavaScript'); +const JavaScript = new Generator('JavaScript'); /** * List of illegal variable names. @@ -36,7 +37,7 @@ Blockly.JavaScript = new Blockly.Generator('JavaScript'); * accidentally clobbering a built-in object or function. * @private */ -Blockly.JavaScript.addReservedWords( +JavaScript.addReservedWords( // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords 'break,case,catch,class,const,continue,debugger,default,delete,do,else,export,extends,finally,for,function,if,import,in,instanceof,new,return,super,switch,this,throw,try,typeof,var,void,while,with,yield,' + 'enum,' + @@ -46,95 +47,95 @@ Blockly.JavaScript.addReservedWords( // Magic variable. 'arguments,' + // Everything in the current environment (835 items in Chrome, 104 in Node). - Object.getOwnPropertyNames(Blockly.utils.global.globalThis).join(',')); + Object.getOwnPropertyNames(globalThis).join(',')); /** * Order of operation ENUMs. * https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence */ -Blockly.JavaScript.ORDER_ATOMIC = 0; // 0 "" ... -Blockly.JavaScript.ORDER_NEW = 1.1; // new -Blockly.JavaScript.ORDER_MEMBER = 1.2; // . [] -Blockly.JavaScript.ORDER_FUNCTION_CALL = 2; // () -Blockly.JavaScript.ORDER_INCREMENT = 3; // ++ -Blockly.JavaScript.ORDER_DECREMENT = 3; // -- -Blockly.JavaScript.ORDER_BITWISE_NOT = 4.1; // ~ -Blockly.JavaScript.ORDER_UNARY_PLUS = 4.2; // + -Blockly.JavaScript.ORDER_UNARY_NEGATION = 4.3; // - -Blockly.JavaScript.ORDER_LOGICAL_NOT = 4.4; // ! -Blockly.JavaScript.ORDER_TYPEOF = 4.5; // typeof -Blockly.JavaScript.ORDER_VOID = 4.6; // void -Blockly.JavaScript.ORDER_DELETE = 4.7; // delete -Blockly.JavaScript.ORDER_AWAIT = 4.8; // await -Blockly.JavaScript.ORDER_EXPONENTIATION = 5.0; // ** -Blockly.JavaScript.ORDER_MULTIPLICATION = 5.1; // * -Blockly.JavaScript.ORDER_DIVISION = 5.2; // / -Blockly.JavaScript.ORDER_MODULUS = 5.3; // % -Blockly.JavaScript.ORDER_SUBTRACTION = 6.1; // - -Blockly.JavaScript.ORDER_ADDITION = 6.2; // + -Blockly.JavaScript.ORDER_BITWISE_SHIFT = 7; // << >> >>> -Blockly.JavaScript.ORDER_RELATIONAL = 8; // < <= > >= -Blockly.JavaScript.ORDER_IN = 8; // in -Blockly.JavaScript.ORDER_INSTANCEOF = 8; // instanceof -Blockly.JavaScript.ORDER_EQUALITY = 9; // == != === !== -Blockly.JavaScript.ORDER_BITWISE_AND = 10; // & -Blockly.JavaScript.ORDER_BITWISE_XOR = 11; // ^ -Blockly.JavaScript.ORDER_BITWISE_OR = 12; // | -Blockly.JavaScript.ORDER_LOGICAL_AND = 13; // && -Blockly.JavaScript.ORDER_LOGICAL_OR = 14; // || -Blockly.JavaScript.ORDER_CONDITIONAL = 15; // ?: -Blockly.JavaScript.ORDER_ASSIGNMENT = 16; // = += -= **= *= /= %= <<= >>= ... -Blockly.JavaScript.ORDER_YIELD = 17; // yield -Blockly.JavaScript.ORDER_COMMA = 18; // , -Blockly.JavaScript.ORDER_NONE = 99; // (...) +JavaScript.ORDER_ATOMIC = 0; // 0 "" ... +JavaScript.ORDER_NEW = 1.1; // new +JavaScript.ORDER_MEMBER = 1.2; // . [] +JavaScript.ORDER_FUNCTION_CALL = 2; // () +JavaScript.ORDER_INCREMENT = 3; // ++ +JavaScript.ORDER_DECREMENT = 3; // -- +JavaScript.ORDER_BITWISE_NOT = 4.1; // ~ +JavaScript.ORDER_UNARY_PLUS = 4.2; // + +JavaScript.ORDER_UNARY_NEGATION = 4.3; // - +JavaScript.ORDER_LOGICAL_NOT = 4.4; // ! +JavaScript.ORDER_TYPEOF = 4.5; // typeof +JavaScript.ORDER_VOID = 4.6; // void +JavaScript.ORDER_DELETE = 4.7; // delete +JavaScript.ORDER_AWAIT = 4.8; // await +JavaScript.ORDER_EXPONENTIATION = 5.0; // ** +JavaScript.ORDER_MULTIPLICATION = 5.1; // * +JavaScript.ORDER_DIVISION = 5.2; // / +JavaScript.ORDER_MODULUS = 5.3; // % +JavaScript.ORDER_SUBTRACTION = 6.1; // - +JavaScript.ORDER_ADDITION = 6.2; // + +JavaScript.ORDER_BITWISE_SHIFT = 7; // << >> >>> +JavaScript.ORDER_RELATIONAL = 8; // < <= > >= +JavaScript.ORDER_IN = 8; // in +JavaScript.ORDER_INSTANCEOF = 8; // instanceof +JavaScript.ORDER_EQUALITY = 9; // == != === !== +JavaScript.ORDER_BITWISE_AND = 10; // & +JavaScript.ORDER_BITWISE_XOR = 11; // ^ +JavaScript.ORDER_BITWISE_OR = 12; // | +JavaScript.ORDER_LOGICAL_AND = 13; // && +JavaScript.ORDER_LOGICAL_OR = 14; // || +JavaScript.ORDER_CONDITIONAL = 15; // ?: +JavaScript.ORDER_ASSIGNMENT = 16; // = += -= **= *= /= %= <<= >>= ... +JavaScript.ORDER_YIELD = 17; // yield +JavaScript.ORDER_COMMA = 18; // , +JavaScript.ORDER_NONE = 99; // (...) /** * List of outer-inner pairings that do NOT require parentheses. * @type {!Array>} */ -Blockly.JavaScript.ORDER_OVERRIDES = [ +JavaScript.ORDER_OVERRIDES = [ // (foo()).bar -> foo().bar // (foo())[0] -> foo()[0] - [Blockly.JavaScript.ORDER_FUNCTION_CALL, Blockly.JavaScript.ORDER_MEMBER], + [JavaScript.ORDER_FUNCTION_CALL, JavaScript.ORDER_MEMBER], // (foo())() -> foo()() - [Blockly.JavaScript.ORDER_FUNCTION_CALL, Blockly.JavaScript.ORDER_FUNCTION_CALL], + [JavaScript.ORDER_FUNCTION_CALL, JavaScript.ORDER_FUNCTION_CALL], // (foo.bar).baz -> foo.bar.baz // (foo.bar)[0] -> foo.bar[0] // (foo[0]).bar -> foo[0].bar // (foo[0])[1] -> foo[0][1] - [Blockly.JavaScript.ORDER_MEMBER, Blockly.JavaScript.ORDER_MEMBER], + [JavaScript.ORDER_MEMBER, JavaScript.ORDER_MEMBER], // (foo.bar)() -> foo.bar() // (foo[0])() -> foo[0]() - [Blockly.JavaScript.ORDER_MEMBER, Blockly.JavaScript.ORDER_FUNCTION_CALL], + [JavaScript.ORDER_MEMBER, JavaScript.ORDER_FUNCTION_CALL], // !(!foo) -> !!foo - [Blockly.JavaScript.ORDER_LOGICAL_NOT, Blockly.JavaScript.ORDER_LOGICAL_NOT], + [JavaScript.ORDER_LOGICAL_NOT, JavaScript.ORDER_LOGICAL_NOT], // a * (b * c) -> a * b * c - [Blockly.JavaScript.ORDER_MULTIPLICATION, Blockly.JavaScript.ORDER_MULTIPLICATION], + [JavaScript.ORDER_MULTIPLICATION, JavaScript.ORDER_MULTIPLICATION], // a + (b + c) -> a + b + c - [Blockly.JavaScript.ORDER_ADDITION, Blockly.JavaScript.ORDER_ADDITION], + [JavaScript.ORDER_ADDITION, JavaScript.ORDER_ADDITION], // a && (b && c) -> a && b && c - [Blockly.JavaScript.ORDER_LOGICAL_AND, Blockly.JavaScript.ORDER_LOGICAL_AND], + [JavaScript.ORDER_LOGICAL_AND, JavaScript.ORDER_LOGICAL_AND], // a || (b || c) -> a || b || c - [Blockly.JavaScript.ORDER_LOGICAL_OR, Blockly.JavaScript.ORDER_LOGICAL_OR] + [JavaScript.ORDER_LOGICAL_OR, JavaScript.ORDER_LOGICAL_OR] ]; /** * Whether the init method has been called. * @type {?boolean} */ -Blockly.JavaScript.isInitialized = false; +JavaScript.isInitialized = false; /** * Initialise the database of variable names. - * @param {!Blockly.Workspace} workspace Workspace to generate code from. + * @param {!Workspace} workspace Workspace to generate code from. */ -Blockly.JavaScript.init = function(workspace) { +JavaScript.init = function(workspace) { // Call Blockly.Generator's init. Object.getPrototypeOf(this).init.call(this); if (!this.nameDB_) { - this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_); + this.nameDB_ = new Names(this.RESERVED_WORDS_); } else { this.nameDB_.reset(); } @@ -145,17 +146,16 @@ Blockly.JavaScript.init = function(workspace) { const defvars = []; // Add developer variables (not created or named by the user). - const devVarList = Blockly.Variables.allDeveloperVariables(workspace); + const devVarList = Variables.allDeveloperVariables(workspace); for (let i = 0; i < devVarList.length; i++) { - defvars.push(this.nameDB_.getName(devVarList[i], - Blockly.Names.DEVELOPER_VARIABLE_TYPE)); + defvars.push( + this.nameDB_.getName(devVarList[i], NameType.DEVELOPER_VARIABLE)); } // Add user variables, but only ones that are being used. - const variables = Blockly.Variables.allUsedVarModels(workspace); + const variables = Variables.allUsedVarModels(workspace); for (let i = 0; i < variables.length; i++) { - defvars.push(this.nameDB_.getName(variables[i].getId(), - Blockly.VARIABLE_CATEGORY_NAME)); + defvars.push(this.nameDB_.getName(variables[i].getId(), NameType.VARIABLE)); } // Declare all of the variables. @@ -170,9 +170,9 @@ Blockly.JavaScript.init = function(workspace) { * @param {string} code Generated code. * @return {string} Completed code. */ -Blockly.JavaScript.finish = function(code) { +JavaScript.finish = function(code) { // Convert the definitions dictionary into a list. - const definitions = Blockly.utils.object.values(this.definitions_); + const definitions = objectUtils.values(this.definitions_); // Call Blockly.Generator's finish. code = Object.getPrototypeOf(this).finish.call(this, code); this.isInitialized = false; @@ -187,7 +187,7 @@ Blockly.JavaScript.finish = function(code) { * @param {string} line Line of generated code. * @return {string} Legal line of code. */ -Blockly.JavaScript.scrubNakedValue = function(line) { +JavaScript.scrubNakedValue = function(line) { return line + ';\n'; }; @@ -198,12 +198,12 @@ Blockly.JavaScript.scrubNakedValue = function(line) { * @return {string} JavaScript string. * @protected */ -Blockly.JavaScript.quote_ = function(string) { +JavaScript.quote_ = function(string) { // Can't use goog.string.quote since Google's style guide recommends // JS string literals use single quotes. string = string.replace(/\\/g, '\\\\') - .replace(/\n/g, '\\\n') - .replace(/'/g, '\\\''); + .replace(/\n/g, '\\\n') + .replace(/'/g, '\\\''); return '\'' + string + '\''; }; @@ -214,7 +214,7 @@ Blockly.JavaScript.quote_ = function(string) { * @return {string} JavaScript string. * @protected */ -Blockly.JavaScript.multiline_quote_ = function(string) { +JavaScript.multiline_quote_ = function(string) { // Can't use goog.string.quote since Google's style guide recommends // JS string literals use single quotes. const lines = string.split(/\n/g).map(this.quote_); @@ -225,26 +225,26 @@ Blockly.JavaScript.multiline_quote_ = function(string) { * Common tasks for generating JavaScript from blocks. * Handles comments for the specified block and any connected value blocks. * Calls any statements following this block. - * @param {!Blockly.Block} block The current block. + * @param {!Block} block The current block. * @param {string} code The JavaScript code created for this block. * @param {boolean=} opt_thisOnly True to generate code for only this statement. * @return {string} JavaScript code with comments and subsequent blocks added. * @protected */ -Blockly.JavaScript.scrub_ = function(block, code, opt_thisOnly) { +JavaScript.scrub_ = function(block, code, opt_thisOnly) { let commentCode = ''; // Only collect comments for blocks that aren't inline. if (!block.outputConnection || !block.outputConnection.targetConnection) { // Collect comment for this block. let comment = block.getCommentText(); if (comment) { - comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3); + comment = stringUtils.wrap(comment, this.COMMENT_WRAP - 3); commentCode += this.prefixLines(comment + '\n', '// '); } // Collect comments for all value arguments. // Don't collect comments for nested statements. for (let i = 0; i < block.inputList.length; i++) { - if (block.inputList[i].type === Blockly.inputTypes.VALUE) { + if (block.inputList[i].type === inputTypes.VALUE) { const childBlock = block.inputList[i].connection.targetBlock(); if (childBlock) { comment = this.allNestedComments(childBlock); @@ -262,15 +262,15 @@ Blockly.JavaScript.scrub_ = function(block, code, opt_thisOnly) { /** * Gets a property and adjusts the value while taking into account indexing. - * @param {!Blockly.Block} block The block. + * @param {!Block} block The block. * @param {string} atId The property ID of the element to get. * @param {number=} opt_delta Value to add. * @param {boolean=} opt_negate Whether to negate the value. * @param {number=} opt_order The highest order acting on this value. * @return {string|number} */ -Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate, - opt_order) { +JavaScript.getAdjusted = function( + block, atId, opt_delta, opt_negate, opt_order) { let delta = opt_delta || 0; let order = opt_order || this.ORDER_NONE; if (block.workspace.options.oneBasedIndex) { @@ -293,7 +293,7 @@ Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate, let at = this.valueToCode(block, atId, outerOrder) || defaultAtIndex; - if (Blockly.utils.string.isNumber(at)) { + if (stringUtils.isNumber(at)) { // If the index is a naked number, adjust it right now. at = Number(at) + delta; if (opt_negate) { @@ -321,3 +321,5 @@ Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate, } return at; }; + +exports = JavaScript; diff --git a/tests/deps.js b/tests/deps.js index e5ed27c44b3..f677ba8c82e 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -267,16 +267,16 @@ goog.addDependency('../../core/xml.js', ['Blockly.Xml'], ['Blockly.Events.utils' goog.addDependency('../../core/zoom_controls.js', ['Blockly.ZoomControls'], ['Blockly.ComponentManager', 'Blockly.Css', 'Blockly.Events.Click', 'Blockly.Events.utils', 'Blockly.IPositionable', 'Blockly.Touch', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.uiPosition', 'Blockly.utils.Rect', 'Blockly.utils.Size', 'Blockly.utils.Svg', 'Blockly.utils.dom'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/dart.js', ['Blockly.Dart'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/dart/all.js', ['Blockly.Dart.all'], ['Blockly.Dart.colour', 'Blockly.Dart.lists', 'Blockly.Dart.logic', 'Blockly.Dart.loops', 'Blockly.Dart.math', 'Blockly.Dart.procedures', 'Blockly.Dart.texts', 'Blockly.Dart.variables', 'Blockly.Dart.variablesDynamic'], {'module': 'goog'}); -goog.addDependency('../../generators/dart/colour.js', ['Blockly.Dart.colour'], ['Blockly.Dart'], {'lang': 'es6'}); -goog.addDependency('../../generators/dart/lists.js', ['Blockly.Dart.lists'], ['Blockly.Dart'], {'lang': 'es6'}); -goog.addDependency('../../generators/dart/logic.js', ['Blockly.Dart.logic'], ['Blockly.Dart'], {'lang': 'es6'}); -goog.addDependency('../../generators/dart/loops.js', ['Blockly.Dart.loops'], ['Blockly.Dart', 'Blockly.utils.string'], {'lang': 'es6'}); -goog.addDependency('../../generators/dart/math.js', ['Blockly.Dart.math'], ['Blockly.Dart'], {'lang': 'es6'}); -goog.addDependency('../../generators/dart/procedures.js', ['Blockly.Dart.procedures'], ['Blockly.Dart'], {'lang': 'es6'}); -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.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.global', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'}); +goog.addDependency('../../generators/dart/colour.js', ['Blockly.Dart.colour'], ['Blockly.Dart'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/dart/lists.js', ['Blockly.Dart.lists'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/dart/logic.js', ['Blockly.Dart.logic'], ['Blockly.Dart'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/dart/loops.js', ['Blockly.Dart.loops'], ['Blockly.Dart', 'Blockly.Names', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/dart/math.js', ['Blockly.Dart.math'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/dart/procedures.js', ['Blockly.Dart.procedures'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/dart/text.js', ['Blockly.Dart.texts'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/dart/variables.js', ['Blockly.Dart.variables'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/dart/variables_dynamic.js', ['Blockly.Dart.variablesDynamic'], ['Blockly.Dart', 'Blockly.Dart.variables'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/javascript.js', ['Blockly.JavaScript'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.global', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/javascript/all.js', ['Blockly.JavaScript.all'], ['Blockly.JavaScript.colour', 'Blockly.JavaScript.lists', 'Blockly.JavaScript.logic', 'Blockly.JavaScript.loops', 'Blockly.JavaScript.math', 'Blockly.JavaScript.procedures', 'Blockly.JavaScript.texts', 'Blockly.JavaScript.variables', 'Blockly.JavaScript.variablesDynamic'], {'module': 'goog'}); goog.addDependency('../../generators/javascript/colour.js', ['Blockly.JavaScript.colour'], ['Blockly.JavaScript'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/javascript/lists.js', ['Blockly.JavaScript.lists'], ['Blockly.JavaScript', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'});