Skip to content

Commit d47c834

Browse files
authored
Merge pull request #25 from mxlint/mf-rule-nested-if-statements
2 parents 306e2e4 + c35b722 commit d47c834

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# METADATA
2+
# scope: package
3+
# title: Nested if-statements
4+
# description: Microflow actions with nested if-statements hide complexity and are harder to maintain.
5+
# authors:
6+
# - Bart Zantingh <bart.zantingh@nl.abnamro.com>
7+
# custom:
8+
# category: Complexity
9+
# rulename: NestedIfStatements
10+
# severity: MEDIUM
11+
# rulenumber: "005_0005"
12+
# remediation: Simplify the expression or use exclusive splits.
13+
# input: "**/*$Microflow.yaml"
14+
package app.mendix.microflows.nested_if_statements
15+
16+
import rego.v1
17+
18+
annotation := rego.metadata.chain()[1].annotations
19+
20+
default allow := false
21+
22+
allow if count(errors) == 0
23+
24+
regex_to_match := `^[\S\s]*(then|else)[\S\s]*(if)[\S\s]*$`
25+
26+
errors contains error if {
27+
some ex_split in input.ObjectCollection.Objects
28+
ex_split["$Type"] == "Microflows$ExclusiveSplit"
29+
30+
regex.match(regex_to_match, ex_split.SplitCondition.Expression)
31+
32+
error := sprintf(
33+
"[%v, %v, %v] Exclusive split with caption '%v' has nested if-statements in its expression",
34+
[
35+
annotation.custom.severity,
36+
annotation.custom.category,
37+
annotation.custom.rulenumber,
38+
ex_split.Caption,
39+
],
40+
)
41+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package app.mendix.microflows.nested_if_statements_test
2+
3+
import data.app.mendix.microflows.nested_if_statements
4+
import rego.v1
5+
6+
# Test data
7+
one_exclusive_split_with_no_nested_ifs := {"ObjectCollection": {
8+
"$Type": "Microflows$MicroflowObjectCollection",
9+
"Objects": [{
10+
"$Type": "Microflows$ExpressionSplitCondition",
11+
"Caption": "no nested ifs",
12+
"SplitCondition": {"Expression": "if $Variable then a else b"},
13+
}],
14+
}}
15+
16+
multiple_exclusive_splits_with_no_nested_ifs := {"ObjectCollection": {
17+
"$Type": "Microflows$MicroflowObjectCollection",
18+
"Objects": [
19+
{
20+
"$Type": "Microflows$ExpressionSplitCondition",
21+
"Caption": "ex spit 1 with no nested ifs",
22+
"SplitCondition": {"Expression": "if $Variable then a else b"},
23+
},
24+
{
25+
"$Type": "Microflows$ExpressionSplitCondition",
26+
"Caption": "ex split 2 with no nested ifs",
27+
"SplitCondition": {"Expression": "if $Variable then c else d"},
28+
},
29+
],
30+
}}
31+
32+
one_exclusive_split_with_else_if := {"ObjectCollection": {
33+
"$Type": "Microflows$MicroflowObjectCollection",
34+
"Objects": [{
35+
"$Type": "Microflows$ExclusiveSplit",
36+
"Caption": "ex split with else-if",
37+
"SplitCondition": {"Expression": "if true then\n\tfalse\nelse if false then\n\ttrue\nelse false"},
38+
}],
39+
}}
40+
41+
one_exclusive_split_with_then_if := {"ObjectCollection": {
42+
"$Type": "Microflows$MicroflowObjectCollection",
43+
"Objects": [{
44+
"$Type": "Microflows$ExclusiveSplit",
45+
"Caption": "ex split with then-if",
46+
"SplitCondition": {"Expression": "if a then if b then c else d else e"},
47+
}],
48+
}}
49+
50+
multiple_exclusive_splits_with_one_nested_if := {"ObjectCollection": {
51+
"$Type": "Microflows$MicroflowObjectCollection",
52+
"Objects": [
53+
{
54+
"$Type": "Microflows$ExclusiveSplit",
55+
"Caption": "no nested ifs",
56+
"SplitCondition": {"Expression": "if $Variable then a else b"},
57+
},
58+
{
59+
"$Type": "Microflows$ExclusiveSplit",
60+
"Caption": "ex split with then-if",
61+
"SplitCondition": {"Expression": "if a then b else if c then d else e"},
62+
},
63+
],
64+
}}
65+
66+
# Test cases
67+
test_should_allow_when_no_exclusive_splits_with_nested_ifs if {
68+
nested_if_statements.allow with input as one_exclusive_split_with_no_nested_ifs
69+
nested_if_statements.allow with input as multiple_exclusive_splits_with_no_nested_ifs
70+
}
71+
72+
test_should_deny_when_exclusive_splits_with_nested_ifs if {
73+
not nested_if_statements.allow with input as one_exclusive_split_with_else_if
74+
not nested_if_statements.allow with input as one_exclusive_split_with_then_if
75+
not nested_if_statements.allow with input as multiple_exclusive_splits_with_one_nested_if
76+
}

0 commit comments

Comments
 (0)