blob: 19d31cddee12b054b8cbd4ebb0426b8ef59b943e [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001#!/usr/bin/env python
2#
3# Copyright 2016 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import os.path as path
8import re
9import subprocess
10import sys
11
Yang Guod8176982019-10-04 20:30:35 +000012import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +000013
14files_to_lint = None
15
16if len(sys.argv) >= 2:
17 if sys.argv[1] == "--help":
18 print("Usage: %s [file|dir|glob]*" % path.basename(sys.argv[0]))
19 print
20 print(" [file|dir|glob]* Path or glob to run eslint on.")
21 print(" If absent, the entire frontend will be checked.")
22 sys.exit(0)
23
24 else:
25 print("Linting only these files:\n %s" % sys.argv[1:])
26 files_to_lint = sys.argv[1:]
27
28is_cygwin = sys.platform == "cygwin"
29
30
31def popen(arguments, cwd=None):
32 return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
33
34
35def to_platform_path(filepath):
36 if not is_cygwin:
37 return filepath
38 return re.sub(r"^/cygdrive/(\w)", "\\1:", filepath)
39
40
41def to_platform_path_exact(filepath):
42 if not is_cygwin:
43 return filepath
44 output, _ = popen(["cygpath", "-w", filepath]).communicate()
45 # pylint: disable=E1103
46 return output.strip().replace("\\", "\\\\")
47
48
49scripts_path = path.dirname(path.abspath(__file__))
50devtools_path = path.dirname(scripts_path)
51devtools_frontend_path = path.join(devtools_path, "front_end")
52
53print("Linting JavaScript with eslint...\n")
54
55
56def js_lint(files_list=None):
57 eslint_errors_found = False
58
59 if files_list is None:
60 files_list = [devtools_frontend_path]
61 files_list = [file_name for file_name in files_list if not file_name.endswith(".eslintrc.js")]
62
63 eslintconfig_path = path.join(devtools_path, ".eslintrc.js")
64 eslintignore_path = path.join(devtools_path, ".eslintignore")
65 exec_command = [
Yang Guod8176982019-10-04 20:30:35 +000066 devtools_paths.node_path(),
67 devtools_paths.eslint_path(),
Blink Reformat4c46d092018-04-07 15:32:37 +000068 "--config",
69 to_platform_path_exact(eslintconfig_path),
70 "--ignore-path",
71 to_platform_path_exact(eslintignore_path),
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000072 "--fix",
Blink Reformat4c46d092018-04-07 15:32:37 +000073 ] + files_list
74
75 eslint_proc = popen(exec_command, cwd=devtools_path)
76 (eslint_proc_out, _) = eslint_proc.communicate()
77 if eslint_proc.returncode != 0:
78 eslint_errors_found = True
79 else:
80 print("eslint exited successfully")
81
82 print(eslint_proc_out)
83 return eslint_errors_found
84
85
86errors_found = js_lint(files_to_lint)
87
88if errors_found:
89 print("ERRORS DETECTED")
90 sys.exit(1)