src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleChecker.py

branch
eric7
changeset 10069
435cc5875135
parent 10059
9e3452188615
child 10116
4a619fb7bd09
equal deleted inserted replaced
10068:7febcdccb2a1 10069:435cc5875135
63 super().__init__(options) 63 super().__init__(options)
64 64
65 self.__repeat = options.repeat 65 self.__repeat = options.repeat
66 self.errors = [] 66 self.errors = []
67 67
68 def error_args(self, line_number, offset, code, check, *args): 68 def error_args(self, line_number, offset, errorCode, check, *args):
69 """ 69 """
70 Public method to collect the error messages. 70 Public method to collect the error messages.
71 71
72 @param line_number line number of the issue (integer) 72 @param line_number line number of the issue (integer)
73 @param offset position within line of the issue (integer) 73 @param offset position within line of the issue (integer)
74 @param code message code (string) 74 @param errorCode error message code (string)
75 @param check reference to the checker function (function) 75 @param check reference to the checker function (function)
76 @param args arguments for the message (list) 76 @param args arguments for the message (list)
77 @return error code (string) 77 @return error code (string)
78 """ 78 """
79 code = super().error_args(line_number, offset, code, check, *args) 79 errorCode = super().error_args(line_number, offset, errorCode, check, *args)
80 if code and (self.counters[code] == 1 or self.__repeat): 80 if errorCode and (self.counters[errorCode] == 1 or self.__repeat):
81 self.errors.append( 81 self.errors.append(
82 { 82 {
83 "file": self.filename, 83 "file": self.filename,
84 "line": line_number, 84 "line": line_number,
85 "offset": offset, 85 "offset": offset,
86 "code": code, 86 "code": errorCode,
87 "args": args, 87 "args": args,
88 } 88 }
89 ) 89 )
90 return code 90 return errorCode
91 91
92 92
93 def extractLineFlags(line, startComment="#", endComment="", flagsLine=False): 93 def extractLineFlags(line, startComment="#", endComment="", flagsLine=False):
94 """ 94 """
95 Function to extract flags starting and ending with '__' from a line 95 Function to extract flags starting and ending with '__' from a line
116 "noqa:{0}".format(f.strip()) 116 "noqa:{0}".format(f.strip())
117 for f in comment[len("noqa:") :].split(",") 117 for f in comment[len("noqa:") :].split(",")
118 ] 118 ]
119 else: 119 else:
120 flags = [ 120 flags = [
121 f.strip() 121 f
122 for f in comment.split() 122 for f in comment.split()
123 if (f.startswith("__") and f.endswith("__")) 123 if (f.startswith("__") and f.endswith("__"))
124 ] 124 ]
125 flags += [ 125 flags += [
126 f.strip().lower() 126 f.lower()
127 for f in comment.split() 127 for f in comment.split()
128 if f in ("noqa", "NOQA", "nosec", "NOSEC", "secok", "SECOK") 128 if f in ("noqa", "NOQA", "nosec", "NOSEC", "secok", "SECOK")
129 ] 129 ]
130 return flags 130 return flags
131 131
132 132
133 def ignoreCode(code, lineFlags): 133 def ignoreCode(errorCode, lineFlags):
134 """ 134 """
135 Function to check, if the given code should be ignored as per line flags. 135 Function to check, if the given code should be ignored as per line flags.
136 136
137 @param code error code to be checked 137 @param errorCode error code to be checked
138 @type str 138 @type str
139 @param lineFlags list of line flags to check against 139 @param lineFlags list of line flags to check against
140 @type list of str 140 @type list of str
141 @return flag indicating to ignore the code 141 @return flag indicating to ignore the error code
142 @rtype bool 142 @rtype bool
143 """ 143 """
144 if lineFlags: 144 if lineFlags:
145 if ( 145 if (
146 "__IGNORE_WARNING__" in lineFlags 146 "__IGNORE_WARNING__" in lineFlags
152 152
153 for flag in lineFlags: 153 for flag in lineFlags:
154 # check individual warning code 154 # check individual warning code
155 if flag.startswith("__IGNORE_WARNING_"): 155 if flag.startswith("__IGNORE_WARNING_"):
156 ignoredCode = flag[2:-2].rsplit("_", 1)[-1] 156 ignoredCode = flag[2:-2].rsplit("_", 1)[-1]
157 if code.startswith(ignoredCode): 157 if errorCode.startswith(ignoredCode):
158 return True 158 return True
159 elif flag.startswith("noqa:"): 159 elif flag.startswith("noqa:"):
160 ignoredCode = flag[len("noqa:") :].strip() 160 ignoredCode = flag[len("noqa:") :].strip()
161 if code.startswith(ignoredCode): 161 if errorCode.startswith(ignoredCode):
162 return True 162 return True
163 163
164 return False 164 return False
165 165
166 166
167 def securityOk(code, lineFlags): 167 def securityOk(errorCode, lineFlags): # noqa: U100
168 """ 168 """
169 Function to check, if the given code is an acknowledged security report. 169 Function to check, if the given error code is an acknowledged security report.
170 170
171 @param code error code to be checked 171 @param errorCode error code to be checked
172 @type str 172 @type str
173 @param lineFlags list of line flags to check against 173 @param lineFlags list of line flags to check against
174 @type list of str 174 @type list of str
175 @return flag indicating an acknowledged security report 175 @return flag indicating an acknowledged security report
176 @rtype bool 176 @rtype bool
181 return False 181 return False
182 182
183 183
184 def codeStyleCheck(filename, source, args): 184 def codeStyleCheck(filename, source, args):
185 """ 185 """
186 Do the code style check and/or fix found errors. 186 Do the source code style check and/or fix found errors.
187 187
188 @param filename source filename 188 @param filename source filename
189 @type str 189 @type str
190 @param source string containing the code to check 190 @param source string containing the source code to check
191 @type str 191 @type str
192 @param args arguments used by the codeStyleCheck function (list of 192 @param args arguments used by the codeStyleCheck function (list of
193 excludeMessages, includeMessages, repeatMessages, fixCodes, 193 excludeMessages, includeMessages, repeatMessages, fixCodes,
194 noFixCodes, fixIssues, maxLineLength, maxDocLineLength, blankLines, 194 noFixCodes, fixIssues, maxLineLength, maxDocLineLength, blankLines,
195 hangClosing, docType, codeComplexityArgs, miscellaneousArgs, 195 hangClosing, docType, codeComplexityArgs, miscellaneousArgs,
206 return __checkCodeStyle(filename, source, args) 206 return __checkCodeStyle(filename, source, args)
207 207
208 208
209 def codeStyleBatchCheck(argumentsList, send, fx, cancelled, maxProcesses=0): 209 def codeStyleBatchCheck(argumentsList, send, fx, cancelled, maxProcesses=0):
210 """ 210 """
211 Module function to check code style for a batch of files. 211 Module function to check source code style for a batch of files.
212 212
213 @param argumentsList list of arguments tuples as given for codeStyleCheck 213 @param argumentsList list of arguments tuples as given for codeStyleCheck
214 @type list 214 @type list
215 @param send reference to send function 215 @param send reference to send function
216 @type func 216 @type func
302 """ 302 """
303 Private module function to perform a syntax check. 303 Private module function to perform a syntax check.
304 304
305 @param filename source filename 305 @param filename source filename
306 @type str 306 @type str
307 @param source string containing the code to check 307 @param source string containing the source code to check
308 @type str 308 @type str
309 @return tuple containing the error dictionary with syntax error details, 309 @return tuple containing the error dictionary with syntax error details,
310 a statistics dictionary and None or a tuple containing two None and 310 a statistics dictionary and None or a tuple containing two None and
311 the generated AST tree 311 the generated AST tree
312 @rtype tuple of (dict, dict, None) or tuple of (None, None, ast.Module) 312 @rtype tuple of (dict, dict, None) or tuple of (None, None, ast.Module)
344 ) 344 )
345 345
346 346
347 def __checkCodeStyle(filename, source, args): 347 def __checkCodeStyle(filename, source, args):
348 """ 348 """
349 Private module function to perform the code style check and/or fix 349 Private module function to perform the source code style check and/or fix
350 found errors. 350 found errors.
351 351
352 @param filename source filename 352 @param filename source filename
353 @type str 353 @type str
354 @param source string containing the code to check 354 @param source string containing the source code to check
355 @type str 355 @type str
356 @param args arguments used by the codeStyleCheck function (list of 356 @param args arguments used by the codeStyleCheck function (list of
357 excludeMessages, includeMessages, repeatMessages, fixCodes, 357 excludeMessages, includeMessages, repeatMessages, fixCodes,
358 noFixCodes, fixIssues, maxLineLength, maxDocLineLength, blankLines, 358 noFixCodes, fixIssues, maxLineLength, maxDocLineLength, blankLines,
359 hangClosing, docType, codeComplexityArgs, miscellaneousArgs, 359 hangClosing, docType, codeComplexityArgs, miscellaneousArgs,
365 keys: 365 keys:
366 <ul> 366 <ul>
367 <li>file: file name</li> 367 <li>file: file name</li>
368 <li>line: line_number</li> 368 <li>line: line_number</li>
369 <li>offset: offset within line</li> 369 <li>offset: offset within line</li>
370 <li>code: message code</li> 370 <li>code: error message code</li>
371 <li>args: list of arguments to format the message</li> 371 <li>args: list of arguments to format the message</li>
372 <li>ignored: flag indicating this issue was ignored</li> 372 <li>ignored: flag indicating this issue was ignored</li>
373 <li>fixed: flag indicating this issue was fixed</li> 373 <li>fixed: flag indicating this issue was fixed</li>
374 <li>autofixing: flag indicating that a fix can be done</li> 374 <li>autofixing: flag indicating that a fix can be done</li>
375 <li>fixcode: message code for the fix</li> 375 <li>fixcode: message code for the fix</li>
602 "securityOk": False, 602 "securityOk": False,
603 } 603 }
604 ) 604 )
605 605
606 if source: 606 if source:
607 code = error["code"] 607 errorCode = error["code"]
608 lineFlags = extractLineFlags(source[lineno - 1].strip()) 608 lineFlags = extractLineFlags(source[lineno - 1].strip())
609 with contextlib.suppress(IndexError): 609 with contextlib.suppress(IndexError):
610 lineFlags += extractLineFlags( 610 lineFlags += extractLineFlags(
611 source[lineno].strip(), flagsLine=True 611 source[lineno].strip(), flagsLine=True
612 ) 612 )
613 613
614 if securityOk(code, lineFlags): 614 if securityOk(errorCode, lineFlags):
615 error["securityOk"] = True 615 error["securityOk"] = True
616 616
617 if ignoreCode(code, lineFlags): 617 if ignoreCode(errorCode, lineFlags):
618 error["ignored"] = True 618 error["ignored"] = True
619 else: 619 else:
620 if fixer: 620 if fixer:
621 res, fixcode, fixargs, id_ = fixer.fixIssue( 621 res, fixcode, fixargs, id_ = fixer.fixIssue(
622 lineno, error["offset"], code 622 lineno, error["offset"], errorCode
623 ) 623 )
624 if res == -1: 624 if res == -1:
625 deferredFixes[id_] = error 625 deferredFixes[id_] = error
626 else: 626 else:
627 error.update( 627 error.update(

eric ide

mercurial