OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Presubmit script for Chromium JS resources. | 5 """Presubmit script for Chromium JS resources. |
6 | 6 |
7 See chrome/browser/PRESUBMIT.py | 7 See chrome/browser/PRESUBMIT.py |
8 """ | 8 """ |
9 | 9 |
10 import regex_check | 10 import regex_check |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 def InheritDocCheck(self, i, line): | 61 def InheritDocCheck(self, i, line): |
62 """Checks for use of '@inheritDoc' instead of '@override'.""" | 62 """Checks for use of '@inheritDoc' instead of '@override'.""" |
63 return self.RegexCheck(i, line, r"\* (@inheritDoc)", | 63 return self.RegexCheck(i, line, r"\* (@inheritDoc)", |
64 "@inheritDoc is deprecated, use @override instead") | 64 "@inheritDoc is deprecated, use @override instead") |
65 | 65 |
66 def PolymerLocalIdCheck(self, i, line): | 66 def PolymerLocalIdCheck(self, i, line): |
67 """Checks for use of element.$.localId.""" | 67 """Checks for use of element.$.localId.""" |
68 return self.RegexCheck(i, line, r"(?<!this)(\.\$)[\[\.]", | 68 return self.RegexCheck(i, line, r"(?<!this)(\.\$)[\[\.]", |
69 "Please only use this.$.localId, not element.$.localId") | 69 "Please only use this.$.localId, not element.$.localId") |
70 | 70 |
71 def RunEsLintChecks(self, affected_js_files): | |
72 """Runs lint checks using ESLint. The ESLint rules being applied are defined | |
73 in the .eslintrc.js configuration file. | |
74 """ | |
75 os_path = self.input_api.os_path | |
76 | |
77 try: | |
78 # Import ESLint. | |
79 _HERE_PATH = os_path.dirname(os_path.realpath(__file__)) | |
Dan Beam
2017/05/25 01:01:20
why do you need to call realpath?
dpapad
2017/05/25 01:11:56
I might not need to, not sure if calling realpath(
| |
80 _SRC_PATH = os_path.normpath(os_path.join(_HERE_PATH, '..', '..')) | |
81 import sys | |
82 old_sys_path = sys.path[:] | |
83 sys.path.append(os_path.join(_SRC_PATH, 'third_party', 'node')) | |
84 import node, node_modules | |
85 finally: | |
86 sys.path = old_sys_path | |
87 | |
88 # Extract paths to be passed to ESLint. | |
89 affected_js_files_paths = [] | |
90 presubmit_path = self.input_api.PresubmitLocalPath() | |
91 for f in affected_js_files: | |
92 affected_js_files_paths.append( | |
93 os_path.relpath(f.AbsoluteLocalPath(), presubmit_path)) | |
94 | |
95 output = node.RunNode([ | |
96 node_modules.PathToEsLint(), | |
97 '--color', | |
98 '--ignore-pattern \'!.eslintrc.js\'', | |
99 ' '.join(affected_js_files_paths)]) | |
100 | |
101 return [self.output_api.PresubmitError(output)] if output else [] | |
102 | |
71 def WrapperTypeCheck(self, i, line): | 103 def WrapperTypeCheck(self, i, line): |
72 """Check for wrappers (new String()) instead of builtins (string).""" | 104 """Check for wrappers (new String()) instead of builtins (string).""" |
73 return self.RegexCheck(i, line, | 105 return self.RegexCheck(i, line, |
74 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type | 106 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type |
75 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)} | 107 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)} |
76 "Don't use wrapper types (i.e. new String() or @type {String})") | 108 "Don't use wrapper types (i.e. new String() or @type {String})") |
77 | 109 |
78 def VarNameCheck(self, i, line): | 110 def VarNameCheck(self, i, line): |
79 """See the style guide. http://goo.gl/eQiXVW""" | 111 """See the style guide. http://goo.gl/eQiXVW""" |
80 return self.RegexCheck(i, line, | 112 return self.RegexCheck(i, line, |
81 r"var (?!g_\w+)(_?[a-z][a-zA-Z]*[_$][\w_$]*)(?<! \$)", | 113 r"var (?!g_\w+)(_?[a-z][a-zA-Z]*[_$][\w_$]*)(?<! \$)", |
82 "Please use var namesLikeThis <https://goo.gl/eQiXVW>") | 114 "Please use var namesLikeThis <https://goo.gl/eQiXVW>") |
83 | 115 |
84 def _GetErrorHighlight(self, start, length): | 116 def _GetErrorHighlight(self, start, length): |
85 """Takes a start position and a length, and produces a row of '^'s to | 117 """Takes a start position and a length, and produces a row of '^'s to |
86 highlight the corresponding part of a string. | 118 highlight the corresponding part of a string. |
87 """ | 119 """ |
88 return start * ' ' + length * '^' | 120 return start * ' ' + length * '^' |
89 | 121 |
90 def RunChecks(self): | 122 def RunChecks(self): |
91 """Check for violations of the Chromium JavaScript style guide. See | 123 """Check for violations of the Chromium JavaScript style guide. See |
92 https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/we b.md#JavaScript | 124 https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/we b.md#JavaScript |
93 """ | 125 """ |
94 results = [] | 126 results = [] |
95 | 127 |
96 affected_files = self.input_api.AffectedFiles(file_filter=self.file_filter, | 128 affected_files = self.input_api.AffectedFiles(file_filter=self.file_filter, |
97 include_deletes=False) | 129 include_deletes=False) |
98 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), | 130 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), |
99 affected_files) | 131 affected_files) |
132 | |
133 if affected_js_files: | |
134 results += self.RunEsLintChecks(affected_js_files) | |
135 | |
100 for f in affected_js_files: | 136 for f in affected_js_files: |
101 error_lines = [] | 137 error_lines = [] |
102 | 138 |
103 for i, line in enumerate(f.NewContents(), start=1): | 139 for i, line in enumerate(f.NewContents(), start=1): |
104 error_lines += filter(None, [ | 140 error_lines += filter(None, [ |
105 self.ChromeSendCheck(i, line), | 141 self.ChromeSendCheck(i, line), |
106 self.CommentIfAndIncludeCheck(i, line), | 142 self.CommentIfAndIncludeCheck(i, line), |
107 self.ConstCheck(i, line), | 143 self.ConstCheck(i, line), |
108 self.GetElementByIdCheck(i, line), | 144 self.GetElementByIdCheck(i, line), |
109 self.EndJsDocCommentCheck(i, line), | 145 self.EndJsDocCommentCheck(i, line), |
110 self.ExtraDotInGenericCheck(i, line), | 146 self.ExtraDotInGenericCheck(i, line), |
111 self.InheritDocCheck(i, line), | 147 self.InheritDocCheck(i, line), |
112 self.PolymerLocalIdCheck(i, line), | 148 self.PolymerLocalIdCheck(i, line), |
113 self.WrapperTypeCheck(i, line), | 149 self.WrapperTypeCheck(i, line), |
114 self.VarNameCheck(i, line), | 150 self.VarNameCheck(i, line), |
115 ]) | 151 ]) |
116 | 152 |
117 if error_lines: | 153 if error_lines: |
118 error_lines = [ | 154 error_lines = [ |
119 'Found JavaScript style violations in %s:' % | 155 'Found JavaScript style violations in %s:' % |
120 f.LocalPath()] + error_lines | 156 f.LocalPath()] + error_lines |
121 results.append(self.output_api.PresubmitError('\n'.join(error_lines))) | 157 results.append(self.output_api.PresubmitError('\n'.join(error_lines))) |
122 | 158 |
123 if results: | 159 if results: |
124 results.append(self.output_api.PresubmitNotifyResult( | 160 results.append(self.output_api.PresubmitNotifyResult( |
125 'See the JavaScript style guide at ' | 161 'See the JavaScript style guide at ' |
126 'https://chromium.googlesource.com/chromium/src/+/master/styleguide/we b/web.md#JavaScript')) | 162 'https://chromium.googlesource.com/chromium/src/+/master/styleguide/we b/web.md#JavaScript')) |
127 | 163 |
128 return results | 164 return results |
OLD | NEW |