multicloud_rule_manager 1.1.4 → 1.1.6
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 +69 -21
package/package.json
CHANGED
package/utils/ruleValidator.js
CHANGED
|
@@ -1,32 +1,80 @@
|
|
|
1
|
+
import fs from "fs";
|
|
1
2
|
import esprima from "esprima";
|
|
2
3
|
|
|
3
4
|
export function validateRule(fileName, content) {
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
|
|
7
|
+
const lines = content.split("\n");
|
|
8
|
+
|
|
9
|
+
let stack = [];
|
|
10
|
+
let hasWhen = false;
|
|
11
|
+
let hasThen = false;
|
|
12
|
+
let thenCode = "";
|
|
13
|
+
let inThen = false;
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < lines.length; i++) {
|
|
16
|
+
const line = lines[i];
|
|
17
|
+
|
|
18
|
+
// controllo blocchi when/then
|
|
19
|
+
if (line.includes("when {")) hasWhen = true;
|
|
20
|
+
if (line.includes("}then {")) {
|
|
21
|
+
hasThen = true;
|
|
22
|
+
inThen = true;
|
|
23
|
+
continue; // skip linea "}then {"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (inThen) {
|
|
27
|
+
if (line.includes("}")) {
|
|
28
|
+
inThen = false;
|
|
29
|
+
} else {
|
|
30
|
+
thenCode += line + "\n"; // raccogli solo il codice JS nel then
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// check parentesi graffe
|
|
35
|
+
for (const char of line) {
|
|
36
|
+
if (char === "{") stack.push("{");
|
|
37
|
+
if (char === "}") {
|
|
38
|
+
if (stack.length === 0) {
|
|
39
|
+
console.error(`❌ Parentesi graffe non bilanciate in ${filePath} riga ${i+1}`);
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
stack.pop();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// check apici
|
|
47
|
+
const singleQuotes = (line.match(/'/g) || []).length;
|
|
48
|
+
const doubleQuotes = (line.match(/"/g) || []).length;
|
|
49
|
+
if (singleQuotes % 2 !== 0 || doubleQuotes % 2 !== 0) {
|
|
50
|
+
console.error(`❌ Apici non bilanciati in ${filePath} riga ${i+1}`);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
13
53
|
}
|
|
14
54
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return
|
|
18
|
-
valid: false,
|
|
19
|
-
message: `Blocco "when {}" mancante in ${fileName}`
|
|
20
|
-
};
|
|
55
|
+
if (!hasWhen || !hasThen) {
|
|
56
|
+
console.error(`❌ Il file ${filePath} deve contenere un blocco "when {} then {}"`);
|
|
57
|
+
return false;
|
|
21
58
|
}
|
|
22
59
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return
|
|
26
|
-
valid: false,
|
|
27
|
-
message: `Blocco "then {}" mancante in ${fileName}`
|
|
28
|
-
};
|
|
60
|
+
if (stack.length > 0) {
|
|
61
|
+
console.error(`❌ Parentesi graffe aperte non chiuse in ${filePath}`);
|
|
62
|
+
return false;
|
|
29
63
|
}
|
|
30
64
|
|
|
31
|
-
|
|
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 ${filePath}: ${err.message}`);
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Esempio utilizzo:
|
|
77
|
+
const file = "./rules/InitRule_0518b880-8b5f-4022-87f5-5771c776bef0.js";
|
|
78
|
+
if (!validateRuleFile(file)) {
|
|
79
|
+
console.error("❌ Deploy bloccato per errori di sintassi");
|
|
32
80
|
}
|