Skip to content

Commit

Permalink
Use the same filtering for UpdateExpression and AssignmentExpression.
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni committed Jan 5, 2014
1 parent cd76049 commit dd37704
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/core/PaperScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,15 @@ var PaperScript = Base.exports.PaperScript = (function() {
}
}
switch (node && node.type) {
case 'UnaryExpression':
// -a
case 'UnaryExpression': // -a
if (node.operator in unaryOperators
&& node.argument.type !== 'Literal') {
var arg = getCode(node.argument);
replaceCode(node, '$_("' + node.operator + '", '
+ arg + ')');
}
break;
case 'BinaryExpression':
// a + b, a - b, a / b, a * b, a == b, a % b, ...
case 'BinaryExpression': // a + b, a - b, a / b, a * b, a == b, ...
if (node.operator in binaryOperators
&& node.left.type !== 'Literal') {
var left = getCode(node.left),
Expand All @@ -187,9 +185,9 @@ var PaperScript = Base.exports.PaperScript = (function() {
+ '", ' + right + ')');
}
break;
case 'UpdateExpression':
// a++, a--
if (!node.prefix && !(parent && (
case 'UpdateExpression': // a++, a--
case 'AssignmentExpression': /// a += b, a -= b
if (!(parent && (
// Filter out for statements to allow loop increments
// to perform well
parent.type === 'ForStatement'
Expand All @@ -203,20 +201,21 @@ var PaperScript = Base.exports.PaperScript = (function() {
// We can't replace that with array[_$_(i, "+", 1)].
|| parent.type === 'MemberExpression'
&& parent.computed))) {
var arg = getCode(node.argument);
replaceCode(node, arg + ' = _$_(' + arg + ', "'
+ node.operator[0] + '", 1)');
}
break;
case 'AssignmentExpression':
/// a += b, a -= b
if (/^.=$/.test(node.operator)
&& node.left.type !== 'Literal'
&& !(parent && parent.type === 'ForStatement')) {
var left = getCode(node.left),
right = getCode(node.right);
replaceCode(node, left + ' = _$_(' + left + ', "'
+ node.operator[0] + '", ' + right + ')');
if (node.type === 'UpdateExpression') {
if (!node.prefix) {
var arg = getCode(node.argument);
replaceCode(node, arg + ' = _$_(' + arg + ', "'
+ node.operator[0] + '", 1)');
}
} else {
if (/^.=$/.test(node.operator)
&& node.left.type !== 'Literal') {
var left = getCode(node.left),
right = getCode(node.right);
replaceCode(node, left + ' = _$_(' + left + ', "'
+ node.operator[0] + '", ' + right + ')');
}
}
}
break;
}
Expand Down

0 comments on commit dd37704

Please sign in to comment.