Skip to content

Commit

Permalink
[[FIX]] Offset line no.s of errors from eval code
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike committed Oct 20, 2016
1 parent af4e000 commit 2a31c94
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
54 changes: 35 additions & 19 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,13 @@ var JSHINT = (function() {
}

// Tracking of "internal" scripts, like eval containing a static string
function addInternalSrc(elem, src) {
var i;
i = {
function addInternalSrc(elem, token) {
JSHINT.internals.push({
id: "(internal)",
elem: elem,
value: src
};
JSHINT.internals.push(i);
return i;
token: token,
code: token.value.replace(/([^\\])(\\*)\2\\n/g, "$1\n")
});
}

function doOption() {
Expand Down Expand Up @@ -2473,13 +2471,13 @@ var JSHINT = (function() {
// to the fact that the behavior was never formally documented). This
// branch should be enabled as part of a major release.
//if (p[0] && p[0].id === "(string)") {
// addInternalSrc(left, p[0].value);
// addInternalSrc(left, p[0]);
//}
} else if (p[0] && p[0].id === "(string)" &&
(left.value === "setTimeout" ||
left.value === "setInterval")) {
warning("W066", left);
addInternalSrc(left, p[0].value);
addInternalSrc(left, p[0]);

// window.setTimeout/setInterval
} else if (p[0] && p[0].id === "(string)" &&
Expand All @@ -2488,7 +2486,7 @@ var JSHINT = (function() {
(left.right === "setTimeout" ||
left.right === "setInterval")) {
warning("W066", left);
addInternalSrc(left, p[0].value);
addInternalSrc(left, p[0]);
}
}
if (!left.identifier && left.id !== "." && left.id !== "[" && left.id !== "=>" &&
Expand Down Expand Up @@ -5171,13 +5169,38 @@ var JSHINT = (function() {
}
}

/**
* Lint dynamically-evaluated code, appending any resulting errors/warnings
* into the global `errors` array.
*
* @param {array} internals - collection of "internals" objects describing
* string tokens that contain evaluated code
* @param {object} options - linting options to apply
* @param {object} globals - globally-defined bindings for the evaluated code
*/
function lintEvalCode(internals, options, globals) {
var priorErrorCount, idx, jdx, internal;

for (idx = 0; idx < internals.length; idx += 1) {
internal = internals[idx];
options.scope = internal.elem;
priorErrorCount = JSHINT.errors.length;

itself(internal.code, options, globals);

for (jdx = priorErrorCount; jdx < JSHINT.errors.length; jdx += 1) {
JSHINT.errors[jdx].line += internal.token.line - 1;
}
}
}

var escapeRegex = function(str) {
return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
};

// The actual JSHINT function itself.
var itself = function(s, o, g) {
var i, k, x, reIgnoreStr, reIgnore;
var x, reIgnoreStr, reIgnore;
var optionKeys;
var newOptionObj = {};
var newIgnoredObj = {};
Expand Down Expand Up @@ -5424,15 +5447,8 @@ var JSHINT = (function() {
}

// Loop over the listed "internals", and check them as well.

if (JSHINT.scope === "(main)") {
o = o || {};

for (i = 0; i < JSHINT.internals.length; i += 1) {
k = JSHINT.internals[i];
o.scope = k.elem;
itself(k.value, o, g);
}
lintEvalCode(JSHINT.internals, o || {}, g);
}

return JSHINT.errors.length === 0;
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,10 @@ exports.insideEval = function (test) {

// The "TestRun" class (and these errors) probably needs some
// facility for checking the expected scope of the error
.addError(1, "Unexpected early end of program.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.addError(13, "Unexpected early end of program.")
.addError(13, "Unrecoverable syntax error. (100% scanned).")
.addError(17, "Unexpected early end of program.")
.addError(17, "Unrecoverable syntax error. (100% scanned).")

.test(src, { es3: true, evil: false });

Expand Down

0 comments on commit 2a31c94

Please sign in to comment.