From e892c0600be058e1688925bdd4611fdfc27abd6a Mon Sep 17 00:00:00 2001
From: Andrew Halberstadt <ahalberstadt@mozilla.com>
Date: Fri, 2 Jun 2017 09:48:22 -0400
Subject: [PATCH] Bug 1288432 - Use new mozlint configuration for eslint
 linter, r=standard8

MozReview-Commit-ID: HX0yA8U15Fw

--HG--
rename : tools/lint/eslint.lint.py => tools/lint/eslint/__init__.py
extra : rebase_source : f11e103038acd8b95b9586026c571a88587a79b2
---
 tools/lint/eslint.yml                         | 10 ++++++++
 .../{eslint.lint.py => eslint/__init__.py}    | 18 +++----------
 tools/lint/eslint/hook_helper.py              | 25 +++++++++++++++++--
 tools/lint/eslint/setup_helper.py             |  4 ---
 4 files changed, 36 insertions(+), 21 deletions(-)
 create mode 100644 tools/lint/eslint.yml
 rename tools/lint/{eslint.lint.py => eslint/__init__.py} (88%)

diff --git a/tools/lint/eslint.yml b/tools/lint/eslint.yml
new file mode 100644
index 0000000000000..09115da23960c
--- /dev/null
+++ b/tools/lint/eslint.yml
@@ -0,0 +1,10 @@
+eslint:
+    description: JavaScript linter
+    # ESLint infra handles its own path filtering, so just include cwd
+    include: ['.']
+    exclude: []
+    # Make sure these are defined on the same line until the hack
+    # in hook_helper.py is fixed.
+    extensions: ['js', 'jsm', 'jsx', 'xml', 'html', 'xhtml']
+    type: external
+    payload: eslint:lint
diff --git a/tools/lint/eslint.lint.py b/tools/lint/eslint/__init__.py
similarity index 88%
rename from tools/lint/eslint.lint.py
rename to tools/lint/eslint/__init__.py
index c6b972a002f5f..4725b90d698a0 100644
--- a/tools/lint/eslint.lint.py
+++ b/tools/lint/eslint/__init__.py
@@ -32,7 +32,7 @@ and try again.
 """.strip()
 
 
-def lint(paths, binary=None, fix=None, setup=None, **lintargs):
+def lint(paths, config, binary=None, fix=None, setup=None, **lintargs):
     """Run eslint."""
     global project_root
     setup_helper.set_project_root(lintargs['root'])
@@ -77,7 +77,7 @@ def lint(paths, binary=None, fix=None, setup=None, **lintargs):
                 # ESLint plugin (bug 1229874).
                 '--plugin', 'html',
                 # This keeps ext as a single argument.
-                '--ext', '[{}]'.format(','.join(setup_helper.EXTENSIONS)),
+                '--ext', '[{}]'.format(','.join(config['extensions'])),
                 '--format', 'json',
                 ] + extra_args + paths
 
@@ -122,18 +122,6 @@ def lint(paths, binary=None, fix=None, setup=None, **lintargs):
                 'path': obj['filePath'],
                 'rule': err.get('ruleId'),
             })
-            results.append(result.from_linter(LINTER, **err))
+            results.append(result.from_config(config, **err))
 
     return results
-
-
-LINTER = {
-    'name': "eslint",
-    'description': "JavaScript linter",
-    # ESLint infra handles its own path filtering, so just include cwd
-    'include': ['.'],
-    'exclude': [],
-    'extensions': setup_helper.EXTENSIONS,
-    'type': 'external',
-    'payload': lint,
-}
diff --git a/tools/lint/eslint/hook_helper.py b/tools/lint/eslint/hook_helper.py
index 85902bdbe8fcb..ad1cafa4ce470 100644
--- a/tools/lint/eslint/hook_helper.py
+++ b/tools/lint/eslint/hook_helper.py
@@ -3,13 +3,31 @@
 
 # This file provides helper functions for the repository hooks for git/hg.
 
-import os
 import json
+import os
+import re
 from subprocess import check_output, CalledProcessError
+
 import setup_helper
 
 ignored = 'File ignored because of a matching ignore pattern. Use "--no-ignore" to override.'
 
+here = os.path.abspath(os.path.dirname(__file__))
+config_path = os.path.join(here, '..', 'eslint.yml')
+
+EXTENSIONS_RE = None
+
+
+def get_extensions():
+    # This is a hacky way to avoid both re-defining extensions and
+    # depending on PyYaml in hg's python path. This will go away
+    # once the vcs hooks are using mozlint directly (bug 1361972)
+    with open(config_path) as fh:
+        line = [l for l in fh.readlines() if 'extensions' in l][0]
+
+    extensions = json.loads(line.split(':', 1)[1].replace('\'', '"'))
+    return [e.lstrip('.') for e in extensions]
+
 
 def is_lintable(filename):
     """Determine if a file is lintable based on the file extension.
@@ -17,7 +35,10 @@ def is_lintable(filename):
     Keyword arguments:
     filename -- the file to check.
     """
-    return setup_helper.EXTENSIONS_RE.match(filename)
+    global EXTENSIONS_RE
+    if not EXTENSIONS_RE:
+        EXTENSIONS_RE = re.compile(r'.+\.(?:%s)$' % '|'.join(get_extensions()))
+    return EXTENSIONS_RE.match(filename)
 
 
 def display(print_func, output_json):
diff --git a/tools/lint/eslint/setup_helper.py b/tools/lint/eslint/setup_helper.py
index 822ee1ead795c..e003c238fad86 100644
--- a/tools/lint/eslint/setup_helper.py
+++ b/tools/lint/eslint/setup_helper.py
@@ -39,10 +39,6 @@ Valid installation paths:
 VERSION_RE = re.compile(r"^\d+\.\d+\.\d+$")
 CARET_VERSION_RANGE_RE = re.compile(r"^\^((\d+)\.\d+\.\d+)$")
 
-LINTED_EXTENSIONS = ['js', 'jsm', 'jsx', 'xml', 'html', 'xhtml']
-EXTENSIONS = [".%s" % x for x in LINTED_EXTENSIONS]
-EXTENSIONS_RE = re.compile(r'.+\.(?:%s)$' % '|'.join(LINTED_EXTENSIONS))
-
 project_root = None
 
 
-- 
GitLab