Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Successfully detecting labels

  • Loading branch information...
commit 4c6d1d245f6dd625dcb2b3d0df548f8a560d755e 1 parent ce5d3d6
@remy remy authored
View
25 public/js/runner/loop-protect.js
@@ -10,7 +10,9 @@ var loopProtect = (function () {
// the standard loops - note that recursive is not supported
var re = /\b(for|while|do)\b/g;
+ var reSingle = /\b(for|while|do)\b/;
var labelRe = /\b([a-z_]{1}\w+:)/i;
+ var comments = /(?:\/\*(?:[\s\S]*?)\*\/)|(?:([\s;])+\/\/(?:.*)$)/gm;
var loopProtect = {};
@@ -85,13 +87,22 @@ var loopProtect = (function () {
}
function directlyBeforeLoop(index, lineNum, lines) {
- re.lastIndex = 0;
-
-
-
-
+ reSingle.lastIndex = 0;
+ labelRe.lastIndex = 0;
+ var beforeLoop = false;
+
+ var theRest = lines.slice(lineNum).join('\n').substr(index).replace(labelRe, '');
+ theRest.replace(reSingle, function (match, capture, i) {
+ var target = theRest.substr(0, i).replace(comments, '').trim();
+ debug('- directlyBeforeLoop: ' + target);
+ if (target.length === 0) {
+ beforeLoop = true;
+ }
+ // strip comments out of the target, and if there's nothing else
+ // it's a valid label...I hope!
+ });
- return false;
+ return beforeLoop;
}
/**
@@ -152,7 +163,7 @@ var loopProtect = (function () {
index = line.indexOf(labelMatch[1]);
if (!inCommentOrString(index, line)) {
if (!inMultilineComment(lineNum, lines)) {
- if (!directlyBeforeLoop(index, lineNum, lines)) {
+ if (directlyBeforeLoop(index, lineNum, lines)) {
debug('- found a label: "' + labelMatch[0] + '"');
labelPostion = lineNum;
} else {
View
44 test/public/runner/loop-protect.test.js
@@ -35,22 +35,22 @@ var code = {
dowhilenested: 'var x=0;\n do\n {\n x++;\n var b = 0;\n do {\n b++; \n } while (b < 3);\n } while (x < 3);\nreturn x;',
disabled: '// noprotect\nvar x=0;\ndo\n {\n x++;\n } while (x < 3);\nreturn x;',
continues: 'var n = 0,\n i = 0,\n j = 0;\n \n outside:\n for (i; i < 10; i += 1) {\n for (j; j < 10; j += 1) {\n if (i === 5 && j === 5) {\n continue outside;\n }\n n += 1;\n }\n }\n \n return n;\n;',
+ labelWithComment: 'label:\n// here be a label\n/*\n and there\'s some good examples in the loop - poop\n*/\nfor (var i = 0; i < 10; i++) {\n}\nreturn i;',
continues2: 'var x = 0;\nLABEL1: do {\n x = x + 2;\n if (x < 100) break LABEL1;\n if (x < 100) continue LABEL1;\n} \nwhile(0);\n\nreturn x;',
onelineforinline: 'function init() {\n for (var i=0;i<2;i++) (function(n) {\nconsole.log(i)})(i);\n}return true;',
notlabels: 'var foo = {\n bar: 1\n };\n \n function doit(i){}\n \n for (var i=0; i<10; i++) {\n doit(i);\n }\n return i;',
notlabels2: '// Weird:\nfor (var i = 0; i < 10; i++) {}\nreturn i;',
- cs: 'var bar, foo;\n\nfoo = function(i) {\n return {\n id: i\n };\n};\n\nbar = function(i) {\n var j, _i, _results;\n\n _results = [];\n for (j = _i = 1; 1 <= i ? _i < i : _i > \n; j = 1 <= i ? ++_i : --_i) {\n _results.push(j);\n }\n return _results;\n};',
+ cs: 'var bar, foo;\n\nfoo = function(i) {\n return {\n id: i\n };\n};\n\nbar = function(i) \n\n var j, _i, _results;\n\n _results = [];\n for (j = _i = 1; 1 <= i ? _i < i : _i > i; j = 1 <= i ? ++_i : --_i) {\n _results.push(j);\n }\n return _results;\n};',
};
+var spy;
-describe('loop', function () {
- var spy;
-
- function run(code) {
- var r = (new Function(code))(); // jshint ignore:line
- return r;
- }
+function run(code) {
+ var r = (new Function(code))(); // jshint ignore:line
+ return r;
+}
+describe('loop', function () {
beforeEach(function () {
spy = sinon.spy(run);
loopProtect.debug(false);
@@ -221,6 +221,13 @@ describe('loop', function () {
assert(spy(compiled) === 3);
});
+});
+
+describe('labels', function () {
+ beforeEach(function () {
+ spy = sinon.spy(run);
+ loopProtect.debug(false);
+ });
it('should handle continue statements and gotos', function () {
var c = code.continues;
@@ -232,8 +239,13 @@ describe('loop', function () {
assert(spy(compiled) === 2);
});
+ it('should handle labels with comments', function () {
+ var c = code.labelWithComment;
+ var compiled = loopProtect.rewriteLoops(c);
+ assert(spy(compiled) === 10);
+ });
+
it('should handle things that *look* like labels', function () {
- loopProtect.debug(true);
var c = code.notlabels2;
var compiled = loopProtect.rewriteLoops(c);
assert(compiled !== c);
@@ -246,15 +258,11 @@ describe('loop', function () {
result = run(compiled);
assert(result === 10, 'actual ' + result);
+ // c = code.cs;
+ // compiled = loopProtect.rewriteLoops(c);
+ // assert(compiled !== c);
+ // result = run(compiled);
+ // assert(result === 10, 'actual ' + result);
});
- it('should allow compiled coffeescript', function () {
- loopProtect.debug(true);
- var c = code.cs;
- var compiled = loopProtect.rewriteLoops(c);
- assert(compiled !== c);
- var result = run(compiled);
- assert(result === 10, 'actual ' + result);
-
- });
});
Please sign in to comment.
Something went wrong with that request. Please try again.