summaryrefslogtreecommitdiffstats
path: root/tools/mercurial/eslintvalidate.py
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /tools/mercurial/eslintvalidate.py
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'tools/mercurial/eslintvalidate.py')
-rw-r--r--tools/mercurial/eslintvalidate.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/tools/mercurial/eslintvalidate.py b/tools/mercurial/eslintvalidate.py
new file mode 100644
index 000000000..5de0f9416
--- /dev/null
+++ b/tools/mercurial/eslintvalidate.py
@@ -0,0 +1,76 @@
+# 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 a matching ignore pattern. 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
+
+ if "line" in message:
+ ui.warn("%s:%d:%d %s\n" % (path, message["line"], message["column"], message["message"]))
+ else:
+ ui.warn("%s: %s\n" % (path, 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:
+ basepath = get_project_root()
+
+ if not basepath:
+ return
+
+ dir = os.path.join(basepath, "tools", "lint", "eslint", "node_modules", ".bin")
+
+ eslint_path = os.path.join(dir, "eslint")
+ if os.path.exists(os.path.join(dir, "eslint.cmd")):
+ eslint_path = os.path.join(dir, "eslint.cmd")
+ output = check_output([eslint_path,
+ "--format", "json", "--plugin", "html"] + files,
+ cwd=basepath)
+ 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)
+
+def get_project_root():
+ file_found = False
+ folder = os.getcwd()
+
+ while (folder):
+ if os.path.exists(os.path.join(folder, 'mach')):
+ file_found = True
+ break
+ else:
+ folder = os.path.dirname(folder)
+
+ if file_found:
+ return os.path.abspath(folder)
+
+ return None