mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 06:00:56 -05:00
Preserve line-breaks when replacing mathematical operators, to not mess with debugging.
This commit is contained in:
parent
be82af9b47
commit
3be1e481e9
1 changed files with 16 additions and 3 deletions
|
@ -138,6 +138,12 @@ Base.exports.PaperScript = (function() {
|
|||
getOffset(node.range[1]));
|
||||
}
|
||||
|
||||
// Returns the code between two nodes, e.g. an operator and white-space.
|
||||
function getBetween(left, right) {
|
||||
return code.substring(getOffset(left.range[1]),
|
||||
getOffset(right.range[0]));
|
||||
}
|
||||
|
||||
// Replaces the node's code with a new version and keeps insertions
|
||||
// information up-to-date.
|
||||
function replaceCode(node, str) {
|
||||
|
@ -188,9 +194,16 @@ Base.exports.PaperScript = (function() {
|
|||
if (node.operator in binaryOperators
|
||||
&& node.left.type !== 'Literal') {
|
||||
var left = getCode(node.left),
|
||||
right = getCode(node.right);
|
||||
replaceCode(node, '__$__(' + left + ', "' + node.operator
|
||||
+ '", ' + right + ')');
|
||||
right = getCode(node.right),
|
||||
between = getBetween(node.left, node.right),
|
||||
operator = node.operator;
|
||||
replaceCode(node, '__$__(' + left + ','
|
||||
// To preserve line-breaks, get the code in between
|
||||
// left & right, and replace the occurrence of the
|
||||
// operator with its string counterpart:
|
||||
+ between.replace(new RegExp('\\' + operator),
|
||||
'"' + operator + '"')
|
||||
+ ', ' + right + ')');
|
||||
}
|
||||
break;
|
||||
case 'UpdateExpression': // a++, a--, ++a, --a
|
||||
|
|
Loading…
Reference in a new issue