gecko-dev/tools/mercurial/eslintvalidate.py
bsilverberg 753cad635e Bug 1250965 - Enhance eslintvalidate.py to validate html files as well, r=mossop
MozReview-Commit-ID: 3rnyWYG84Uc

--HG--
extra : transplant_source : %CA%91o%D9e%B4%B2%EF%C0%24%A5O%AD%EDbq%D2G%CBX
2016-02-24 13:33:04 -05:00

46 lines
1.4 KiB
Python

# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
import os
import sys
import re
import json
from subprocess import check_output, CalledProcessError
lintable = re.compile(r'.+\.(?:js|jsm|jsx|xml|html)$')
ignored = "File ignored because of your .eslintignore file. Use --no-ignore to override."
def is_lintable(filename):
return lintable.match(filename)
def display(ui, output):
results = json.loads(output)
for file in results:
path = os.path.relpath(file["filePath"])
for message in file["messages"]:
if message["message"] == ignored:
continue
ui.warn("%s:%d:%d %s\n" % (path, message["line"], message["column"], message["message"]))
def eslinthook(ui, repo, node=None, **opts):
ctx = repo[node]
if len(ctx.parents()) > 1:
return 0
deleted = repo.status(ctx.p1().node(), ctx.node()).deleted
files = [f for f in ctx.files() if f not in deleted and is_lintable(f)]
if len(files) == 0:
return
try:
output = check_output(["eslint", "--format", "json", "--plugin", "html"] + files)
display(ui, output)
except CalledProcessError as ex:
display(ui, ex.output)
ui.warn("ESLint found problems in your changes, please correct them.\n")
def reposetup(ui, repo):
ui.setconfig('hooks', 'commit.eslint', eslinthook)