mirror of
https://github.com/google/blockly.git
synced 2026-01-07 09:00:11 +01:00
chore: update var to const and let in dart block generators (#5654)
This commit is contained in:
@@ -18,14 +18,14 @@ Blockly.Dart.addReservedWords('Html,Math');
|
||||
|
||||
Blockly.Dart['text'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
||||
const code = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_multiline'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.Dart.multiline_quote_(block.getFieldValue('TEXT'));
|
||||
var order = code.indexOf('+') !== -1 ? Blockly.Dart.ORDER_ADDITIVE :
|
||||
const code = Blockly.Dart.multiline_quote_(block.getFieldValue('TEXT'));
|
||||
const order = code.indexOf('+') !== -1 ? Blockly.Dart.ORDER_ADDITIVE :
|
||||
Blockly.Dart.ORDER_ATOMIC;
|
||||
return [code, order];
|
||||
};
|
||||
@@ -35,54 +35,56 @@ Blockly.Dart['text_join'] = function(block) {
|
||||
switch (block.itemCount_) {
|
||||
case 0:
|
||||
return ['\'\'', Blockly.Dart.ORDER_ATOMIC];
|
||||
case 1:
|
||||
var element = Blockly.Dart.valueToCode(block, 'ADD0',
|
||||
case 1: {
|
||||
const element = Blockly.Dart.valueToCode(block, 'ADD0',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var code = element + '.toString()';
|
||||
const code = element + '.toString()';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
default:
|
||||
var elements = new Array(block.itemCount_);
|
||||
for (var i = 0; i < block.itemCount_; i++) {
|
||||
}
|
||||
default: {
|
||||
const elements = new Array(block.itemCount_);
|
||||
for (let i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] = Blockly.Dart.valueToCode(block, 'ADD' + i,
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = '[' + elements.join(',') + '].join()';
|
||||
const code = '[' + elements.join(',') + '].join()';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Dart['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
var varName = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'),
|
||||
const varName = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.VARIABLE_CATEGORY_NAME);
|
||||
var value = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
const value = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
return varName + ' = [' + varName + ', ' + value + '].join();\n';
|
||||
};
|
||||
|
||||
Blockly.Dart['text_length'] = function(block) {
|
||||
// String or array length.
|
||||
var text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
const text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return [text + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
const text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return [text + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
var operator = block.getFieldValue('END') === 'FIRST' ?
|
||||
const operator = block.getFieldValue('END') === 'FIRST' ?
|
||||
'indexOf' : 'lastIndexOf';
|
||||
var substring = Blockly.Dart.valueToCode(block, 'FIND',
|
||||
const substring = Blockly.Dart.valueToCode(block, 'FIND',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
const text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var code = text + '.' + operator + '(' + substring + ')';
|
||||
const code = text + '.' + operator + '(' + substring + ')';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE];
|
||||
}
|
||||
@@ -92,82 +94,90 @@ Blockly.Dart['text_indexOf'] = function(block) {
|
||||
Blockly.Dart['text_charAt'] = function(block) {
|
||||
// Get letter at index.
|
||||
// Note: Until January 2013 this block did not have the WHERE input.
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var textOrder = (where === 'FIRST' || where === 'FROM_START') ?
|
||||
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
const textOrder = (where === 'FIRST' || where === 'FROM_START') ?
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX : Blockly.Dart.ORDER_NONE;
|
||||
var text = Blockly.Dart.valueToCode(block, 'VALUE', textOrder) || '\'\'';
|
||||
const text = Blockly.Dart.valueToCode(block, 'VALUE', textOrder) || '\'\'';
|
||||
let at;
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
var code = text + '[0]';
|
||||
case 'FIRST': {
|
||||
const code = text + '[0]';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT');
|
||||
var code = text + '[' + at + ']';
|
||||
}
|
||||
case 'FROM_START': {
|
||||
at = Blockly.Dart.getAdjusted(block, 'AT');
|
||||
const code = text + '[' + at + ']';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
case 'LAST':
|
||||
at = 1;
|
||||
// Fall through.
|
||||
case 'FROM_END':
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT', 1);
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
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];',
|
||||
'}']);
|
||||
code = functionName + '(' + text + ', ' + at + ')';
|
||||
const code = functionName + '(' + text + ', ' + at + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
case 'RANDOM':
|
||||
}
|
||||
case 'RANDOM': {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
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];',
|
||||
'}']);
|
||||
code = functionName + '(' + text + ')';
|
||||
const code = functionName + '(' + text + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
}
|
||||
throw Error('Unhandled option (text_charAt).');
|
||||
};
|
||||
|
||||
Blockly.Dart['text_getSubstring'] = function(block) {
|
||||
// Get substring.
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
var requiresLengthCall = (where1 !== 'FROM_END' && where2 === 'FROM_START');
|
||||
var textOrder = requiresLengthCall ? Blockly.Dart.ORDER_UNARY_POSTFIX :
|
||||
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;
|
||||
var text = Blockly.Dart.valueToCode(block, 'STRING', textOrder) || '\'\'';
|
||||
const text = Blockly.Dart.valueToCode(block, 'STRING', textOrder) || '\'\'';
|
||||
let code;
|
||||
if (where1 === 'FIRST' && where2 === 'LAST') {
|
||||
var code = text;
|
||||
code = text;
|
||||
return [code, Blockly.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':
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
||||
at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false,
|
||||
at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
at1 = text + '.length - ' + at1;
|
||||
break;
|
||||
case 'FIRST':
|
||||
var at1 = '0';
|
||||
at1 = '0';
|
||||
break;
|
||||
default:
|
||||
throw Error('Unhandled option (text_getSubstring).');
|
||||
}
|
||||
let at2;
|
||||
switch (where2) {
|
||||
case 'FROM_START':
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1);
|
||||
at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1);
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false,
|
||||
at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
at2 = text + '.length - ' + at2;
|
||||
break;
|
||||
@@ -176,15 +186,16 @@ Blockly.Dart['text_getSubstring'] = function(block) {
|
||||
default:
|
||||
throw Error('Unhandled option (text_getSubstring).');
|
||||
}
|
||||
|
||||
if (where2 === 'LAST') {
|
||||
var code = text + '.substring(' + at1 + ')';
|
||||
code = text + '.substring(' + at1 + ')';
|
||||
} else {
|
||||
var code = text + '.substring(' + at1 + ', ' + at2 + ')';
|
||||
code = text + '.substring(' + at1 + ', ' + at2 + ')';
|
||||
}
|
||||
} else {
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2');
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
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) {',
|
||||
@@ -204,7 +215,7 @@ Blockly.Dart['text_getSubstring'] = function(block) {
|
||||
' at2 = getAt(where2, at2) + 1;',
|
||||
' return text.substring(at1, at2);',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ', \'' +
|
||||
code = functionName + '(' + text + ', \'' +
|
||||
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
@@ -212,21 +223,22 @@ Blockly.Dart['text_getSubstring'] = function(block) {
|
||||
|
||||
Blockly.Dart['text_changeCase'] = function(block) {
|
||||
// Change capitalization.
|
||||
var OPERATORS = {
|
||||
const OPERATORS = {
|
||||
'UPPERCASE': '.toUpperCase()',
|
||||
'LOWERCASE': '.toLowerCase()',
|
||||
'TITLECASE': null
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('CASE')];
|
||||
var textOrder = operator ? Blockly.Dart.ORDER_UNARY_POSTFIX :
|
||||
const operator = OPERATORS[block.getFieldValue('CASE')];
|
||||
const textOrder = operator ? Blockly.Dart.ORDER_UNARY_POSTFIX :
|
||||
Blockly.Dart.ORDER_NONE;
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT', textOrder) || '\'\'';
|
||||
const text = Blockly.Dart.valueToCode(block, 'TEXT', textOrder) || '\'\'';
|
||||
let code;
|
||||
if (operator) {
|
||||
// Upper and lower case are functions built into Dart.
|
||||
var code = text + operator;
|
||||
code = text + operator;
|
||||
} else {
|
||||
// Title case is not a native Dart function. Define one.
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
const functionName = Blockly.Dart.provideFunction_(
|
||||
'text_toTitleCase',
|
||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(String str) {',
|
||||
@@ -243,27 +255,27 @@ Blockly.Dart['text_changeCase'] = function(block) {
|
||||
' }',
|
||||
' return title.toString();',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ')';
|
||||
code = functionName + '(' + text + ')';
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_trim'] = function(block) {
|
||||
// Trim spaces.
|
||||
var OPERATORS = {
|
||||
const OPERATORS = {
|
||||
'LEFT': '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')',
|
||||
'RIGHT': '.replaceFirst(new RegExp(r\'\\s+$\'), \'\')',
|
||||
'BOTH': '.trim()'
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
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];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
var msg = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
const msg = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
return 'print(' + msg + ');\n';
|
||||
};
|
||||
@@ -272,16 +284,17 @@ Blockly.Dart['text_prompt_ext'] = function(block) {
|
||||
// Prompt function.
|
||||
Blockly.Dart.definitions_['import_dart_html'] =
|
||||
'import \'dart:html\' as Html;';
|
||||
let msg;
|
||||
if (block.getField('TEXT')) {
|
||||
// Internal message.
|
||||
var msg = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
||||
msg = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
||||
} else {
|
||||
// External message.
|
||||
var msg = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
msg = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = 'Html.window.prompt(' + msg + ', \'\')';
|
||||
var toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
||||
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;';
|
||||
@@ -293,12 +306,12 @@ Blockly.Dart['text_prompt_ext'] = function(block) {
|
||||
Blockly.Dart['text_prompt'] = Blockly.Dart['text_prompt_ext'];
|
||||
|
||||
Blockly.Dart['text_count'] = function(block) {
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
const text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var sub = Blockly.Dart.valueToCode(block, 'SUB',
|
||||
const sub = Blockly.Dart.valueToCode(block, 'SUB',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
// Substring count is not a native Dart function. Define one.
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
const functionName = Blockly.Dart.provideFunction_(
|
||||
'text_count',
|
||||
['int ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(String haystack, String needle) {',
|
||||
@@ -316,18 +329,18 @@ Blockly.Dart['text_count'] = function(block) {
|
||||
' }',
|
||||
' return count;',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ', ' + sub + ')';
|
||||
const code = functionName + '(' + text + ', ' + sub + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_replace'] = function(block) {
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
const text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var from = Blockly.Dart.valueToCode(block, 'FROM',
|
||||
const from = Blockly.Dart.valueToCode(block, 'FROM',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var to = Blockly.Dart.valueToCode(block, 'TO',
|
||||
const to = Blockly.Dart.valueToCode(block, 'TO',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var code = text + '.replaceAll(' + from + ', ' + to + ')';
|
||||
const code = text + '.replaceAll(' + from + ', ' + to + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
@@ -335,8 +348,8 @@ Blockly.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?
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
const text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var code = 'new String.fromCharCodes(' + text + '.runes.toList().reversed)';
|
||||
const code = 'new String.fromCharCodes(' + text + '.runes.toList().reversed)';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_PREFIX];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user