diff --git a/tools/lint/eslint.yml b/tools/lint/eslint.yml new file mode 100644 index 0000000000000000000000000000000000000000..09115da23960c41d3091bb8e1a7e205a388f3500 --- /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 c6b972a002f5f32c6d1582a012f975270b5e85f7..4725b90d698a043c8e663464084c7afa3b56fed2 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 85902bdbe8fcb3729a7d5e805f0da9e025be9fe5..ad1cafa4ce47025e946a771c9479594a74cb11b4 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 822ee1ead795c6a91c9d1a0ba2fcb71e901d2eb9..e003c238fad86bdf79c8562e120d7cbb4226ebfa 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