Repository: jshint/jshint
Pull request #2524 (2015-07-05 00:16:00)

Fix ignore
Triggering a test build to ensure this patch still passes on latest `master` and with my change applied.


Patch:
diff --git a/src/jshint.js b/src/jshint.js
index f0578597..e874559d 100644
--- a/src/jshint.js
+++ b/src/jshint.js
@@ -601,12 +601,6 @@ var JSHINT = (function() {
 
         if (key === "ignore") {
           switch (val) {
-          case "start":
-            state.ignoreLinterErrors = true;
-            break;
-          case "end":
-            state.ignoreLinterErrors = false;
-            break;
           case "line":
             state.ignoredLines[nt.line] = true;
             removeIgnoredMessages();
@@ -756,14 +750,6 @@ var JSHINT = (function() {
 
       if (state.tokens.next.isSpecial) {
         doOption();
-
-      // The `ignore` directive normally prevents tokens from being lexed, but
-      // the directive is only applied when the code is visited by the Pratt
-      // parser. `peek` operations may cause tokens to be lexed in advance of
-      // parsing (see the `lookahead` array); such tokens must be explicitly
-      // skipped here.
-      } else if (state.ignoreLinterErrors) {
-        continue;
       } else {
         if (state.tokens.next.id !== "(endline)") {
           break;
@@ -5155,7 +5141,6 @@ var JSHINT = (function() {
       options: state.option
     };
 
-    var members = [];
     var fu, f, i, j, n, globals;
 
     if (itself.errors.length) {
@@ -5215,7 +5200,6 @@ var JSHINT = (function() {
       data.unused = unuseds;
     }
 
-    members = [];
     for (n in member) {
       if (typeof member[n] === "number") {
         data.member = member;
diff --git a/src/lex.js b/src/lex.js
index 1434061d..94bc5edc 100644
--- a/src/lex.js
+++ b/src/lex.js
@@ -123,6 +123,9 @@ function Lexer(source) {
   for (var i = 0; i < state.option.indent; i += 1) {
     state.tab += " ";
   }
+
+  // Blank out non-multi-line-commented lines when ignoring linter errors
+  this.ignoreLinterErrors = false;
 }
 
 Lexer.prototype = {
@@ -399,6 +402,7 @@ Lexer.prototype = {
     var rest = this.input.substr(2);
     var startLine = this.line;
     var startChar = this.char;
+    var self = this;
 
     // Create a comment token object and make sure it
     // has all the data JSHint needs to work with special
@@ -453,6 +457,26 @@ Lexer.prototype = {
           commentType = "globals";
           break;
         default:
+          var options = body.split(":").map(function(v) {
+            return v.replace(/^\s+/, "").replace(/\s+$/, "");
+          });
+
+          if (options.length === 2) {
+            switch (options[0]) {
+            case "ignore":
+              switch (options[1]) {
+              case "start":
+                self.ignoringLinterErrors = true;
+                isSpecial = false;
+                break;
+              case "end":
+                self.ignoringLinterErrors = false;
+                isSpecial = false;
+                break;
+              }
+            }
+          }
+
           commentType = str;
         }
       });
@@ -1506,7 +1530,7 @@ Lexer.prototype = {
 
     // If we are ignoring linter errors, replace the input with empty string
     // if it doesn't already at least start or end a multi-line comment
-    if (state.ignoreLinterErrors === true) {
+    if (this.ignoringLinterErrors === true) {
       if (!startsWith("/*", "//") && !(this.inComment && endsWith("*/"))) {
         this.input = "";
       }
diff --git a/src/state.js b/src/state.js
index 88798006..2106989a 100644
--- a/src/state.js
+++ b/src/state.js
@@ -60,9 +60,6 @@ var state = {
     this.forinifcheckneeded = false;
     this.nameStack = new NameStack();
     this.inClassBody = false;
-
-    // Blank out non-multi-line-commented lines when ignoring linter errors
-    this.ignoreLinterErrors = false;
   }
 };
 


Test patch:
diff --git a/tests/unit/parser.js b/tests/unit/parser.js
index f6971e14..41140cfd 100644
--- a/tests/unit/parser.js
+++ b/tests/unit/parser.js
@@ -6682,6 +6682,21 @@ exports.trailingCommaInArrayBindingPatternParameters = function (test) {
   test.done();
 };
 
+exports.testGH1879 = function (test) {
+  var code = [
+    "function Foo() {",
+    "  return;",
+    "  // jshint ignore:start",
+    "  return [];",
+    "  // jshint ignore:end",
+    "}"
+  ];
+
+  TestRun(test)
+    .test(code);
+
+  test.done();
+};
 
 exports.commaAfterRestElementInArrayBindingPattern = function (test) {
   var code = [