Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1 | #!/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 | |
| 7 | import os.path as path |
| 8 | import re |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
Yang Guo | d817698 | 2019-10-04 20:30:35 +0000 | [diff] [blame] | 12 | import devtools_paths |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 13 | |
| 14 | files_to_lint = None |
| 15 | |
| 16 | if 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 | |
| 28 | is_cygwin = sys.platform == "cygwin" |
| 29 | |
| 30 | |
| 31 | def popen(arguments, cwd=None): |
| 32 | return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 33 | |
| 34 | |
| 35 | def to_platform_path(filepath): |
| 36 | if not is_cygwin: |
| 37 | return filepath |
| 38 | return re.sub(r"^/cygdrive/(\w)", "\\1:", filepath) |
| 39 | |
| 40 | |
| 41 | def 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 | |
| 49 | scripts_path = path.dirname(path.abspath(__file__)) |
| 50 | devtools_path = path.dirname(scripts_path) |
| 51 | devtools_frontend_path = path.join(devtools_path, "front_end") |
| 52 | |
| 53 | print("Linting JavaScript with eslint...\n") |
| 54 | |
| 55 | |
| 56 | def 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 Guo | d817698 | 2019-10-04 20:30:35 +0000 | [diff] [blame] | 66 | devtools_paths.node_path(), |
| 67 | devtools_paths.eslint_path(), |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 68 | "--config", |
| 69 | to_platform_path_exact(eslintconfig_path), |
| 70 | "--ignore-path", |
| 71 | to_platform_path_exact(eslintignore_path), |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 72 | "--fix", |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 73 | ] + 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 | |
| 86 | errors_found = js_lint(files_to_lint) |
| 87 | |
| 88 | if errors_found: |
| 89 | print("ERRORS DETECTED") |
| 90 | sys.exit(1) |