multicloud_rule_manager 1.1.8 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/utils/ruleValidator.js +77 -50
package/package.json
CHANGED
package/utils/ruleValidator.js
CHANGED
|
@@ -13,62 +13,89 @@ export function validateRule(fileName, content) {
|
|
|
13
13
|
let inThen = false;
|
|
14
14
|
|
|
15
15
|
for (let i = 0; i < lines.length; i++) {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
const lines = content.split("\n");
|
|
17
|
+
|
|
18
|
+
let stack = [];
|
|
19
|
+
let hasWhen = false;
|
|
20
|
+
let hasThen = false;
|
|
21
|
+
let thenCode = "";
|
|
22
|
+
let inThen = false;
|
|
23
|
+
let thenStack = []; // stack per parentesi dentro then
|
|
24
|
+
|
|
25
|
+
for (let i = 0; i < lines.length; i++) {
|
|
26
|
+
const line = lines[i];
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
if (line.includes("
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
// controllo blocchi when/then
|
|
29
|
+
if (line.includes("when {")) hasWhen = true;
|
|
30
|
+
if (line.includes("}then {")) {
|
|
31
|
+
hasThen = true;
|
|
32
|
+
inThen = true;
|
|
33
|
+
continue; // skip linea "}then {"
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// dentro then {}
|
|
37
|
+
if (inThen) {
|
|
38
|
+
// aggiungi linea al codice then
|
|
39
|
+
thenCode += line + "\n";
|
|
40
|
+
|
|
41
|
+
// aggiorna stack interno per bilanciamento
|
|
42
|
+
for (const char of line) {
|
|
43
|
+
if (char === "{") thenStack.push("{");
|
|
44
|
+
if (char === "}") {
|
|
45
|
+
if (thenStack.length === 0) {
|
|
46
|
+
inThen = false; // chiuso il then
|
|
47
|
+
} else {
|
|
48
|
+
thenStack.pop();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// esci dal then se stack vuoto
|
|
54
|
+
if (thenStack.length === 0 && line.includes("}")) {
|
|
55
|
+
inThen = false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
continue; // passa al prossimo
|
|
31
59
|
}
|
|
32
|
-
}
|
|
33
60
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
61
|
+
// check parentesi graffe globali
|
|
62
|
+
for (const char of line) {
|
|
63
|
+
if (char === "{") stack.push("{");
|
|
64
|
+
if (char === "}") {
|
|
65
|
+
if (stack.length === 0) {
|
|
66
|
+
console.error(`❌ Parentesi graffe non bilanciate in ${fileName} riga ${i + 1}`);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
stack.pop();
|
|
41
70
|
}
|
|
42
|
-
stack.pop();
|
|
43
71
|
}
|
|
72
|
+
|
|
73
|
+
// check apici
|
|
74
|
+
const singleQuotes = (line.match(/'/g) || []).length;
|
|
75
|
+
const doubleQuotes = (line.match(/"/g) || []).length;
|
|
76
|
+
if (singleQuotes % 2 !== 0 || doubleQuotes % 2 !== 0) {
|
|
77
|
+
console.error(`❌ Apici non bilanciati in ${fileName} riga ${i + 1}`);
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!hasWhen || !hasThen) {
|
|
83
|
+
console.error(`❌ Il file ${fileName} deve contenere un blocco "when {} then {}"`);
|
|
84
|
+
return false;
|
|
44
85
|
}
|
|
45
86
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const doubleQuotes = (line.match(/"/g) || []).length;
|
|
49
|
-
if (singleQuotes % 2 !== 0 || doubleQuotes % 2 !== 0) {
|
|
50
|
-
console.error(`❌ Apici non bilanciati in ${fileName} riga ${i+1}`);
|
|
87
|
+
if (stack.length > 0) {
|
|
88
|
+
console.error(`❌ Parentesi graffe aperte non chiuse in ${fileName}`);
|
|
51
89
|
return false;
|
|
52
90
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// check sintassi JS del codice dentro then {}
|
|
66
|
-
try {
|
|
67
|
-
if (thenCode.trim()) esprima.parseScript(thenCode, { tolerant: true });
|
|
68
|
-
} catch (err) {
|
|
69
|
-
console.error(`❌ Errore sintassi JS in ${fileName}: ${err.message}`);
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
91
|
+
|
|
92
|
+
// check sintassi JS dentro then {}
|
|
93
|
+
try {
|
|
94
|
+
if (thenCode.trim()) esprima.parseScript(thenCode, { tolerant: true });
|
|
95
|
+
} catch (err) {
|
|
96
|
+
console.error(`❌ Errore sintassi JS in ${fileName}: ${err.message}`);
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return true;
|
|
101
|
+
}
|