update
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from .base import BaseEngine
|
from .base import BaseEngine
|
||||||
|
from ast_checker.labels import label
|
||||||
|
|
||||||
|
|
||||||
class CountNodeEngine(BaseEngine):
|
class CountNodeEngine(BaseEngine):
|
||||||
@@ -7,9 +8,9 @@ class CountNodeEngine(BaseEngine):
|
|||||||
min_count = rule.get("min")
|
min_count = rule.get("min")
|
||||||
max_count = rule.get("max")
|
max_count = rule.get("max")
|
||||||
if min_count is not None and count < min_count:
|
if min_count is not None and count < min_count:
|
||||||
return rule.get("message") or f"{target} 至少出现 {min_count} 次,当前 {count} 次"
|
return rule.get("message") or f"{rule.get('label') or label(target)} 至少出现 {min_count} 次,当前 {count} 次"
|
||||||
if max_count is not None and count > max_count:
|
if max_count is not None and count > max_count:
|
||||||
return rule.get("message") or f"{target} 至多出现 {max_count} 次,当前 {count} 次"
|
return rule.get("message") or f"{rule.get('label') or label(target)} 至多出现 {max_count} 次,当前 {count} 次"
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def check(self, tree, rule, language, mapping):
|
def check(self, tree, rule, language, mapping):
|
||||||
@@ -30,4 +31,4 @@ class CountNodeEngine(BaseEngine):
|
|||||||
parts.append(f"至少 {min_count} 次")
|
parts.append(f"至少 {min_count} 次")
|
||||||
if max_count is not None:
|
if max_count is not None:
|
||||||
parts.append(f"至多 {max_count} 次")
|
parts.append(f"至多 {max_count} 次")
|
||||||
return f"{target} " + "、".join(parts)
|
return f"{rule.get('label') or label(target)} " + "、".join(parts)
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
from .base import BaseEngine
|
from .base import BaseEngine
|
||||||
|
from ast_checker.labels import label
|
||||||
|
|
||||||
|
|
||||||
class MustExistNodeEngine(BaseEngine):
|
class MustExistNodeEngine(BaseEngine):
|
||||||
def _message(self, rule):
|
def _message(self, rule):
|
||||||
return rule.get("message") or f"必须使用 {rule['target']}"
|
return rule.get("message") or f"必须使用 {rule.get('label') or label(rule['target'])}"
|
||||||
|
|
||||||
def check(self, tree, rule, language, mapping):
|
def check(self, tree, rule, language, mapping):
|
||||||
node_type = mapping.get(rule["target"], rule["target"])
|
node_type = mapping.get(rule["target"], rule["target"])
|
||||||
@@ -17,7 +18,7 @@ class MustExistNodeEngine(BaseEngine):
|
|||||||
|
|
||||||
class MustNotExistNodeEngine(BaseEngine):
|
class MustNotExistNodeEngine(BaseEngine):
|
||||||
def _message(self, rule):
|
def _message(self, rule):
|
||||||
return rule.get("message") or f"不能使用 {rule['target']}"
|
return rule.get("message") or f"不能使用 {rule.get('label') or label(rule['target'])}"
|
||||||
|
|
||||||
def check(self, tree, rule, language, mapping):
|
def check(self, tree, rule, language, mapping):
|
||||||
node_type = mapping.get(rule["target"], rule["target"])
|
node_type = mapping.get(rule["target"], rule["target"])
|
||||||
|
|||||||
21
ast_checker/labels.py
Normal file
21
ast_checker/labels.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
TARGET_LABELS: dict[str, str] = {
|
||||||
|
"for_loop": "for 循环",
|
||||||
|
"while_loop": "while 循环",
|
||||||
|
"if_statement": "if 条件",
|
||||||
|
"else_clause": "else 子句",
|
||||||
|
"function_definition": "函数定义",
|
||||||
|
"return": "return 语句",
|
||||||
|
"break": "break 语句",
|
||||||
|
"continue": "continue 语句",
|
||||||
|
"list_comprehension": "列表推导式",
|
||||||
|
"list_literal": "列表",
|
||||||
|
"dict_literal": "字典",
|
||||||
|
"set_literal": "集合",
|
||||||
|
"f_string": "f-string",
|
||||||
|
"try_except": "try-except",
|
||||||
|
"class_definition": "类定义",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def label(target: str) -> str:
|
||||||
|
return TARGET_LABELS.get(target, target)
|
||||||
Reference in New Issue
Block a user