|
40 | 40 | from cfengine_cli.utils import UserError |
41 | 41 |
|
42 | 42 | 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"} |
43 | 55 |
|
44 | 56 |
|
45 | 57 | @dataclass |
@@ -179,7 +191,7 @@ class State: |
179 | 191 | block_name: str | None = None |
180 | 192 | promise_type: str | None = None # "vars" | "files" | "classes" | ... | None |
181 | 193 | attribute_name: str | None = None # "if" | "string" | "slist" | ... | None |
182 | | - namespace: str = "default" # "ns" | "default" | ... | |
| 194 | + namespace: str = DEFAULT_NAMESPACE # "ns" | "default" | ... | |
183 | 195 | mode: Mode = Mode.NONE |
184 | 196 | walking: bool = False |
185 | 197 | strict: bool = True |
@@ -244,7 +256,7 @@ def start_file(self, policy_file: PolicyFile): |
244 | 256 | assert self.mode != Mode.NONE |
245 | 257 |
|
246 | 258 | self.policy_file = policy_file |
247 | | - self.namespace = "default" |
| 259 | + self.namespace = DEFAULT_NAMESPACE |
248 | 260 | self.walking = True |
249 | 261 |
|
250 | 262 | def end_file(self) -> None: |
@@ -562,24 +574,14 @@ def _lint_node( |
562 | 574 | return 1 |
563 | 575 | if state.promise_type == "vars" and node.type == "promise": |
564 | 576 | 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 | | - } |
575 | 577 | # Attributes are children of a promise, and attribute names are children of attributes |
576 | 578 | # Need to iterate inside to find the attribute name (data, ilist, int, etc.) |
577 | 579 | value_nodes = [] |
578 | 580 | for attr in attribute_nodes: |
579 | 581 | for child in attr.children: |
580 | 582 | if child.type != "attribute_name": |
581 | 583 | continue |
582 | | - if _text(child) in vars_types: |
| 584 | + if _text(child) in VARS_TYPES: |
583 | 585 | # Ignore the other attributes which are not values |
584 | 586 | value_nodes.append(child) |
585 | 587 |
|
@@ -658,23 +660,17 @@ def _lint_node( |
658 | 660 | if ( |
659 | 661 | state.block_keyword == "promise" |
660 | 662 | 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) |
667 | 664 | ): |
668 | 665 | _highlight_range(node, lines) |
669 | 666 | print( |
670 | 667 | f"Error: Invalid attribute name '{state.attribute_name}' in '{state.block_name}' custom promise type definition {location}" |
671 | 668 | ) |
672 | 669 | return 1 |
673 | 670 | if node.type == "call": |
674 | | - known_faulty_defs = {"regex_replace"} |
675 | 671 | call, _, *args, _ = node.children # f ( a1 , a2 , a..N ) |
676 | 672 | call = _text(call) |
677 | | - if call in known_faulty_defs: |
| 673 | + if call in KNOWN_FAULTY_FUNCTION_DEFS: |
678 | 674 | return 0 |
679 | 675 |
|
680 | 676 | args = list(filter(",".__ne__, iter(_text(x) for x in args))) |
|
0 commit comments