mirror of
https://github.com/google/blockly.git
synced 2026-01-10 10:27:08 +01:00
chore: Convert == to === and != to !== where possible. (#5599)
This commit is contained in:
@@ -71,7 +71,7 @@ Blockly.Python['lists_indexOf'] = function(block) {
|
||||
var firstIndexAdjustment = '';
|
||||
var lastIndexAdjustment = ' - 1';
|
||||
}
|
||||
if (block.getFieldValue('END') == 'FIRST') {
|
||||
if (block.getFieldValue('END') === 'FIRST') {
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'first_index',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
@@ -98,60 +98,60 @@ Blockly.Python['lists_getIndex'] = function(block) {
|
||||
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var listOrder = (where == 'RANDOM') ? Blockly.Python.ORDER_NONE :
|
||||
var listOrder = (where === 'RANDOM') ? Blockly.Python.ORDER_NONE :
|
||||
Blockly.Python.ORDER_MEMBER;
|
||||
var list = Blockly.Python.valueToCode(block, 'VALUE', listOrder) || '[]';
|
||||
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
if (mode == 'GET') {
|
||||
if (mode === 'GET') {
|
||||
var code = list + '[0]';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
} else if (mode === 'GET_REMOVE') {
|
||||
var code = list + '.pop(0)';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
} else if (mode === 'REMOVE') {
|
||||
return list + '.pop(0)\n';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
if (mode == 'GET') {
|
||||
if (mode === 'GET') {
|
||||
var code = list + '[-1]';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
} else if (mode === 'GET_REMOVE') {
|
||||
var code = list + '.pop()';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
} else if (mode === 'REMOVE') {
|
||||
return list + '.pop()\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT');
|
||||
if (mode == 'GET') {
|
||||
if (mode === 'GET') {
|
||||
var code = list + '[' + at + ']';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
} else if (mode === 'GET_REMOVE') {
|
||||
var code = list + '.pop(' + at + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
} else if (mode === 'REMOVE') {
|
||||
return list + '.pop(' + at + ')\n';
|
||||
}
|
||||
break;
|
||||
case'FROM_END':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT', 1, true);
|
||||
if (mode == 'GET') {
|
||||
if (mode === 'GET') {
|
||||
var code = list + '[' + at + ']';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
} else if (mode === 'GET_REMOVE') {
|
||||
var code = list + '.pop(' + at + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
} else if (mode === 'REMOVE') {
|
||||
return list + '.pop(' + at + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'RANDOM':
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
if (mode == 'GET') {
|
||||
if (mode === 'GET') {
|
||||
code = 'random.choice(' + list + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else {
|
||||
@@ -161,9 +161,9 @@ Blockly.Python['lists_getIndex'] = function(block) {
|
||||
' x = int(random.random() * len(myList))',
|
||||
' return myList.pop(x)']);
|
||||
code = functionName + '(' + list + ')';
|
||||
if (mode == 'GET_REMOVE') {
|
||||
if (mode === 'GET_REMOVE') {
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
} else if (mode === 'REMOVE') {
|
||||
return code + '\n';
|
||||
}
|
||||
}
|
||||
@@ -196,32 +196,32 @@ Blockly.Python['lists_setIndex'] = function(block) {
|
||||
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
if (mode == 'SET') {
|
||||
if (mode === 'SET') {
|
||||
return list + '[0] = ' + value + '\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
} else if (mode === 'INSERT') {
|
||||
return list + '.insert(0, ' + value + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
if (mode == 'SET') {
|
||||
if (mode === 'SET') {
|
||||
return list + '[-1] = ' + value + '\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
} else if (mode === 'INSERT') {
|
||||
return list + '.append(' + value + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT');
|
||||
if (mode == 'SET') {
|
||||
if (mode === 'SET') {
|
||||
return list + '[' + at + '] = ' + value + '\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
} else if (mode === 'INSERT') {
|
||||
return list + '.insert(' + at + ', ' + value + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT', 1, true);
|
||||
if (mode == 'SET') {
|
||||
if (mode === 'SET') {
|
||||
return list + '[' + at + '] = ' + value + '\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
} else if (mode === 'INSERT') {
|
||||
return list + '.insert(' + at + ', ' + value + ')\n';
|
||||
}
|
||||
break;
|
||||
@@ -231,10 +231,10 @@ Blockly.Python['lists_setIndex'] = function(block) {
|
||||
var xVar = Blockly.Python.nameDB_.getDistinctName(
|
||||
'tmp_x', Blockly.VARIABLE_CATEGORY_NAME);
|
||||
code += xVar + ' = int(random.random() * len(' + list + '))\n';
|
||||
if (mode == 'SET') {
|
||||
if (mode === 'SET') {
|
||||
code += list + '[' + xVar + '] = ' + value + '\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
} else if (mode === 'INSERT') {
|
||||
code += list + '.insert(' + xVar + ', ' + value + ')\n';
|
||||
return code;
|
||||
}
|
||||
@@ -252,7 +252,7 @@ Blockly.Python['lists_getSublist'] = function(block) {
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.Python.getAdjustedInt(block, 'AT1');
|
||||
if (at1 == '0') {
|
||||
if (at1 === 0) {
|
||||
at1 = '';
|
||||
}
|
||||
break;
|
||||
@@ -276,7 +276,7 @@ Blockly.Python['lists_getSublist'] = function(block) {
|
||||
if (!Blockly.isNumber(String(at2))) {
|
||||
Blockly.Python.definitions_['import_sys'] = 'import sys';
|
||||
at2 += ' or sys.maxsize';
|
||||
} else if (at2 == '0') {
|
||||
} else if (at2 === 0) {
|
||||
at2 = '';
|
||||
}
|
||||
break;
|
||||
@@ -322,13 +322,13 @@ Blockly.Python['lists_sort'] = function(block) {
|
||||
Blockly.Python['lists_split'] = function(block) {
|
||||
// Block for splitting text into a list, or joining a list into text.
|
||||
var mode = block.getFieldValue('MODE');
|
||||
if (mode == 'SPLIT') {
|
||||
if (mode === 'SPLIT') {
|
||||
var value_input = Blockly.Python.valueToCode(block, 'INPUT',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var value_delim = Blockly.Python.valueToCode(block, 'DELIM',
|
||||
Blockly.Python.ORDER_NONE);
|
||||
var code = value_input + '.split(' + value_delim + ')';
|
||||
} else if (mode == 'JOIN') {
|
||||
} else if (mode === 'JOIN') {
|
||||
var value_input = Blockly.Python.valueToCode(block, 'INPUT',
|
||||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
var value_delim = Blockly.Python.valueToCode(block, 'DELIM',
|
||||
|
||||
@@ -33,7 +33,7 @@ Blockly.Python['controls_if'] = function(block) {
|
||||
Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block),
|
||||
Blockly.Python.INDENT) + branchCode;
|
||||
}
|
||||
code += (n == 0 ? 'if ' : 'elif ') + conditionCode + ':\n' + branchCode;
|
||||
code += (n === 0 ? 'if ' : 'elif ') + conditionCode + ':\n' + branchCode;
|
||||
++n;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
@@ -72,8 +72,8 @@ Blockly.Python['logic_compare'] = function(block) {
|
||||
|
||||
Blockly.Python['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
var operator = (block.getFieldValue('OP') == 'AND') ? 'and' : 'or';
|
||||
var order = (operator == 'and') ? Blockly.Python.ORDER_LOGICAL_AND :
|
||||
var operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
|
||||
var order = (operator === 'and') ? Blockly.Python.ORDER_LOGICAL_AND :
|
||||
Blockly.Python.ORDER_LOGICAL_OR;
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'A', order);
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'B', order);
|
||||
@@ -83,7 +83,7 @@ Blockly.Python['logic_operation'] = function(block) {
|
||||
argument1 = 'False';
|
||||
} else {
|
||||
// Single missing arguments have no effect on the return value.
|
||||
var defaultArgument = (operator == 'and') ? 'True' : 'False';
|
||||
var defaultArgument = (operator === 'and') ? 'True' : 'False';
|
||||
if (!argument0) {
|
||||
argument0 = defaultArgument;
|
||||
}
|
||||
@@ -105,7 +105,7 @@ Blockly.Python['logic_negate'] = function(block) {
|
||||
|
||||
Blockly.Python['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'True' : 'False';
|
||||
var code = (block.getFieldValue('BOOL') === 'TRUE') ? 'True' : 'False';
|
||||
return [code, Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ Blockly.Python['controls_repeat'] = Blockly.Python['controls_repeat_ext'];
|
||||
|
||||
Blockly.Python['controls_whileUntil'] = function(block) {
|
||||
// Do while/until loop.
|
||||
var until = block.getFieldValue('MODE') == 'UNTIL';
|
||||
var until = block.getFieldValue('MODE') === 'UNTIL';
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'BOOL',
|
||||
until ? Blockly.Python.ORDER_LOGICAL_NOT :
|
||||
Blockly.Python.ORDER_NONE) || 'False';
|
||||
@@ -107,14 +107,14 @@ Blockly.Python['controls_for'] = function(block) {
|
||||
if (argument0 <= argument1) {
|
||||
// Count up.
|
||||
argument1++;
|
||||
if (argument0 == 0 && increment == 1) {
|
||||
if (argument0 === 0 && increment === 1) {
|
||||
// If starting index is 0, omit it.
|
||||
range = argument1;
|
||||
} else {
|
||||
range = argument0 + ', ' + argument1;
|
||||
}
|
||||
// If increment isn't 1, it must be explicit.
|
||||
if (increment != 1) {
|
||||
if (increment !== 1) {
|
||||
range += ', ' + increment;
|
||||
}
|
||||
} else {
|
||||
@@ -154,7 +154,7 @@ Blockly.Python['controls_for'] = function(block) {
|
||||
var endVar = scrub(argument1, '_end');
|
||||
var incVar = scrub(increment, '_inc');
|
||||
|
||||
if (typeof startVar == 'number' && typeof endVar == 'number') {
|
||||
if (typeof startVar === 'number' && typeof endVar === 'number') {
|
||||
if (startVar < endVar) {
|
||||
range = defineUpRange();
|
||||
} else {
|
||||
|
||||
@@ -22,10 +22,10 @@ Blockly.Python['math_number'] = function(block) {
|
||||
// Numeric value.
|
||||
var code = Number(block.getFieldValue('NUM'));
|
||||
var order;
|
||||
if (code == Infinity) {
|
||||
if (code === Infinity) {
|
||||
code = 'float("inf")';
|
||||
order = Blockly.Python.ORDER_FUNCTION_CALL;
|
||||
} else if (code == -Infinity) {
|
||||
} else if (code === -Infinity) {
|
||||
code = '-float("inf")';
|
||||
order = Blockly.Python.ORDER_UNARY_SIGN;
|
||||
} else {
|
||||
@@ -63,14 +63,14 @@ Blockly.Python['math_single'] = function(block) {
|
||||
var operator = block.getFieldValue('OP');
|
||||
var code;
|
||||
var arg;
|
||||
if (operator == 'NEG') {
|
||||
if (operator === 'NEG') {
|
||||
// Negation is a special case given its different operator precedence.
|
||||
code = Blockly.Python.valueToCode(block, 'NUM',
|
||||
Blockly.Python.ORDER_UNARY_SIGN) || '0';
|
||||
return ['-' + code, Blockly.Python.ORDER_UNARY_SIGN];
|
||||
}
|
||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||
if (operator == 'SIN' || operator == 'COS' || operator == 'TAN') {
|
||||
if (operator === 'SIN' || operator === 'COS' || operator === 'TAN') {
|
||||
arg = Blockly.Python.valueToCode(block, 'NUM',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
|
||||
} else {
|
||||
@@ -150,7 +150,7 @@ Blockly.Python['math_constant'] = function(block) {
|
||||
'INFINITY': ['float(\'inf\')', Blockly.Python.ORDER_ATOMIC]
|
||||
};
|
||||
var constant = block.getFieldValue('CONSTANT');
|
||||
if (constant != 'INFINITY') {
|
||||
if (constant !== 'INFINITY') {
|
||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||
}
|
||||
return CONSTANTS[constant];
|
||||
@@ -163,7 +163,7 @@ Blockly.Python['math_number_property'] = function(block) {
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
|
||||
var dropdown_property = block.getFieldValue('PROPERTY');
|
||||
var code;
|
||||
if (dropdown_property == 'PRIME') {
|
||||
if (dropdown_property === 'PRIME') {
|
||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||
'from numbers import Number';
|
||||
@@ -211,7 +211,7 @@ Blockly.Python['math_number_property'] = function(block) {
|
||||
var divisor = Blockly.Python.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE);
|
||||
// If 'divisor' is some code that evals to 0, Python will raise an error.
|
||||
if (!divisor || divisor == '0') {
|
||||
if (!divisor || divisor === '0') {
|
||||
return ['False', Blockly.Python.ORDER_ATOMIC];
|
||||
}
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
@@ -258,8 +258,8 @@ Blockly.Python['math_on_list'] = function(block) {
|
||||
'from numbers import Number';
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'math_mean',
|
||||
// This operation excludes null and values that aren't int or float:',
|
||||
// math_mean([null, null, "aString", 1, 9]) == 5.0.',
|
||||
// This operation excludes null and values that aren't int or float:
|
||||
// math_mean([null, null, "aString", 1, 9]) -> 5.0
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
|
||||
' localList = [e for e in myList if isinstance(e, Number)]',
|
||||
' if not localList: return',
|
||||
@@ -272,7 +272,7 @@ Blockly.Python['math_on_list'] = function(block) {
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'math_median',
|
||||
// This operation excludes null values:
|
||||
// math_median([null, null, 1, 3]) == 2.0.
|
||||
// math_median([null, null, 1, 3]) -> 2.0
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
|
||||
' localList = sorted([e for e in myList if isinstance(e, Number)])',
|
||||
' if not localList: return',
|
||||
@@ -288,7 +288,7 @@ Blockly.Python['math_on_list'] = function(block) {
|
||||
'math_modes',
|
||||
// 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].
|
||||
// Mode of [3, 'x', 'x', 1, 1, 2, '3'] -> ['x', 1]
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(some_list):',
|
||||
' modes = []',
|
||||
' # Using a lists of [item, count] to keep count rather than dict',
|
||||
|
||||
@@ -24,7 +24,7 @@ Blockly.Python['procedures_defreturn'] = function(block) {
|
||||
var variables = Blockly.Variables.allUsedVarModels(workspace) || [];
|
||||
for (var i = 0, variable; (variable = variables[i]); i++) {
|
||||
var varName = variable.name;
|
||||
if (block.getVars().indexOf(varName) == -1) {
|
||||
if (block.getVars().indexOf(varName) === -1) {
|
||||
globals.push(Blockly.Python.nameDB_.getName(varName,
|
||||
Blockly.VARIABLE_CATEGORY_NAME));
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ Blockly.Python['text'] = function(block) {
|
||||
Blockly.Python['text_multiline'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.Python.multiline_quote_(block.getFieldValue('TEXT'));
|
||||
var order = code.indexOf('+') != -1 ? Blockly.Python.ORDER_ADDITIVE :
|
||||
var order = code.indexOf('+') !== -1 ? Blockly.Python.ORDER_ADDITIVE :
|
||||
Blockly.Python.ORDER_ATOMIC;
|
||||
return [code, order];
|
||||
};
|
||||
@@ -113,7 +113,7 @@ Blockly.Python['text_isEmpty'] = function(block) {
|
||||
Blockly.Python['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
// Should we allow for non-case sensitive???
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ? 'find' : 'rfind';
|
||||
var operator = block.getFieldValue('END') === 'FIRST' ? 'find' : 'rfind';
|
||||
var substring = Blockly.Python.valueToCode(block, 'FIND',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var text = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
@@ -129,7 +129,7 @@ Blockly.Python['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 == 'RANDOM') ? Blockly.Python.ORDER_NONE :
|
||||
var textOrder = (where === 'RANDOM') ? Blockly.Python.ORDER_NONE :
|
||||
Blockly.Python.ORDER_MEMBER;
|
||||
var text = Blockly.Python.valueToCode(block, 'VALUE', textOrder) || '\'\'';
|
||||
switch (where) {
|
||||
@@ -169,7 +169,7 @@ Blockly.Python['text_getSubstring'] = function(block) {
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.Python.getAdjustedInt(block, 'AT1');
|
||||
if (at1 == '0') {
|
||||
if (at1 === 0) {
|
||||
at1 = '';
|
||||
}
|
||||
break;
|
||||
@@ -193,7 +193,7 @@ Blockly.Python['text_getSubstring'] = function(block) {
|
||||
if (!Blockly.isNumber(String(at2))) {
|
||||
Blockly.Python.definitions_['import_sys'] = 'import sys';
|
||||
at2 += ' or sys.maxsize';
|
||||
} else if (at2 == '0') {
|
||||
} else if (at2 === 0) {
|
||||
at2 = '';
|
||||
}
|
||||
break;
|
||||
@@ -260,7 +260,7 @@ Blockly.Python['text_prompt_ext'] = function(block) {
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = functionName + '(' + msg + ')';
|
||||
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
|
||||
var toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
||||
if (toNumber) {
|
||||
code = 'float(' + code + ')';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user