From 6c267b2961a257ee09e2bb976ac7fd497ad2a334 Mon Sep 17 00:00:00 2001 From: PaulaScharf Date: Wed, 13 Nov 2024 16:19:01 +0100 Subject: [PATCH 1/2] replace special characters in variable names with underscores --- src/components/Blockly/generator/variables.js | 6 +++--- src/components/Blockly/msg/de/ui.js | 1 + src/components/Blockly/msg/en/ui.js | 1 + src/components/CodeEditor/ErrorView.js | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/components/Blockly/generator/variables.js b/src/components/Blockly/generator/variables.js index 092a34c2..a8dfaf27 100644 --- a/src/components/Blockly/generator/variables.js +++ b/src/components/Blockly/generator/variables.js @@ -26,8 +26,8 @@ const setVariableFunction = function (defaultValue) { var code = ""; if (myVar !== undefined) { Blockly.Arduino.variables_[variableName + myVar.type] = - myVar.type + " " + myVar.name + ";\n"; - code = variableName + " = " + (variableValue || defaultValue) + ";\n"; + myVar.type + " " + myVar.name.replace(/_/g, '__').replace(/[^a-zA-Z0-9_]/g, '_') + ";\n"; + code = myVar.name.replace(/_/g, '__').replace(/[^a-zA-Z0-9_]/g, '_') + " = " + (variableValue || defaultValue) + ";\n"; } return code; }; @@ -47,7 +47,7 @@ const getVariableFunction = function (block) { // block.getFieldValue("VAR"), // Blockly.Variables.NAME_TYPE // ); - var code = myVar.name; + var code = myVar.name.replace(/_/g, '__').replace(/[^a-zA-Z0-9_]/g, '_'); return [code, Blockly["Arduino"].ORDER_ATOMIC]; }; diff --git a/src/components/Blockly/msg/de/ui.js b/src/components/Blockly/msg/de/ui.js index 28edd324..1181491f 100644 --- a/src/components/Blockly/msg/de/ui.js +++ b/src/components/Blockly/msg/de/ui.js @@ -339,6 +339,7 @@ export const UI = { display_not_declared: "Stelle sicher, dasss du das Display im Setup initialisiert hast.", + variable_redeclared: "Stelle sicher, dass du keine Sonderzeichen in deinen Variablennamen verwendest. Dazu gehören z.B. Leerzeichen, Sternchen oder Anführungszeichen.", /** * Code Editor * */ diff --git a/src/components/Blockly/msg/en/ui.js b/src/components/Blockly/msg/en/ui.js index cec412d7..837e8426 100644 --- a/src/components/Blockly/msg/en/ui.js +++ b/src/components/Blockly/msg/en/ui.js @@ -344,6 +344,7 @@ export const UI = { suggestion_pre_text: "Maybe you should try:", display_not_declared: "Initialise the display in the setup() function", + variable_redeclared: "Make sure that you do not use any special characters in your variable names. This includes spaces, asterisks or quotation marks.", /** * Device Selection * */ diff --git a/src/components/CodeEditor/ErrorView.js b/src/components/CodeEditor/ErrorView.js index 7459476f..9571a5ae 100644 --- a/src/components/CodeEditor/ErrorView.js +++ b/src/components/CodeEditor/ErrorView.js @@ -10,6 +10,7 @@ export const ErrorView = ({ error }) => { const errorSuggestions = { "'display' was not declared in this scope": Blockly.Msg.display_not_declared, + "redefinition of": Blockly.Msg.variable_redeclared, }; const getSuggestion = (process) => { From 653d40055d28306a18c285a2546465877a4ebebe Mon Sep 17 00:00:00 2001 From: PaulaScharf Date: Wed, 13 Nov 2024 17:06:01 +0100 Subject: [PATCH 2/2] variable redeclaration suggestion also shown for "conflicting ..." --- src/components/CodeEditor/ErrorView.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/CodeEditor/ErrorView.js b/src/components/CodeEditor/ErrorView.js index 9571a5ae..7a11ddc2 100644 --- a/src/components/CodeEditor/ErrorView.js +++ b/src/components/CodeEditor/ErrorView.js @@ -11,6 +11,7 @@ export const ErrorView = ({ error }) => { "'display' was not declared in this scope": Blockly.Msg.display_not_declared, "redefinition of": Blockly.Msg.variable_redeclared, + "conflicting declaration": Blockly.Msg.variable_redeclared, }; const getSuggestion = (process) => {