Bug 1288432 - Use new mozlint configuration for eslint linter, r?standard8 draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Fri, 02 Jun 2017 09:48:22 -0400
changeset 588436 a238eb1a281c0b5df3cb732cd4d10fd0cd1c2385
parent 588435 a518dafb94767020e8f670f7b4e71cbf22af1cc7
child 631570 c50ad280d25cc477d86a68d8fc60137646cedfea
push id62032
push userahalberstadt@mozilla.com
push dateFri, 02 Jun 2017 19:52:29 +0000
reviewersstandard8
bugs1288432
milestone55.0a1
Bug 1288432 - Use new mozlint configuration for eslint linter, r?standard8 MozReview-Commit-ID: HX0yA8U15Fw
tools/lint/eslint.lint.py
tools/lint/eslint.yml
tools/lint/eslint/__init__.py
tools/lint/eslint/hook_helper.py
tools/lint/eslint/setup_helper.py
new file mode 100644
--- /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
rename from tools/lint/eslint.lint.py
rename to tools/lint/eslint/__init__.py
--- a/tools/lint/eslint.lint.py
+++ b/tools/lint/eslint/__init__.py
@@ -27,17 +27,17 @@ environment variable, and then at your l
 eslint and needed plugins with:
 
 mach eslint --setup
 
 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'])
 
     module_path = setup_helper.get_project_root()
 
     if not setup_helper.check_node_executables_valid():
         return 1
@@ -72,17 +72,17 @@ def lint(paths, binary=None, fix=None, s
     extra_args = lintargs.get('extra_args') or []
     cmd_args = [binary,
                 # Enable the HTML plugin.
                 # We can't currently enable this in the global config file
                 # because it has bad interactions with the SublimeText
                 # 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
 
     # eslint requires that --fix be set before the --ext argument.
     if fix:
         cmd_args.insert(1, '--fix')
 
     shell = False
@@ -117,23 +117,11 @@ def lint(paths, binary=None, fix=None, s
         for err in errors:
             err.update({
                 'hint': err.get('fix'),
                 'level': 'error' if err['severity'] == 2 else 'warning',
                 'lineno': err.get('line'),
                 '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,
-}
--- a/tools/lint/eslint/hook_helper.py
+++ b/tools/lint/eslint/hook_helper.py
@@ -1,28 +1,49 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
 # This file provides helper functions for the repository hooks for git/hg.
 
+import json
 import os
-import json
+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.
 
     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):
     """Formats an ESLint result into a human readable message.
 
     Keyword arguments:
     print_func -- A function to call to print the output.
     output_json -- the json ESLint results to format.
--- a/tools/lint/eslint/setup_helper.py
+++ b/tools/lint/eslint/setup_helper.py
@@ -34,20 +34,16 @@ option in the node installation) and try
 
 Valid installation paths:
 """.strip()
 
 
 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
 
 
 def eslint_setup():
     """Ensure eslint is optimally configured.
 
     This command will inspect your eslint configuration and
     guide you through an interactive wizard helping you configure