Skip to content

Commit

Permalink
Fixed #839: Add support for prereq files
Browse files Browse the repository at this point in the history
  • Loading branch information
valueof committed Jul 1, 2013
1 parent 28dae4b commit 1c70362
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function loadReporter(fp) {
// Should prevent lots of directory traversal &
// lookups when liniting an entire project
var findFileResults = {};

/**
* Searches for a file with a specified name starting with
* 'dir' and going all the way up either until it finds the file
Expand Down Expand Up @@ -240,19 +241,31 @@ function collect(fp, files, ignores, ext) {
function lint(code, results, config, data, file) {
var globals;
var lintData;
var buffer = [];

config = config || {};
config = JSON.parse(JSON.stringify(config));

// Remove potential Unicode BOM.
code = code.replace(/^\uFEFF/, "");
if (config.prereq) {
config.prereq.forEach(function (fp) {
fp = path.join(config.dirname, fp);
if (shjs.test("-e", fp))
buffer.push(shjs.cat(fp));
});
delete config.prereq;
}

if (config.globals) {
globals = config.globals;
delete config.globals;
}

if (!JSHINT(code, config, globals)) {
delete config.dirname;
buffer.push(code);
buffer = buffer.join("\n");
buffer = buffer.replace(/^\uFEFF/, ""); // Remove potential Unicode BOM.

if (!JSHINT(buffer, config, globals)) {
JSHINT.errors.forEach(function (err) {
if (err) {
results.push({ file: file || "stdin", error: err });
Expand Down Expand Up @@ -286,7 +299,9 @@ var exports = {
}

try {
return JSON.parse(removeComments(shjs.cat(fp)));
var config = JSON.parse(removeComments(shjs.cat(fp)));
config.dirname = path.dirname(fp);
return config;
} catch (err) {
cli.error("Can't parse config file: " + fp);
process.exit(1);
Expand Down
27 changes: 27 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ exports.group = {
test.done();
},

testPrereq: function (test) {
sinon.stub(shjs, "cat")
.withArgs(sinon.match(/file\.js$/)).returns("a();")
.withArgs(sinon.match(/prereq.js$/)).returns("var a = 1;")
.withArgs(sinon.match(/config.json$/))
.returns("{\"undef\":true,\"prereq\":[\"prereq.js\", \"prereq2.js\"]}");

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

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

cli.interpret([
"node", "jshint", "file.js", "--config", "config.json"
]);

shjs.cat.restore();
shjs.test.restore();

test.done();
},

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

Expand Down

0 comments on commit 1c70362

Please sign in to comment.