Skip to content

Commit

Permalink
Merge pull request #360 from sensebox/fix/variable-names
Browse files Browse the repository at this point in the history
replace special characters in variable names with underscores
  • Loading branch information
mariopesch authored Nov 14, 2024
2 parents bdacd60 + 653d400 commit d9e647b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/components/Blockly/generator/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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];
};

Expand Down
1 change: 1 addition & 0 deletions src/components/Blockly/msg/de/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* */
Expand Down
1 change: 1 addition & 0 deletions src/components/Blockly/msg/en/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* */
Expand Down
2 changes: 2 additions & 0 deletions src/components/CodeEditor/ErrorView.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const ErrorView = ({ error }) => {
const errorSuggestions = {
"'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) => {
Expand Down

0 comments on commit d9e647b

Please sign in to comment.