From 3bba227620567f333fddba96cb8dacdcd3932a3a Mon Sep 17 00:00:00 2001 From: William Dimaculangan Date: Sat, 23 Apr 2016 11:44:18 +0800 Subject: [PATCH] Stop inject from mangling label when offset from loop --- public/js/lib/loop-protect/loop-protect.js | 7 +++++-- test/public/js/loop-protect-test.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 test/public/js/loop-protect-test.js diff --git a/public/js/lib/loop-protect/loop-protect.js b/public/js/lib/loop-protect/loop-protect.js index 60031599f5..b4f2fdb454 100644 --- a/public/js/lib/loop-protect/loop-protect.js +++ b/public/js/lib/loop-protect/loop-protect.js @@ -130,6 +130,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; } var ignore = {}; var pushonly = {}; var labelPostion = null; + var labelIndex = -1; function insertReset(lineNum, line, matchPosition) { // recompile the line with the reset **just** before the actual loop @@ -179,6 +180,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; } if (directlyBeforeLoop(index, lineNum, lines)) { DEBUG && debug('- found a label: "' + labelMatch[0] + '"'); // jshint ignore:line labelPostion = lineNum; + labelIndex = index; } else { DEBUG && debug('- ignored "label", false positive'); // jshint ignore:line } @@ -309,10 +311,11 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; } DEBUG && debug('- reset inserted above matched label on line ' + labelPostion); // jshint ignore:line if (recompiled[labelPostion] === undefined) { labelPostion--; - matchPosition = 0; + labelIndex = 0; } - recompiled[labelPostion] = insertReset(printLineNumber, recompiled[labelPostion], matchPosition); + recompiled[labelPostion] = insertReset(printLineNumber, recompiled[labelPostion], labelIndex); labelPostion = null; + labelIndex = -1; } } diff --git a/test/public/js/loop-protect-test.js b/test/public/js/loop-protect-test.js new file mode 100644 index 0000000000..6ec3cbebdd --- /dev/null +++ b/test/public/js/loop-protect-test.js @@ -0,0 +1,15 @@ +'use strict'; + +let loopProtect = require('../../../public/js/lib/loop-protect/loop-protect'); + +let test = require('tape'); + +test('LoopProtect injection', function(t) { + t.plan(1); + + // Label indented 2 spaces - loop indented three spaces + t.true( + loopProtect(' loop1:\n while(true) {\n\n}').indexOf('loop1') > 0, + 'Should keep loop label intact if not lined up with loop.' + ); +}); \ No newline at end of file