Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Presubmit script for Chromium JS resources. | |
| 6 | |
| 7 See chrome/browser/resources/PRESUBMIT.py | |
| 8 """ | |
| 9 | |
| 10 class JSChecker(object): | |
| 11 def __init__(self, input_api, output_api, file_filter=None): | |
| 12 self.input_api = input_api | |
| 13 self.output_api = output_api | |
| 14 self.file_filter = file_filter | |
| 15 | |
| 16 def RunChecks(self): | |
| 17 """Check for violations of the Chromium JavaScript style guide. See | |
| 18 http://chromium.org/developers/web-development-style-guide#TOC-JavaScript | |
| 19 """ | |
| 20 | |
| 21 import sys | |
| 22 old_path = sys.path | |
| 23 | |
| 24 try: | |
| 25 closure_linter_path = self.input_api.os_path.join( | |
| 26 self.input_api.change.RepositoryRoot(), | |
| 27 "third_party", | |
| 28 "closure_linter") | |
| 29 | |
| 30 sys.path.insert(0, closure_linter_path) | |
| 31 | |
| 32 from closure_linter import checker, errors | |
| 33 from closure_linter.common import errorhandler | |
| 34 | |
| 35 finally: | |
| 36 sys.path = old_path | |
| 37 | |
| 38 class ErrorHandlerImpl(errorhandler.ErrorHandler): | |
| 39 """Filters out errors that don't apply to Chromium JavaScript code.""" | |
| 40 | |
| 41 def __init__(self): | |
| 42 self._errors = [] | |
| 43 | |
| 44 def HandleFile(self, filename, first_token): | |
| 45 self._filename = filename | |
| 46 | |
| 47 def HandleError(self, error): | |
| 48 if (self._valid(error)): | |
| 49 error.filename = self._filename | |
| 50 self._errors.append(error) | |
| 51 | |
| 52 def GetErrors(self): | |
| 53 return self._errors | |
| 54 | |
| 55 def HasErrors(self): | |
| 56 return bool(self._errors) | |
| 57 | |
| 58 def _valid(self, error): | |
| 59 """Check whether an error is valid. Most errors are valid, with a few | |
| 60 exceptions which are listed here. | |
| 61 """ | |
| 62 | |
| 63 return error.code not in [ | |
| 64 errors.COMMA_AT_END_OF_LITERAL, | |
| 65 errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE, | |
| 66 errors.JSDOC_TAG_DESCRIPTION_ENDS_WITH_INVALID_CHARACTER, | |
| 67 ] | |
| 68 | |
| 69 results = [] | |
| 70 | |
| 71 affected_files = self.input_api.change.AffectedFiles( | |
| 72 file_filter=self.file_filter) | |
| 73 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), | |
| 74 affected_files) | |
| 75 for f in affected_js_files: | |
| 76 error_lines = [] | |
| 77 | |
| 78 # check for getElementById() | |
| 79 for i, line in enumerate(f.NewContents(), start=1): | |
| 80 if 'getElementById' in line: | |
| 81 error_lines.append(' line %d: %s\n%s' % ( | |
| 82 i, | |
| 83 "Use $('id') instead of document.getElementById('id')", | |
| 84 line)) | |
| 85 | |
| 86 # Use closure_linter to check for several different errors | |
| 87 error_handler = ErrorHandlerImpl() | |
| 88 js_checker = checker.JavaScriptStyleChecker(error_handler) | |
| 89 js_checker.Check(self.input_api.os_path.join( | |
| 90 self.input_api.change.RepositoryRoot(), | |
| 91 f.LocalPath())) | |
| 92 | |
| 93 for error in error_handler.GetErrors(): | |
| 94 error_msg = ' line %d: E%04d: %s\n%s' % ( | |
| 95 error.token.line_number, | |
| 96 error.code, | |
| 97 error.message, | |
| 98 error.token.line) | |
| 99 error_lines.append(error_msg) | |
| 100 | |
| 101 if error_lines: | |
| 102 error_lines = [ | |
| 103 'Found JavaScript style violations in %s:' % | |
| 104 f.LocalPath()] + error_lines | |
| 105 results.append(self.output_api.PresubmitError('\n'.join(error_lines))) | |
| 106 | |
| 107 if results: | |
| 108 results.append(self.output_api.PresubmitNotifyResult( | |
| 109 'See the JavaScript style guide at ' | |
| 110 'http://www.chromium.org/developers/web-development-style-guide' | |
| 111 '#TOC-JavaScript and if you have any feedback about the JavaScript ' | |
| 112 'PRESUBMIT check, contact tbreisacher@chromium.org')) | |
| 113 | |
| 114 return results | |
| 115 | |
|
M-A Ruel
2012/02/15 19:51:06
remove empty line.
| |
| OLD | NEW |