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

Side by Side Diff: tools/web_dev_style/js_checker.py

Issue 2872703003: WebUI: Hook up ESLint to web_dev_style PRESUBMIT. (Closed)
Patch Set: Fix path. Created 3 years, 7 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 | « tools/web_dev_style/OWNERS ('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
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Presubmit script for Chromium JS resources. 5 """Presubmit script for Chromium JS resources.
6 6
7 See chrome/browser/PRESUBMIT.py 7 See chrome/browser/PRESUBMIT.py
8 """ 8 """
9 9
10 import regex_check 10 import regex_check
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 return self.RegexCheck(i, line, 73 return self.RegexCheck(i, line,
74 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type 74 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type
75 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)} 75 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)}
76 "Don't use wrapper types (i.e. new String() or @type {String})") 76 "Don't use wrapper types (i.e. new String() or @type {String})")
77 77
78 def VarNameCheck(self, i, line): 78 def VarNameCheck(self, i, line):
79 """See the style guide. http://goo.gl/eQiXVW""" 79 """See the style guide. http://goo.gl/eQiXVW"""
80 return self.RegexCheck(i, line, 80 return self.RegexCheck(i, line,
81 r"var (?!g_\w+)(_?[a-z][a-zA-Z]*[_$][\w_$]*)(?<! \$)", 81 r"var (?!g_\w+)(_?[a-z][a-zA-Z]*[_$][\w_$]*)(?<! \$)",
82 "Please use var namesLikeThis <https://goo.gl/eQiXVW>") 82 "Please use var namesLikeThis <https://goo.gl/eQiXVW>")
83 83
Dan Beam 2017/05/24 02:13:04 \n\n between file-level globals
dpapad 2017/05/24 22:43:42 This file does not seem to follow that rule (all m
84 def RunEsLintChecks(self, affected_js_files):
Dan Beam 2017/05/24 02:13:04 arguable nit: maybe insert this method alphabetica
dpapad 2017/05/24 22:43:42 Done.
85 """Runs lint checks using ESLint. The ESLint rules being applied are defined
86 in the .eslintrc.js configuration file.
87 """
88 # Import ESLint.
89 import os
90 _HERE_PATH = os.path.dirname(os.path.realpath(__file__))
91 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..'))
92 import sys
93 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node'))
Dan Beam 2017/05/24 02:13:04 you should undo the effects of this sys.path mungi
dpapad 2017/05/24 22:43:42 Done. Note, I only wrapped the part that imports i
94 import node
95 import node_modules
96
97 # Extract paths to be passed to ESLint.
98 affected_js_files_paths = []
99 presubmit_path = self.input_api.PresubmitLocalPath()
100 for f in affected_js_files:
101 affected_js_files_paths.append(
102 os.path.relpath(f.AbsoluteLocalPath(), presubmit_path))
Dan Beam 2017/05/24 02:13:04 hmmmm, is there a reason you have to ask for the /
dpapad 2017/05/24 22:43:42 I did not find a way to achieve the correct path w
103
104 output = node.RunNode([
105 node_modules.PathToEsLint(),
106 '--color',
107 '--ignore-pattern \'!.eslintrc.js\'',
108 ' '.join(affected_js_files_paths)])
109
110 if len(output) > 0:
Dan Beam 2017/05/24 02:13:04 empty arrays are falsy in python, so you can just
dpapad 2017/05/24 22:43:42 Done.
111 return [self.output_api.PresubmitError(output)]
112 return []
113
84 def _GetErrorHighlight(self, start, length): 114 def _GetErrorHighlight(self, start, length):
85 """Takes a start position and a length, and produces a row of '^'s to 115 """Takes a start position and a length, and produces a row of '^'s to
86 highlight the corresponding part of a string. 116 highlight the corresponding part of a string.
87 """ 117 """
88 return start * ' ' + length * '^' 118 return start * ' ' + length * '^'
89 119
90 def RunChecks(self): 120 def RunChecks(self):
91 """Check for violations of the Chromium JavaScript style guide. See 121 """Check for violations of the Chromium JavaScript style guide. See
92 https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/we b.md#JavaScript 122 https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/we b.md#JavaScript
93 """ 123 """
94 results = [] 124 results = []
95 125
96 affected_files = self.input_api.AffectedFiles(file_filter=self.file_filter, 126 affected_files = self.input_api.AffectedFiles(file_filter=self.file_filter,
97 include_deletes=False) 127 include_deletes=False)
98 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), 128 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'),
99 affected_files) 129 affected_files)
130
131 if len(affected_js_files) > 0:
Dan Beam 2017/05/24 02:13:05 same nit, re: if len(x) > 0 vs just if x:
dpapad 2017/05/24 22:43:42 Done.
132 results += self.RunEsLintChecks(affected_js_files)
133
100 for f in affected_js_files: 134 for f in affected_js_files:
101 error_lines = [] 135 error_lines = []
102 136
103 for i, line in enumerate(f.NewContents(), start=1): 137 for i, line in enumerate(f.NewContents(), start=1):
104 error_lines += filter(None, [ 138 error_lines += filter(None, [
105 self.ChromeSendCheck(i, line), 139 self.ChromeSendCheck(i, line),
106 self.CommentIfAndIncludeCheck(i, line), 140 self.CommentIfAndIncludeCheck(i, line),
107 self.ConstCheck(i, line), 141 self.ConstCheck(i, line),
108 self.GetElementByIdCheck(i, line), 142 self.GetElementByIdCheck(i, line),
109 self.EndJsDocCommentCheck(i, line), 143 self.EndJsDocCommentCheck(i, line),
110 self.ExtraDotInGenericCheck(i, line), 144 self.ExtraDotInGenericCheck(i, line),
111 self.InheritDocCheck(i, line), 145 self.InheritDocCheck(i, line),
112 self.PolymerLocalIdCheck(i, line), 146 self.PolymerLocalIdCheck(i, line),
113 self.WrapperTypeCheck(i, line), 147 self.WrapperTypeCheck(i, line),
114 self.VarNameCheck(i, line), 148 self.VarNameCheck(i, line),
115 ]) 149 ])
116 150
117 if error_lines: 151 if error_lines:
118 error_lines = [ 152 error_lines = [
119 'Found JavaScript style violations in %s:' % 153 'Found JavaScript style violations in %s:' %
120 f.LocalPath()] + error_lines 154 f.LocalPath()] + error_lines
121 results.append(self.output_api.PresubmitError('\n'.join(error_lines))) 155 results.append(self.output_api.PresubmitError('\n'.join(error_lines)))
122 156
123 if results: 157 if results:
124 results.append(self.output_api.PresubmitNotifyResult( 158 results.append(self.output_api.PresubmitNotifyResult(
125 'See the JavaScript style guide at ' 159 'See the JavaScript style guide at '
126 'https://chromium.googlesource.com/chromium/src/+/master/styleguide/we b/web.md#JavaScript')) 160 'https://chromium.googlesource.com/chromium/src/+/master/styleguide/we b/web.md#JavaScript'))
127 161
128 return results 162 return results
OLDNEW
« no previous file with comments | « tools/web_dev_style/OWNERS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698