| 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 import regex_check | 10 import regex_check |
| 11 | 11 |
| 12 | 12 |
| 13 class JSChecker(object): | 13 class JSChecker(object): |
| 14 def __init__(self, input_api, output_api, file_filter=None): | 14 def __init__(self, input_api, output_api, file_filter=None): |
| 15 self.input_api = input_api | 15 self.input_api = input_api |
| 16 self.output_api = output_api | 16 self.output_api = output_api |
| 17 self.file_filter = file_filter | 17 self.file_filter = file_filter |
| 18 | 18 |
| 19 def RegexCheck(self, line_number, line, regex, message): | 19 def RegexCheck(self, line_number, line, regex, message): |
| 20 return regex_check.RegexCheck( | 20 return regex_check.RegexCheck( |
| 21 self.input_api.re, line_number, line, regex, message) | 21 self.input_api.re, line_number, line, regex, message) |
| 22 | 22 |
| 23 def ChromeSendCheck(self, i, line): | 23 def ChromeSendCheck(self, i, line): |
| 24 """Checks for a particular misuse of 'chrome.send'.""" | 24 """Checks for a particular misuse of 'chrome.send'.""" |
| 25 return self.RegexCheck(i, line, r"chrome\.send\('[^']+'\s*(, \[\])\)", | 25 return self.RegexCheck(i, line, r"chrome\.send\('[^']+'\s*(, \[\])\)", |
| 26 'Passing an empty array to chrome.send is unnecessary') | 26 'Passing an empty array to chrome.send is unnecessary') |
| 27 | 27 |
| 28 def CommentIfAndIncludeCheck(self, line_number, line): |
| 29 return self.RegexCheck(line_number, line, r'(?<!\/\/ )(<if|<include) ', |
| 30 '<if> or <include> should be in a single line comment with a space ' + |
| 31 'after the slashes. Examples:\n' + |
| 32 ' // <include src="...">\n' + |
| 33 ' // <if expr="chromeos">\n' + |
| 34 ' // </if>\n') |
| 35 |
| 28 def ConstCheck(self, i, line): | 36 def ConstCheck(self, i, line): |
| 29 """Check for use of the 'const' keyword.""" | 37 """Check for use of the 'const' keyword.""" |
| 30 if self.input_api.re.search(r'\*\s+@const', line): | 38 if self.input_api.re.search(r'\*\s+@const', line): |
| 31 # Probably a JsDoc line | 39 # Probably a JsDoc line |
| 32 return '' | 40 return '' |
| 33 | 41 |
| 34 return self.RegexCheck(i, line, r'(?:^|\s|\()(const)\s', | 42 return self.RegexCheck(i, line, r'(?:^|\s|\()(const)\s', |
| 35 'Use /** @const */ var varName; instead of const varName;') | 43 'Use /** @const */ var varName; instead of const varName;') |
| 36 | 44 |
| 37 def EndJsDocCommentCheck(self, i, line): | 45 def EndJsDocCommentCheck(self, i, line): |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 return self.RegexCheck(i, line, | 80 return self.RegexCheck(i, line, |
| 73 r"var (?!g_\w+)([a-z]*[_$][\w_$]*)(?<! \$)", | 81 r"var (?!g_\w+)([a-z]*[_$][\w_$]*)(?<! \$)", |
| 74 "Please use var namesLikeThis <http://goo.gl/uKir6>") | 82 "Please use var namesLikeThis <http://goo.gl/uKir6>") |
| 75 | 83 |
| 76 def _GetErrorHighlight(self, start, length): | 84 def _GetErrorHighlight(self, start, length): |
| 77 """Takes a start position and a length, and produces a row of '^'s to | 85 """Takes a start position and a length, and produces a row of '^'s to |
| 78 highlight the corresponding part of a string. | 86 highlight the corresponding part of a string. |
| 79 """ | 87 """ |
| 80 return start * ' ' + length * '^' | 88 return start * ' ' + length * '^' |
| 81 | 89 |
| 82 def _MakeErrorOrWarning(self, error_text, filename): | |
| 83 """Takes a few lines of text indicating a style violation and turns it into | |
| 84 a PresubmitError (if |filename| is in a directory where we've already | |
| 85 taken out all the style guide violations) or a PresubmitPromptWarning | |
| 86 (if it's in a directory where we haven't done that yet). | |
| 87 """ | |
| 88 # TODO(tbreisacher): Once we've cleaned up the style nits in all of | |
| 89 # resources/ we can get rid of this function. | |
| 90 path = self.input_api.os_path | |
| 91 resources = path.join(self.input_api.PresubmitLocalPath(), 'resources') | |
| 92 dirs = ( | |
| 93 path.join(resources, 'bookmark_manager'), | |
| 94 path.join(resources, 'extensions'), | |
| 95 path.join(resources, 'file_manager'), | |
| 96 path.join(resources, 'help'), | |
| 97 path.join(resources, 'history'), | |
| 98 path.join(resources, 'net_export'), | |
| 99 path.join(resources, 'net_internals'), | |
| 100 path.join(resources, 'network_action_predictor'), | |
| 101 path.join(resources, 'ntp4'), | |
| 102 path.join(resources, 'options'), | |
| 103 path.join(resources, 'password_manager_internals'), | |
| 104 path.join(resources, 'print_preview'), | |
| 105 path.join(resources, 'profiler'), | |
| 106 path.join(resources, 'sync_promo'), | |
| 107 path.join(resources, 'tracing'), | |
| 108 path.join(resources, 'uber'), | |
| 109 ) | |
| 110 if filename.startswith(dirs): | |
| 111 return self.output_api.PresubmitError(error_text) | |
| 112 else: | |
| 113 return self.output_api.PresubmitPromptWarning(error_text) | |
| 114 | |
| 115 def RunChecks(self): | 90 def RunChecks(self): |
| 116 """Check for violations of the Chromium JavaScript style guide. See | 91 """Check for violations of the Chromium JavaScript style guide. See |
| 117 http://chromium.org/developers/web-development-style-guide#TOC-JavaScript | 92 http://chromium.org/developers/web-development-style-guide#TOC-JavaScript |
| 118 """ | 93 """ |
| 119 results = [] | 94 results = [] |
| 120 | 95 |
| 121 affected_files = self.input_api.change.AffectedFiles( | 96 affected_files = self.input_api.change.AffectedFiles( |
| 122 file_filter=self.file_filter, | 97 file_filter=self.file_filter, |
| 123 include_deletes=False) | 98 include_deletes=False) |
| 124 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), | 99 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), |
| 125 affected_files) | 100 affected_files) |
| 126 for f in affected_js_files: | 101 for f in affected_js_files: |
| 127 error_lines = [] | 102 error_lines = [] |
| 128 | 103 |
| 129 # Check for the following: | |
| 130 # * document.getElementById() | |
| 131 # * the 'const' keyword | |
| 132 # * Passing an empty array to 'chrome.send()' | |
| 133 for i, line in enumerate(f.NewContents(), start=1): | 104 for i, line in enumerate(f.NewContents(), start=1): |
| 134 error_lines += filter(None, [ | 105 error_lines += filter(None, [ |
| 135 self.ChromeSendCheck(i, line), | 106 self.ChromeSendCheck(i, line), |
| 107 self.CommentIfAndIncludeCheck(i, line), |
| 136 self.ConstCheck(i, line), | 108 self.ConstCheck(i, line), |
| 137 self.GetElementByIdCheck(i, line), | 109 self.GetElementByIdCheck(i, line), |
| 138 self.EndJsDocCommentCheck(i, line), | 110 self.EndJsDocCommentCheck(i, line), |
| 139 self.ExtraDotInGenericCheck(i, line), | 111 self.ExtraDotInGenericCheck(i, line), |
| 140 self.InheritDocCheck(i, line), | 112 self.InheritDocCheck(i, line), |
| 141 self.PolymerLocalIdCheck(i, line), | 113 self.PolymerLocalIdCheck(i, line), |
| 142 self.WrapperTypeCheck(i, line), | 114 self.WrapperTypeCheck(i, line), |
| 143 self.VarNameCheck(i, line), | 115 self.VarNameCheck(i, line), |
| 144 ]) | 116 ]) |
| 145 | 117 |
| 146 if error_lines: | 118 if error_lines: |
| 147 error_lines = [ | 119 error_lines = [ |
| 148 'Found JavaScript style violations in %s:' % | 120 'Found JavaScript style violations in %s:' % |
| 149 f.LocalPath()] + error_lines | 121 f.LocalPath()] + error_lines |
| 150 results.append(self._MakeErrorOrWarning( | 122 results.append(self.output_api.PresubmitError('\n'.join(error_lines))) |
| 151 '\n'.join(error_lines), f.AbsoluteLocalPath())) | |
| 152 | 123 |
| 153 if results: | 124 if results: |
| 154 results.append(self.output_api.PresubmitNotifyResult( | 125 results.append(self.output_api.PresubmitNotifyResult( |
| 155 'See the JavaScript style guide at ' | 126 'See the JavaScript style guide at ' |
| 156 'http://www.chromium.org/developers/web-development-style-guide' | 127 'http://www.chromium.org/developers/web-development-style-guide' |
| 157 '#TOC-JavaScript')) | 128 '#TOC-JavaScript')) |
| 158 | 129 |
| 159 return results | 130 return results |
| OLD | NEW |