diff --git a/python_ta/reporters/json_reporter.py b/python_ta/reporters/json_reporter.py index 60f89fd8e..ea22e3983 100644 --- a/python_ta/reporters/json_reporter.py +++ b/python_ta/reporters/json_reporter.py @@ -1,9 +1,10 @@ import json +from typing import Dict, List from pylint.interfaces import IReporter from pylint.reporters.ureports.nodes import BaseLayout -from .core import PythonTaReporter +from .core import NewMessage, PythonTaReporter class JSONReporter(PythonTaReporter): @@ -17,6 +18,8 @@ class JSONReporter(PythonTaReporter): OUTPUT_FILENAME = "pyta_report.json" + messages: Dict[str, List[NewMessage]] + def display_messages(self, layout: BaseLayout) -> None: """Hook for displaying the messages of the reporter @@ -30,7 +33,23 @@ def display_messages(self, layout: BaseLayout) -> None: output = [] for k, msgs in self.messages.items(): output.append( - {"filename": k, "msgs": [msg._replace(node=None)._asdict() for msg in msgs]} + { + "filename": k, + "msgs": [ + {**msg._replace(node=None)._asdict(), **self._snippet_endings(msg)} + for msg in msgs + ], + } ) self.writeln(json.dumps(output, indent=4)) + + def _snippet_endings(self, msg: NewMessage) -> Dict[str, int]: + """Return a dictionary containing the index of the last line and the index of the last column of the + error reported by the message in msg. + """ + if msg.node is None: + line_end, column_end = msg.line, len(self.source_lines[msg.line - 1]) + else: + line_end, column_end = msg.node.end_lineno, msg.node.end_col_offset + return {"line_end": line_end, "column_end": column_end}