eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleChecker.py

changeset 7610
df7025fe26a3
parent 7360
9190402e4505
child 7611
d546c4e72f52
equal deleted inserted replaced
7609:d5aff4fd0ef8 7610:df7025fe26a3
72 """ 72 """
73 code = super(CodeStyleCheckerReport, self).error_args( 73 code = super(CodeStyleCheckerReport, self).error_args(
74 line_number, offset, code, check, *args) 74 line_number, offset, code, check, *args)
75 if code and (self.counters[code] == 1 or self.__repeat): 75 if code and (self.counters[code] == 1 or self.__repeat):
76 self.errors.append( 76 self.errors.append(
77 (self.filename, line_number, offset, (code, args)) 77 {
78 "file": self.filename,
79 "line": line_number,
80 "offset": offset,
81 "code": code,
82 "args": args,
83 }
78 ) 84 )
79 return code 85 return code
80 86
81 87
82 def extractLineFlags(line, startComment="#", endComment="", flagsLine=False): 88 def extractLineFlags(line, startComment="#", endComment="", flagsLine=False):
259 noFixCodes, fixIssues, maxLineLength, maxDocLineLength, blankLines, 265 noFixCodes, fixIssues, maxLineLength, maxDocLineLength, blankLines,
260 hangClosing, docType, codeComplexityArgs, miscellaneousArgs, 266 hangClosing, docType, codeComplexityArgs, miscellaneousArgs,
261 annotationArgs, errors, eol, encoding, backup) 267 annotationArgs, errors, eol, encoding, backup)
262 @type list of (str, str, bool, str, str, bool, int, list of (int, int), 268 @type list of (str, str, bool, str, str, bool, int, list of (int, int),
263 bool, str, dict, dict, list of str, str, str, bool) 269 bool, str, dict, dict, list of str, str, str, bool)
264 @return tuple of statistics (dict) and list of results (tuple for each 270 @return tuple of statistics data and list of result dictionaries with
265 found violation of style (lineno, position, text, ignored, fixed, 271 keys:
266 autofixing, fixedMsg)) 272 <ul>
267 @rtype tuple of (dict, list of tuples of (int, int, str, bool, bool, bool, 273 <li>file: file name</li>
268 str)) 274 <li>line: line_number</li>
275 <li>offset: offset within line</li>
276 <li>code: message code</li>
277 <li>args: list of arguments to format the message</li>
278 <li>ignored: flag indicating this issue was ignored</li>
279 <li>fixed: flag indicating this issue was fixed</li>
280 <li>autofixing: flag indicating that a fix can be done</li>
281 <li>fixcode: message code for the fix</li>
282 <li>fixargs: list of arguments to format the fix message</li>
283 </ul>
284 @rtype tuple of (dict, list of dict)
269 """ 285 """
270 (excludeMessages, includeMessages, repeatMessages, fixCodes, noFixCodes, 286 (excludeMessages, includeMessages, repeatMessages, fixCodes, noFixCodes,
271 fixIssues, maxLineLength, maxDocLineLength, blankLines, hangClosing, 287 fixIssues, maxLineLength, maxDocLineLength, blankLines, hangClosing,
272 docType, codeComplexityArgs, miscellaneousArgs, annotationArgs, errors, 288 docType, codeComplexityArgs, miscellaneousArgs, annotationArgs, errors,
273 eol, encoding, backup) = args 289 eol, encoding, backup) = args
320 hang_closing=hangClosing, 336 hang_closing=hangClosing,
321 ) 337 )
322 report = styleGuide.check_files([filename]) 338 report = styleGuide.check_files([filename])
323 stats.update(report.counters) 339 stats.update(report.counters)
324 errors = report.errors 340 errors = report.errors
325 341
326 # check documentation style 342 # check documentation style
327 docStyleChecker = DocStyleChecker( 343 docStyleChecker = DocStyleChecker(
328 source, filename, select, ignore, [], repeatMessages, 344 source, filename, select, ignore, [], repeatMessages,
329 maxLineLength=maxDocLineLength, docType=docType) 345 maxLineLength=maxDocLineLength, docType=docType)
330 docStyleChecker.run() 346 docStyleChecker.run()
356 annotationsChecker.run() 372 annotationsChecker.run()
357 stats.update(annotationsChecker.counters) 373 stats.update(annotationsChecker.counters)
358 errors += annotationsChecker.errors 374 errors += annotationsChecker.errors
359 375
360 errorsDict = {} 376 errorsDict = {}
361 for _fname, lineno, position, text in errors: 377 for error in errors:
362 if lineno > len(source): 378 if error["line"] > len(source):
363 lineno = len(source) 379 error["line"] = len(source)
364 # inverse processing of messages and fixes 380 # inverse processing of messages and fixes
365 errorLine = errorsDict.setdefault(lineno, []) 381 errorLine = errorsDict.setdefault(error["line"], [])
366 errorLine.append([position, text]) 382 errorLine.append((error["offset"], error))
367 deferredFixes = {} 383 deferredFixes = {}
368 results = [] 384 results = []
369 for lineno, errors in errorsDict.items(): 385 for lineno, errorsList in errorsDict.items():
370 errors.sort(key=lambda x: x[0], reverse=True) 386 errorsList.sort(key=lambda x: x[0], reverse=True)
371 for position, text in errors: 387 for _, error in errorsList:
372 if source: 388 if source:
373 code = text[0] 389 code = error["code"]
374 lineFlags = extractLineFlags(source[lineno - 1].strip()) 390 lineFlags = extractLineFlags(source[lineno - 1].strip())
375 try: 391 try:
376 lineFlags += extractLineFlags(source[lineno].strip(), 392 lineFlags += extractLineFlags(source[lineno].strip(),
377 flagsLine=True) 393 flagsLine=True)
378 except IndexError: 394 except IndexError:
379 pass 395 pass
380 if not ignoreCode(code, lineFlags): 396 if not ignoreCode(code, lineFlags):
381 if fixer: 397 if fixer:
382 res, msg, id_ = fixer.fixIssue(lineno, position, text) 398 pass
399 res, fixcode, fixargs, id_ = fixer.fixIssue(
400 lineno, error["offset"], code)
383 if res == -1: 401 if res == -1:
384 itm = [lineno, position, text] 402 deferredFixes[id_] = error
385 deferredFixes[id_] = itm
386 else: 403 else:
387 itm = [lineno, position, text, False, 404 error.update({
388 res == 1, True, msg] 405 "ignored": False,
406 "fixed": res == 1,
407 "autofixing": True,
408 "fixcode": fixcode,
409 "fixargs": fixargs,
410 })
389 else: 411 else:
390 itm = [lineno, position, text, False, 412 error.update({
391 False, False, ''] 413 "ignored": False,
392 results.append(itm) 414 "fixed": False,
415 "autofixing": False,
416 "fixcode": "",
417 "fixargs": [],
418 })
393 else: 419 else:
394 results.append([lineno, position, text, True, 420 error.update({
395 False, False, '']) 421 "ignored": True,
422 "fixed": False,
423 "autofixing": False,
424 "fixcode": "",
425 "fixargs": [],
426 })
396 else: 427 else:
397 results.append([lineno, position, text, False, 428 error.update({
398 False, False, '']) 429 "ignored": False,
430 "fixed": False,
431 "autofixing": False,
432 "fixcode": "",
433 "fixargs": [],
434 })
435
436 results.append(error)
399 437
400 if fixer: 438 if fixer:
401 deferredResults = fixer.finalize() 439 deferredResults = fixer.finalize()
402 for id_ in deferredResults: 440 for id_ in deferredResults:
403 fixed, msg = deferredResults[id_] 441 fixed, fixcode, fixargs = deferredResults[id_]
404 itm = deferredFixes[id_] 442 error = deferredFixes[id_]
405 itm.extend([False, fixed == 1, True, msg]) 443 error.update({
406 444 "ignored": False,
407 errMsg = fixer.saveFile(encoding) 445 "fixed": fixed == 1,
408 if errMsg: 446 "autofixing": True,
409 for result in results: 447 "fixcode": fixcode,
410 result[-1] = errMsg 448 "fixargs": fixargs,
449 })
450
451 saveError = fixer.saveFile(encoding)
452 if saveError:
453 for error in results:
454 error.update({
455 "fixcode": saveError[0],
456 "fixargs": saveError[1],
457 })
411 458
412 return stats, results 459 return stats, results
413 460
414 # 461 #
415 # eflag: noqa = M702 462 # eflag: noqa = M702

eric ide

mercurial