Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Unified Diff: PRESUBMIT.py

Issue 9288045: PRESUBMIT check for JavaScript style errors (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: PRESUBMIT.py
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index aa79141a90fa5217e6f29f1b7646c0744a21ffd5..a4b8d2832b3af727f44341b29703ced92cd5eb2f 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -8,10 +8,6 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into gcl.
"""
-
-import re
-
-
_EXCLUDED_PATHS = (
r"^breakpad[\\\/].*",
r"^native_client_sdk[\\\/].*",
@@ -30,6 +26,114 @@ _TEST_ONLY_WARNING = (
'Email joi@chromium.org if you have questions.')
+def _CheckJavaScriptStyle(input_api, output_api):
+ """Check for JavaScript style violations."""
Dan Beam 2012/02/07 09:08:00 Mention that this is checking against our web deve
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Done.
+
+ import closure_linter.common.errorhandler
Dan Beam 2012/02/07 09:08:00 Are these actually local imports or is it the same
M-A Ruel 2012/02/07 14:16:04 That's a local import, it's fine.
+
+ class ErrorHandlerImpl(closure_linter.common.errorhandler.ErrorHandler):
+ """Implementation of ErrorHandler that collects all errors except those
+ that don't apply for Chromium JavaScript code.
+ """
+
+ def __init__(self):
+ self._errors = []
+
+ def HandleFile(self, filename, first_token):
+ self._filename = filename
+
+ def HandleError(self, error):
+ if (self._valid(error)):
+ error.filename = self._filename
+ self._errors.append(error)
+
+ def GetErrors(self):
+ return self._errors
+
+ def HasErrors(self):
+ return bool(self._errors)
+
+ def _valid(self, error):
+ """Check whether an error is valid. Most errors are valid, with a few
+ exceptions which are listed here.
+ """
+
+ import closure_linter.errors
+
+ return error.code not in [
+ closure_linter.errors.COMMA_AT_END_OF_LITERAL,
+ closure_linter.errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE,
+ closure_linter.errors.
+ JSDOC_TAG_DESCRIPTION_ENDS_WITH_INVALID_CHARACTER,
+ ]
+
+ import closure_linter.checker
+
+ # Only check the following folders. OWNERS of folders containing JavaScript
+ # code can opt-in to this check by adding the folder here.
+ join = input_api.os_path.join
+ checked_folders = [
+ join('chrome', 'browser', 'resources', 'ntp4'),
+ join('chrome', 'browser', 'resources', 'options2'),
+ ]
+
+ def inCheckedFolder(affected_file):
Dan Beam 2012/02/07 09:08:00 I thought we used in_checked_folder for local func
M-A Ruel 2012/02/07 14:16:04 Right.
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Done.
+ return any(affected_file.LocalPath().startswith(cf)
Dan Beam 2012/02/07 09:08:00 Is this safe to assume? What if I have a CL that
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Good point, I'm not sure what we can do about it e
+ for cf in checked_folders)
+
+ def jsOrHtml(affected_file):
+ return input_api.re.search('\.(js|html?)$', affected_file.LocalPath())
Dan Beam 2012/02/07 09:08:00 Why are you checking HTML?
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 It's already equipped to check JS code in <script>
+
+ def fileFilter(affected_file):
+ return jsOrHtml(affected_file) and inCheckedFolder(affected_file)
+
+ results = []
+
+ for f in input_api.change.AffectedFiles(file_filter=fileFilter):
+ errorLines = []
Dan Beam 2012/02/07 09:08:00 camelCase in JS, under_scores in Python and C++ fo
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Done.
+
+ # check for getElementById()
+ for i, line in enumerate(f.NewContents(), start=1):
Dan Beam 2012/02/07 09:08:00 Whay are you starting at 1?
M-A Ruel 2012/02/07 14:16:04 Because line numbers starting at 0 are unintuitive
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Neither way is particularly more intuitive than th
+ if 'getElementById' in line:
Dan Beam 2012/02/07 09:08:00 nit: I think this should be slightly more robust,
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 I think this is probably okay as is, to be honest.
+ errorLines.append(' line %d: %s\n%s' % (
+ i,
+ 'Use $() instead of document.getElementById()',
Dan Beam 2012/02/07 09:08:00 nit: $('id') to show we're not using jQuery (thoug
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Done.
+ line))
+
+ if input_api.re.search(r'\bconst\b', line):
Dan Beam 2012/02/07 09:08:00 Doesn't closure have something for this?
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 You would think so, but no :(
+ errorLines.append(' line %d: %s\n%s' % (
+ i,
+ 'Use |var| instead of |const|. See http://crbug.com/80149',
+ line))
+
+ # Use closure_linter to check for several different errors
+ error_handler = ErrorHandlerImpl()
+ checker = closure_linter.checker.JavaScriptStyleChecker(error_handler)
+ checker.Check(f.LocalPath())
+
+ for error in error_handler.GetErrors():
+ errorMsg = ' line %d: E%04d: %s\n%s' % (
+ error.token.line_number,
+ error.code,
Dan Beam 2012/02/07 09:08:00 Do you think the average user will care about this
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Generally, no, but if this check complains about s
+ error.message,
+ error.token.line)
+ errorLines.append(errorMsg)
+
+ if errorLines:
+ errorLines = [
+ 'Found JavaScript style violations in %s:' %
+ f.LocalPath()] + errorLines
+ results.append(output_api.PresubmitError('\n'.join(errorLines)))
+
+ if results:
+ results.append(output_api.PresubmitNotifyResult(
+ 'See the JavaScript style guide at '
+ 'http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml'
Dan Beam 2012/02/07 09:08:00 This is not necessarily the guide we're following.
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Done.
+ ' and contact tbreisacher@chromium.org for feedback on this'
Dan Beam 2012/02/07 09:08:00 nit: This sounds like they're contacting you for f
Tyler Breisacher (Chromium) 2012/02/08 02:49:28 Done.
+ ' PRESUBMIT check.'))
+
+ return results
+
def _CheckNoInterfacesInBase(input_api, output_api):
"""Checks to make sure no files in libbase.a have |@interface|."""
@@ -214,6 +318,7 @@ def _CheckNoNewOldCallback(input_api, output_api):
def _CommonChecks(input_api, output_api):
"""Checks common to both upload and commit."""
results = []
+ results.extend(_CheckJavaScriptStyle(input_api, output_api))
results.extend(input_api.canned_checks.PanProjectChecks(
input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
results.extend(_CheckNoInterfacesInBase(input_api, output_api))
@@ -340,6 +445,7 @@ def GetPreferredTrySlaves(project, change):
return ['mac_rel']
preferred = ['win_rel', 'linux_rel', 'mac_rel']
aura_re = '_aura[^/]*[.][^/]*'
- if any(re.search(aura_re, f.LocalPath()) for f in change.AffectedFiles()):
+ if any(input_api.re.search(aura_re, f.LocalPath())
Dan Beam 2012/02/07 09:08:00 Where exactly are you getting this input_api varia
M-A Ruel 2012/02/07 14:16:04 It's not Tyler's fault. Imports are slow in python
+ for f in change.AffectedFiles()):
preferred.append('linux_chromeos')
return preferred
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698