mirror of
https://github.com/Aider-AI/aider.git
synced 2025-06-02 18:54:59 +00:00
Fixed tree-sitter FutureWarning and refactored parsing logic in linter.
This commit is contained in:
parent
3cc0a3c263
commit
c26833419d
1 changed files with 27 additions and 10 deletions
|
@ -1,22 +1,24 @@
|
||||||
import os
|
import os
|
||||||
import tree_sitter
|
import tree_sitter
|
||||||
|
import sys
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
# tree_sitter is throwing a FutureWarning
|
||||||
|
warnings.simplefilter("ignore", category=FutureWarning)
|
||||||
|
from tree_sitter_languages import get_language, get_parser # noqa: E402
|
||||||
|
|
||||||
def parse_file_for_errors(file_path):
|
def parse_file_for_errors(file_path):
|
||||||
"""
|
|
||||||
Parses the given file using tree-sitter and prints out the line number of every syntax/parse error.
|
lang = "python"
|
||||||
"""
|
|
||||||
# Load the language
|
language = get_language(lang)
|
||||||
Language = tree_sitter.Language
|
parser = get_parser(lang)
|
||||||
PARSER = tree_sitter.Parser()
|
|
||||||
LANGUAGE = Language(os.path.join('build', 'my-languages.so'), 'python')
|
|
||||||
PARSER.set_language(LANGUAGE)
|
|
||||||
|
|
||||||
# Read the file content
|
# Read the file content
|
||||||
with open(file_path, 'r') as file:
|
with open(file_path, 'r') as file:
|
||||||
content = file.read()
|
content = file.read()
|
||||||
|
|
||||||
# Parse the content
|
tree = parser.parse(bytes(content, "utf8"))
|
||||||
tree = PARSER.parse(bytes(content, "utf8"))
|
|
||||||
|
|
||||||
# Traverse the tree to find errors
|
# Traverse the tree to find errors
|
||||||
def traverse_tree(node):
|
def traverse_tree(node):
|
||||||
|
@ -26,3 +28,18 @@ def parse_file_for_errors(file_path):
|
||||||
traverse_tree(child)
|
traverse_tree(child)
|
||||||
|
|
||||||
traverse_tree(tree.root_node)
|
traverse_tree(tree.root_node)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""
|
||||||
|
Main function to parse files provided as command line arguments.
|
||||||
|
"""
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("Usage: python linter.py <file1> <file2> ...")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
for file_path in sys.argv[1:]:
|
||||||
|
print(f"Checking file: {file_path}")
|
||||||
|
parse_file_for_errors(file_path)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue