Skip to content

Commit

Permalink
Added the ability for a project specific .jshint file
Browse files Browse the repository at this point in the history
Tests included. Options in a .jshint file from a user's $HOME directory are overridden by options within a .jshint file in the cwd.
  • Loading branch information
Matthew Kitt committed Mar 29, 2011
1 parent 8b6df20 commit 5fe004a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/cli.js
Expand Up @@ -10,11 +10,30 @@ function _version() {
_sys.print(JSON.parse(_fs.readFileSync(__dirname + "/../package.json", "utf-8")).version + "\n");
}

function _mergeConfigs(homerc, cwdrc) {
var homeConfig = JSON.parse(_fs.readFileSync(homerc, "utf-8")),
cwdConfig = JSON.parse(_fs.readFileSync(cwdrc, "utf-8"));

for (var prop in cwdConfig) {
if (typeof prop === 'string') {

if (prop === 'predef') {
var merged = homeConfig.predef.concat(cwdConfig.predef);
homeConfig.predef = merged;
} else {
homeConfig[prop] = cwdConfig[prop];
}
}
}
return homeConfig;
}

module.exports = {
interpret: function (args) {
var config, reporter,
options = require('argsparser').parse(args),
defaultConfig = _path.join(process.env.HOME, '.jshintrc'),
projectConfig = _path.join(process.cwd(), '.jshintrc'),
customConfig = options["--config"],
customReporter = options["--reporter"],
targets = typeof options.node === "string" ?
Expand Down Expand Up @@ -44,7 +63,7 @@ module.exports = {
}
} else {
try {
config = JSON.parse(_fs.readFileSync(defaultConfig, "utf-8"));
config = _mergeConfigs(defaultConfig, projectConfig);
} catch (f) {}
}

Expand Down
19 changes: 19 additions & 0 deletions test/cli.js
Expand Up @@ -64,6 +64,25 @@ describe("cli", function () {
expect(fs.readFileSync).toHaveBeenCalledWith(path.join(process.env.HOME, '.jshintrc'), "utf-8");
});

it("looks for a project specific config file", function () {
var config = {prefdef: []},
path = require('path');

spyOn(fs, "readFileSync").andReturn(JSON.stringify(config));
cli.interpret(["node", "file.js", "file.js"]);
expect(fs.readFileSync).toHaveBeenCalledWith(path.join(process.cwd(), '.jshintrc'), "utf-8");
});

it("overrides options from the $HOME .jshintrc file with options from the cwd .jshintrc file", function() {
var config = '{"evil": true,"predef":["Monkeys","Elephants"]}';
fs.writeFileSync('.jshintrc', config, "utf-8");
cli.interpret(["node", "file.js", "file.js"]);
expect(hint.hint.mostRecentCall.args[1].predef).toContain("Monkeys");
expect(hint.hint.mostRecentCall.args[1].predef).toContain("Elephants");
expect(hint.hint.mostRecentCall.args[1].evil).toEqual(true);
fs.unlinkSync('.jshintrc');
});

it("interprets --version and logs the current package version", function () {
var data = {version: 1};

Expand Down

0 comments on commit 5fe004a

Please sign in to comment.