Remove unneeded generated parens around function calls in indexOf blocks.

This commit is contained in:
Neil Fraser 2016-06-13 18:49:18 -07:00
commit fd6f743f0e
7 changed files with 55 additions and 25 deletions

View file

@ -1037,7 +1037,7 @@ Blockly.BlockSvg.prototype.disposeUiEffect = function() {
this.workspace.getParentSvg().appendChild(clone);
clone.bBox_ = clone.getBBox();
// Start the animation.
Blockly.BlockSvg.disposeUiStep_(clone, this.RTL, new Date(),
Blockly.BlockSvg.disposeUiStep_(clone, this.RTL, new Date,
this.workspace.scale);
};
@ -1052,7 +1052,7 @@ Blockly.BlockSvg.prototype.disposeUiEffect = function() {
* @private
*/
Blockly.BlockSvg.disposeUiStep_ = function(clone, rtl, start, workspaceScale) {
var ms = (new Date()) - start;
var ms = new Date - start;
var percent = ms / 150;
if (percent > 1) {
goog.dom.removeNode(clone);
@ -1094,7 +1094,7 @@ Blockly.BlockSvg.prototype.connectionUiEffect = function() {
'stroke': '#888', 'stroke-width': 10},
this.workspace.getParentSvg());
// Start the animation.
Blockly.BlockSvg.connectionUiStep_(ripple, new Date(), this.workspace.scale);
Blockly.BlockSvg.connectionUiStep_(ripple, new Date, this.workspace.scale);
};
/**
@ -1105,7 +1105,7 @@ Blockly.BlockSvg.prototype.connectionUiEffect = function() {
* @private
*/
Blockly.BlockSvg.connectionUiStep_ = function(ripple, start, workspaceScale) {
var ms = (new Date()) - start;
var ms = new Date - start;
var percent = ms / 150;
if (percent > 1) {
goog.dom.removeNode(ripple);
@ -1136,7 +1136,7 @@ Blockly.BlockSvg.prototype.disconnectUiEffect = function() {
magnitude *= -1;
}
// Start the animation.
Blockly.BlockSvg.disconnectUiStep_(this.svgGroup_, magnitude, new Date());
Blockly.BlockSvg.disconnectUiStep_(this.svgGroup_, magnitude, new Date);
};
/**
@ -1150,7 +1150,7 @@ Blockly.BlockSvg.disconnectUiStep_ = function(group, magnitude, start) {
var DURATION = 200; // Milliseconds.
var WIGGLES = 3; // Half oscillations.
var ms = (new Date()) - start;
var ms = new Date - start;
var percent = ms / DURATION;
if (percent > 1) {

View file

@ -950,7 +950,7 @@ Blockly.WorkspaceSvg.prototype.playAudio = function(name, opt_volume) {
var sound = this.SOUNDS_[name];
if (sound) {
// Don't play one sound on top of another.
var now = new Date();
var now = new Date;
if (now - this.lastSound_ < Blockly.SOUND_LIMIT) {
return;
}

View file

@ -90,8 +90,12 @@ Blockly.Dart['lists_getIndex'] = function(block) {
var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.Dart.valueToCode(block, 'AT',
Blockly.Dart.ORDER_UNARY_PREFIX) || '1';
var list = Blockly.Dart.valueToCode(block, 'VALUE',
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
// Special case to avoid wrapping function calls in unneeded parenthesis.
// func()[0] is prefered over (func())[0]
var valueBlock = this.getInputTargetBlock('VALUE');
var order = (valueBlock && valueBlock.type == 'procedures_callreturn') ?
Blockly.Dart.ORDER_NONE : Blockly.Dart.ORDER_UNARY_POSTFIX;
var list = Blockly.Dart.valueToCode(block, 'VALUE', order) || '[]';
if (where == 'FIRST') {
if (mode == 'GET') {

View file

@ -98,8 +98,12 @@ Blockly.JavaScript['lists_getIndex'] = function(block) {
var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.JavaScript.valueToCode(block, 'AT',
Blockly.JavaScript.ORDER_UNARY_NEGATION) || '1';
var list = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
// Special case to avoid wrapping function calls in unneeded parenthesis.
// func()[0] is prefered over (func())[0]
var valueBlock = this.getInputTargetBlock('VALUE');
var order = (valueBlock && valueBlock.type == 'procedures_callreturn') ?
Blockly.JavaScript.ORDER_NONE : Blockly.JavaScript.ORDER_MEMBER;
var list = Blockly.JavaScript.valueToCode(block, 'VALUE', order) || '[]';
if (where == 'FIRST') {
if (mode == 'GET') {

View file

@ -159,8 +159,17 @@ Blockly.Lua['lists_getIndex'] = function(block) {
var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.Lua.valueToCode(block, 'AT',
Blockly.Lua.ORDER_ADDITIVE) || '1';
var list = Blockly.Lua.valueToCode(block, 'VALUE',
Blockly.Lua.ORDER_HIGH) || '({})';
if (mode == 'GET') {
// Special case to avoid wrapping function calls in unneeded parenthesis.
// func()[0] is prefered over (func())[0]
var valueBlock = this.getInputTargetBlock('VALUE');
var order = (valueBlock && valueBlock.type == 'procedures_callreturn') ?
Blockly.Lua.ORDER_NONE : Blockly.Lua.ORDER_HIGH;
} else {
// List will be an argument in a function call.
var order = Blockly.Lua.ORDER_NONE;
}
var list = Blockly.Lua.valueToCode(block, 'VALUE', order) || '({})';
var getIndex_ = Blockly.Lua.lists.getIndex_;
var gensym_ = Blockly.Lua.lists.gensym_;
@ -329,7 +338,7 @@ Blockly.Lua['lists_sort'] = function(block) {
var functionName = Blockly.Lua.provideFunction_(
'list_sort',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
'(list, typev, direction)',
' local t = {}',
' for n,v in pairs(list) do table.insert(t, v) end', // Shallow-copy.
@ -351,9 +360,9 @@ Blockly.Lua['lists_sort'] = function(block) {
' return t',
'end']);
var code = functionName +
var code = functionName +
'(' + listCode + ',"' + type + '", ' + direction + ')';
return [code, Blockly.Lua.ORDER_HIGH];
return [code, Blockly.Lua.ORDER_HIGH];
};
Blockly.Lua['lists_split'] = function(block) {

View file

@ -132,8 +132,17 @@ Blockly.PHP['lists_getIndex'] = function(block) {
var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.PHP.valueToCode(block, 'AT',
Blockly.PHP.ORDER_UNARY_NEGATION) || '1';
var list = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
if (mode == 'GET') {
// Special case to avoid wrapping function calls in unneeded parenthesis.
// func()[0] is prefered over (func())[0]
var valueBlock = this.getInputTargetBlock('VALUE');
var order = (valueBlock && valueBlock.type == 'procedures_callreturn') ?
Blockly.PHP.ORDER_NONE : Blockly.PHP.ORDER_FUNCTION_CALL;
} else {
// List will be an argument in a function call.
var order = Blockly.PHP.ORDER_COMMA;
}
var list = Blockly.PHP.valueToCode(block, 'VALUE', order) || 'array()';
if (where == 'FIRST') {
if (mode == 'GET') {

View file

@ -105,8 +105,12 @@ Blockly.Python['lists_getIndex'] = function(block) {
var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.Python.valueToCode(block, 'AT',
Blockly.Python.ORDER_UNARY_SIGN) || '1';
var list = Blockly.Python.valueToCode(block, 'VALUE',
Blockly.Python.ORDER_MEMBER) || '[]';
// Special case to avoid wrapping function calls in unneeded parenthesis.
// func()[0] is prefered over (func())[0]
var valueBlock = this.getInputTargetBlock('VALUE');
var order = (valueBlock && valueBlock.type == 'procedures_callreturn') ?
Blockly.Python.ORDER_NONE : Blockly.Python.ORDER_MEMBER;
var list = Blockly.Python.valueToCode(block, 'VALUE', order) || '[]';
if (where == 'FIRST') {
if (mode == 'GET') {
@ -314,12 +318,12 @@ Blockly.Python['lists_getSublist'] = function(block) {
Blockly.Python['lists_sort'] = function(block) {
// Block for sorting a list.
var listCode = (Blockly.Python.valueToCode(block, 'LIST',
var listCode = (Blockly.Python.valueToCode(block, 'LIST',
Blockly.Python.ORDER_MEMBER) || '[]');
var type = block.getFieldValue('TYPE');
var reverse = block.getFieldValue('DIRECTION') === '1' ? 'False' : 'True';
var sortFunctionName = Blockly.Python.provideFunction_('lists_sort',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
'(listv, type, reversev):',
' def tryfloat(s):',
' try:',
@ -329,14 +333,14 @@ Blockly.Python['lists_sort'] = function(block) {
' keyFuncts = {',
' "NUMERIC": tryfloat,',
' "TEXT": str,',
' "IGNORE_CASE": lambda s: str(s).lower()',
' "IGNORE_CASE": lambda s: str(s).lower()',
' }',
' keyv = keyFuncts[type]',
' tmp_list = list(listv)', // Clone the list.
' return sorted(tmp_list, key=keyv, reverse=reversev)'
]);
]);
var code = sortFunctionName +
var code = sortFunctionName +
'(' + listCode + ', "' + type + '", ' + reverse + ')';
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};