Skip to content
Snippets Groups Projects
Commit e0a8f01c authored by Robin Leber's avatar Robin Leber
Browse files

check for valid const names

parent 7d95251b
No related branches found
No related tags found
No related merge requests found
...@@ -40,7 +40,7 @@ export const handlePipeBinding = (pipeMapping, editor) => { ...@@ -40,7 +40,7 @@ export const handlePipeBinding = (pipeMapping, editor) => {
let line = editor.state.doc.line(lineNumber); let line = editor.state.doc.line(lineNumber);
let position = line.from; let position = line.from;
let pipeNameUserGiven = pipe.pipeName; let pipeNameUserGiven = pipe.pipeName;
let pipeNameDeklaration = transformPipeName(pipeNameUserGiven); let pipeNameDeklaration = makeValidConstName(pipeNameUserGiven);
pipeNameUserGiven.replace(/\s+/g, ""); pipeNameUserGiven.replace(/\s+/g, "");
let insertCode = `\t\tconst ${pipeNameDeklaration} = "${pipeNameUserGiven}"\n\t\tawait channel.assertQueue(${pipeNameDeklaration}, {\n\t\t\tdurable: false\n\t\t});\n`; let insertCode = `\t\tconst ${pipeNameDeklaration} = "${pipeNameUserGiven}"\n\t\tawait channel.assertQueue(${pipeNameDeklaration}, {\n\t\t\tdurable: false\n\t\t});\n`;
let transaction = editor.state.update({ let transaction = editor.state.update({
...@@ -55,9 +55,43 @@ export const handlePipeBinding = (pipeMapping, editor) => { ...@@ -55,9 +55,43 @@ export const handlePipeBinding = (pipeMapping, editor) => {
}); });
}; };
const transformPipeName = (str) => { const makeValidConstName = (str) => {
if (!str) return str; // Entferne nicht erlaubte Zeichen, nur Buchstaben, Zahlen, _ und $ sind erlaubt
const noSpacesStr = str.replace(/\s+/g, ""); let validStr = str.replace(/[^a-zA-Z0-9_$]/g, "");
if (noSpacesStr.length === 0) return "";
return noSpacesStr.charAt(0).toLowerCase() + noSpacesStr.slice(1); // Stelle sicher, dass der Name nicht mit einer Zahl beginnt
if (/^[0-9]/.test(validStr)) {
validStr = "_" + validStr;
}
// Überprüfen und anpassen, falls der Name ein reserviertes Wort ist (einfaches Beispiel)
const reservedWords = new Set([
"var",
"let",
"const",
"function",
"if",
"else",
"for",
"while",
"return",
"null",
"true",
"false",
"new",
"class",
"import",
"export",
"default",
]);
if (reservedWords.has(validStr)) {
validStr = "_" + validStr;
}
// Fallback, falls der String nach Entfernen aller ungültigen Zeichen leer ist
if (validStr.length === 0) {
validStr = "_const";
}
return validStr;
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment