Refactored the linter script to handle language detection and error traversal more efficiently.

This commit is contained in:
Paul Gauthier 2024-05-17 14:27:53 -07:00
parent cb8a487c89
commit 494aa0140b

View file

@ -1,20 +1,22 @@
import os import os
import tree_sitter
import sys import sys
import warnings import warnings
from pathlib import Path from pathlib import Path
from aider.dump import dump import tree_sitter
from grep_ast import TreeContext, filename_to_lang from grep_ast import TreeContext, filename_to_lang
from aider.dump import dump
# tree_sitter is throwing a FutureWarning # tree_sitter is throwing a FutureWarning
warnings.simplefilter("ignore", category=FutureWarning) warnings.simplefilter("ignore", category=FutureWarning)
from tree_sitter_languages import get_language, get_parser # noqa: E402 from tree_sitter_languages import get_language, get_parser # noqa: E402
def basic_lint(fname, code):
def basic_lint(fname, code):
lang = filename_to_lang(fname) lang = filename_to_lang(fname)
if not lang:
return
language = get_language(lang) language = get_language(lang)
parser = get_parser(lang) parser = get_parser(lang)
@ -46,10 +48,11 @@ def basic_lint(fname, code):
return output return output
# Traverse the tree to find errors and print context # Traverse the tree to find errors and print context
def traverse_tree(node): def traverse_tree(node):
errors = [] errors = []
if node.type == 'ERROR' or node.is_missing: if node.type == "ERROR" or node.is_missing:
line_no = node.start_point[0] line_no = node.start_point[0]
errors.append(line_no) errors.append(line_no)
@ -58,6 +61,7 @@ def traverse_tree(node):
return errors return errors
def main(): def main():
""" """
Main function to parse files provided as command line arguments. Main function to parse files provided as command line arguments.
@ -72,5 +76,6 @@ def main():
if errors: if errors:
print(errors) print(errors)
if __name__ == "__main__": if __name__ == "__main__":
main() main()