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

Unified Diff: chrome/browser/resources/PRESUBMIT.py

Issue 9288045: PRESUBMIT check for JavaScript style errors (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use endswith instead of regex 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: chrome/browser/resources/PRESUBMIT.py
diff --git a/chrome/browser/resources/PRESUBMIT.py b/chrome/browser/resources/PRESUBMIT.py
new file mode 100644
index 0000000000000000000000000000000000000000..4470f436e07c619a1c5635aa6f1cbdb711ddaf88
--- /dev/null
+++ b/chrome/browser/resources/PRESUBMIT.py
@@ -0,0 +1,125 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Presubmit script for Chromium HTML/CSS/JS resources.
+
+See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
+for more details about the presubmit API built into gcl.
+"""
+
+
+def _CheckJavaScriptStyle(input_api, output_api):
+ """Check for violations of the Chromium JavaScript style guide. See
+ http://chromium.org/developers/web-development-style-guide#TOC-JavaScript
+ """
+
+ import closure_linter.checker
+ import closure_linter.common.errorhandler
+ 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.
+
+ class ErrorHandlerImpl(closure_linter.common.errorhandler.ErrorHandler):
+ """Filters out errors that don't apply to Chromium JavaScript code."""
+
+ def __init__(self):
+ 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
+
+ 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.
+ """
+
+ 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,
+ ]
+
+ # 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 in_checked_folder(affected_file):
+ return any(affected_file.LocalPath().startswith(cf)
+ for cf in checked_folders)
+
+ def js_or_html(affected_file):
+ return affected_file.LocalPath().endswith(('.js', '.html'))
+
+ def file_filter(affected_file):
+ return js_or_html(affected_file) and in_checked_folder(affected_file)
+
+ results = []
+
+ for f in input_api.change.AffectedFiles(file_filter=file_filter):
+ error_lines = []
+
+ # check for getElementById()
+ for i, line in enumerate(f.NewContents(), start=1):
+ if 'getElementById' in line:
+ error_lines.append(' line %d: %s\n%s' % (
+ i,
+ "Use $('id') instead of document.getElementById('id')",
+ line))
+
+ if input_api.re.search(r'\bconst\b', line):
+ error_lines.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(join(input_api.change.RepositoryRoot(), f.LocalPath()))
+
+ for error in error_handler.GetErrors():
+ 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.
+ error.token.line_number,
+ error.code,
+ error.message,
+ error.token.line)
+ error_lines.append(errorMsg)
+
+ if error_lines:
+ error_lines = [
+ 'Found JavaScript style violations in %s:' %
+ f.LocalPath()] + error_lines
+ results.append(output_api.PresubmitError('\n'.join(error_lines)))
+
+ if results:
+ results.append(output_api.PresubmitNotifyResult(
+ 'See the JavaScript style guide at'
+ ' 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.
+ '#TOC-JavaScript and if you have any feedback about the JavaScript'
+ ' PRESUBMIT check, contact tbreisacher@chromium.org'))
+
+ return results
+
+
+def CheckChangeOnUpload(input_api, output_api):
+ return _CheckJavaScriptStyle(input_api, output_api)
+
+
+def CheckChangeOnCommit(input_api, output_api):
+ return _CheckJavaScriptStyle(input_api, output_api)
+
« 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