Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / web_dev_style / js_checker.py
blob08b8bf59723e73773e4750a4ed3b65d3b6c20b1b
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
8 """
10 import regex_check
13 class JSChecker(object):
14 def __init__(self, input_api, output_api, file_filter=None):
15 self.input_api = input_api
16 self.output_api = output_api
17 self.file_filter = file_filter
19 def RegexCheck(self, line_number, line, regex, message):
20 return regex_check.RegexCheck(
21 self.input_api.re, line_number, line, regex, message)
23 def ChromeSendCheck(self, i, line):
24 """Checks for a particular misuse of 'chrome.send'."""
25 return self.RegexCheck(i, line, r"chrome\.send\('[^']+'\s*(, \[\])\)",
26 'Passing an empty array to chrome.send is unnecessary')
28 def ConstCheck(self, i, line):
29 """Check for use of the 'const' keyword."""
30 if self.input_api.re.search(r'\*\s+@const', line):
31 # Probably a JsDoc line
32 return ''
34 return self.RegexCheck(i, line, r'(?:^|\s|\()(const)\s',
35 'Use /** @const */ var varName; instead of const varName;')
37 def EndJsDocCommentCheck(self, i, line):
38 msg = 'End JSDoc comments with */ instead of **/'
39 def _check(regex):
40 return self.RegexCheck(i, line, regex, msg)
41 return _check(r'^\s*(\*\*/)\s*$') or _check(r'/\*\* @[a-zA-Z]+.* (\*\*/)')
43 def GetElementByIdCheck(self, i, line):
44 """Checks for use of 'document.getElementById' instead of '$'."""
45 return self.RegexCheck(i, line, r"(document\.getElementById)\('",
46 "Use $('id'), from chrome://resources/js/util.js, instead of "
47 "document.getElementById('id')")
49 def InheritDocCheck(self, i, line):
50 """Checks for use of '@inheritDoc' instead of '@override'."""
51 return self.RegexCheck(i, line, r"\* (@inheritDoc)",
52 "@inheritDoc is deprecated, use @override instead")
54 def WrapperTypeCheck(self, i, line):
55 """Check for wrappers (new String()) instead of builtins (string)."""
56 return self.RegexCheck(i, line,
57 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type
58 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)}
59 "Don't use wrapper types (i.e. new String() or @type {String})")
61 def VarNameCheck(self, i, line):
62 """See the style guide. http://goo.gl/uKir6"""
63 return self.RegexCheck(i, line,
64 r"var (?!g_\w+)([a-z]*[_$][\w_$]*)(?<! \$)",
65 "Please use var namesLikeThis <http://goo.gl/uKir6>")
67 def _GetErrorHighlight(self, start, length):
68 """Takes a start position and a length, and produces a row of '^'s to
69 highlight the corresponding part of a string.
70 """
71 return start * ' ' + length * '^'
73 def _MakeErrorOrWarning(self, error_text, filename):
74 """Takes a few lines of text indicating a style violation and turns it into
75 a PresubmitError (if |filename| is in a directory where we've already
76 taken out all the style guide violations) or a PresubmitPromptWarning
77 (if it's in a directory where we haven't done that yet).
78 """
79 # TODO(tbreisacher): Once we've cleaned up the style nits in all of
80 # resources/ we can get rid of this function.
81 path = self.input_api.os_path
82 resources = path.join(self.input_api.PresubmitLocalPath(), 'resources')
83 dirs = (
84 path.join(resources, 'bookmark_manager'),
85 path.join(resources, 'extensions'),
86 path.join(resources, 'file_manager'),
87 path.join(resources, 'help'),
88 path.join(resources, 'history'),
89 path.join(resources, 'memory_internals'),
90 path.join(resources, 'net_export'),
91 path.join(resources, 'net_internals'),
92 path.join(resources, 'network_action_predictor'),
93 path.join(resources, 'ntp4'),
94 path.join(resources, 'options'),
95 path.join(resources, 'password_manager_internals'),
96 path.join(resources, 'print_preview'),
97 path.join(resources, 'profiler'),
98 path.join(resources, 'sync_promo'),
99 path.join(resources, 'tracing'),
100 path.join(resources, 'uber'),
102 if filename.startswith(dirs):
103 return self.output_api.PresubmitError(error_text)
104 else:
105 return self.output_api.PresubmitPromptWarning(error_text)
107 def ClosureLint(self, file_to_lint, source=None):
108 """Lints |file_to_lint| and returns the errors."""
110 import sys
111 import warnings
112 old_path = sys.path
113 old_filters = warnings.filters
115 try:
116 closure_linter_path = self.input_api.os_path.join(
117 self.input_api.change.RepositoryRoot(),
118 "third_party",
119 "closure_linter")
120 gflags_path = self.input_api.os_path.join(
121 self.input_api.change.RepositoryRoot(),
122 "third_party",
123 "python_gflags")
125 sys.path.insert(0, closure_linter_path)
126 sys.path.insert(0, gflags_path)
128 warnings.filterwarnings('ignore', category=DeprecationWarning)
130 from closure_linter import errors, runner
131 from closure_linter.common import errorhandler
132 import gflags
134 finally:
135 sys.path = old_path
136 warnings.filters = old_filters
138 class ErrorHandlerImpl(errorhandler.ErrorHandler):
139 """Filters out errors that don't apply to Chromium JavaScript code."""
141 def __init__(self, re):
142 self._errors = []
143 self.re = re
145 def HandleFile(self, filename, first_token):
146 self._filename = filename
148 def HandleError(self, error):
149 if (self._valid(error)):
150 error.filename = self._filename
151 self._errors.append(error)
153 def GetErrors(self):
154 return self._errors
156 def HasErrors(self):
157 return bool(self._errors)
159 def _valid(self, error):
160 """Check whether an error is valid. Most errors are valid, with a few
161 exceptions which are listed here.
164 is_grit_statement = bool(
165 self.re.search("</?(include|if)", error.token.line))
167 # Ignore missing spaces before "(" until Promise#catch issue is solved.
168 # http://crbug.com/338301
169 if (error.code == errors.MISSING_SPACE and error.token.string == '(' and
170 'catch(' in error.token.line):
171 return False
173 # Ignore "}.bind(" errors. http://crbug.com/397697
174 if (error.code == errors.MISSING_SEMICOLON_AFTER_FUNCTION and
175 '}.bind(' in error.token.line):
176 return False
178 return not is_grit_statement and error.code not in [
179 errors.COMMA_AT_END_OF_LITERAL,
180 errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE,
181 errors.LINE_TOO_LONG,
182 errors.MISSING_JSDOC_TAG_THIS,
185 # Whitelist Polymer-specific JsDoc tags.
186 gflags.FLAGS.custom_jsdoc_tags = ('group', 'element', 'attribute',
187 'default')
188 error_handler = ErrorHandlerImpl(self.input_api.re)
189 runner.Run(file_to_lint, error_handler, source=source)
190 return error_handler.GetErrors()
192 def RunChecks(self):
193 """Check for violations of the Chromium JavaScript style guide. See
194 http://chromium.org/developers/web-development-style-guide#TOC-JavaScript
196 results = []
198 affected_files = self.input_api.change.AffectedFiles(
199 file_filter=self.file_filter,
200 include_deletes=False)
201 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'),
202 affected_files)
203 for f in affected_js_files:
204 error_lines = []
206 # Check for the following:
207 # * document.getElementById()
208 # * the 'const' keyword
209 # * Passing an empty array to 'chrome.send()'
210 for i, line in enumerate(f.NewContents(), start=1):
211 error_lines += filter(None, [
212 self.ChromeSendCheck(i, line),
213 self.ConstCheck(i, line),
214 self.GetElementByIdCheck(i, line),
215 self.InheritDocCheck(i, line),
216 self.WrapperTypeCheck(i, line),
217 self.VarNameCheck(i, line),
220 # Use closure linter to check for several different errors.
221 lint_errors = self.ClosureLint(self.input_api.os_path.join(
222 self.input_api.change.RepositoryRoot(), f.LocalPath()))
224 for error in lint_errors:
225 highlight = self._GetErrorHighlight(
226 error.token.start_index, error.token.length)
227 error_msg = ' line %d: E%04d: %s\n%s\n%s' % (
228 error.token.line_number,
229 error.code,
230 error.message,
231 error.token.line.rstrip(),
232 highlight)
233 error_lines.append(error_msg)
235 if error_lines:
236 error_lines = [
237 'Found JavaScript style violations in %s:' %
238 f.LocalPath()] + error_lines
239 results.append(self._MakeErrorOrWarning(
240 '\n'.join(error_lines), f.AbsoluteLocalPath()))
242 if results:
243 results.append(self.output_api.PresubmitNotifyResult(
244 'See the JavaScript style guide at '
245 'http://www.chromium.org/developers/web-development-style-guide'
246 '#TOC-JavaScript and if you have any feedback about the JavaScript '
247 'PRESUBMIT check, contact tbreisacher@chromium.org or '
248 'dbeam@chromium.org'))
250 return results