Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Error on invalid JSHINT(...) input

This patch makes JSHint to return an error whenever you try to use
the function with an invalid first parameter. First parameter has
to be either a non-empty string or a non-empty array.

Closes GH-567.
  • Loading branch information...
commit a207fc38050cb19269358b083c8d7b7f88c6dbf8 1 parent 72801c4
@valueof valueof authored
Showing with 48 additions and 8 deletions.
  1. +26 −8 jshint.js
  2. +22 −0 tests/unit/core.js
View
34 jshint.js
@@ -833,10 +833,8 @@ var JSHINT = (function () {
function F() {} // Used by Object.create
function is_own(object, name) {
-
-// The object.hasOwnProperty method fails when the property under consideration
-// is named 'hasOwnProperty'. So we have to use this more convoluted form.
-
+ // The object.hasOwnProperty method fails when the property under consideration
+ // is named 'hasOwnProperty'. So we have to use this more convoluted form.
return Object.prototype.hasOwnProperty.call(object, name);
}
@@ -846,7 +844,11 @@ var JSHINT = (function () {
}
}
-// Provide critical ES5 functions to ES3.
+ function isString(obj) {
+ return Object.prototype.toString.call(obj) === '[object String]';
+ }
+
+ // Provide critical ES5 functions to ES3.
if (typeof Array.isArray !== 'function') {
Array.isArray = function (o) {
@@ -873,7 +875,7 @@ var JSHINT = (function () {
};
}
-// Non standard methods
+ // Non standard methods
if (typeof String.prototype.entityify !== 'function') {
String.prototype.entityify = function () {
@@ -4183,9 +4185,9 @@ loop: for (;;) {
}
-// The actual JSHINT function itself.
-
+ // The actual JSHINT function itself.
var itself = function (s, o, g) {
+ /*global console */
var a, i, k, x,
optionKeys,
newOptionObj = {};
@@ -4194,6 +4196,22 @@ loop: for (;;) {
JSHINT.undefs = [];
predefined = Object.create(standard);
combine(predefined, g || {});
+
+ if (!isString(s) && !Array.isArray(s)) {
+ errorAt("Input is neither a string nor an array of strings.", 0);
+ return false;
+ }
+
+ if (isString(s) && /^\s*$/g.test(s)) {
+ errorAt("Input is an empty string.", 0);
+ return false;
+ }
+
+ if (s.length === 0) {
+ errorAt("Input is an empty array.", 0);
+ return false;
+ }
+
if (o) {
a = o.predef;
if (a) {
View
22 tests/unit/core.js
@@ -399,3 +399,25 @@ exports.multilineArray = function () {
TestRun().test(src);
};
+
+exports.testInvalidSource = function () {
+ TestRun()
+ .addError(0, "Input is neither a string nor an array of strings.")
+ .test(undefined);
+
+ TestRun()
+ .addError(0, "Input is neither a string nor an array of strings.")
+ .test(null);
+
+ TestRun()
+ .addError(0, "Input is neither a string nor an array of strings.")
+ .test({});
+
+ TestRun()
+ .addError(0, "Input is an empty string.")
+ .test("");
+
+ TestRun()
+ .addError(0, "Input is an empty array.")
+ .test([]);
+};
Please sign in to comment.
Something went wrong with that request. Please try again.