Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 WebUI 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/git cl, and see | |
| 9 http://www.chromium.org/developers/web-development-style-guide for the rules | |
| 10 we're checking against here. | |
| 11 """ | |
| 12 | |
| 13 # TODO(dbeam): Real CSS parser? pycss? http://code.google.com/p/pycss/ | |
| 14 | |
| 15 class CSSChecker(object): | |
| 16 def __init__(self, input_api, output_api, file_filter=None): | |
| 17 self.input_api = input_api | |
| 18 self.output_api = output_api | |
| 19 self.file_filter = file_filter | |
| 20 | |
| 21 def RunChecks(self): | |
| 22 # We use this a lot, so make a nick name variable. | |
| 23 re = self.input_api.re | |
| 24 | |
| 25 def _collapseable_hex(s): | |
| 26 return (len(s) == 6 and s[0] == s[1] and s[2] == s[3] and s[4] == s[5]) | |
| 27 | |
| 28 def _is_gray(s): | |
| 29 return s[0] == s[1] == s[2] if len(s) == 3 else s[0:2] == s[2:4] == s[4:6] | |
| 30 | |
| 31 def _remove_ats(s): | |
| 32 return re.sub(re.compile(r'@\w+.*?{(.*{.*?})+.*?}', re.DOTALL), '\\1', s) | |
| 33 | |
| 34 def _remove_comments(s): | |
| 35 return re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', s) | |
| 36 | |
| 37 def _rgb_from_hex(s): | |
| 38 if len(s) == 3: | |
| 39 r, g, b = s[0] + s[0], s[1] + s[1], s[2] + s[2] | |
| 40 else: | |
| 41 r, g, b = s[0:2], s[2:4], s[4:6] | |
| 42 return int(r, base=16), int(g, base=16), int(b, base=16) | |
| 43 | |
| 44 def alphabetize_props(file): | |
| 45 errors = [] | |
| 46 for rule in re.finditer(r'{(.*?)}', file, re.DOTALL): | |
| 47 rules = filter(lambda r: r.find(': ') >= 0, | |
| 48 map(lambda t: t.strip(), rule.group(1).split(';'))[0:-1]) | |
| 49 props = map(lambda r: r[0:r.find(':')], rules) | |
| 50 if props != sorted(props): | |
| 51 errors.append(' %s;\n' % (';\n '.join(rules))) | |
| 52 return errors | |
| 53 | |
| 54 def braces_have_space_before_and_nothing_after(line): | |
| 55 return re.search(r'(?:^|\S){|{\s*\S+\s*$', line) | |
| 56 | |
| 57 def classes_use_dashes(line): | |
| 58 # Intentionally dumbed down version of CSS 2.1 grammar for class without | |
| 59 # non-ASCII, escape chars, or whitespace. | |
| 60 m = re.search(r'\.(-?[_a-zA-Z0-9-]+).*[,{]\s*$', line) | |
| 61 return (m and (m.group(1).lower() != m.group(1) or | |
| 62 m.group(1).find('_') >= 0)) | |
| 63 | |
| 64 def close_brace_on_new_line(line): | |
| 65 return (line.find('}') >= 0 and re.search(r'[^ }]', line)) | |
| 66 | |
| 67 def colons_have_space_after(line): | |
| 68 return re.search(r':(?!\/\/)\S(?!.*[{,]\s*$)', line) | |
| 69 | |
| 70 def favor_single_quotes(lines): | |
| 71 return line.find('"') >= 0 | |
|
Tyler Breisacher (Chromium)
2012/02/15 01:22:41
the argument is "lines" but then you use "line" in
Dan Beam
2012/02/15 01:39:53
Thank you for this catch. line existed in the pre
| |
| 72 | |
| 73 # Shared between hex_could_be_shorter and rgb_if_not_gray. | |
| 74 hex_reg = (r'#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})(?=[^_a-zA-Z0-9-]|$)' | |
| 75 r'(?!.*(?:{.*|,\s*)$)') | |
| 76 def hex_could_be_shorter(line): | |
| 77 m = re.search(hex_reg, line) | |
| 78 return (m and _collapseable_hex(m.group(1))) | |
| 79 | |
| 80 small_seconds = r'(?:^|[^_a-zA-Z0-9-])(0?\.[0-9]+)s(?!-?[_a-zA-Z0-9-])' | |
| 81 def milliseconds_for_small_times(line): | |
| 82 return re.search(small_seconds, line) | |
| 83 | |
| 84 def one_rule_per_line(line): | |
| 85 return re.search('(.*:(?!\/\/)){2,}(?!.*[,{]\s*$)', line) | |
| 86 | |
| 87 any_reg = re.compile(r':(?:-webkit-)?any\(.*?\)', re.DOTALL) | |
| 88 multi_sels = re.compile(r'(?:}[\n\s]*)?([^,]+,(?=[^{}]+?{).*[,{])\s*$', | |
| 89 re.MULTILINE) | |
| 90 def one_selector_per_line(file): | |
| 91 errors = [] | |
| 92 for b in re.finditer(multi_sels, re.sub(any_reg, '', file)): | |
| 93 errors.append(' ' + b.group(1).strip()) | |
| 94 return errors | |
| 95 | |
| 96 def rgb_if_not_gray(line): | |
| 97 m = re.search(hex_reg, line) | |
| 98 return (m and not _is_gray(m.group(1))) | |
| 99 | |
| 100 def suggest_ms_from_s(line): | |
| 101 ms = int(float(re.search(small_seconds, line).group(1)) * 1000) | |
| 102 return ' (replace with %dms)' % ms | |
| 103 | |
| 104 def suggest_rgb_from_hex(line): | |
| 105 suggestions = ['rgb(%d, %d, %d)' % _rgb_from_hex(h.group(1)) | |
| 106 for h in re.finditer(hex_reg, line)] | |
| 107 return ' (replace with %s)' % ', '.join(suggestions) | |
| 108 | |
| 109 def suggest_short_hex(line): | |
| 110 h = re.search(hex_reg, line).group(1) | |
| 111 return ' (replace with #%s)' % (h[0] + h[2] + h[4]) | |
| 112 | |
| 113 hsl = r'hsl\([^\)]*(?:[, ]|(?<=\())(?:0?\.?)?0%' | |
| 114 zeros = (r'[^0-9]0(?:\.0?)?' | |
| 115 r'(?:px|em|%|in|cm|mm|pc|pt|ex|deg|g?rad|m?s|k?hz)' | |
| 116 r'(?!\s*\{)') | |
| 117 def zero_length_values(line): | |
| 118 return (re.search(zeros, line) and not re.search(hsl, line)) | |
| 119 | |
| 120 added_or_modified_files_checks = [ | |
| 121 { 'desc': 'Alphabetize properties and list vendor specific (i.e. ' | |
| 122 '-webkit) above standard.', | |
| 123 'test': alphabetize_props, | |
| 124 'multiline': True, | |
| 125 }, | |
| 126 { 'desc': 'Start braces ({) end a selector, have a space before them ' | |
| 127 'and no rules after.', | |
| 128 'test': braces_have_space_before_and_nothing_after, | |
| 129 }, | |
| 130 { 'desc': 'Classes use .dash-form.', | |
| 131 'test': classes_use_dashes, | |
| 132 }, | |
| 133 { 'desc': 'Always put a rule closing brace (}) on a new line.', | |
| 134 'test': close_brace_on_new_line, | |
| 135 }, | |
| 136 { 'desc': 'Colons (:) should have a space after them.', | |
| 137 'test': colons_have_space_after, | |
| 138 }, | |
| 139 { 'desc': 'Use single quotes (\') instead of double quotes (") in ' | |
| 140 'strings.', | |
| 141 'test': favor_single_quotes, | |
| 142 }, | |
| 143 { 'desc': 'Use abbreviated hex (#rgb) when in form #rrggbb.', | |
| 144 'test': hex_could_be_shorter, | |
| 145 'after': suggest_short_hex, | |
| 146 }, | |
| 147 { 'desc': 'Use milliseconds for time measurements under 1 second.', | |
| 148 'test': milliseconds_for_small_times, | |
| 149 'after': suggest_ms_from_s, | |
| 150 }, | |
| 151 { 'desc': 'One rule per line (what not to do: color: red; margin: 0;).', | |
| 152 'test': one_rule_per_line, | |
| 153 }, | |
| 154 { 'desc': 'One selector per line (what not to do: a, b {}).', | |
| 155 'test': one_selector_per_line, | |
| 156 'multiline': True, | |
| 157 }, | |
| 158 { 'desc': 'Use rgb() over #hex when not a shade of gray (like #333).', | |
| 159 'test': rgb_if_not_gray, | |
| 160 'after': suggest_rgb_from_hex, | |
| 161 }, | |
| 162 { 'desc': 'Make all zero length terms (i.e. 0px) 0 unless inside of ' | |
| 163 'hsl() or part of @keyframe.', | |
| 164 'test': zero_length_values, | |
| 165 }, | |
| 166 ] | |
| 167 | |
| 168 results = [] | |
| 169 affected_files = self.input_api.AffectedFiles(include_deletes=False, | |
| 170 file_filter=self.file_filter) | |
| 171 files = [] | |
| 172 for f in affected_files: | |
| 173 # Remove all /*comments*/ and @at-keywords; we're not using a real parser. | |
| 174 file_contents = _remove_ats(_remove_comments('\n'.join(f.NewContents()))) | |
| 175 files.append((f.LocalPath(), file_contents)) | |
| 176 | |
| 177 # Only look at CSS files for now. | |
| 178 for f in filter(lambda f: f[0].endswith('.css'), files): | |
|
Tyler Breisacher (Chromium)
2012/02/15 01:22:41
I think you've already filtered this to check only
Dan Beam
2012/02/15 01:39:53
Yeah, I shouldn't be, so I added .js and .html to
| |
| 179 file_errors = [] | |
| 180 for check in added_or_modified_files_checks: | |
| 181 # If the check is multiline, it receieves the whole file and gives us | |
| 182 # back a list of things wrong. If the check isn't multiline, we pass it | |
| 183 # each line and the check returns something truthy if there's an issue. | |
| 184 if ('multiline' in check and check['multiline']): | |
| 185 check_errors = check['test'](f[1]) | |
| 186 if len(check_errors) > 0: | |
| 187 # There are currently no multiline checks with ['after']. | |
| 188 file_errors.append('- %s\n%s' % | |
| 189 (check['desc'], '\n'.join(check_errors).rstrip())) | |
| 190 else: | |
| 191 check_errors = [] | |
| 192 lines = f[1].splitlines() | |
| 193 for lnum in range(0, len(lines)): | |
| 194 line = lines[lnum] | |
| 195 if check['test'](line): | |
| 196 error = ' ' + line.strip() | |
| 197 if 'after' in check: | |
| 198 error += check['after'](line) | |
| 199 check_errors.append(error) | |
| 200 if len(check_errors) > 0: | |
| 201 file_errors.append('- %s\n%s' % | |
| 202 (check['desc'], '\n'.join(check_errors))) | |
| 203 if len(file_errors) > 0: | |
| 204 results.append(self.output_api.PresubmitNotifyResult( | |
| 205 '%s:\n%s' % (f[0], '\n\n'.join(file_errors)))) | |
| 206 | |
| 207 return results | |
| OLD | NEW |