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

Side by Side Diff: chrome/browser/resources/web_dev_style/js_checker.py

Issue 9288045: PRESUBMIT check for JavaScript style errors (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: exclude options_bundle.js 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/resources/PRESUBMIT.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 """Presubmit script for Chromium JS resources.
6
7 See chrome/browser/resources/PRESUBMIT.py
8 """
9
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
15
16 def RunChecks(self):
17 """Check for violations of the Chromium JavaScript style guide. See
18 http://chromium.org/developers/web-development-style-guide#TOC-JavaScript
19 """
20
21 import sys
22 old_path = sys.path
23
24 try:
25 closure_linter_path = self.input_api.os_path.join(
26 self.input_api.change.RepositoryRoot(),
27 "third_party",
28 "closure_linter")
29 gflags_path = self.input_api.os_path.join(
30 self.input_api.change.RepositoryRoot(),
31 "third_party",
32 "python_gflags")
33
34 sys.path.insert(0, closure_linter_path)
35 sys.path.insert(0, gflags_path)
36
37 from closure_linter import checker, errors
38 from closure_linter.common import errorhandler
39
40 finally:
41 sys.path = old_path
42
43 class ErrorHandlerImpl(errorhandler.ErrorHandler):
44 """Filters out errors that don't apply to Chromium JavaScript code."""
45
46 def __init__(self):
47 self._errors = []
48
49 def HandleFile(self, filename, first_token):
50 self._filename = filename
51
52 def HandleError(self, error):
53 if (self._valid(error)):
54 error.filename = self._filename
55 self._errors.append(error)
56
57 def GetErrors(self):
58 return self._errors
59
60 def HasErrors(self):
61 return bool(self._errors)
62
63 def _valid(self, error):
64 """Check whether an error is valid. Most errors are valid, with a few
65 exceptions which are listed here.
66 """
67
68 return error.code not in [
69 errors.COMMA_AT_END_OF_LITERAL,
70 errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE,
71 errors.JSDOC_TAG_DESCRIPTION_ENDS_WITH_INVALID_CHARACTER,
72 ]
73
74 results = []
75
76 affected_files = self.input_api.change.AffectedFiles(
77 file_filter=self.file_filter)
78 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'),
79 affected_files)
80 for f in affected_js_files:
81 # Skip options_bundle.js since it's just a bunch of <include>s
82 if self.input_api.os_path.split(f.LocalPath())[1] == 'options_bundle.js':
83 continue
84
85 error_lines = []
86
87 # check for getElementById()
88 for i, line in enumerate(f.NewContents(), start=1):
89 if 'getElementById' in line:
90 error_lines.append(' line %d: %s\n%s' % (
91 i,
92 "Use $('id') instead of document.getElementById('id')",
93 line))
94
95 # Use closure_linter to check for several different errors
96 error_handler = ErrorHandlerImpl()
97 js_checker = checker.JavaScriptStyleChecker(error_handler)
98 js_checker.Check(self.input_api.os_path.join(
99 self.input_api.change.RepositoryRoot(),
100 f.LocalPath()))
101
102 for error in error_handler.GetErrors():
103 error_msg = ' line %d: E%04d: %s\n%s' % (
104 error.token.line_number,
105 error.code,
106 error.message,
107 error.token.line)
108 error_lines.append(error_msg)
109
110 if error_lines:
111 error_lines = [
112 'Found JavaScript style violations in %s:' %
113 f.LocalPath()] + error_lines
114 results.append(self.output_api.PresubmitError('\n'.join(error_lines)))
115
116 if results:
117 results.append(self.output_api.PresubmitNotifyResult(
118 'See the JavaScript style guide at '
119 'http://www.chromium.org/developers/web-development-style-guide'
120 '#TOC-JavaScript and if you have any feedback about the JavaScript '
121 'PRESUBMIT check, contact tbreisacher@chromium.org'))
122
123 return results
OLDNEW
« no previous file with comments | « chrome/browser/resources/PRESUBMIT.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698