Skip to content

Commit

Permalink
Ability to specify options on per-directory basis.
Browse files Browse the repository at this point in the history
Closes jshint#1401.
  • Loading branch information
Anton Kovalyov committed Apr 2, 2014
1 parent 744a744 commit df60b9c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,18 @@ function lint(code, results, config, data, file) {
delete config.globals;
}

if (config.overrides) {
if (file) {
_.each(config.overrides, function (options, pattern) {
if ((new RegExp(pattern)).test(file)) _.extend(config, options);
});
}

delete config.overrides;
}

delete config.dirname;

buffer.push(code);
buffer = buffer.join("\n");
buffer = buffer.replace(/^\uFEFF/, ""); // Remove potential Unicode BOM.
Expand Down
51 changes: 51 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,57 @@ exports.group = {
test.done();
},

testOverrides: function (test) {
var dir = __dirname + "/../examples/";
var rep = require("../examples/reporter.js");
var config = {
"asi": true,
"overrides": {
"bar.js$": {
"asi": false
}
}
};

sinon.stub(process, "cwd").returns(dir);
sinon.stub(rep, "reporter");
sinon.stub(shjs, "cat")
.withArgs(sinon.match(/foo\.js$/)).returns("a()")
.withArgs(sinon.match(/bar\.js$/)).returns("a()")
.withArgs(sinon.match(/config\.json$/))
.returns(JSON.stringify(config));

sinon.stub(shjs, "test")
.withArgs("-e", sinon.match(/foo\.js$/)).returns(true)
.withArgs("-e", sinon.match(/bar\.js$/)).returns(true)
.withArgs("-e", sinon.match(/config\.json$/)).returns(true);

cli.exit.restore();
sinon.stub(cli, "exit")
.withArgs(0).returns(true)
.withArgs(1).throws("ProcessExit");

// Test successful file
cli.interpret([
"node", "jshint", "foo.js", "--config", "config.json", "--reporter", "reporter.js"
]);
test.ok(rep.reporter.args[0][0].length === 0);

// Test overriden, failed file
cli.interpret([
"node", "jshint", "bar.js", "--config", "config.json", "--reporter", "reporter.js"
]);
test.ok(rep.reporter.args[1][0].length > 0, "Error was expected but not thrown");
test.equal(rep.reporter.args[1][0][0].error.code, "W033");

process.cwd.restore();
rep.reporter.restore();
shjs.cat.restore();
shjs.test.restore();

test.done();
},

testReporter: function (test) {
test.expect(5);

Expand Down

0 comments on commit df60b9c

Please sign in to comment.