Skip to content

Commit

Permalink
Add ignoreDelimiters option
Browse files Browse the repository at this point in the history
  • Loading branch information
kangax committed Sep 11, 2014
1 parent bc6e97e commit e496983
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ var JSHINT = (function () {
// false - don't emit any warnings
// true - warn if any variable is used before its definition
// "nofunc" - warn for any variable but function declarations
ignore : false // start/end ignoring lines of code, bypassing the lexer
ignore : false, // start/end ignoring lines of code, bypassing the lexer
// start - start ignoring lines, including the current line
// end - stop ignoring lines, starting on the next line
// line - ignore warnings / errors for just a single line
// (this option does not bypass the lexer)
ignoreDelimiters: false // array of start/end delimiters used to ignore
// certain chunks from code
},

// These are JSHint boolean options which are shared with JSLint
Expand Down Expand Up @@ -4640,9 +4642,13 @@ var JSHINT = (function () {
};
};

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

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

state.tokens.prev = state.tokens.curr = state.tokens.next = state.syntax["(begin)"];

if (o && o.ignoreDelimiters) {

if (!Array.isArray(o.ignoreDelimiters)) {
o.ignoreDelimiters = [o.ignoreDelimiters];
}

o.ignoreDelimiters.forEach(function (delimiterPair) {
if (!delimiterPair.start || !delimiterPair.end)
return;

reIgnoreStr = escapeRegex(delimiterPair.start) +
".*?" +
escapeRegex(delimiterPair.end);

reIgnore = new RegExp(reIgnoreStr, "ig");

s = s.replace(reIgnore, "");
});
}

lex = new Lexer(s);

lex.on("warning", function (ev) {
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/fixtures/ignoreDelimiters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(function foo() {
'use strict';
someMethod('<?php foo('test') ?>');
return '<%= asset_path('test.png') %>' + '<% some_method() %>';
})();
17 changes: 17 additions & 0 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1647,3 +1647,20 @@ exports.removeglobal = function (test) {

test.done();
};

exports.ignoreDelimiters = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/ignoreDelimiters.js", "utf8");

TestRun(test)
.test(src, {
ignoreDelimiters: [
{ start: "<%=", end: "%>" },
{ start: "<%", end: "%>" },
{ start: "<?php", end: "?>" },
{ start: "foo" },
{ end: "bar" }
]
});

test.done();
};

0 comments on commit e496983

Please sign in to comment.