mirror of
https://github.com/google/blockly.git
synced 2026-01-04 23:50:12 +01:00
Remove unneeded String() and str() calls.
String literals in JS and Python don’t need to be coerced to strings.
This commit is contained in:
@@ -35,6 +35,25 @@ Blockly.JavaScript['text'] = function(block) {
|
||||
return [code, Blockly.JavaScript.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
/**
|
||||
* Enclose the provided value in 'String(...)' function.
|
||||
* Leave string literals alone.
|
||||
* @param {string} value Code evaluating to a value.
|
||||
* @return {string} Code evaluating to a string.
|
||||
* @private
|
||||
*/
|
||||
Blockly.JavaScript.text.forceString_ = function(value) {
|
||||
if (Blockly.JavaScript.text.forceString_.strRegExp.test(value)) {
|
||||
return value;
|
||||
}
|
||||
return 'String(' + value + ')';
|
||||
};
|
||||
|
||||
/**
|
||||
* Regular expression to detect a single-quoted string literal.
|
||||
*/
|
||||
Blockly.JavaScript.text.forceString_.strRegExp = /^\s*'([^']|\\')*'\s*$/;
|
||||
|
||||
Blockly.JavaScript['text_join'] = function(block) {
|
||||
// Create a string made up of any number of elements of any type.
|
||||
switch (block.itemCount_) {
|
||||
@@ -43,14 +62,15 @@ Blockly.JavaScript['text_join'] = function(block) {
|
||||
case 1:
|
||||
var element = Blockly.JavaScript.valueToCode(block, 'ADD0',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var code = 'String(' + element + ')';
|
||||
var code = Blockly.JavaScript.text.forceString_(element);
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
case 2:
|
||||
var element0 = Blockly.JavaScript.valueToCode(block, 'ADD0',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var element1 = Blockly.JavaScript.valueToCode(block, 'ADD1',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var code = 'String(' + element0 + ') + String(' + element1 + ')';
|
||||
var code = Blockly.JavaScript.text.forceString_(element0) + ' + ' +
|
||||
Blockly.JavaScript.text.forceString_(element1);
|
||||
return [code, Blockly.JavaScript.ORDER_ADDITION];
|
||||
default:
|
||||
var elements = new Array(block.itemCount_);
|
||||
@@ -69,7 +89,7 @@ Blockly.JavaScript['text_append'] = function(block) {
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
return varName + ' = String(' + varName + ') + String(' + value + ');\n';
|
||||
return varName + ' += ' + Blockly.JavaScript.text.forceString_(value) + ';\n';
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_length'] = function(block) {
|
||||
@@ -142,11 +162,11 @@ Blockly.JavaScript['text_charAt'] = function(block) {
|
||||
|
||||
/**
|
||||
* Returns an expression calculating the index into a string.
|
||||
* @private
|
||||
* @param {string} stringName Name of the string, used to calculate length.
|
||||
* @param {string} where The method of indexing, selected by dropdown in Blockly
|
||||
* @param {string=} opt_at The optional offset when indexing from start/end.
|
||||
* @return {string} Index expression.
|
||||
* @private
|
||||
*/
|
||||
Blockly.JavaScript.text.getIndex_ = function(stringName, where, opt_at) {
|
||||
if (where == 'FIRST') {
|
||||
|
||||
Reference in New Issue
Block a user