chore: Use ES6 template strings in CSS and code generators (#5902)

* Unindent CSS, save 3 kb of code.
* Convert generator functions to template strings. 
This resolves #5761.
This commit is contained in:
Neil Fraser
2022-01-28 17:58:43 -08:00
committed by GitHub
parent a31003fab9
commit 1f6a1bd8d9
39 changed files with 2194 additions and 2062 deletions

View File

@@ -32,22 +32,22 @@ PHP['text_multiline'] = function(block) {
PHP['text_join'] = function(block) {
// Create a string made up of any number of elements of any type.
if (block.itemCount_ === 0) {
return ['\'\'', PHP.ORDER_ATOMIC];
return ["''", PHP.ORDER_ATOMIC];
} else if (block.itemCount_ === 1) {
const element = PHP.valueToCode(block, 'ADD0', PHP.ORDER_NONE) || '\'\'';
const element = PHP.valueToCode(block, 'ADD0', PHP.ORDER_NONE) || "''";
const code = element;
return [code, PHP.ORDER_NONE];
} else if (block.itemCount_ === 2) {
const element0 =
PHP.valueToCode(block, 'ADD0', PHP.ORDER_STRING_CONCAT) || '\'\'';
PHP.valueToCode(block, 'ADD0', PHP.ORDER_STRING_CONCAT) || "''";
const element1 =
PHP.valueToCode(block, 'ADD1', PHP.ORDER_STRING_CONCAT) || '\'\'';
PHP.valueToCode(block, 'ADD1', PHP.ORDER_STRING_CONCAT) || "''";
const code = element0 + ' . ' + element1;
return [code, PHP.ORDER_STRING_CONCAT];
} else {
const elements = new Array(block.itemCount_);
for (let i = 0; i < block.itemCount_; i++) {
elements[i] = PHP.valueToCode(block, 'ADD' + i, PHP.ORDER_NONE) || '\'\'';
elements[i] = PHP.valueToCode(block, 'ADD' + i, PHP.ORDER_NONE) || "''";
}
const code = 'implode(\'\', array(' + elements.join(',') + '))';
return [code, PHP.ORDER_FUNCTION_CALL];
@@ -58,24 +58,27 @@ PHP['text_append'] = function(block) {
// Append to a variable in place.
const varName =
PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
const value = PHP.valueToCode(block, 'TEXT', PHP.ORDER_ASSIGNMENT) || '\'\'';
const value = PHP.valueToCode(block, 'TEXT', PHP.ORDER_ASSIGNMENT) || "''";
return varName + ' .= ' + value + ';\n';
};
PHP['text_length'] = function(block) {
// String or array length.
const functionName = PHP.provideFunction_('length', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {',
' if (is_string($value)) {', ' return strlen($value);', ' } else {',
' return count($value);', ' }', '}'
]);
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '\'\'';
const functionName = PHP.provideFunction_('length', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($value) {
if (is_string($value)) {
return strlen($value);
}
return count($value);
}
`);
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || "''";
return [functionName + '(' + text + ')', PHP.ORDER_FUNCTION_CALL];
};
PHP['text_isEmpty'] = function(block) {
// Is the string null or array empty?
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '\'\'';
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || "''";
return ['empty(' + text + ')', PHP.ORDER_FUNCTION_CALL];
};
@@ -83,8 +86,8 @@ PHP['text_indexOf'] = function(block) {
// Search the text for a substring.
const operator =
block.getFieldValue('END') === 'FIRST' ? 'strpos' : 'strrpos';
const substring = PHP.valueToCode(block, 'FIND', PHP.ORDER_NONE) || '\'\'';
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '\'\'';
const substring = PHP.valueToCode(block, 'FIND', PHP.ORDER_NONE) || "''";
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || "''";
let errorIndex = ' -1';
let indexAdjustment = '';
if (block.workspace.options.oneBasedIndex) {
@@ -94,13 +97,12 @@ PHP['text_indexOf'] = function(block) {
const functionName = PHP.provideFunction_(
block.getFieldValue('END') === 'FIRST' ? 'text_indexOf' :
'text_lastIndexOf',
[
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($text, $search) {',
' $pos = ' + operator + '($text, $search);',
' return $pos === false ? ' + errorIndex + ' : $pos' +
indexAdjustment + ';',
'}'
]);
`
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text, $search) {
$pos = ${operator}($text, $search);
return $pos === false ? ${errorIndex} : $pos${indexAdjustment};
}
`);
const code = functionName + '(' + text + ', ' + substring + ')';
return [code, PHP.ORDER_FUNCTION_CALL];
};
@@ -109,7 +111,7 @@ PHP['text_charAt'] = function(block) {
// Get letter at index.
const where = block.getFieldValue('WHERE') || 'FROM_START';
const textOrder = (where === 'RANDOM') ? PHP.ORDER_NONE : PHP.ORDER_NONE;
const text = PHP.valueToCode(block, 'VALUE', textOrder) || '\'\'';
const text = PHP.valueToCode(block, 'VALUE', textOrder) || "''";
switch (where) {
case 'FIRST': {
const code = 'substr(' + text + ', 0, 1)';
@@ -130,10 +132,11 @@ PHP['text_charAt'] = function(block) {
return [code, PHP.ORDER_FUNCTION_CALL];
}
case 'RANDOM': {
const functionName = PHP.provideFunction_('text_random_letter', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($text) {',
' return $text[rand(0, strlen($text) - 1)];', '}'
]);
const functionName = PHP.provideFunction_('text_random_letter', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text) {
return $text[rand(0, strlen($text) - 1)];
}
`);
const code = functionName + '(' + text + ')';
return [code, PHP.ORDER_FUNCTION_CALL];
}
@@ -145,36 +148,35 @@ PHP['text_getSubstring'] = function(block) {
// Get substring.
const where1 = block.getFieldValue('WHERE1');
const where2 = block.getFieldValue('WHERE2');
const text = PHP.valueToCode(block, 'STRING', PHP.ORDER_NONE) || '\'\'';
const text = PHP.valueToCode(block, 'STRING', PHP.ORDER_NONE) || "''";
if (where1 === 'FIRST' && where2 === 'LAST') {
const code = text;
return [code, PHP.ORDER_NONE];
} else {
const at1 = PHP.getAdjusted(block, 'AT1');
const at2 = PHP.getAdjusted(block, 'AT2');
const functionName = PHP.provideFunction_('text_get_substring', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($text, $where1, $at1, $where2, $at2) {',
' if ($where1 == \'FROM_END\') {',
' $at1 = strlen($text) - 1 - $at1;',
' } else if ($where1 == \'FIRST\') {',
' $at1 = 0;',
' } else if ($where1 != \'FROM_START\') {',
' throw new Exception(\'Unhandled option (text_get_substring).\');',
' }',
' $length = 0;',
' if ($where2 == \'FROM_START\') {',
' $length = $at2 - $at1 + 1;',
' } else if ($where2 == \'FROM_END\') {',
' $length = strlen($text) - $at1 - $at2;',
' } else if ($where2 == \'LAST\') {',
' $length = strlen($text) - $at1;',
' } else {',
' throw new Exception(\'Unhandled option (text_get_substring).\');',
' }',
' return substr($text, $at1, $length);',
'}'
]);
const functionName = PHP.provideFunction_('text_get_substring', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text, $where1, $at1, $where2, $at2) {
if ($where1 == 'FROM_END') {
$at1 = strlen($text) - 1 - $at1;
} else if ($where1 == 'FIRST') {
$at1 = 0;
} else if ($where1 != 'FROM_START') {
throw new Exception('Unhandled option (text_get_substring).');
}
$length = 0;
if ($where2 == 'FROM_START') {
$length = $at2 - $at1 + 1;
} else if ($where2 == 'FROM_END') {
$length = strlen($text) - $at1 - $at2;
} else if ($where2 == 'LAST') {
$length = strlen($text) - $at1;
} else {
throw new Exception('Unhandled option (text_get_substring).');
}
return substr($text, $at1, $length);
}
`);
const code = functionName + '(' + text + ', \'' + where1 + '\', ' + at1 +
', \'' + where2 + '\', ' + at2 + ')';
return [code, PHP.ORDER_FUNCTION_CALL];
@@ -183,7 +185,7 @@ PHP['text_getSubstring'] = function(block) {
PHP['text_changeCase'] = function(block) {
// Change capitalization.
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\'';
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
let code;
if (block.getFieldValue('CASE') === 'UPPERCASE') {
code = 'strtoupper(' + text + ')';
@@ -199,13 +201,13 @@ PHP['text_trim'] = function(block) {
// Trim spaces.
const OPERATORS = {'LEFT': 'ltrim', 'RIGHT': 'rtrim', 'BOTH': 'trim'};
const operator = OPERATORS[block.getFieldValue('MODE')];
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\'';
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
return [operator + '(' + text + ')', PHP.ORDER_FUNCTION_CALL];
};
PHP['text_print'] = function(block) {
// Print statement.
const msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\'';
const msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
return 'print(' + msg + ');\n';
};
@@ -217,7 +219,7 @@ PHP['text_prompt_ext'] = function(block) {
msg = PHP.quote_(block.getFieldValue('TEXT'));
} else {
// External message.
msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\'';
msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
}
let code = 'readline(' + msg + ')';
const toNumber = block.getFieldValue('TYPE') === 'NUMBER';
@@ -230,8 +232,8 @@ PHP['text_prompt_ext'] = function(block) {
PHP['text_prompt'] = PHP['text_prompt_ext'];
PHP['text_count'] = function(block) {
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\'';
const sub = PHP.valueToCode(block, 'SUB', PHP.ORDER_NONE) || '\'\'';
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
const sub = PHP.valueToCode(block, 'SUB', PHP.ORDER_NONE) || "''";
const code = 'strlen(' + sub + ') === 0' +
' ? strlen(' + text + ') + 1' +
' : substr_count(' + text + ', ' + sub + ')';
@@ -239,15 +241,15 @@ PHP['text_count'] = function(block) {
};
PHP['text_replace'] = function(block) {
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\'';
const from = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || '\'\'';
const to = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || '\'\'';
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
const from = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || "''";
const to = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || "''";
const code = 'str_replace(' + from + ', ' + to + ', ' + text + ')';
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['text_reverse'] = function(block) {
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\'';
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
const code = 'strrev(' + text + ')';
return [code, PHP.ORDER_FUNCTION_CALL];
};