-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
133 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from pathlib import Path | ||
from .message import IfTuple, ImportStarUsage, Location, Message | ||
import ast | ||
|
||
def check_statement(path: Path, stmt: ast.stmt) -> list[Message]: | ||
"""recursive function to parse statement""" | ||
messages = [] | ||
|
||
match stmt: | ||
case ast.FunctionDef() | ast.AsyncFunctionDef() | ast.ClassDef(): | ||
for body_stmt in stmt.body: | ||
messages.extend(check_statement(path, body_stmt)) | ||
case ast.For() | ast.AsyncFor() | ast.While(): | ||
for body_stmt in stmt.body: | ||
messages.extend(check_statement(path, body_stmt)) | ||
for orelse_stmt in stmt.orelse: | ||
messages.extend(check_statement(path, orelse_stmt)) | ||
case ( | ||
ast.Return() | ||
| ast.Delete() | ||
| ast.Assign() | ||
| ast.AugAssign() | ||
| ast.AnnAssign() | ||
| ast.Raise() | ||
| ast.Assert() | ||
| ast.Import() | ||
| ast.Global() | ||
| ast.Nonlocal() | ||
| ast.Expr() | ||
| ast.Pass() | ||
| ast.Break() | ||
| ast.Continue() | ||
): | ||
pass # Do nothing for these statement types | ||
case ast.If(): | ||
# Check if the test is a tuple | ||
if isinstance(stmt.test, ast.Tuple): # Assuming node is a tuple type | ||
messages.append( | ||
IfTuple( | ||
filename=path, | ||
location=Location(row=stmt.lineno, column=stmt.col_offset), | ||
) | ||
) | ||
for body_stmt in stmt.body: | ||
messages.extend(check_statement(path, body_stmt)) | ||
for orelse_stmt in stmt.orelse: | ||
messages.extend(check_statement(path, orelse_stmt)) | ||
|
||
case ast.With() | ast.AsyncWith(): | ||
for body_stmt in stmt.body: | ||
messages.extend(check_statement(path, body_stmt)) | ||
case ast.ImportFrom(): | ||
print(123) | ||
for alias in stmt.names: | ||
if alias.name == "*": | ||
messages.append( | ||
ImportStarUsage( | ||
filename=path, | ||
location=Location(row=stmt.lineno, column=stmt.col_offset), | ||
) | ||
) | ||
case ast.ImportFrom() | ast.Try(): | ||
raise NotImplementedError | ||
case _: | ||
print("BUG!: ", type(stmt)) | ||
# Add more elif clauses for other statement types... | ||
|
||
return messages | ||
|
||
|
||
def check_ast(path: Path, python_ast: list[ast.stmt]) -> list[Message]: | ||
messages = [] | ||
for stmt in python_ast: | ||
messages.extend(check_statement(path, stmt)) | ||
print("All Messages:", messages) | ||
return messages |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from ruff_in_python.message import Message | ||
from ruff_in_python.parser import parse_file | ||
from ruff_in_python.check import check_ast | ||
|
||
|
||
@dataclass | ||
class CacheMetadata: | ||
size: int | ||
mtime: int | ||
|
||
|
||
@dataclass | ||
class CheckResult: | ||
metadata: CacheMetadata | ||
messages: list[Message] | ||
|
||
def check_path(self, path: Path) -> list[Message]: | ||
# TODO: skip cache | ||
|
||
# Run the linter | ||
python_ast = parse_file(path) | ||
messages = check_ast(path, python_ast) | ||
|
||
# TODO: set cache | ||
|
||
return messages | ||
|
||
|
||
if __name__ == "__main__": | ||
result = CheckResult("dummy", []) | ||
result.check_path("tests/foo.py") | ||
|
||
result = CheckResult("dummy", []) | ||
result.check_path("tests/bar.py") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
if (1, 2): | ||
pass | ||
if (3, 4): | ||
pass | ||
|
||
for _ in range(5): | ||
if True: | ||
|