Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: third_party/WebKit/Source/devtools/scripts/lint_javascript.py

Issue 2338753003: DevTools: Make eslint mandatory and check node.js/npm modules in presubmit (Closed)
Patch Set: auto-install local node.js for linux Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright 2016 The Chromium Authors. All rights reserved. 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 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 import os
8 import os.path as path 7 import os.path as path
9 import re 8 import re
10 import subprocess 9 import subprocess
11 import sys 10 import sys
12 11
12 import install_node_deps
13
13 files_to_lint = None 14 files_to_lint = None
14 15
15 if len(sys.argv) >= 2: 16 if len(sys.argv) >= 2:
16 if sys.argv[1] == "--help": 17 if sys.argv[1] == "--help":
17 print("Usage: %s [file|dir|glob]*" % path.basename(sys.argv[0])) 18 print("Usage: %s [file|dir|glob]*" % path.basename(sys.argv[0]))
18 print 19 print
19 print(" [file|dir|glob]* Path or glob to run eslint on.") 20 print(" [file|dir|glob]* Path or glob to run eslint on.")
20 print(" If absent, the entire frontend will be checked .") 21 print(" If absent, the entire frontend will be checked .")
21 sys.exit(0) 22 sys.exit(0)
22 23
(...skipping 18 matching lines...) Expand all
41 def to_platform_path_exact(filepath): 42 def to_platform_path_exact(filepath):
42 if not is_cygwin: 43 if not is_cygwin:
43 return filepath 44 return filepath
44 output, _ = popen(["cygpath", "-w", filepath]).communicate() 45 output, _ = popen(["cygpath", "-w", filepath]).communicate()
45 # pylint: disable=E1103 46 # pylint: disable=E1103
46 return output.strip().replace("\\", "\\\\") 47 return output.strip().replace("\\", "\\\\")
47 48
48 scripts_path = path.dirname(path.abspath(__file__)) 49 scripts_path = path.dirname(path.abspath(__file__))
49 devtools_path = path.dirname(scripts_path) 50 devtools_path = path.dirname(scripts_path)
50 devtools_frontend_path = path.join(devtools_path, "front_end") 51 devtools_frontend_path = path.join(devtools_path, "front_end")
51 52 eslint_path = path.join(devtools_path, "node_modules", ".bin", "eslint")
52
53 # Based on http://stackoverflow.com/questions/377017/test-if-executable-exists-i n-python.
54 def which(program):
55 def is_exe(fpath):
56 return path.isfile(fpath) and os.access(fpath, os.X_OK)
57
58 fpath, fname = path.split(program)
59 if fpath:
60 if is_exe(program):
61 return program
62 else:
63 for part in os.environ["PATH"].split(os.pathsep):
64 part = part.strip("\"")
65 exe_file = path.join(part, program)
66 if is_exe(exe_file):
67 return exe_file
68 return None
69
70 53
71 print("Linting JavaScript with eslint...\n") 54 print("Linting JavaScript with eslint...\n")
72 55
73 56
74 def js_lint(files_list=None): 57 def js_lint(files_list=None):
75 eslint_errors_found = False 58 eslint_errors_found = False
76 59 if not path.isfile(eslint_path):
77 eslint_path = which("eslint") 60 print("Failed to run eslint, run ./scripts/install_node_deps.py to insta ll eslint")
lushnikov 2016/09/27 03:55:13 wait, this doesn't install eslint any more? Should
chenwilliam 2016/09/27 17:30:57 Running install_node_deps.py executes "npm install
78 if not eslint_path: 61 eslint_errors_found = True
79 print("!! NOTE: Skipping JavaScript linting because eslint is not instal led.")
80 print("!! npm install -g eslint")
81 eslint_errors_found = False # Linting is opt-in for now, so this is a s oft failure
82 return eslint_errors_found 62 return eslint_errors_found
83 63
84 if files_list is None: 64 if files_list is None:
85 files_list = [devtools_frontend_path] 65 files_list = [devtools_frontend_path]
86 files_list = [file_name for file_name in files_list if not file_name.endswit h(".eslintrc.js")] 66 files_list = [file_name for file_name in files_list if not file_name.endswit h(".eslintrc.js")]
87 67
88 eslintconfig_path = path.join(devtools_path, "front_end/.eslintrc.js") 68 eslintconfig_path = path.join(devtools_path, "front_end/.eslintrc.js")
89 eslintignore_path = path.join(devtools_path, "front_end/.eslintignore") 69 eslintignore_path = path.join(devtools_path, "front_end/.eslintignore")
90 exec_command = [ 70 exec_command = [
71 install_node_deps.get_node_path(),
91 eslint_path, 72 eslint_path,
92 "--config", to_platform_path_exact(eslintconfig_path), 73 "--config", to_platform_path_exact(eslintconfig_path),
93 "--ignore-path", to_platform_path_exact(eslintignore_path), 74 "--ignore-path", to_platform_path_exact(eslintignore_path),
94 ] + files_list 75 ] + files_list
95 76
96 eslint_proc = popen(exec_command) 77 eslint_proc = popen(exec_command)
97 (eslint_proc_out, _) = eslint_proc.communicate() 78 (eslint_proc_out, _) = eslint_proc.communicate()
98 if eslint_proc.returncode != 0: 79 if eslint_proc.returncode != 0:
99 eslint_errors_found = True 80 eslint_errors_found = True
100 else: 81 else:
101 print("eslint exited successfully") 82 print("eslint exited successfully")
102 83
103 print(eslint_proc_out) 84 print(eslint_proc_out)
104 return eslint_errors_found 85 return eslint_errors_found
105 86
106 87
107 errors_found = js_lint(files_to_lint) 88 errors_found = js_lint(files_to_lint)
108 89
109 if errors_found: 90 if errors_found:
110 print("ERRORS DETECTED") 91 print("ERRORS DETECTED")
111 sys.exit(1) 92 sys.exit(1)
112 else:
113 print("OK")
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698