chore: Convert == to === and != to !== where possible. (#5599)

This commit is contained in:
Neil Fraser
2021-10-15 09:17:04 -07:00
committed by GitHub
parent 7ac1e27cd6
commit c929b3015b
183 changed files with 1409 additions and 1409 deletions

View File

@@ -71,7 +71,7 @@ Blockly.Lua['lists_indexOf'] = function(block) {
Blockly.Lua.ORDER_NONE) || '\'\'';
var list = Blockly.Lua.valueToCode(block, 'VALUE',
Blockly.Lua.ORDER_NONE) || '{}';
if (block.getFieldValue('END') == 'FIRST') {
if (block.getFieldValue('END') === 'FIRST') {
var functionName = Blockly.Lua.provideFunction_(
'first_index',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
@@ -107,13 +107,13 @@ Blockly.Lua['lists_indexOf'] = function(block) {
* @private
*/
Blockly.Lua.lists.getIndex_ = function(listName, where, opt_at) {
if (where == 'FIRST') {
if (where === 'FIRST') {
return '1';
} else if (where == 'FROM_END') {
} else if (where === 'FROM_END') {
return '#' + listName + ' + 1 - ' + opt_at;
} else if (where == 'LAST') {
} else if (where === 'LAST') {
return '#' + listName;
} else if (where == 'RANDOM') {
} else if (where === 'RANDOM') {
return 'math.random(#' + listName + ')';
} else {
return opt_at;
@@ -131,12 +131,12 @@ Blockly.Lua['lists_getIndex'] = function(block) {
// If `list` would be evaluated more than once (which is the case for LAST,
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
if ((where == 'LAST' || where == 'FROM_END' || where == 'RANDOM') &&
if ((where === 'LAST' || where === 'FROM_END' || where === 'RANDOM') &&
!list.match(/^\w+$/)) {
// `list` is an expression, so we may not evaluate it more than once.
if (mode == 'REMOVE') {
if (mode === 'REMOVE') {
// We can use multiple statements.
var atOrder = (where == 'FROM_END') ? Blockly.Lua.ORDER_ADDITIVE :
var atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_ADDITIVE :
Blockly.Lua.ORDER_NONE;
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
var listVar = Blockly.Lua.nameDB_.getDistinctName(
@@ -149,23 +149,23 @@ Blockly.Lua['lists_getIndex'] = function(block) {
// We need to create a procedure to avoid reevaluating values.
var at = Blockly.Lua.valueToCode(block, 'AT', Blockly.Lua.ORDER_NONE) ||
'1';
if (mode == 'GET') {
if (mode === 'GET') {
var functionName = Blockly.Lua.provideFunction_(
'list_get_' + where.toLowerCase(),
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
// The value for 'FROM_END' and'FROM_START' depends on `at` so
// we add it as a parameter.
((where == 'FROM_END' || where == 'FROM_START') ?
((where === 'FROM_END' || where === 'FROM_START') ?
', at)' : ')'),
' return t[' + getIndex_('t', where, 'at') + ']',
'end']);
} else { // mode == 'GET_REMOVE'
} else { // `mode` === 'GET_REMOVE'
var functionName = Blockly.Lua.provideFunction_(
'list_remove_' + where.toLowerCase(),
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
// The value for 'FROM_END' and'FROM_START' depends on `at` so
// we add it as a parameter.
((where == 'FROM_END' || where == 'FROM_START') ?
((where === 'FROM_END' || where === 'FROM_START') ?
', at)' : ')'),
' return table.remove(t, ' + getIndex_('t', where, 'at') + ')',
'end']);
@@ -173,25 +173,25 @@ Blockly.Lua['lists_getIndex'] = function(block) {
var code = functionName + '(' + list +
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
// pass it.
((where == 'FROM_END' || where == 'FROM_START') ? ', ' + at : '') +
((where === 'FROM_END' || where === 'FROM_START') ? ', ' + at : '') +
')';
return [code, Blockly.Lua.ORDER_HIGH];
}
} else {
// Either `list` is a simple variable, or we only need to refer to `list`
// once.
var atOrder = (mode == 'GET' && where == 'FROM_END') ?
var atOrder = (mode === 'GET' && where === 'FROM_END') ?
Blockly.Lua.ORDER_ADDITIVE : Blockly.Lua.ORDER_NONE;
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
at = getIndex_(list, where, at);
if (mode == 'GET') {
if (mode === 'GET') {
var code = list + '[' + at + ']';
return [code, Blockly.Lua.ORDER_HIGH];
} else {
var code = 'table.remove(' + list + ', ' + at + ')';
if (mode == 'GET_REMOVE') {
if (mode === 'GET_REMOVE') {
return [code, Blockly.Lua.ORDER_HIGH];
} else { // `mode` == 'REMOVE'
} else { // `mode` === 'REMOVE'
return code + '\n';
}
}
@@ -214,7 +214,7 @@ Blockly.Lua['lists_setIndex'] = function(block) {
var code = '';
// If `list` would be evaluated more than once (which is the case for LAST,
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
if ((where == 'LAST' || where == 'FROM_END' || where == 'RANDOM') &&
if ((where === 'LAST' || where === 'FROM_END' || where === 'RANDOM') &&
!list.match(/^\w+$/)) {
// `list` is an expression, so we may not evaluate it more than once.
// We can use multiple statements.
@@ -223,13 +223,13 @@ Blockly.Lua['lists_setIndex'] = function(block) {
code = listVar + ' = ' + list + '\n';
list = listVar;
}
if (mode == 'SET') {
if (mode === 'SET') {
code += list + '[' + getIndex_(list, where, at) + '] = ' + value;
} else { // `mode` == 'INSERT'
} else { // `mode` === 'INSERT'
// LAST is a special case, because we want to insert
// *after* not *before*, the existing last element.
code += 'table.insert(' + list + ', ' +
(getIndex_(list, where, at) + (where == 'LAST' ? ' + 1' : '')) +
(getIndex_(list, where, at) + (where === 'LAST' ? ' + 1' : '')) +
', ' + value + ')';
}
return code + '\n';
@@ -252,8 +252,8 @@ Blockly.Lua['lists_getSublist'] = function(block) {
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(source' +
// The value for 'FROM_END' and'FROM_START' depends on `at` so
// we add it as a parameter.
((where1 == 'FROM_END' || where1 == 'FROM_START') ? ', at1' : '') +
((where2 == 'FROM_END' || where2 == 'FROM_START') ? ', at2' : '') +
((where1 === 'FROM_END' || where1 === 'FROM_START') ? ', at1' : '') +
((where2 === 'FROM_END' || where2 === 'FROM_START') ? ', at2' : '') +
')',
' local t = {}',
' local start = ' + getIndex_('source', where1, 'at1'),
@@ -266,8 +266,8 @@ Blockly.Lua['lists_getSublist'] = function(block) {
var code = functionName + '(' + list +
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
// pass it.
((where1 == 'FROM_END' || where1 == 'FROM_START') ? ', ' + at1 : '') +
((where2 == 'FROM_END' || where2 == 'FROM_START') ? ', ' + at2 : '') +
((where1 === 'FROM_END' || where1 === 'FROM_START') ? ', ' + at1 : '') +
((where2 === 'FROM_END' || where2 === 'FROM_START') ? ', ' + at2 : '') +
')';
return [code, Blockly.Lua.ORDER_HIGH];
};
@@ -316,7 +316,7 @@ Blockly.Lua['lists_split'] = function(block) {
Blockly.Lua.ORDER_NONE) || '\'\'';
var mode = block.getFieldValue('MODE');
var functionName;
if (mode == 'SPLIT') {
if (mode === 'SPLIT') {
if (!input) {
input = '\'\'';
}
@@ -338,7 +338,7 @@ Blockly.Lua['lists_split'] = function(block) {
' end',
' return t',
'end']);
} else if (mode == 'JOIN') {
} else if (mode === 'JOIN') {
if (!input) {
input = '{}';
}

View File

@@ -72,8 +72,8 @@ Blockly.Lua['logic_compare'] = function(block) {
Blockly.Lua['logic_operation'] = function(block) {
// Operations 'and', 'or'.
var operator = (block.getFieldValue('OP') == 'AND') ? 'and' : 'or';
var order = (operator == 'and') ? Blockly.Lua.ORDER_AND :
var operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
var order = (operator === 'and') ? Blockly.Lua.ORDER_AND :
Blockly.Lua.ORDER_OR;
var argument0 = Blockly.Lua.valueToCode(block, 'A', order);
var argument1 = Blockly.Lua.valueToCode(block, 'B', order);
@@ -83,7 +83,7 @@ Blockly.Lua['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.Lua['logic_negate'] = function(block) {
Blockly.Lua['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.Lua.ORDER_ATOMIC];
};

View File

@@ -34,7 +34,7 @@ Blockly.Lua.CONTINUE_STATEMENT = 'goto continue\n';
* @private
*/
Blockly.Lua.addContinueLabel_ = function(branch) {
if (branch.indexOf(Blockly.Lua.CONTINUE_STATEMENT) != -1) {
if (branch.indexOf(Blockly.Lua.CONTINUE_STATEMENT) !== -1) {
// False positives are possible (e.g. a string literal), but are harmless.
return branch + Blockly.Lua.INDENT + '::continue::\n';
} else {
@@ -71,7 +71,7 @@ Blockly.Lua['controls_repeat'] = Blockly.Lua['controls_repeat_ext'];
Blockly.Lua['controls_whileUntil'] = function(block) {
// Do while/until loop.
var until = block.getFieldValue('MODE') == 'UNTIL';
var until = block.getFieldValue('MODE') === 'UNTIL';
var argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
until ? Blockly.Lua.ORDER_UNARY :
Blockly.Lua.ORDER_NONE) || 'false';

View File

@@ -46,18 +46,18 @@ Blockly.Lua['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.
arg = Blockly.Lua.valueToCode(block, 'NUM',
Blockly.Lua.ORDER_UNARY) || '0';
return ['-' + arg, Blockly.Lua.ORDER_UNARY];
}
if (operator == 'POW10') {
if (operator === 'POW10') {
arg = Blockly.Lua.valueToCode(block, 'NUM',
Blockly.Lua.ORDER_EXPONENTIATION) || '0';
return ['10 ^ ' + arg, Blockly.Lua.ORDER_EXPONENTIATION];
}
if (operator == 'ROUND') {
if (operator === 'ROUND') {
arg = Blockly.Lua.valueToCode(block, 'NUM',
Blockly.Lua.ORDER_ADDITIVE) || '0';
} else {
@@ -134,7 +134,7 @@ Blockly.Lua['math_number_property'] = function(block) {
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
var dropdown_property = block.getFieldValue('PROPERTY');
var code;
if (dropdown_property == 'PRIME') {
if (dropdown_property === 'PRIME') {
// Prime is a special case as it is not a one-liner test.
var functionName = Blockly.Lua.provideFunction_(
'math_isPrime',
@@ -180,7 +180,7 @@ Blockly.Lua['math_number_property'] = function(block) {
Blockly.Lua.ORDER_MULTIPLICATIVE);
// If 'divisor' is some code that evals to 0, Lua will produce a nan.
// Let's produce nil if we can determine this at compile-time.
if (!divisor || divisor == '0') {
if (!divisor || divisor === '0') {
return ['nil', Blockly.Lua.ORDER_ATOMIC];
}
// The normal trick to implement ?: with and/or doesn't work here:

View File

@@ -24,21 +24,21 @@ Blockly.Lua['text'] = function(block) {
Blockly.Lua['text_multiline'] = function(block) {
// Text value.
var code = Blockly.Lua.multiline_quote_(block.getFieldValue('TEXT'));
var order = code.indexOf('..') != -1 ? Blockly.Lua.ORDER_CONCATENATION :
var order = code.indexOf('..') !== -1 ? Blockly.Lua.ORDER_CONCATENATION :
Blockly.Lua.ORDER_ATOMIC;
return [code, order];
};
Blockly.Lua['text_join'] = function(block) {
// Create a string made up of any number of elements of any type.
if (block.itemCount_ == 0) {
if (block.itemCount_ === 0) {
return ['\'\'', Blockly.Lua.ORDER_ATOMIC];
} else if (block.itemCount_ == 1) {
} else if (block.itemCount_ === 1) {
var element = Blockly.Lua.valueToCode(block, 'ADD0',
Blockly.Lua.ORDER_NONE) || '\'\'';
var code = 'tostring(' + element + ')';
return [code, Blockly.Lua.ORDER_HIGH];
} else if (block.itemCount_ == 2) {
} else if (block.itemCount_ === 2) {
var element0 = Blockly.Lua.valueToCode(block, 'ADD0',
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
var element1 = Blockly.Lua.valueToCode(block, 'ADD1',
@@ -85,7 +85,7 @@ Blockly.Lua['text_indexOf'] = function(block) {
Blockly.Lua.ORDER_NONE) || '\'\'';
var text = Blockly.Lua.valueToCode(block, 'VALUE',
Blockly.Lua.ORDER_NONE) || '\'\'';
if (block.getFieldValue('END') == 'FIRST') {
if (block.getFieldValue('END') === 'FIRST') {
var functionName = Blockly.Lua.provideFunction_(
'firstIndexOf',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
@@ -118,13 +118,13 @@ Blockly.Lua['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 atOrder = (where == 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
var atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
Blockly.Lua.ORDER_NONE;
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
var text = Blockly.Lua.valueToCode(block, 'VALUE',
Blockly.Lua.ORDER_NONE) || '\'\'';
var code;
if (where == 'RANDOM') {
if (where === 'RANDOM') {
var functionName = Blockly.Lua.provideFunction_(
'text_random_letter',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str)',
@@ -133,14 +133,14 @@ Blockly.Lua['text_charAt'] = function(block) {
'end']);
code = functionName + '(' + text + ')';
} else {
if (where == 'FIRST') {
if (where === 'FIRST') {
var start = '1';
} else if (where == 'LAST') {
} else if (where === 'LAST') {
var start = '-1';
} else {
if (where == 'FROM_START') {
if (where === 'FROM_START') {
var start = at;
} else if (where == 'FROM_END') {
} else if (where === 'FROM_END') {
var start = '-' + at;
} else {
throw Error('Unhandled option (text_charAt).');
@@ -169,14 +169,14 @@ Blockly.Lua['text_getSubstring'] = function(block) {
// Get start index.
var where1 = block.getFieldValue('WHERE1');
var at1Order = (where1 == 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
var at1Order = (where1 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
Blockly.Lua.ORDER_NONE;
var at1 = Blockly.Lua.valueToCode(block, 'AT1', at1Order) || '1';
if (where1 == 'FIRST') {
if (where1 === 'FIRST') {
var start = 1;
} else if (where1 == 'FROM_START') {
} else if (where1 === 'FROM_START') {
var start = at1;
} else if (where1 == 'FROM_END') {
} else if (where1 === 'FROM_END') {
var start = '-' + at1;
} else {
throw Error('Unhandled option (text_getSubstring)');
@@ -184,14 +184,14 @@ Blockly.Lua['text_getSubstring'] = function(block) {
// Get end index.
var where2 = block.getFieldValue('WHERE2');
var at2Order = (where2 == 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
var at2Order = (where2 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
Blockly.Lua.ORDER_NONE;
var at2 = Blockly.Lua.valueToCode(block, 'AT2', at2Order) || '1';
if (where2 == 'LAST') {
if (where2 === 'LAST') {
var end = -1;
} else if (where2 == 'FROM_START') {
} else if (where2 === 'FROM_START') {
var end = at2;
} else if (where2 == 'FROM_END') {
} else if (where2 === 'FROM_END') {
var end = '-' + at2;
} else {
throw Error('Unhandled option (text_getSubstring)');
@@ -205,11 +205,11 @@ Blockly.Lua['text_changeCase'] = function(block) {
var operator = block.getFieldValue('CASE');
var text = Blockly.Lua.valueToCode(block, 'TEXT',
Blockly.Lua.ORDER_NONE) || '\'\'';
if (operator == 'UPPERCASE') {
if (operator === 'UPPERCASE') {
var functionName = 'string.upper';
} else if (operator == 'LOWERCASE') {
} else if (operator === 'LOWERCASE') {
var functionName = 'string.lower';
} else if (operator == 'TITLECASE') {
} else if (operator === 'TITLECASE') {
var functionName = Blockly.Lua.provideFunction_(
'text_titlecase',
// There are shorter versions at
@@ -278,7 +278,7 @@ Blockly.Lua['text_prompt_ext'] = function(block) {
'end']);
var code = functionName + '(' + msg + ')';
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
var toNumber = block.getFieldValue('TYPE') === 'NUMBER';
if (toNumber) {
code = 'tonumber(' + code + ', 10)';
}