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

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

Issue 1907353005: DevTools: Add lint_javascript.py to drive eslint (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove extra log Created 4 years, 8 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
« no previous file with comments | « third_party/WebKit/Source/devtools/front_end/.eslintignore ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2012 Google Inc. All rights reserved.
caseq 2016/04/26 01:39:44 Let's bring the 3-line chromium authors copyright?
paulirish 2016/04/26 03:37:43 done
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import os
31 import os.path as path
32 import re
33 import subprocess
34 import sys
35
36 files_to_lint = None
37
38 if len(sys.argv) == 2:
39 if sys.argv[1] == '--help':
caseq 2016/04/26 01:39:44 Let's settle on one quote style? Either is good pe
paulirish 2016/04/26 03:37:43 double. you got it.
40 print("Usage: %s [file|dir|glob]*" % path.basename(sys.argv[0]))
41 print
42 print(" [file|dir|glob]* Path or glob to run eslint on.")
43 print(" If absent, the entire frontend will be checked .")
44 sys.exit(0)
45
46 else:
47 print 'Linting only this path:\n %s' % sys.argv[1:]
48 files_to_lint = sys.argv[1:]
49
50
51 is_cygwin = sys.platform == 'cygwin'
52
53
54 def popen(arguments):
55 return subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess .STDOUT)
56
57
58 def to_platform_path(filepath):
59 if not is_cygwin:
60 return filepath
61 return re.sub(r'^/cygdrive/(\w)', '\\1:', filepath)
62
63
64 def to_platform_path_exact(filepath):
65 if not is_cygwin:
66 return filepath
67 output, _ = popen(['cygpath', '-w', filepath]).communicate()
68 # pylint: disable=E1103
69 return output.strip().replace('\\', '\\\\')
70
71 scripts_path = path.dirname(path.abspath(__file__))
72 devtools_path = path.dirname(scripts_path)
73 devtools_frontend_path = path.join(devtools_path, 'front_end')
74
75
76 # Based on http://stackoverflow.com/questions/377017/test-if-executable-exists-i n-python.
77 def which(program):
78 def is_exe(fpath):
79 return path.isfile(fpath) and os.access(fpath, os.X_OK)
80
81 fpath, fname = path.split(program)
82 if fpath:
83 if is_exe(program):
84 return program
85 else:
86 for part in os.environ["PATH"].split(os.pathsep):
87 part = part.strip('"')
88 exe_file = path.join(part, program)
89 if is_exe(exe_file):
90 return exe_file
91 return None
92
93
94 print 'Linting JavaScript with eslint...\n'
95 errors_found = False
caseq 2016/04/26 01:39:44 move into js_lint?
paulirish 2016/04/26 03:37:43 Seems good. done.
96
97
98 def js_lint(files_list=None):
99 eslint_path = which('eslint')
100 if not eslint_path:
101 print '!! Skipping JavaScript linting because eslint is not installed.'
102 print '!! npm install -g eslint'
103 errors_found = False # Linting is opt-in for now, so this is a soft fai lure
104 return errors_found
105
106 if files_list is None:
107 files_list = [devtools_frontend_path]
108
109 eslintconfig_path = path.join(devtools_path, 'front_end/.eslintrc.js')
110 eslintignore_path = path.join(devtools_path, 'front_end/.eslintignore')
111 exec_command = [
112 eslint_path,
113 '--config', to_platform_path_exact(eslintconfig_path),
114 '--ignore-path', to_platform_path_exact(eslintignore_path),
115 ' '.join(files_list)
116 ]
117
118 eslint_proc = popen(exec_command)
119 (eslint_proc_out, _) = eslint_proc.communicate()
120 if eslint_proc.returncode != 0:
121 errors_found = True
122 print eslint_proc_out
caseq 2016/04/26 01:39:44 move out of conditional?
paulirish 2016/04/26 03:37:43 done
123 else:
124 print 'eslint exited successfully'
125 print eslint_proc_out
126
127 return errors_found
128
129 errors_found = js_lint(files_to_lint)
130
131 if errors_found:
132 print 'ERRORS DETECTED'
133 sys.exit(1)
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/devtools/front_end/.eslintignore ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698