Skip to content

Commit

Permalink
Merge pull request #1841 from kangax/master
Browse files Browse the repository at this point in the history
Add ignoreDelimiters option
  • Loading branch information
rwaldron committed Sep 16, 2014
2 parents cfbc819 + 5c10fb3 commit 53b06a6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
32 changes: 30 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 @@ -4708,9 +4710,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 @@ -4849,6 +4855,28 @@ 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) +
"[\\s\\S]*?" +
escapeRegex(delimiterPair.end);

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

s = s.replace(reIgnore, function(match) {
return match.replace(/./g, " ");
});
});
}

lex = new Lexer(s);

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

test.done();
};

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

TestRun(test)
// make sure line/column are still reported properly
.addError(6, "Missing semicolon.", { character: 37 })
.test(src, {
ignoreDelimiters: [
{ start: "<%=", end: "%>" },
{ start: "<%", end: "%>" },
{ start: "<?php", end: "?>" },
// make sure single tokens are ignored
{ start: "foo" },
{ end: "bar" }
]
});

test.done();
};

0 comments on commit 53b06a6

Please sign in to comment.