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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 def InheritDocCheck(self, i, line): | 55 def InheritDocCheck(self, i, line): |
56 """Checks for use of '@inheritDoc' instead of '@override'.""" | 56 """Checks for use of '@inheritDoc' instead of '@override'.""" |
57 return self.RegexCheck(i, line, r"\* (@inheritDoc)", | 57 return self.RegexCheck(i, line, r"\* (@inheritDoc)", |
58 "@inheritDoc is deprecated, use @override instead") | 58 "@inheritDoc is deprecated, use @override instead") |
59 | 59 |
60 def PolymerLocalIdCheck(self, i, line): | 60 def PolymerLocalIdCheck(self, i, line): |
61 """Checks for use of element.$.localId.""" | 61 """Checks for use of element.$.localId.""" |
62 return self.RegexCheck(i, line, r"(?<!this)(\.\$)[\[\.]", | 62 return self.RegexCheck(i, line, r"(?<!this)(\.\$)[\[\.]", |
63 "Please only use this.$.localId, not element.$.localId") | 63 "Please only use this.$.localId, not element.$.localId") |
64 | 64 |
65 def RunEsLintChecks(self, affected_js_files): | 65 def RunEsLintChecks(self, affected_js_files_paths, format='stylish'): |
66 """Runs lint checks using ESLint. The ESLint rules being applied are defined | 66 """Runs lint checks using ESLint. The ESLint rules being applied are defined |
67 in the .eslintrc.js configuration file. | 67 in the .eslintrc.js configuration file. |
68 """ | 68 """ |
69 os_path = self.input_api.os_path | |
70 | |
71 try: | 69 try: |
72 # Import ESLint. | 70 # Import ESLint. |
71 os_path = self.input_api.os_path | |
73 _HERE_PATH = os_path.dirname(os_path.realpath(__file__)) | 72 _HERE_PATH = os_path.dirname(os_path.realpath(__file__)) |
74 _SRC_PATH = os_path.normpath(os_path.join(_HERE_PATH, '..', '..')) | 73 _SRC_PATH = os_path.normpath(os_path.join(_HERE_PATH, '..', '..')) |
75 import sys | 74 import sys |
76 old_sys_path = sys.path[:] | 75 old_sys_path = sys.path[:] |
77 sys.path.append(os_path.join(_SRC_PATH, 'third_party', 'node')) | 76 sys.path.append(os_path.join(_SRC_PATH, 'third_party', 'node')) |
78 import node, node_modules | 77 import node, node_modules |
79 finally: | 78 finally: |
80 sys.path = old_sys_path | 79 sys.path = old_sys_path |
81 | 80 |
82 # Extract paths to be passed to ESLint. | |
83 affected_js_files_paths = [] | |
84 presubmit_path = self.input_api.PresubmitLocalPath() | |
85 for f in affected_js_files: | |
86 affected_js_files_paths.append( | |
87 os_path.relpath(f.AbsoluteLocalPath(), presubmit_path)) | |
88 | |
89 output = node.RunNode([ | 81 output = node.RunNode([ |
90 node_modules.PathToEsLint(), | 82 node_modules.PathToEsLint(), |
91 '--color', | 83 '--color', |
84 '--format', format, | |
92 '--ignore-pattern \'!.eslintrc.js\'', | 85 '--ignore-pattern \'!.eslintrc.js\'', |
93 ' '.join(affected_js_files_paths)]) | 86 ' '.join(affected_js_files_paths)]) |
94 | 87 |
95 return [self.output_api.PresubmitError(output)] if output else [] | 88 return [self.output_api.PresubmitError(output)] if output else [] |
96 | 89 |
97 def WrapperTypeCheck(self, i, line): | 90 def WrapperTypeCheck(self, i, line): |
98 """Check for wrappers (new String()) instead of builtins (string).""" | 91 """Check for wrappers (new String()) instead of builtins (string).""" |
99 return self.RegexCheck(i, line, | 92 return self.RegexCheck(i, line, |
100 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type | 93 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type |
101 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)} | 94 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)} |
(...skipping 15 matching lines...) Expand all Loading... | |
117 """Check for violations of the Chromium JavaScript style guide. See | 110 """Check for violations of the Chromium JavaScript style guide. See |
118 https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/we b.md#JavaScript | 111 https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/we b.md#JavaScript |
119 """ | 112 """ |
120 results = [] | 113 results = [] |
121 | 114 |
122 affected_files = self.input_api.AffectedFiles(file_filter=self.file_filter, | 115 affected_files = self.input_api.AffectedFiles(file_filter=self.file_filter, |
123 include_deletes=False) | 116 include_deletes=False) |
124 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), | 117 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), |
125 affected_files) | 118 affected_files) |
126 | 119 |
120 # Extract paths to be passed to ESLint. | |
121 def get_path(f): | |
122 return os_path.relpath( | |
123 f.AbsoluteLocalPath(), self.input_api.PresubmitLocalPath()) | |
124 | |
127 if affected_js_files: | 125 if affected_js_files: |
128 results += self.RunEsLintChecks(affected_js_files) | 126 affected_js_files_paths = map(get_path, affected_js_files) |
127 results += self.RunEsLintChecks(affected_js_files_paths) | |
dpapad
2017/05/30 22:11:30
I had to change the signature of RunEsLintChecks,
Dan Beam
2017/05/30 23:11:08
wait, why doesn't this work?
https://cs.chromium.o
dpapad
2017/05/31 01:30:06
Updated the code to use a MockFile().
| |
129 | 128 |
130 for f in affected_js_files: | 129 for f in affected_js_files: |
131 error_lines = [] | 130 error_lines = [] |
132 | 131 |
133 for i, line in enumerate(f.NewContents(), start=1): | 132 for i, line in enumerate(f.NewContents(), start=1): |
134 error_lines += filter(None, [ | 133 error_lines += filter(None, [ |
135 self.ChromeSendCheck(i, line), | 134 self.ChromeSendCheck(i, line), |
136 self.CommentIfAndIncludeCheck(i, line), | 135 self.CommentIfAndIncludeCheck(i, line), |
137 self.ConstCheck(i, line), | 136 self.ConstCheck(i, line), |
138 self.EndJsDocCommentCheck(i, line), | 137 self.EndJsDocCommentCheck(i, line), |
139 self.ExtraDotInGenericCheck(i, line), | 138 self.ExtraDotInGenericCheck(i, line), |
140 self.InheritDocCheck(i, line), | 139 self.InheritDocCheck(i, line), |
141 self.PolymerLocalIdCheck(i, line), | 140 self.PolymerLocalIdCheck(i, line), |
142 self.WrapperTypeCheck(i, line), | 141 self.WrapperTypeCheck(i, line), |
143 self.VarNameCheck(i, line), | 142 self.VarNameCheck(i, line), |
144 ]) | 143 ]) |
145 | 144 |
146 if error_lines: | 145 if error_lines: |
147 error_lines = [ | 146 error_lines = [ |
148 'Found JavaScript style violations in %s:' % | 147 'Found JavaScript style violations in %s:' % |
149 f.LocalPath()] + error_lines | 148 f.LocalPath()] + error_lines |
150 results.append(self.output_api.PresubmitError('\n'.join(error_lines))) | 149 results.append(self.output_api.PresubmitError('\n'.join(error_lines))) |
151 | 150 |
152 if results: | 151 if results: |
153 results.append(self.output_api.PresubmitNotifyResult( | 152 results.append(self.output_api.PresubmitNotifyResult( |
154 'See the JavaScript style guide at ' | 153 'See the JavaScript style guide at ' |
155 'https://chromium.googlesource.com/chromium/src/+/master/styleguide/we b/web.md#JavaScript')) | 154 'https://chromium.googlesource.com/chromium/src/+/master/styleguide/we b/web.md#JavaScript')) |
156 | 155 |
157 return results | 156 return results |
OLD | NEW |