Skip to content

Commit

Permalink
[[FIX]] Tolerate whitespace in inline directives
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmryan authored and jugglinmike committed Dec 24, 2017
1 parent badc7a4 commit efeb0f8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lex.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,17 @@ Lexer.prototype = {
body = body.substr(str.length + 1);
}

// To handle rarer case when special word is separated from label by
// multiple spaces or tabs
var strIndex = body.indexOf(str);
if (!isSpecial && strIndex >= 0 && body.charAt(strIndex + str.length) === " ") {
var isAllWhitespace = body.substr(0, strIndex).trim().length === 0;
if (isAllWhitespace) {
isSpecial = true;
body = body.substr(str.length + strIndex);
}
}

if (!isSpecial) {
return;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/fixtures/inline-tabs-spaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function a() {
// jshint unused: true
var x;
}

function b() {
/* jshint
unused: true
*/
var y;
}

function c() {
/* jshint
unused: true
*/
var z;
}
12 changes: 12 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ exports["jshint option comments multi line/option, leading and trailing space"]
test.done();
};

exports["jshint option inline comments, leading and trailing tabs and spaces"] = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/inline-tabs-spaces.js", "utf8");

TestRun(test)
.addError(3, 9, "'x' is defined but never used.")
.addError(10, 9, "'y' is defined but never used.")
.addError(17, 9, "'z' is defined but never used.")
.test(src);

test.done();
};

exports.shebang = function (test) {
var code = [
"#!test",
Expand Down

0 comments on commit efeb0f8

Please sign in to comment.