This commit is contained in:
2026-05-25 23:06:01 -06:00
parent 92eeb91fd7
commit 1498e1046f
2 changed files with 22 additions and 20 deletions

View File

@@ -47,6 +47,9 @@ class MustNotCallFunctionEngine(_FunctionCallBase):
class CountFunctionCallEngine(_FunctionCallBase):
def _message(self, rule, count):
target = rule["target"]
exact = rule.get("exact")
if exact is not None and count != exact:
return rule.get("message") or f"{target}() 需要调用 {exact} 次,当前 {count}"
min_count = rule.get("min")
max_count = rule.get("max")
if min_count is not None and count < min_count:
@@ -62,13 +65,14 @@ class CountFunctionCallEngine(_FunctionCallBase):
def describe(self, rule, language, mapping):
target = rule["target"]
min_count = rule.get("min")
max_count = rule.get("max")
if rule.get("message"):
return rule["message"]
exact = rule.get("exact")
if exact is not None:
return f"{target}() 调用 {exact}"
parts = []
if min_count is not None:
parts.append(f"至少 {min_count}")
if max_count is not None:
parts.append(f"至多 {max_count}")
if rule.get("min") is not None:
parts.append(f"至少 {rule['min']}")
if rule.get("max") is not None:
parts.append(f"至多 {rule['max']}")
return f"{target}() " + "".join(parts)

View File

@@ -4,12 +4,12 @@ from ast_checker.labels import label
class CountNodeEngine(BaseEngine):
def _message(self, rule, count):
target = rule["target"]
name = rule.get("label") or label(target)
name = rule.get("label") or label(rule["target"])
exact = rule.get("exact")
if exact is not None and count != exact:
return rule.get("message") or f"{name} 需要出现 {exact} 次,当前 {count}"
min_count = rule.get("min")
max_count = rule.get("max")
if min_count is not None and min_count == max_count and count != min_count:
return rule.get("message") or f"{name} 需要出现 {min_count} 次,当前 {count}"
if min_count is not None and count < min_count:
return rule.get("message") or f"{name} 至少出现 {min_count} 次,当前 {count}"
if max_count is not None and count > max_count:
@@ -24,17 +24,15 @@ class CountNodeEngine(BaseEngine):
return [msg] if msg else []
def describe(self, rule, language, mapping):
target = rule["target"]
name = rule.get("label") or label(target)
min_count = rule.get("min")
max_count = rule.get("max")
name = rule.get("label") or label(rule["target"])
if rule.get("message"):
return rule["message"]
if min_count is not None and min_count == max_count:
return f"{name} 出现 {min_count}"
exact = rule.get("exact")
if exact is not None:
return f"{name} 出现 {exact}"
parts = []
if min_count is not None:
parts.append(f"至少 {min_count}")
if max_count is not None:
parts.append(f"至多 {max_count}")
if rule.get("min") is not None:
parts.append(f"至少 {rule['min']}")
if rule.get("max") is not None:
parts.append(f"至多 {rule['max']}")
return f"{name} " + "".join(parts)