Support prefixed UpdateExpressions and fix a bug in suffixed UpdateExpression when assigning to plain variables.

Related #492
This commit is contained in:
Jürg Lehni 2014-07-26 12:40:00 +02:00
parent 54f0197eba
commit 06366fb0ed

View file

@ -209,32 +209,35 @@ Base.exports.PaperScript = (function() {
+ '", ' + right + ')'); + '", ' + right + ')');
} }
break; break;
case 'UpdateExpression': // a++, a-- case 'UpdateExpression': // a++, a--, ++a, --a
case 'AssignmentExpression': /// a += b, a -= b case 'AssignmentExpression': /// a += b, a -= b
if (!(parent && ( var parentType = parent && parent.type;
if (!(
// Filter out for statements to allow loop increments // Filter out for statements to allow loop increments
// to perform well // to perform well
parent.type === 'ForStatement' parentType === 'ForStatement'
// We need to filter out parents that are comparison // We need to filter out parents that are comparison
// operators, e.g. for situations like `if (++i < 1)`, // operators, e.g. for situations like `if (++i < 1)`,
// as we can't replace that with // as we can't replace that with
// `if (__$__(i, "+", 1) < 1)` // `if (__$__(i, "+", 1) < 1)`
// Match any operator beginning with =, !, < and >. // Match any operator beginning with =, !, < and >.
|| parent.type === 'BinaryExpression' || parentType === 'BinaryExpression'
&& /^[=!<>]/.test(parent.operator) && /^[=!<>]/.test(parent.operator)
// array[i++] is a MemberExpression with computed = true // array[i++] is a MemberExpression with computed = true
// We can't replace that with array[__$__(i, "+", 1)]. // We can't replace that with array[__$__(i, "+", 1)].
|| parent.type === 'MemberExpression' || parentType === 'MemberExpression' && parent.computed
&& parent.computed))) { )) {
if (node.type === 'UpdateExpression') { if (node.type === 'UpdateExpression') {
if (!node.prefix) { var arg = getCode(node.argument);
var arg = getCode(node.argument); var str = arg + ' = __$__(' + arg
var str = arg + ' = __$__(' + arg + ', "' + node.operator[0] + '", 1)';
+ ', "' + node.operator[0] + '", 1)'; // If this is not a prefixed update expression
if (parent.type === 'AssignmentExpression') // (++a, --a), assign the old value before updating it.
str = arg + '; ' + str; if (!node.prefix
replaceCode(node, str); && (parentType === 'AssignmentExpression'
} || parentType === 'VariableDeclarator'))
str = arg + '; ' + str;
replaceCode(node, str);
} else { // AssignmentExpression } else { // AssignmentExpression
if (/^.=$/.test(node.operator) if (/^.=$/.test(node.operator)
&& node.left.type !== 'Literal') { && node.left.type !== 'Literal') {
@ -298,7 +301,6 @@ Base.exports.PaperScript = (function() {
} }
// Now do the parsing magic // Now do the parsing magic
walkAST(parse(code, { ranges: true })); walkAST(parse(code, { ranges: true }));
console.log(code);
if (sourceMap) { if (sourceMap) {
// Adjust the line offset of the resulting code if required. // Adjust the line offset of the resulting code if required.
// This is part of a browser hack, see above. // This is part of a browser hack, see above.