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

Side by Side 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: fixes from Dan 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
« PRESUBMIT.py ('K') | « 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 HTML/CSS/JS resources.
6
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
9 """
10
11
12 def _CheckJavaScriptStyle(input_api, output_api):
13 """Check for violations of the Chromium JavaScript style guide. See
14 http://www.chromium.org/developers/web-development-style-guide#TOC-JavaScri pt
M-A Ruel 2012/02/08 14:30:39 if you remove www., it fits 80 cols. :)
Tyler Breisacher (Chromium) 2012/02/08 18:43:41 Done.
15 """
16
17 import closure_linter.common.errorhandler
18
19 class ErrorHandlerImpl(closure_linter.common.errorhandler.ErrorHandler):
20 """Implementation of ErrorHandler that collects all errors except those
M-A Ruel 2012/02/08 14:30:39 I think the following conveys enough information i
Tyler Breisacher (Chromium) 2012/02/08 18:43:41 Done.
21 that don't apply for Chromium JavaScript code.
22 """
23
24 def __init__(self):
25 self._errors = []
26
27 def HandleFile(self, filename, first_token):
28 self._filename = filename
29
30 def HandleError(self, error):
31 if (self._valid(error)):
32 error.filename = self._filename
33 self._errors.append(error)
34
35 def GetErrors(self):
36 return self._errors
37
38 def HasErrors(self):
39 return bool(self._errors)
40
41 def _valid(self, error):
42 """Check whether an error is valid. Most errors are valid, with a few
43 exceptions which are listed here.
44 """
45
46 import closure_linter.errors
M-A Ruel 2012/02/08 14:30:39 Move the import at function level, otherwise this
Tyler Breisacher (Chromium) 2012/02/08 18:43:41 Done.
47
48 return error.code not in [
49 closure_linter.errors.COMMA_AT_END_OF_LITERAL,
50 closure_linter.errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE,
51 closure_linter.errors.
52 JSDOC_TAG_DESCRIPTION_ENDS_WITH_INVALID_CHARACTER,
53 ]
54
55 import closure_linter.checker
M-A Ruel 2012/02/08 14:30:39 Keep all the imports together around line 17
Tyler Breisacher (Chromium) 2012/02/08 18:43:41 Done.
56
57 # Only check the following folders. OWNERS of folders containing JavaScript
58 # code can opt-in to this check by adding the folder here.
59 join = input_api.os_path.join
60 checked_folders = [
61 join('chrome', 'browser', 'resources', 'ntp4'),
M-A Ruel 2012/02/08 14:30:39 Note that you are already in chrome/browser/resour
Tyler Breisacher (Chromium) 2012/02/08 18:43:41 True, but LocalPath() is relative to src/ so it st
M-A Ruel 2012/02/08 18:54:54 Oh that's annoying that the local path isn't rebas
62 join('chrome', 'browser', 'resources', 'options2'),
63 ]
64
65 def in_checked_folder(affected_file):
66 return any(affected_file.LocalPath().startswith(cf)
67 for cf in checked_folders)
68
69 def js_or_html(affected_file):
70 return input_api.re.search('\.(js|html?)$', affected_file.LocalPath())
71
72 def file_filter(affected_file):
73 return js_or_html(affected_file) and in_checked_folder(affected_file)
74
75 results = []
76
77 for f in input_api.change.AffectedFiles(file_filter=file_filter):
78 error_lines = []
79
80 # check for getElementById()
81 for i, line in enumerate(f.NewContents(), start=1):
82 if 'getElementById' in line:
83 error_lines.append(' line %d: %s\n%s' % (
84 i,
85 "Use $('id') instead of document.getElementById('id')",
86 line))
87
88 if input_api.re.search(r'\bconst\b', line):
89 error_lines.append(' line %d: %s\n%s' % (
90 i,
91 'Use |var| instead of |const|. See http://crbug.com/80149',
92 line))
93
94 # Use closure_linter to check for several different errors
95 error_handler = ErrorHandlerImpl()
96 checker = closure_linter.checker.JavaScriptStyleChecker(error_handler)
97 checker.Check(join(input_api.change.RepositoryRoot(), f.LocalPath()))
98
99 for error in error_handler.GetErrors():
100 errorMsg = ' line %d: E%04d: %s\n%s' % (
101 error.token.line_number,
102 error.code,
103 error.message,
104 error.token.line)
105 error_lines.append(errorMsg)
106
107 if error_lines:
108 error_lines = [
109 'Found JavaScript style violations in %s:' %
110 f.LocalPath()] + error_lines
111 results.append(output_api.PresubmitError('\n'.join(error_lines)))
112
113 if results:
114 results.append(output_api.PresubmitNotifyResult(
115 'See the JavaScript style guide at '
116 'http://www.chromium.org/developers/web-development-style-guide#TOC-Java Script'
M-A Ruel 2012/02/08 14:30:39 you can split the line on two lines; 'http://www.c
Tyler Breisacher (Chromium) 2012/02/08 18:43:41 Done.
117 ' and if you have any feedback about the JavaScript PRESUBMIT check,'
118 ' contact tbreisacher@chromium.org'))
119
120 return results
121
122 def _CommonChecks(input_api, output_api):
M-A Ruel 2012/02/08 14:30:39 optional: This is probably overweight. I'd just ca
Tyler Breisacher (Chromium) 2012/02/08 18:43:41 I was thinking Dan's CSS checking code would get m
123 """Checks common to both upload and commit."""
124 results = []
125 results.extend(_CheckJavaScriptStyle(input_api, output_api))
126 return results
127
128 def CheckChangeOnUpload(input_api, output_api):
129 results = []
130 results.extend(_CommonChecks(input_api, output_api))
131 return results
132
133 def CheckChangeOnCommit(input_api, output_api):
134 results = []
135 results.extend(_CommonChecks(input_api, output_api))
136 return results
137
OLDNEW
« PRESUBMIT.py ('K') | « PRESUBMIT.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698