def to_violation(self, result: Dict[str, Any], message: Dict[str, Any]) -> Violation: path = self.trim_base(result["filePath"]) startLine = message["line"] endLine = message.get("endLine", startLine) # todo: remove white-space diffs for non-py? source = result["source"][startLine - 1:endLine] # line numbers are 1-indexed check_id = message.get("ruleId", None) if check_id: link = f"https://eslint.org/docs/rules/{check_id}" else: check_id = "error" link = "" return Violation( tool_id=EslintTool.ESLINT_TOOL_ID, check_id=check_id, path=path, line=startLine, column=message["column"], message=message["message"], severity=message["severity"], syntactic_context="\n".join(source).strip(), link=link, )
def to_violation(self, result: Dict[str, Any], message: Dict[str, Any]) -> Violation: path = self.trim_base(result["filePath"]) startLine = message["line"] endLine = message.get("endLine", startLine) source = result["source"][startLine - 1:endLine] # line numbers are 1-indexed check_id = message.get("ruleId", None) if check_id: link = self.to_link(check_id) else: check_id = "error" link = "" return Violation( tool_id=EslintTool.ESLINT_TOOL_ID, check_id=check_id, path=path, line=startLine, column=message["column"], message=message["message"], severity=message["severity"], syntactic_context="\n".join(source).rstrip(), link=link, )
def __error_to_violation(self, error: Dict[str, Any]) -> Violation: return Violation( check_id="error", tool_id=BanditTool.TOOL_ID, path=self.trim_base(error["filename"]), severity=2, line=0, column=0, message=error["reason"], syntactic_context="", link=None, )
def __result_to_violation(self, result: Dict[str, Any]) -> Violation: path = self.trim_base(result["filename"]) link = result.get("more_info", None) # Remove bandit line numbers, empty lines, and leading / trailing whitespace bandit_source = result["code"].rstrip() # Remove trailing whitespace test_id = result["test_id"] check_id = BANDIT_TO_BENTO.get(test_id, test_id) line_range = result["line_range"] def in_line_range(bandit_code_line: str) -> bool: # Check if string with format `3 def do_it(cmd: str) -> None:` # starts with line number that is within reported line_range # of finding for idx, ch in enumerate(bandit_code_line): if not ch.isdigit(): num = int(bandit_code_line[:idx]) return num in line_range return False # bandit might include extra lines before and after # a finding. Filter those out and filter out line numbers lines = [ s.lstrip(BanditParser.LINE_NO_CHARS).rstrip() for s in bandit_source.split("\n") if in_line_range(s) ] nonempty = [l for l in lines if l] source = "\n".join(nonempty) if source == "" and result["line_number"] != 0: source = ( fetch_line_in_file(self.base_path / path, result["line_number"]) or "<no source found>" ) return Violation( check_id=check_id, tool_id=BanditTool.TOOL_ID, path=path, line=result["line_number"], column=0, message=result["issue_text"], severity=BanditParser.SEVERITY.get(result["issue_severity"], 1), syntactic_context=source, link=link, )
def to_violation(self, result: Dict[str, Any]) -> Violation: path = self.trim_base(result["path"]) abspath = self.base_path / path check_id = str(result["code"]) line = result["line"] return Violation( tool_id=PyreTool.TOOL_ID, check_id=check_id, path=path, line=line, column=result["column"], message=result["description"], severity=2, syntactic_context=fetch_line_in_file(abspath, line) or "<no source found>", link="https://pyre-check.org/docs/error-types.html", )
def to_violation(self, result: Dict[str, Any]) -> Violation: source = (result["physical_line"] or "").rstrip() # Remove trailing whitespace path = self.trim_base(result["filename"]) check_id = result["code"] return Violation( tool_id=self.tool().tool_id(), check_id=self.id_to_name(check_id), path=path, line=result["line_number"], column=result["column_number"], message=result["text"], severity=2, syntactic_context=source, link=self.id_to_link(check_id), )
def to_violation(self, output_rule: Dict[str, Any]) -> Violation: output = output_rule["output"] check_id = output_rule["id"] message = output_rule.get("message") parts = output.split(":") path = parts[0] path = self.trim_base(path) line_no = int(parts[1]) code_snippet = ":".join(parts[2:]) return Violation( tool_id=GrepTool.TOOL_ID, check_id=check_id, path=path, line=line_no, column=1, message=message or code_snippet, severity=2, syntactic_context=code_snippet or "<no context>", )