mirror of
https://github.com/google/blockly.git
synced 2026-01-06 08:30:13 +01:00
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:
@@ -55,18 +55,18 @@ Python['text_join'] = function(block) {
|
||||
// Should we allow joining by '-' or ',' or any other characters?
|
||||
switch (block.itemCount_) {
|
||||
case 0:
|
||||
return ['\'\'', Python.ORDER_ATOMIC];
|
||||
return ["''", Python.ORDER_ATOMIC];
|
||||
case 1: {
|
||||
const element =
|
||||
Python.valueToCode(block, 'ADD0', Python.ORDER_NONE) || '\'\'';
|
||||
Python.valueToCode(block, 'ADD0', Python.ORDER_NONE) || "''";
|
||||
const codeAndOrder = forceString(element);
|
||||
return codeAndOrder;
|
||||
}
|
||||
case 2: {
|
||||
const element0 =
|
||||
Python.valueToCode(block, 'ADD0', Python.ORDER_NONE) || '\'\'';
|
||||
Python.valueToCode(block, 'ADD0', Python.ORDER_NONE) || "''";
|
||||
const element1 =
|
||||
Python.valueToCode(block, 'ADD1', Python.ORDER_NONE) || '\'\'';
|
||||
Python.valueToCode(block, 'ADD1', Python.ORDER_NONE) || "''";
|
||||
const code = forceString(element0)[0] + ' + ' + forceString(element1)[0];
|
||||
return [code, Python.ORDER_ADDITIVE];
|
||||
}
|
||||
@@ -74,7 +74,7 @@ Python['text_join'] = function(block) {
|
||||
const elements = [];
|
||||
for (let i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] =
|
||||
Python.valueToCode(block, 'ADD' + i, Python.ORDER_NONE) || '\'\'';
|
||||
Python.valueToCode(block, 'ADD' + i, Python.ORDER_NONE) || "''";
|
||||
}
|
||||
const tempVar = Python.nameDB_.getDistinctName('x', NameType.VARIABLE);
|
||||
const code = '\'\'.join([str(' + tempVar + ') for ' + tempVar + ' in [' +
|
||||
@@ -88,19 +88,19 @@ Python['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
const varName =
|
||||
Python.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||
const value = Python.valueToCode(block, 'TEXT', Python.ORDER_NONE) || '\'\'';
|
||||
const value = Python.valueToCode(block, 'TEXT', Python.ORDER_NONE) || "''";
|
||||
return varName + ' = str(' + varName + ') + ' + forceString(value)[0] + '\n';
|
||||
};
|
||||
|
||||
Python['text_length'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
const text = Python.valueToCode(block, 'VALUE', Python.ORDER_NONE) || '\'\'';
|
||||
const text = Python.valueToCode(block, 'VALUE', Python.ORDER_NONE) || "''";
|
||||
return ['len(' + text + ')', Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Python['text_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
const text = Python.valueToCode(block, 'VALUE', Python.ORDER_NONE) || '\'\'';
|
||||
const text = Python.valueToCode(block, 'VALUE', Python.ORDER_NONE) || "''";
|
||||
const code = 'not len(' + text + ')';
|
||||
return [code, Python.ORDER_LOGICAL_NOT];
|
||||
};
|
||||
@@ -110,9 +110,9 @@ Python['text_indexOf'] = function(block) {
|
||||
// Should we allow for non-case sensitive???
|
||||
const operator = block.getFieldValue('END') === 'FIRST' ? 'find' : 'rfind';
|
||||
const substring =
|
||||
Python.valueToCode(block, 'FIND', Python.ORDER_NONE) || '\'\'';
|
||||
Python.valueToCode(block, 'FIND', Python.ORDER_NONE) || "''";
|
||||
const text =
|
||||
Python.valueToCode(block, 'VALUE', Python.ORDER_MEMBER) || '\'\'';
|
||||
Python.valueToCode(block, 'VALUE', Python.ORDER_MEMBER) || "''";
|
||||
const code = text + '.' + operator + '(' + substring + ')';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Python.ORDER_ADDITIVE];
|
||||
@@ -126,7 +126,7 @@ Python['text_charAt'] = function(block) {
|
||||
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
const textOrder =
|
||||
(where === 'RANDOM') ? Python.ORDER_NONE : Python.ORDER_MEMBER;
|
||||
const text = Python.valueToCode(block, 'VALUE', textOrder) || '\'\'';
|
||||
const text = Python.valueToCode(block, 'VALUE', textOrder) || "''";
|
||||
switch (where) {
|
||||
case 'FIRST': {
|
||||
const code = text + '[0]';
|
||||
@@ -148,10 +148,11 @@ Python['text_charAt'] = function(block) {
|
||||
}
|
||||
case 'RANDOM': {
|
||||
Python.definitions_['import_random'] = 'import random';
|
||||
const functionName = Python.provideFunction_('text_random_letter', [
|
||||
'def ' + Python.FUNCTION_NAME_PLACEHOLDER_ + '(text):',
|
||||
' x = int(random.random() * len(text))', ' return text[x];'
|
||||
]);
|
||||
const functionName = Python.provideFunction_('text_random_letter', `
|
||||
def ${Python.FUNCTION_NAME_PLACEHOLDER_}(text):
|
||||
x = int(random.random() * len(text))
|
||||
return text[x]
|
||||
`);
|
||||
const code = functionName + '(' + text + ')';
|
||||
return [code, Python.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
@@ -164,7 +165,7 @@ Python['text_getSubstring'] = function(block) {
|
||||
const where1 = block.getFieldValue('WHERE1');
|
||||
const where2 = block.getFieldValue('WHERE2');
|
||||
const text =
|
||||
Python.valueToCode(block, 'STRING', Python.ORDER_MEMBER) || '\'\'';
|
||||
Python.valueToCode(block, 'STRING', Python.ORDER_MEMBER) || "''";
|
||||
let at1;
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
@@ -217,7 +218,7 @@ Python['text_changeCase'] = function(block) {
|
||||
'TITLECASE': '.title()'
|
||||
};
|
||||
const operator = OPERATORS[block.getFieldValue('CASE')];
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || '\'\'';
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || "''";
|
||||
const code = text + operator;
|
||||
return [code, Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
@@ -230,30 +231,33 @@ Python['text_trim'] = function(block) {
|
||||
'BOTH': '.strip()'
|
||||
};
|
||||
const operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || '\'\'';
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || "''";
|
||||
const code = text + operator;
|
||||
return [code, Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Python['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
const msg = Python.valueToCode(block, 'TEXT', Python.ORDER_NONE) || '\'\'';
|
||||
const msg = Python.valueToCode(block, 'TEXT', Python.ORDER_NONE) || "''";
|
||||
return 'print(' + msg + ')\n';
|
||||
};
|
||||
|
||||
Python['text_prompt_ext'] = function(block) {
|
||||
// Prompt function.
|
||||
const functionName = Python.provideFunction_('text_prompt', [
|
||||
'def ' + Python.FUNCTION_NAME_PLACEHOLDER_ + '(msg):', ' try:',
|
||||
' return raw_input(msg)', ' except NameError:', ' return input(msg)'
|
||||
]);
|
||||
const functionName = Python.provideFunction_('text_prompt', `
|
||||
def ${Python.FUNCTION_NAME_PLACEHOLDER_}(msg):
|
||||
try:
|
||||
return raw_input(msg)
|
||||
except NameError:
|
||||
return input(msg)
|
||||
`);
|
||||
let msg;
|
||||
if (block.getField('TEXT')) {
|
||||
// Internal message.
|
||||
msg = Python.quote_(block.getFieldValue('TEXT'));
|
||||
} else {
|
||||
// External message.
|
||||
msg = Python.valueToCode(block, 'TEXT', Python.ORDER_NONE) || '\'\'';
|
||||
msg = Python.valueToCode(block, 'TEXT', Python.ORDER_NONE) || "''";
|
||||
}
|
||||
let code = functionName + '(' + msg + ')';
|
||||
const toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
||||
@@ -266,22 +270,22 @@ Python['text_prompt_ext'] = function(block) {
|
||||
Python['text_prompt'] = Python['text_prompt_ext'];
|
||||
|
||||
Python['text_count'] = function(block) {
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || '\'\'';
|
||||
const sub = Python.valueToCode(block, 'SUB', Python.ORDER_NONE) || '\'\'';
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || "''";
|
||||
const sub = Python.valueToCode(block, 'SUB', Python.ORDER_NONE) || "''";
|
||||
const code = text + '.count(' + sub + ')';
|
||||
return [code, Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Python['text_replace'] = function(block) {
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || '\'\'';
|
||||
const from = Python.valueToCode(block, 'FROM', Python.ORDER_NONE) || '\'\'';
|
||||
const to = Python.valueToCode(block, 'TO', Python.ORDER_NONE) || '\'\'';
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || "''";
|
||||
const from = Python.valueToCode(block, 'FROM', Python.ORDER_NONE) || "''";
|
||||
const to = Python.valueToCode(block, 'TO', Python.ORDER_NONE) || "''";
|
||||
const code = text + '.replace(' + from + ', ' + to + ')';
|
||||
return [code, Python.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Python['text_reverse'] = function(block) {
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || '\'\'';
|
||||
const text = Python.valueToCode(block, 'TEXT', Python.ORDER_MEMBER) || "''";
|
||||
const code = text + '[::-1]';
|
||||
return [code, Python.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user