Skip to content

Commit 0deaaa0

Browse files
authored
Implementation of Design Queries in Rego (#68)
2 parents d585edf + b429baa commit 0deaaa0

19 files changed

Lines changed: 426 additions & 317 deletions

glitch/analysis/design/imperative_abstraction.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from glitch.analysis.design.smell_checker import DesignSmellChecker
55
from glitch.repr.inter import *
66

7+
# Deprecated: This rule has been rewritten in Rego.
8+
# This is kept for reference and possible future use since there are no tests for the Rego version yet.
79

810
class ImperativeAbstraction(DesignSmellChecker):
911
def __count_atomic_units(self, ub: UnitBlock) -> Tuple[int, int]:

glitch/analysis/design/long_resource.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

glitch/analysis/design/misplaced_attribute.py

Lines changed: 0 additions & 64 deletions
This file was deleted.

glitch/analysis/design/multifaceted_abstraction.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

glitch/analysis/design/too_many_variables.py

Lines changed: 0 additions & 57 deletions
This file was deleted.

glitch/analysis/design/visitor.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ def __init__(self, tech: Tech, fallback: set[str]) -> None:
1616
from glitch.analysis.design.duplicate_block import DuplicateBlock
1717
from glitch.analysis.design.imperative_abstraction import ImperativeAbstraction
1818
from glitch.analysis.design.improper_alignment import ImproperAlignmentTabs, ImproperAlignment, PuppetImproperAlignment
19-
from glitch.analysis.design.long_resource import LongResource
2019
from glitch.analysis.design.long_statement import LongStatement
21-
from glitch.analysis.design.misplaced_attribute import ChefMisplacedAttribute, PuppetMisplacedAttribute
22-
from glitch.analysis.design.multifaceted_abstraction import MultifacetedAbstraction
23-
from glitch.analysis.design.too_many_variables import TooManyVariables
2420

2521
DESIGN_CHECKER_ERRORS: Dict[Type[DesignSmellChecker], str] = {
2622
UnguardedVariable: "implementation_unguarded_variable",
@@ -29,12 +25,7 @@ def __init__(self, tech: Tech, fallback: set[str]) -> None:
2925
ImproperAlignmentTabs: "implementation_improper_alignment",
3026
ImproperAlignment: "implementation_improper_alignment",
3127
PuppetImproperAlignment: "implementation_improper_alignment",
32-
LongResource: "design_long_resource",
3328
LongStatement: "implementation_long_statement",
34-
ChefMisplacedAttribute: "design_misplaced_attribute",
35-
PuppetMisplacedAttribute: "design_misplaced_attribute",
36-
MultifacetedAbstraction: "design_multifaceted_abstraction",
37-
TooManyVariables: "implementation_too_many_variables"
3829
}
3930

4031
self.checkers: List[DesignSmellChecker] = []
@@ -95,12 +86,6 @@ def check_unitblock(self, u: UnitBlock, file: str) -> List[Error]:
9586
else:
9687
self.code_lines = []
9788

98-
self.first_non_comm_line = inf
99-
for i, line in enumerate(self.code_lines):
100-
if not line.startswith(self.comment):
101-
self.first_non_comm_line = i + 1
102-
break
103-
10489
self.variable_stack.append(len(self.variables_names))
10590
for attr in u.attributes:
10691
self.variables_names.append(attr.name)
@@ -165,10 +150,7 @@ def check_variable(self, v: Variable, file: str) -> list[Error]:
165150
return []
166151

167152
def check_comment(self, c: Comment, file: str) -> list[Error]:
168-
errors: List[Error] = []
169-
if c.line >= self.first_non_comm_line:
170-
errors.append(Error("design_avoid_comments", c, file, repr(c)))
171-
return errors
153+
return []
172154

173155

174156
# NOTE: in the end of the file to avoid circular import
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package glitch
2+
3+
import data.glitch_lib
4+
5+
get_first_line(elements) = line {
6+
count(elements) > 0
7+
line = { elements[0].line }
8+
}
9+
get_first_line(elements) = set() {
10+
count(elements) == 0
11+
}
12+
13+
Glitch_Analysis[result] {
14+
parent := glitch_lib._gather_parent_unit_blocks[_]
15+
parent.path != ""
16+
count(parent.comments) > 0
17+
18+
lines := { parent.line | parent.line > 0 }
19+
| get_first_line(parent.atomic_units)
20+
| get_first_line(parent.statements)
21+
| get_first_line(parent.unit_blocks)
22+
23+
count(lines) > 0
24+
line := min(lines)
25+
26+
comment := parent.comments[_]
27+
comment.line >= line
28+
29+
result := {{
30+
"type": "design_avoid_comments",
31+
"element": comment,
32+
"path": parent.path,
33+
"description": "Avoid comments - Comments may lead to bad code or be used as a way to justify bad code."
34+
}}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package glitch
2+
3+
import data.glitch_lib
4+
5+
Glitch_Analysis[result] {
6+
parent := glitch_lib._gather_parent_unit_blocks[_]
7+
parent.path != ""
8+
9+
resources := count(glitch_lib.all_atomic_units(parent))
10+
executions := count({au |
11+
au := glitch_lib.all_atomic_units(parent)[_]
12+
au.type == data.design.exec_atomic_units[_]
13+
})
14+
15+
executions > 2
16+
(executions / resources) > 0.2
17+
18+
result := {{
19+
"type": "design_imperative_abstraction",
20+
"element": parent,
21+
"path": parent.path,
22+
"description": "Imperative abstraction - The presence of imperative statements defies the purpose of IaC declarative languages."
23+
}}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package glitch
2+
3+
import data.glitch_lib
4+
5+
Glitch_Analysis[result] {
6+
parent := glitch_lib._gather_parent_unit_blocks[_]
7+
parent.path != ""
8+
atomic_units := glitch_lib.all_atomic_units(parent)
9+
node := atomic_units[_]
10+
node.type == data.design.exec_atomic_units[_]
11+
12+
lines := [
13+
line |
14+
attr := node.attributes[_]
15+
line := split(attr.code, "\n")[_]
16+
not regex.match("^\\s*$", line)
17+
]
18+
19+
count(lines) > 7
20+
21+
result := {{
22+
"type": "design_long_resource",
23+
"element": node,
24+
"path": parent.path,
25+
"description": "Long Resource - Long resources may decrease the readability and maintainability of the code."
26+
}}
27+
}

0 commit comments

Comments
 (0)