OLD | NEW |
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 class JSChecker(object): | 10 class JSChecker(object): |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 gflags_path = self.input_api.os_path.join( | 138 gflags_path = self.input_api.os_path.join( |
139 self.input_api.change.RepositoryRoot(), | 139 self.input_api.change.RepositoryRoot(), |
140 "third_party", | 140 "third_party", |
141 "python_gflags") | 141 "python_gflags") |
142 | 142 |
143 sys.path.insert(0, closure_linter_path) | 143 sys.path.insert(0, closure_linter_path) |
144 sys.path.insert(0, gflags_path) | 144 sys.path.insert(0, gflags_path) |
145 | 145 |
146 warnings.filterwarnings('ignore', category=DeprecationWarning) | 146 warnings.filterwarnings('ignore', category=DeprecationWarning) |
147 | 147 |
148 from closure_linter import checker, errors | 148 from closure_linter import errors, runner |
149 from closure_linter.common import errorhandler | 149 from closure_linter.common import errorhandler |
150 | 150 |
151 finally: | 151 finally: |
152 sys.path = old_path | 152 sys.path = old_path |
153 warnings.filters = old_filters | 153 warnings.filters = old_filters |
154 | 154 |
155 class ErrorHandlerImpl(errorhandler.ErrorHandler): | 155 class ErrorHandlerImpl(errorhandler.ErrorHandler): |
156 """Filters out errors that don't apply to Chromium JavaScript code.""" | 156 """Filters out errors that don't apply to Chromium JavaScript code.""" |
157 | 157 |
158 def __init__(self, re): | 158 def __init__(self, re): |
(...skipping 24 matching lines...) Expand all Loading... |
183 | 183 |
184 # Ignore missing spaces before "(" until Promise#catch issue is solved. | 184 # Ignore missing spaces before "(" until Promise#catch issue is solved. |
185 # http://crbug.com/338301 | 185 # http://crbug.com/338301 |
186 if (error.code == errors.MISSING_SPACE and error.token.string == '(' and | 186 if (error.code == errors.MISSING_SPACE and error.token.string == '(' and |
187 'catch(' in error.token.line): | 187 'catch(' in error.token.line): |
188 return False | 188 return False |
189 | 189 |
190 return not is_grit_statement and error.code not in [ | 190 return not is_grit_statement and error.code not in [ |
191 errors.COMMA_AT_END_OF_LITERAL, | 191 errors.COMMA_AT_END_OF_LITERAL, |
192 errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE, | 192 errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE, |
193 errors.JSDOC_TAG_DESCRIPTION_ENDS_WITH_INVALID_CHARACTER, | |
194 errors.LINE_TOO_LONG, | 193 errors.LINE_TOO_LONG, |
195 errors.MISSING_JSDOC_TAG_THIS, | 194 errors.MISSING_JSDOC_TAG_THIS, |
196 ] | 195 ] |
197 | 196 |
198 results = [] | 197 results = [] |
199 | 198 |
200 affected_files = self.input_api.change.AffectedFiles( | 199 affected_files = self.input_api.change.AffectedFiles( |
201 file_filter=self.file_filter, | 200 file_filter=self.file_filter, |
202 include_deletes=False) | 201 include_deletes=False) |
203 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), | 202 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), |
(...skipping 10 matching lines...) Expand all Loading... |
214 self.ChromeSendCheck(i, line), | 213 self.ChromeSendCheck(i, line), |
215 self.ConstCheck(i, line), | 214 self.ConstCheck(i, line), |
216 self.GetElementByIdCheck(i, line), | 215 self.GetElementByIdCheck(i, line), |
217 self.InheritDocCheck(i, line), | 216 self.InheritDocCheck(i, line), |
218 self.WrapperTypeCheck(i, line), | 217 self.WrapperTypeCheck(i, line), |
219 self.VarNameCheck(i, line), | 218 self.VarNameCheck(i, line), |
220 ]) | 219 ]) |
221 | 220 |
222 # Use closure_linter to check for several different errors | 221 # Use closure_linter to check for several different errors |
223 error_handler = ErrorHandlerImpl(self.input_api.re) | 222 error_handler = ErrorHandlerImpl(self.input_api.re) |
224 js_checker = checker.JavaScriptStyleChecker(error_handler) | 223 file_to_check = self.input_api.os_path.join( |
225 js_checker.Check(self.input_api.os_path.join( | 224 self.input_api.change.RepositoryRoot(), f.LocalPath()) |
226 self.input_api.change.RepositoryRoot(), | 225 runner.Run(file_to_check, error_handler) |
227 f.LocalPath())) | |
228 | 226 |
229 for error in error_handler.GetErrors(): | 227 for error in error_handler.GetErrors(): |
230 highlight = self.error_highlight( | 228 highlight = self.error_highlight( |
231 error.token.start_index, error.token.length) | 229 error.token.start_index, error.token.length) |
232 error_msg = ' line %d: E%04d: %s\n%s\n%s' % ( | 230 error_msg = ' line %d: E%04d: %s\n%s\n%s' % ( |
233 error.token.line_number, | 231 error.token.line_number, |
234 error.code, | 232 error.code, |
235 error.message, | 233 error.message, |
236 error.token.line.rstrip(), | 234 error.token.line.rstrip(), |
237 highlight) | 235 highlight) |
238 error_lines.append(error_msg) | 236 error_lines.append(error_msg) |
239 | 237 |
240 if error_lines: | 238 if error_lines: |
241 error_lines = [ | 239 error_lines = [ |
242 'Found JavaScript style violations in %s:' % | 240 'Found JavaScript style violations in %s:' % |
243 f.LocalPath()] + error_lines | 241 f.LocalPath()] + error_lines |
244 results.append(self._makeErrorOrWarning( | 242 results.append(self._makeErrorOrWarning( |
245 '\n'.join(error_lines), f.AbsoluteLocalPath())) | 243 '\n'.join(error_lines), f.AbsoluteLocalPath())) |
246 | 244 |
247 if results: | 245 if results: |
248 results.append(self.output_api.PresubmitNotifyResult( | 246 results.append(self.output_api.PresubmitNotifyResult( |
249 'See the JavaScript style guide at ' | 247 'See the JavaScript style guide at ' |
250 'http://www.chromium.org/developers/web-development-style-guide' | 248 'http://www.chromium.org/developers/web-development-style-guide' |
251 '#TOC-JavaScript and if you have any feedback about the JavaScript ' | 249 '#TOC-JavaScript and if you have any feedback about the JavaScript ' |
252 'PRESUBMIT check, contact tbreisacher@chromium.org or ' | 250 'PRESUBMIT check, contact tbreisacher@chromium.org or ' |
253 'dbeam@chromium.org')) | 251 'dbeam@chromium.org')) |
254 | 252 |
255 return results | 253 return results |
OLD | NEW |