Bug 1369829 - Make eslint-plugin-mozilla more flexible with finding the root directory of the repository. r?Mossop draft
authorMark Banner <standard8@mozilla.com>
Thu, 15 Jun 2017 15:57:04 +0100
changeset 594834 0b48bcd3044e54147c40f20f5c085efaedd28eea
parent 594702 035c25bef7b5e4175006e63eff10c61c2eef73f1
child 633541 3172bb8c19c14941551b89367bc204407c4508c8
push id64158
push userbmo:standard8@mozilla.com
push dateThu, 15 Jun 2017 14:57:27 +0000
reviewersMossop
bugs1369829
milestone56.0a1
Bug 1369829 - Make eslint-plugin-mozilla more flexible with finding the root directory of the repository. r?Mossop MozReview-Commit-ID: KmQS9RKwf3K
tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
tools/lint/eslint/eslint-plugin-mozilla/package.json
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
@@ -521,36 +521,41 @@ module.exports = {
   /**
    * Gets the root directory of the repository by walking up directories from
    * this file until a .eslintignore file is found. If this fails, the same
    * procedure will be attempted from the current working dir.
    * @return {String} The absolute path of the repository directory
    */
   get rootDir() {
     if (!gRootDir) {
-      function searchUpForIgnore(dirName) {
+      function searchUpForIgnore(dirName, filename) {
         let parsed = path.parse(dirName);
         while (parsed.root !== dirName) {
-          if (fs.existsSync(path.join(dirName, ".eslintignore"))) {
+          if (fs.existsSync(path.join(dirName, filename))) {
             return dirName;
           }
           // Move up a level
           dirName = parsed.dir;
           parsed = path.parse(dirName);
         }
         return null;
       }
 
-      let possibleRoot = searchUpForIgnore(path.dirname(module.filename));
+      let possibleRoot = searchUpForIgnore(path.dirname(module.filename), ".eslintignore");
+      if (!possibleRoot) {
+        possibleRoot = searchUpForIgnore(path.resolve(), ".eslintignore");
+      }
       if (!possibleRoot) {
-        possibleRoot = searchUpForIgnore(path.resolve());
-        if (!possibleRoot) {
-          // We've couldn't find a root from the module or CWD
-          throw new Error("Unable to find root of repository");
-        }
+        possibleRoot = searchUpForIgnore(path.resolve(), "package.json");
+      }
+      if (!possibleRoot) {
+        // We've couldn't find a root from the module or CWD, so lets just go
+        // for the CWD. We really don't want to throw if possible, as that
+        // tends to give confusing results when used with ESLint.
+        possibleRoot = process.cwd();
       }
 
       gRootDir = possibleRoot;
     }
 
     return gRootDir;
   },
 
--- a/tools/lint/eslint/eslint-plugin-mozilla/package.json
+++ b/tools/lint/eslint/eslint-plugin-mozilla/package.json
@@ -1,11 +1,11 @@
 {
   "name": "eslint-plugin-mozilla",
-  "version": "0.3.3",
+  "version": "0.3.4",
   "description": "A collection of rules that help enforce JavaScript coding standard in the Mozilla project.",
   "keywords": [
     "eslint",
     "eslintplugin",
     "eslint-plugin",
     "mozilla",
     "firefox"
   ],