Skip to content
Browse files

Changed the lookup process for .jshintrc/.jshintignore.

Start with cwd, traverse up filesystem and fall back on HOME, if available.
  • Loading branch information...
1 parent a2e9718 commit c170fec2216ba83989b74e015c5803b19058438b Dominic Barnes committed with brentlintner Mar 10, 2012
Showing with 65 additions and 33 deletions.
  1. +63 −32 lib/cli.js
  2. +2 −1 lib/hint.js
View
95 lib/cli.js
@@ -28,22 +28,57 @@ function _loadAndParseConfig(filePath) {
JSON.parse(_removeJsComments(fs.readFileSync(filePath, "utf-8"))) : {};
}
-function _mergeConfigs(homerc, cwdrc) {
- var homeConfig = _loadAndParseConfig(homerc),
- cwdConfig = _loadAndParseConfig(cwdrc),
- prop;
-
- for (prop in cwdConfig) {
- if (typeof prop === 'string') {
- if (prop === 'predef') {
- homeConfig.predef = (homeConfig.predef || []).concat(cwdConfig.predef);
- } else {
- homeConfig[prop] = cwdConfig[prop];
- }
+/**
+ * This function searches for a file with a specified name, it starts
+ * with the dir passed, and traverses up the filesystem until it either
+ * finds the file, or hits the root
+ *
+ * @param {String} name Filename to search for (.jshintrc, .jshintignore)
+ * @param {String} dir Defaults to process.cwd()
+ */
+function _searchFile(name, dir) {
+ var lastDir, filename;
+ dir = dir || process.cwd();
+
+ do {
+ filename = path.normalize(path.join(dir, name));
+
+ // if dir is the same as lastDir, then we are unable to traverse
+ // any higher up the filesystem, must be the root
+ // FIXME: is there any better way to do this??
+ if (dir === lastDir) {
+ filename = false;
+ break;
}
+
+ if (!existsSync(filename)) {
+ filename = false;
+ } else {
+ break;
+ }
+
+ lastDir = dir;
+ dir = path.normalize(path.join(dir, ".."));
+ } while (dir);
+
+ return filename;
+}
+
+function _findConfig(target) {
+ var name = ".jshintrc",
+ projectConfig = _searchFile(name),
+ homeConfig = path.normalize(path.join(process.env.HOME, name));
+
+ if (projectConfig) {
+ return projectConfig;
}
- return homeConfig;
+ // if no project config, check $HOME
+ if (existsSync(homeConfig)) {
+ return homeConfig;
+ }
+
+ return false;
}
function _print(results) {
@@ -68,13 +103,11 @@ module.exports = {
interpret: function (args) {
var config, reporter, ignore,
options = argsparser.parse(args),
- pathsToIgnore = path.join(process.cwd(), '.jshintignore'),
- defaultConfig = path.join(process.env.HOME, '.jshintrc'),
- projectConfig = path.join(process.cwd(), '.jshintrc'),
customConfig = options["--config"],
customReporter = options["--reporter"] ? path.resolve(process.cwd(), options["--reporter"]) : null,
- targets = options.node;
-
+ targets = options.node,
+ ignoreFile, ignores;
+
//could be on Windows which we are looking for an attribute ending in 'node.exe'
if (targets === undefined) {
(function () {
@@ -91,7 +124,6 @@ module.exports = {
targets = typeof targets === "string" ? null : targets.slice(1);
-
if (options["--version"]) {
_version();
return;
@@ -110,11 +142,7 @@ module.exports = {
customReporter = "./reporters/non_error.js";
}
- if (customConfig) {
- config = _loadAndParseConfig(customConfig);
- } else {
- config = _mergeConfigs(defaultConfig, projectConfig);
- }
+ config = _loadAndParseConfig(customConfig ? customConfig : _findConfig());
if (customReporter) {
try {
@@ -126,15 +154,18 @@ module.exports = {
}
}
- if (existsSync(pathsToIgnore)) {
- ignore = fs.readFileSync(pathsToIgnore, "utf-8").split("\n").map(function (line) {
- return line.trim();
- }).filter(function (line) {
- return !!line;
- });
+ ignoreFile = _searchFile(".jshintignore");
+
+ if (ignoreFile) {
+ ignores = fs.readFileSync(ignoreFile, "utf8").split("\n")
+ .filter(function (line) {
+ return !!line.trim();
+ })
+ .map(function (line) {
+ return path.resolve(path.dirname(ignoreFile), line.trim());
+ });
}
- _print(hint.hint(targets, config, reporter, ignore));
+ _print(hint.hint(targets, config, reporter, ignores));
}
};
-
View
3 lib/hint.js
@@ -58,10 +58,11 @@ function isDirectory(aPath) {
function _shouldIgnore(somePath, ignore) {
function isIgnored(p) {
var fnmatch = minimatch(somePath, p, {nocase: true}),
+ absmatch = path.resolve(somePath) === p,
lsmatch = isDirectory(p) && p.match(/^[^\/]*\/?$/) &&
somePath.match(new RegExp("^" + p + ".*"));
- return !!(fnmatch || lsmatch);
+ return !!(fnmatch || lsmatch || absmatch);
}
return ignore.some(function (ignorePath) {

0 comments on commit c170fec

Please sign in to comment.
Something went wrong with that request. Please try again.