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

Side by Side Diff: tools/web_dev_style/js_checker.py

Issue 2872703003: WebUI: Hook up ESLint to web_dev_style PRESUBMIT. (Closed)
Patch Set: Addressing comments. Created 3 years, 7 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
« no previous file with comments | « tools/web_dev_style/OWNERS ('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
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
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 # Import ESLint.
76 try:
77 import os
Dan Beam 2017/05/24 22:57:53 use self.input_api.os_path instead of `import os`
dpapad 2017/05/24 23:46:02 Done.
78 _HERE_PATH = os.path.dirname(os.path.realpath(__file__))
Dan Beam 2017/05/24 22:57:53 i'm not totalllly sure how __file__ works here, bu
dpapad 2017/05/24 23:46:02 input_api.PresubmitLocalPath() returns the locatio
Dan Beam 2017/05/25 00:14:51 probably not. if it works for now with `git cl pr
79 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..'))
80 import sys
81 _NODE_PATH = os.path.join(_SRC_PATH, 'third_party', 'node')
82 old_path = sys.path[::]
Dan Beam 2017/05/24 23:01:34 nit: [::] -> [:]
dpapad 2017/05/24 23:46:02 Done.
83 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node'))
84 import node
85 import node_modules
Dan Beam 2017/05/24 23:01:34 import node, node_modules
dpapad 2017/05/24 23:46:02 Done.
86 finally:
87 sys.path = old_path
88
89 # Extract paths to be passed to ESLint.
90 affected_js_files_paths = []
91 presubmit_path = self.input_api.PresubmitLocalPath()
92 for f in affected_js_files:
93 affected_js_files_paths.append(
94 os.path.relpath(f.AbsoluteLocalPath(), presubmit_path))
95
96 output = node.RunNode([
97 node_modules.PathToEsLint(),
98 '--color',
99 '--ignore-pattern \'!.eslintrc.js\'',
100 ' '.join(affected_js_files_paths)])
101
102 return [self.output_api.PresubmitError(output)] if output else []
103
71 def WrapperTypeCheck(self, i, line): 104 def WrapperTypeCheck(self, i, line):
72 """Check for wrappers (new String()) instead of builtins (string).""" 105 """Check for wrappers (new String()) instead of builtins (string)."""
73 return self.RegexCheck(i, line, 106 return self.RegexCheck(i, line,
74 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type 107 r"(?:/\*)?\*.*?@(?:param|return|type) ?" # /** @param/@return/@type
75 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)} 108 r"{[^}]*\b(String|Boolean|Number)\b[^}]*}", # {(Boolean|Number|String)}
76 "Don't use wrapper types (i.e. new String() or @type {String})") 109 "Don't use wrapper types (i.e. new String() or @type {String})")
77 110
78 def VarNameCheck(self, i, line): 111 def VarNameCheck(self, i, line):
79 """See the style guide. http://goo.gl/eQiXVW""" 112 """See the style guide. http://goo.gl/eQiXVW"""
80 return self.RegexCheck(i, line, 113 return self.RegexCheck(i, line,
81 r"var (?!g_\w+)(_?[a-z][a-zA-Z]*[_$][\w_$]*)(?<! \$)", 114 r"var (?!g_\w+)(_?[a-z][a-zA-Z]*[_$][\w_$]*)(?<! \$)",
82 "Please use var namesLikeThis <https://goo.gl/eQiXVW>") 115 "Please use var namesLikeThis <https://goo.gl/eQiXVW>")
83 116
84 def _GetErrorHighlight(self, start, length): 117 def _GetErrorHighlight(self, start, length):
85 """Takes a start position and a length, and produces a row of '^'s to 118 """Takes a start position and a length, and produces a row of '^'s to
86 highlight the corresponding part of a string. 119 highlight the corresponding part of a string.
87 """ 120 """
88 return start * ' ' + length * '^' 121 return start * ' ' + length * '^'
89 122
90 def RunChecks(self): 123 def RunChecks(self):
91 """Check for violations of the Chromium JavaScript style guide. See 124 """Check for violations of the Chromium JavaScript style guide. See
92 https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/we b.md#JavaScript 125 https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/we b.md#JavaScript
93 """ 126 """
94 results = [] 127 results = []
95 128
96 affected_files = self.input_api.AffectedFiles(file_filter=self.file_filter, 129 affected_files = self.input_api.AffectedFiles(file_filter=self.file_filter,
97 include_deletes=False) 130 include_deletes=False)
98 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'), 131 affected_js_files = filter(lambda f: f.LocalPath().endswith('.js'),
99 affected_files) 132 affected_files)
133
134 if affected_js_files:
135 results += self.RunEsLintChecks(affected_js_files)
136
100 for f in affected_js_files: 137 for f in affected_js_files:
101 error_lines = [] 138 error_lines = []
102 139
103 for i, line in enumerate(f.NewContents(), start=1): 140 for i, line in enumerate(f.NewContents(), start=1):
104 error_lines += filter(None, [ 141 error_lines += filter(None, [
105 self.ChromeSendCheck(i, line), 142 self.ChromeSendCheck(i, line),
106 self.CommentIfAndIncludeCheck(i, line), 143 self.CommentIfAndIncludeCheck(i, line),
107 self.ConstCheck(i, line), 144 self.ConstCheck(i, line),
108 self.GetElementByIdCheck(i, line), 145 self.GetElementByIdCheck(i, line),
109 self.EndJsDocCommentCheck(i, line), 146 self.EndJsDocCommentCheck(i, line),
110 self.ExtraDotInGenericCheck(i, line), 147 self.ExtraDotInGenericCheck(i, line),
111 self.InheritDocCheck(i, line), 148 self.InheritDocCheck(i, line),
112 self.PolymerLocalIdCheck(i, line), 149 self.PolymerLocalIdCheck(i, line),
113 self.WrapperTypeCheck(i, line), 150 self.WrapperTypeCheck(i, line),
114 self.VarNameCheck(i, line), 151 self.VarNameCheck(i, line),
115 ]) 152 ])
116 153
117 if error_lines: 154 if error_lines:
118 error_lines = [ 155 error_lines = [
119 'Found JavaScript style violations in %s:' % 156 'Found JavaScript style violations in %s:' %
120 f.LocalPath()] + error_lines 157 f.LocalPath()] + error_lines
121 results.append(self.output_api.PresubmitError('\n'.join(error_lines))) 158 results.append(self.output_api.PresubmitError('\n'.join(error_lines)))
122 159
123 if results: 160 if results:
124 results.append(self.output_api.PresubmitNotifyResult( 161 results.append(self.output_api.PresubmitNotifyResult(
125 'See the JavaScript style guide at ' 162 'See the JavaScript style guide at '
126 'https://chromium.googlesource.com/chromium/src/+/master/styleguide/we b/web.md#JavaScript')) 163 'https://chromium.googlesource.com/chromium/src/+/master/styleguide/we b/web.md#JavaScript'))
127 164
128 return results 165 return results
OLDNEW
« no previous file with comments | « tools/web_dev_style/OWNERS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698