Skip to content

Commit 495ef8f

Browse files
olehermanseclaude
andcommitted
lint.py: Extracted constants / magic strings into named constants / variables
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent d9066ba commit 495ef8f

1 file changed

Lines changed: 17 additions & 21 deletions

File tree

src/cfengine_cli/lint.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040
from cfengine_cli.utils import UserError
4141

4242
LINT_EXTENSIONS = (".cf", ".json")
43+
DEFAULT_NAMESPACE = "default"
44+
VARS_TYPES = {
45+
"data",
46+
"ilist",
47+
"int",
48+
"real",
49+
"rlist",
50+
"slist",
51+
"string",
52+
}
53+
PROMISE_BLOCK_ATTRIBUTES = ("path", "interpreter")
54+
KNOWN_FAULTY_FUNCTION_DEFS = {"regex_replace"}
4355

4456

4557
@dataclass
@@ -179,7 +191,7 @@ class State:
179191
block_name: str | None = None
180192
promise_type: str | None = None # "vars" | "files" | "classes" | ... | None
181193
attribute_name: str | None = None # "if" | "string" | "slist" | ... | None
182-
namespace: str = "default" # "ns" | "default" | ... |
194+
namespace: str = DEFAULT_NAMESPACE # "ns" | "default" | ... |
183195
mode: Mode = Mode.NONE
184196
walking: bool = False
185197
strict: bool = True
@@ -244,7 +256,7 @@ def start_file(self, policy_file: PolicyFile):
244256
assert self.mode != Mode.NONE
245257

246258
self.policy_file = policy_file
247-
self.namespace = "default"
259+
self.namespace = DEFAULT_NAMESPACE
248260
self.walking = True
249261

250262
def end_file(self) -> None:
@@ -562,24 +574,14 @@ def _lint_node(
562574
return 1
563575
if state.promise_type == "vars" and node.type == "promise":
564576
attribute_nodes = [x for x in node.children if x.type == "attribute"]
565-
# Each vars promise must include exactly 1 of these attributes (a value):
566-
vars_types = {
567-
"data",
568-
"ilist",
569-
"int",
570-
"real",
571-
"rlist",
572-
"slist",
573-
"string",
574-
}
575577
# Attributes are children of a promise, and attribute names are children of attributes
576578
# Need to iterate inside to find the attribute name (data, ilist, int, etc.)
577579
value_nodes = []
578580
for attr in attribute_nodes:
579581
for child in attr.children:
580582
if child.type != "attribute_name":
581583
continue
582-
if _text(child) in vars_types:
584+
if _text(child) in VARS_TYPES:
583585
# Ignore the other attributes which are not values
584586
value_nodes.append(child)
585587

@@ -658,23 +660,17 @@ def _lint_node(
658660
if (
659661
state.block_keyword == "promise"
660662
and node.type == "attribute_name"
661-
and state.attribute_name
662-
not in (
663-
None,
664-
"path",
665-
"interpreter",
666-
)
663+
and state.attribute_name not in (None, *PROMISE_BLOCK_ATTRIBUTES)
667664
):
668665
_highlight_range(node, lines)
669666
print(
670667
f"Error: Invalid attribute name '{state.attribute_name}' in '{state.block_name}' custom promise type definition {location}"
671668
)
672669
return 1
673670
if node.type == "call":
674-
known_faulty_defs = {"regex_replace"}
675671
call, _, *args, _ = node.children # f ( a1 , a2 , a..N )
676672
call = _text(call)
677-
if call in known_faulty_defs:
673+
if call in KNOWN_FAULTY_FUNCTION_DEFS:
678674
return 0
679675

680676
args = list(filter(",".__ne__, iter(_text(x) for x in args)))

0 commit comments

Comments
 (0)