|
| 1 | +import re |
| 2 | + |
| 3 | +import tree_sitter_clarity |
| 4 | +from stacy_analyzer.print_message import TerminalColors |
| 5 | +from tree_sitter import Language, Tree, Node, Parser |
| 6 | + |
| 7 | +__CLARITY__ = Language(tree_sitter_clarity.language()) |
| 8 | +__TIMES__ = 3 |
| 9 | + |
| 10 | +from stacy_analyzer.visitor import Visitor |
| 11 | +from stacy_analyzer.node_iterator import NodeIterator |
| 12 | + |
| 13 | + |
| 14 | +class LinterRunner: |
| 15 | + source: str |
| 16 | + tree: Tree |
| 17 | + root_node: Node |
| 18 | + iterator: NodeIterator |
| 19 | + lints: [Visitor] |
| 20 | + round_number: int |
| 21 | + allowed_lints: dict[str, ([int], Node)] = {} |
| 22 | + |
| 23 | + def __init__(self, source: str, print_output: bool, src_name: str): |
| 24 | + self.src_name = src_name |
| 25 | + self.source = source |
| 26 | + parser = Parser(__CLARITY__) |
| 27 | + self.tree = parser.parse(bytes(self.source, "utf8")) |
| 28 | + self.root_node = self.tree.root_node |
| 29 | + self.iterator = NodeIterator(self.root_node) |
| 30 | + self.lints = [] |
| 31 | + self.round_number = 0 |
| 32 | + self.print_output = print_output |
| 33 | + self.allowed_lints = {} |
| 34 | + |
| 35 | + def run_lints(self, node: Node): |
| 36 | + for lint in self.lints: |
| 37 | + lint.visit_node(node, self.round_number) |
| 38 | + |
| 39 | + def add_lint(self, lint): |
| 40 | + self.lints.append(lint) |
| 41 | + return self |
| 42 | + |
| 43 | + def add_lints(self, lint_classes: [Visitor]): |
| 44 | + for lint_class in lint_classes: |
| 45 | + lint = lint_class(self.print_output) |
| 46 | + lint.add_source(self.source, self.src_name) |
| 47 | + self.lints.append(lint) |
| 48 | + |
| 49 | + def reset_cursor(self): |
| 50 | + self.iterator = NodeIterator(self.root_node) |
| 51 | + |
| 52 | + def run(self): |
| 53 | + self.pre_run_checks() |
| 54 | + for i in range(__TIMES__): |
| 55 | + self.round_number = self.round_number + 1 |
| 56 | + |
| 57 | + for node in self.iterator: |
| 58 | + self.run_lints(node) |
| 59 | + self.reset_cursor() |
| 60 | + |
| 61 | + self.post_run_checks() |
| 62 | + |
| 63 | + return [finding for lint in self.lints |
| 64 | + for finding in lint.get_findings()] |
| 65 | + |
| 66 | + def pre_run_checks(self): |
| 67 | + for node in self.iterator: |
| 68 | + allowed: [AllowedComment] = check_comments(node) |
| 69 | + if not allowed: |
| 70 | + continue |
| 71 | + for allowed_comment in allowed: |
| 72 | + if allowed_comment.name not in self.allowed_lints: |
| 73 | + self.allowed_lints[allowed_comment.name] = ( |
| 74 | + [allowed_comment.line_number], allowed_comment.comment_node) |
| 75 | + else: |
| 76 | + self.allowed_lints[allowed_comment.name][0].append(allowed_comment.line_number) |
| 77 | + |
| 78 | + self.reset_cursor() |
| 79 | + for lint in self.lints: |
| 80 | + lint.set_ignores(self.allowed_lints) |
| 81 | + |
| 82 | + def post_run_checks(self): |
| 83 | + for lint in self.lints: |
| 84 | + findings: dict[str, ([int], Node)] = lint.get_ignored_findings() |
| 85 | + name = lint.Name |
| 86 | + if name not in self.allowed_lints or findings[name][0] == []: |
| 87 | + continue |
| 88 | + warn_magic_comment(findings[name][0], name) |
| 89 | + |
| 90 | + |
| 91 | +class AllowedComment: |
| 92 | + name: str |
| 93 | + comment_node: Node |
| 94 | + line_number: int |
| 95 | + |
| 96 | + def __init__(self, finding: str, comment_node: Node): |
| 97 | + self.name = finding |
| 98 | + self.comment_node = comment_node |
| 99 | + self.line_number = comment_node.range.start_point.row + 1 |
| 100 | + |
| 101 | + |
| 102 | +def extract_detectors(comment_text: str) -> [str]: |
| 103 | + allow_comment = re.search(r'#\[allow\((.*?)\)]', comment_text) |
| 104 | + return allow_comment.group(1).split(',') if allow_comment else [] |
| 105 | + |
| 106 | + |
| 107 | +def check_comments(node) -> [AllowedComment]: |
| 108 | + if node.grammar_name != "comment": |
| 109 | + return [] |
| 110 | + comment_text = node.text.decode("utf-8") |
| 111 | + detectors = extract_detectors(comment_text) |
| 112 | + |
| 113 | + return [AllowedComment(detector.strip(), node) for detector in detectors] |
| 114 | + |
| 115 | + |
| 116 | +def warn_magic_comment(lines: [int], name: str): |
| 117 | + for line in lines: |
| 118 | + print(f"{TerminalColors.ERROR}[WARNING]{TerminalColors.ENDC} " |
| 119 | + f"Magic comment in line {line} for detector {TerminalColors.BOLD}{name}{TerminalColors.ENDC} is not used") |
0 commit comments