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 HTML/CSS/JS resources. | |
| 6 | |
| 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | |
| 8 for more details about the presubmit API built into gcl. | |
| 9 """ | |
| 10 | |
| 11 | |
| 12 def _CheckJavaScriptStyle(input_api, output_api): | |
| 13 """Check for violations of the Chromium JavaScript style guide. See | |
| 14 http://chromium.org/developers/web-development-style-guide#TOC-JavaScript | |
| 15 """ | |
| 16 | |
| 17 import closure_linter.checker | |
| 18 import closure_linter.common.errorhandler | |
| 19 import closure_linter.errors | |
|
Dan Beam
2012/02/08 19:46:54
nit: It'd save you one line and some typing of clo
Tyler Breisacher (Chromium)
2012/02/08 20:03:56
Done.
| |
| 20 | |
| 21 class ErrorHandlerImpl(closure_linter.common.errorhandler.ErrorHandler): | |
| 22 """Filters out errors that don't apply to Chromium JavaScript code.""" | |
| 23 | |
| 24 def __init__(self): | |
| 25 self._errors = [] | |
|
Dan Beam
2012/02/08 19:46:54
nit: Is there something in closure that makes you
M-A Ruel
2012/02/08 19:57:25
You didn't see it because I never use protected va
Tyler Breisacher (Chromium)
2012/02/08 20:03:56
Looks like this is right: http://google-styleguide
| |
| 26 | |
| 27 def HandleFile(self, filename, first_token): | |
| 28 self._filename = filename | |
| 29 | |
| 30 def HandleError(self, error): | |
| 31 if (self._valid(error)): | |
| 32 error.filename = self._filename | |
| 33 self._errors.append(error) | |
| 34 | |
| 35 def GetErrors(self): | |
| 36 return self._errors | |
| 37 | |
| 38 def HasErrors(self): | |
| 39 return bool(self._errors) | |
| 40 | |
| 41 def _valid(self, error): | |
| 42 """Check whether an error is valid. Most errors are valid, with a few | |
| 43 exceptions which are listed here. | |
| 44 """ | |
| 45 | |
| 46 return error.code not in [ | |
| 47 closure_linter.errors.COMMA_AT_END_OF_LITERAL, | |
| 48 closure_linter.errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE, | |
| 49 closure_linter.errors. | |
| 50 JSDOC_TAG_DESCRIPTION_ENDS_WITH_INVALID_CHARACTER, | |
| 51 ] | |
| 52 | |
| 53 # Only check the following folders. OWNERS of folders containing JavaScript | |
| 54 # code can opt-in to this check by adding the folder here. | |
| 55 join = input_api.os_path.join | |
| 56 checked_folders = [ | |
| 57 join('chrome', 'browser', 'resources', 'ntp4'), | |
| 58 join('chrome', 'browser', 'resources', 'options2'), | |
| 59 ] | |
| 60 | |
| 61 def in_checked_folder(affected_file): | |
| 62 return any(affected_file.LocalPath().startswith(cf) | |
| 63 for cf in checked_folders) | |
| 64 | |
| 65 def js_or_html(affected_file): | |
| 66 return affected_file.LocalPath().endswith(('.js', '.html')) | |
| 67 | |
| 68 def file_filter(affected_file): | |
| 69 return js_or_html(affected_file) and in_checked_folder(affected_file) | |
| 70 | |
| 71 results = [] | |
| 72 | |
| 73 for f in input_api.change.AffectedFiles(file_filter=file_filter): | |
| 74 error_lines = [] | |
| 75 | |
| 76 # check for getElementById() | |
| 77 for i, line in enumerate(f.NewContents(), start=1): | |
| 78 if 'getElementById' in line: | |
| 79 error_lines.append(' line %d: %s\n%s' % ( | |
| 80 i, | |
| 81 "Use $('id') instead of document.getElementById('id')", | |
| 82 line)) | |
| 83 | |
| 84 if input_api.re.search(r'\bconst\b', line): | |
| 85 error_lines.append(' line %d: %s\n%s' % ( | |
| 86 i, | |
| 87 'Use |var| instead of |const|. See http://crbug.com/80149', | |
| 88 line)) | |
| 89 | |
| 90 # Use closure_linter to check for several different errors | |
| 91 error_handler = ErrorHandlerImpl() | |
| 92 checker = closure_linter.checker.JavaScriptStyleChecker(error_handler) | |
| 93 checker.Check(join(input_api.change.RepositoryRoot(), f.LocalPath())) | |
| 94 | |
| 95 for error in error_handler.GetErrors(): | |
| 96 errorMsg = ' line %d: E%04d: %s\n%s' % ( | |
|
Dan Beam
2012/02/08 19:46:54
nit: error_msg
Tyler Breisacher (Chromium)
2012/02/08 20:03:56
Done.
| |
| 97 error.token.line_number, | |
| 98 error.code, | |
| 99 error.message, | |
| 100 error.token.line) | |
| 101 error_lines.append(errorMsg) | |
| 102 | |
| 103 if error_lines: | |
| 104 error_lines = [ | |
| 105 'Found JavaScript style violations in %s:' % | |
| 106 f.LocalPath()] + error_lines | |
| 107 results.append(output_api.PresubmitError('\n'.join(error_lines))) | |
| 108 | |
| 109 if results: | |
| 110 results.append(output_api.PresubmitNotifyResult( | |
| 111 'See the JavaScript style guide at' | |
| 112 ' http://www.chromium.org/developers/web-development-style-guide' | |
|
Dan Beam
2012/02/08 19:46:54
nit: spaces at the end instead of front, similar t
Tyler Breisacher (Chromium)
2012/02/08 20:03:56
Done.
| |
| 113 '#TOC-JavaScript and if you have any feedback about the JavaScript' | |
| 114 ' PRESUBMIT check, contact tbreisacher@chromium.org')) | |
| 115 | |
| 116 return results | |
| 117 | |
| 118 | |
| 119 def CheckChangeOnUpload(input_api, output_api): | |
| 120 return _CheckJavaScriptStyle(input_api, output_api) | |
| 121 | |
| 122 | |
| 123 def CheckChangeOnCommit(input_api, output_api): | |
| 124 return _CheckJavaScriptStyle(input_api, output_api) | |
| 125 | |
| OLD | NEW |