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.
5 """Presubmit script for Chromium JS resources.
7 See chrome/browser/PRESUBMIT.py
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
16 def RegexCheck(self, line_number, line, regex, message):
17 """Searches for |regex| in |line| to check for a particular style
18 violation, returning a message like the one below if the regex matches.
19 The |regex| must have exactly one capturing group so that the relevant
20 part of |line| can be highlighted. If more groups are needed, use
21 "(?:...)" to make a non-capturing group. Sample message:
23 line 6: Use var instead of const.
27 match = self.input_api.re.search(regex, line)
29 assert len(match.groups()) == 1
30 start = match.start(1)
31 length = match.end(1) - start
32 return ' line %d: %s\n%s\n%s' % (
36 self.error_highlight(start, length))
39 def ChromeSendCheck(self, i, line):
40 """Checks for a particular misuse of 'chrome.send'."""
41 return self.RegexCheck(i, line, r"chrome\.send\('[^']+'\s*(, \[\])\)",
42 'Passing an empty array to chrome.send is unnecessary')
44 def ConstCheck(self, i, line):
45 """Check for use of the 'const' keyword."""
46 if self.input_api.re.search(r'\*\s+@const', line):
47 # Probably a JsDoc line
50 return self.RegexCheck(i, line, r'(?:^|\s|\()(const)\s',
51 'Use /** @const */ var varName; instead of const varName;')
53 def EndJsDocCommentCheck(self, i, line):
54 msg = 'End JSDoc comments with */ instead of **/'
56 return self.RegexCheck(i, line, regex, msg)
57 return _check(r'^\s*(\*\*/)\s*$') or _check(r'/\*\* @[a-zA-Z]+.* (\*\*/)')
59 def GetElementByIdCheck(self, i, line):
60 """Checks for use of 'document.getElementById' instead of '$'."""
61 return self.RegexCheck(i, line, r"(document\.getElementById)\('",
62 "Use $('id'), from chrome://resources/js/util.js, instead of "
63 "document.getElementById('id')")
65 def InheritDocCheck(self, i, line):
66 """Checks for use of '@inheritDoc' instead of '@override'."""
67 return self.RegexCheck(i, line, r"\* (@inheritDoc)",
68 "@inheritDoc is deprecated, use @override instead")
70 def WrapperTypeCheck(self, i, line):
71 """Check for wrappers (new String()) instead of builtins (string)."""
72 return self.RegexCheck(i, line,
73 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type
74 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)}
75 "Don't use wrapper types (i.e. new String() or @type {String})")
77 def VarNameCheck(self, i, line):
78 """See the style guide. http://goo.gl/uKir6"""
79 return self.RegexCheck(i, line,
80 r"var (?!g_\w+)([a-z]*[_$][\w_$]*)(?<! \$)",
81 "Please use var namesLikeThis <http://goo.gl/uKir6>")
83 def error_highlight(self, start, length):
84 """Takes a start position and a length, and produces a row of '^'s to
85 highlight the corresponding part of a string.
87 return start * ' ' + length * '^'
89 def _makeErrorOrWarning(self, error_text, filename):
90 """Takes a few lines of text indicating a style violation and turns it into
91 a PresubmitError (if |filename| is in a directory where we've already
92 taken out all the style guide violations) or a PresubmitPromptWarning
93 (if it's in a directory where we haven't done that yet).
95 # TODO(tbreisacher): Once we've cleaned up the style nits in all of
96 # resources/ we can get rid of this function.
97 path = self.input_api.os_path
98 resources = path.join(self.input_api.PresubmitLocalPath(), 'resources')
100 path.join(resources, 'bookmark_manager'),
101 path.join(resources, 'extensions'),
102 path.join(resources, 'file_manager'),
103 path.join(resources, 'help'),
104 path.join(resources, 'history'),
105 path.join(resources, 'memory_internals'),
106 path.join(resources, 'net_export'),
107 path.join(resources, 'net_internals'),
108 path.join(resources, 'network_action_predictor'),
109 path.join(resources, 'ntp4'),
110 path.join(resources, 'options'),
111 path.join(resources, 'password_manager_internals'),
112 path.join(resources, 'print_preview'),
113 path.join(resources, 'profiler'),
114 path.join(resources, 'sync_promo'),
115 path.join(resources, 'tracing'),
116 path.join(resources, 'uber'),
118 if filename.startswith(dirs):
119 return self.output_api.PresubmitError(error_text)
121 return self.output_api.PresubmitPromptWarning(error_text)
124 """Check for violations of the Chromium JavaScript style guide. See
125 http://chromium.org/developers/web-development-style-guide#TOC-JavaScript
131 old_filters = warnings.filters
134 closure_linter_path = self.input_api.os_path.join(
135 self.input_api.change.RepositoryRoot(),
138 gflags_path = self.input_api.os_path.join(
139 self.input_api.change.RepositoryRoot(),
143 sys.path.insert(0, closure_linter_path)
144 sys.path.insert(0, gflags_path)
146 warnings.filterwarnings('ignore', category=DeprecationWarning)
148 from closure_linter import checker, errors
149 from closure_linter.common import errorhandler
153 warnings.filters = old_filters
155 class ErrorHandlerImpl(errorhandler.ErrorHandler):
156 """Filters out errors that don't apply to Chromium JavaScript code."""
158 def __init__(self, re):
162 def HandleFile(self, filename, first_token):
163 self._filename = filename
165 def HandleError(self, error):
166 if (self._valid(error)):
167 error.filename = self._filename
168 self._errors.append(error)
174 return bool(self._errors)
176 def _valid(self, error):
177 """Check whether an error is valid. Most errors are valid, with a few
178 exceptions which are listed here.
181 is_grit_statement = bool(
182 self.re.search("</?(include|if)", error.token.line))
184 # Ignore missing spaces before "(" until Promise#catch issue is solved.
185 # http://crbug.com/338301
186 if (error.code == errors.MISSING_SPACE and error.token.string == '(' and
187 'catch(' in error.token.line):
190 return not is_grit_statement and error.code not in [
191 errors.COMMA_AT_END_OF_LITERAL,
192 errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE,
193 errors.JSDOC_TAG_DESCRIPTION_ENDS_WITH_INVALID_CHARACTER,
194 errors.LINE_TOO_LONG,
195 errors.MISSING_JSDOC_TAG_THIS,
200 affected_files = self.input_api.change.AffectedFiles(
201 file_filter=self.file_filter,
202 include_deletes=False)
203 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'),
205 for f in affected_js_files:
208 # Check for the following:
209 # * document.getElementById()
210 # * the 'const' keyword
211 # * Passing an empty array to 'chrome.send()'
212 for i, line in enumerate(f.NewContents(), start=1):
213 error_lines += filter(None, [
214 self.ChromeSendCheck(i, line),
215 self.ConstCheck(i, line),
216 self.GetElementByIdCheck(i, line),
217 self.InheritDocCheck(i, line),
218 self.WrapperTypeCheck(i, line),
219 self.VarNameCheck(i, line),
222 # Use closure_linter to check for several different errors
223 error_handler = ErrorHandlerImpl(self.input_api.re)
224 js_checker = checker.JavaScriptStyleChecker(error_handler)
225 js_checker.Check(self.input_api.os_path.join(
226 self.input_api.change.RepositoryRoot(),
229 for error in error_handler.GetErrors():
230 highlight = self.error_highlight(
231 error.token.start_index, error.token.length)
232 error_msg = ' line %d: E%04d: %s\n%s\n%s' % (
233 error.token.line_number,
236 error.token.line.rstrip(),
238 error_lines.append(error_msg)
242 'Found JavaScript style violations in %s:' %
243 f.LocalPath()] + error_lines
244 results.append(self._makeErrorOrWarning(
245 '\n'.join(error_lines), f.AbsoluteLocalPath()))
248 results.append(self.output_api.PresubmitNotifyResult(
249 'See the JavaScript style guide at '
250 'http://www.chromium.org/developers/web-development-style-guide'
251 '#TOC-JavaScript and if you have any feedback about the JavaScript '
252 'PRESUBMIT check, contact tbreisacher@chromium.org or '
253 'dbeam@chromium.org'))