Bug 1365414 - Update eslint-plugin-mozilla to also search for MC root from CWD. r?standard8 draft
authorBryce Van Dyk <bvandyk@mozilla.com>
Thu, 18 May 2017 08:09:45 +1200
changeset 580772 4ab6e8a93ea4a0fd61a343829d15042d9b61a63d
parent 575938 d8762cb967423618ff0a488f14745f60964e5c49
child 629380 756831f26a73bb5563a79bf03d01f50607bfdd91
push id59657
push userbvandyk@mozilla.com
push dateThu, 18 May 2017 22:58:02 +0000
reviewersstandard8
bugs1365414
milestone55.0a1
Bug 1365414 - Update eslint-plugin-mozilla to also search for MC root from CWD. r?standard8 The eslint-plugin-mozilla currently searches for Mozilla Central root by walking up from its installed dir. However, if the plugin is installed outside of central, such as globally, it will not find the root. This changeset retains that behaviour, but will also perform the same check from the current working directly before failing. MozReview-Commit-ID: 2L3JqLTuVDS
tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
@@ -514,36 +514,45 @@ module.exports = {
   getIsWorker(filePath) {
     let filename = path.basename(this.cleanUpPath(filePath)).toLowerCase();
 
     return filename.includes("worker");
   },
 
   /**
    * Gets the root directory of the repository by walking up directories from
-   * this file until a .eslintignore file is found.
+   * 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) {
-      let dirName = path.dirname(module.filename);
+      function searchUpForIgnore(dirName) {
+        let parsed = path.parse(dirName);
+        while (parsed.root !== dirName) {
+          if (fs.existsSync(path.join(dirName, ".eslintignore"))) {
+            return dirName;
+          }
+          // Move up a level
+          dirName = parsed.dir;
+          parsed = path.parse(dirName);
+        }
+        return null;
+      }
 
-      while (true) {
-        const parsed = path.parse(dirName);
-        if (parsed.root === dirName) {
-          // We've reached the top of the filesystem
+      let possibleRoot = searchUpForIgnore(path.dirname(module.filename));
+      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");
         }
-        dirName = parsed.dir;
-        if (fs.existsSync(path.join(dirName, ".eslintignore"))) {
-          break;
-        }
       }
 
-      gRootDir = dirName;
+      gRootDir = possibleRoot;
     }
 
     return gRootDir;
   },
 
   /**
    * ESLint may be executed from various places: from mach, at the root of the
    * repository, or from a directory in the repository when, for instance,