Skip to content

Commit

Permalink
Repaired and improved findSyntaxError function (#470)
Browse files Browse the repository at this point in the history
This has been broken ever since I changed the code to use an iframe,
since the SyntaxError thrown from evaluating code in the iframe is not
the same as the SyntaxError in the parent frame.

Also made it run the code in strict mode so strict-mode-specific errors
are reported properly, and not include lines that are added by
validation code (and thus don't get included in the editor's line count)
  • Loading branch information
pppery committed Mar 26, 2021
1 parent 0857b80 commit d368daa
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions scripts/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Game.prototype.validate = function(allCode, playerCode, restartingLevelFromScrip
}

var allowjQuery = dummyMap._properties.showDummyDom;
// setup iframe in which code is run. As a side effect, this sets `this._eval` correctly
// setup iframe in which code is run. As a side effect, this sets `this._eval`
// and `this.SyntaxError` correctly.
var userEnv = this.initIframe(allowjQuery);

// evaluate the code to get startLevel() and (opt) validateLevel() methods
Expand Down Expand Up @@ -109,7 +110,7 @@ Game.prototype.validate = function(allCode, playerCode, restartingLevelFromScrip
}

var exceptionText = e.toString();
if (e instanceof SyntaxError) {
if (e instanceof this.SyntaxError) {
var lineNum = this.findSyntaxError(allCode, e.message);
if (lineNum) {
exceptionText = "[Line " + lineNum + "] " + exceptionText;
Expand Down Expand Up @@ -244,6 +245,7 @@ Game.prototype.initIframe = function(allowjQuery){
var iframewindow = iframe.contentWindow;
if (iframewindow.eval) {
this._eval = iframewindow.eval;
this.SyntaxError = iframewindow.SyntaxError;
}
// delete any unwated global variables in the iframe
function purgeObject(object) {
Expand Down Expand Up @@ -317,14 +319,25 @@ Game.prototype.secureProperty = function(object, prop, objecttype){
// of code where a given error occurs
Game.prototype.findSyntaxError = function(code, errorMsg) {
var lines = code.split('\n');
// One line at the top is the added declarations and doesn't
// correspond to any real editor code
var phantomLines = 1;
for (var i = 1; i <= lines.length; i++) {
var line = lines[i - 1];
var startStartLevel = "map._startOfStartLevelReached()";
var endStartLevel = "map._endOfStartLevelReached()";
if (line == startStartLevel || line == endStartLevel ) {
// This line was added by the editor and doesn't show up to the user
// so shouldn't be counted.
phantomLines += 1;
}
var testCode = lines.slice(0, i).join('\n');

try {
this._eval(testCode);
this._eval("'use strict';" + testCode);
} catch (e) {
if (e.message === errorMsg) {
return i;
return i - phantomLines;
}
}
}
Expand Down

0 comments on commit d368daa

Please sign in to comment.