From 80131f0398c4bf53063ee3c18035c51a4710fd8e Mon Sep 17 00:00:00 2001 From: sasensi Date: Fri, 23 Nov 2018 11:54:42 +0100 Subject: [PATCH] Fix PaperScript#compile() with prefix operators Closes #1611 --- src/core/PaperScript.js | 5 +++++ test/tests/PaperScript.js | 20 ++++++++++++++++++++ test/tests/load.js | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 test/tests/PaperScript.js diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index 00a40985..bdd8f4ab 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -268,6 +268,11 @@ Base.exports.PaperScript = function() { str = exp; str = arg + '; ' + str; } + // If this is a prefixed update expression (++a / --a), + // wrap expression in IIFE to avoid several bugs (#1450) + if (node.prefix) { + str = '(function(){return '+str+';})()'; + } replaceCode(node, str); } else { // AssignmentExpression if (/^.=$/.test(node.operator) diff --git a/test/tests/PaperScript.js b/test/tests/PaperScript.js new file mode 100644 index 00000000..fc07f950 --- /dev/null +++ b/test/tests/PaperScript.js @@ -0,0 +1,20 @@ +/* + * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. + * http://paperjs.org/ + * + * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey + * http://scratchdisk.com/ & https://puckey.studio/ + * + * Distributed under the MIT license. See LICENSE file for details. + * + * All rights reserved. + */ + +QUnit.module('PaperScript'); + +test('PaperScript#compile() with prefix increment/decrement operators', function() { + var code = 'var x = 1; var y = 1 * --x;'; + var compiled = PaperScript.compile(code, paper); + PaperScript.execute(compiled, paper); + expect(0); +}); diff --git a/test/tests/load.js b/test/tests/load.js index 5ceca1ce..a2546737 100644 --- a/test/tests/load.js +++ b/test/tests/load.js @@ -63,6 +63,8 @@ /*#*/ include('Numerical.js'); +/*#*/ include('PaperScript.js'); + // There is no need to test interactions in node context. if (!isNodeContext) { /*#*/ include('Interactions.js');