@@ -23,7 +23,6 @@ class CodeGenerator {
2323 this . addLine ( ) ;
2424 }
2525 this . generateImports ( ) ;
26- this . generateVariableDeclarations ( ) ;
2726 this . generateMainFunction ( ) ;
2827 return this . code . trim ( ) ; // Trim to remove any trailing newlines
2928 }
@@ -33,27 +32,6 @@ class CodeGenerator {
3332 this . addLine ( ) ;
3433 }
3534
36- generateVariableDeclarations ( ) {
37- const declaredVariables = new Set ( ) ;
38- this . nodes . forEach ( node => {
39- if ( node . type === 'Variable' ) {
40- const { name, type, initialValue } = node . properties ;
41- if ( name && type && ! declaredVariables . has ( name ) ) {
42- let declaration = `${ this . settings . useConst ? 'const' : 'let' } ${ name } ` ;
43- if ( initialValue !== undefined && initialValue !== '' ) {
44- declaration += ` = ${ this . getTypedValue ( type , initialValue ) } ` ;
45- }
46- this . addLine ( declaration ) ;
47- this . variables . set ( name , type ) ;
48- declaredVariables . add ( name ) ;
49- }
50- }
51- } ) ;
52- if ( declaredVariables . size > 0 ) {
53- this . addLine ( ) ;
54- }
55- }
56-
5735 generateMainFunction ( ) {
5836 this . hasAsyncOperations = this . nodes . some ( node => node . type === 'HttpRequest' || node . type === 'WaitForSeconds' ) ;
5937 const asyncKeyword = this . hasAsyncOperations ? 'async ' : '' ;
@@ -202,8 +180,21 @@ class CodeGenerator {
202180
203181 // Generate case statements
204182 cases . forEach ( ( caseObj , index ) => {
205- const caseValue = ignoreCase ? caseObj . value . toLowerCase ( ) : caseObj . value ;
206- this . addLine ( `case ${ JSON . stringify ( caseValue ) } :` ) ;
183+ // Convert the case value based on its type
184+ let caseValue ;
185+ switch ( caseObj . type ) {
186+ case 'number' :
187+ caseValue = Number ( caseObj . value ) ;
188+ break ;
189+ case 'boolean' :
190+ caseValue = caseObj . value === 'true' ;
191+ break ;
192+ default : // string
193+ caseValue = ignoreCase ? caseObj . value . toLowerCase ( ) : caseObj . value ;
194+ caseValue = JSON . stringify ( caseValue ) ;
195+ }
196+
197+ this . addLine ( `case ${ caseValue } :` ) ;
207198 this . indentLevel ++ ;
208199
209200 // Find and generate code for the case branch
@@ -282,11 +273,24 @@ class CodeGenerator {
282273 }
283274
284275 handleVariableNode ( node ) {
285- const { name } = node . properties ;
276+ const { name, type , initialValue } = node . properties ;
286277 const inputValue = this . getNodeInputValue ( node , 1 ) ;
287- if ( inputValue !== undefined && inputValue !== name ) {
288- this . addLine ( `${ name } = ${ inputValue } ;` ) ;
278+
279+ if ( ! this . variables . has ( name ) ) {
280+ // First time this variable is used - declare and initialize it
281+ let declaration = `${ this . settings . useConst ? 'const' : 'let' } ${ name } ` ;
282+ if ( inputValue !== undefined ) {
283+ declaration += ` = ${ inputValue } ` ;
284+ } else if ( initialValue !== undefined && initialValue !== '' ) {
285+ declaration += ` = ${ this . getTypedValue ( type , initialValue ) } ` ;
286+ }
287+ this . addLine ( declaration ) ;
288+ this . variables . set ( name , type ) ;
289+ } else if ( inputValue !== undefined && inputValue !== name ) {
290+ // Variable already declared, just update its value
291+ this . addLine ( `${ name } = ${ inputValue } ` ) ;
289292 }
293+
290294 this . nodeOutputs . set ( node . id , name ) ;
291295 }
292296
@@ -435,7 +439,8 @@ class CodeGenerator {
435439 } else {
436440 const indentation = ' ' . repeat ( this . indentLevel ) ;
437441 const semicolon = this . settings . useSemicolons && this . shouldAddSemicolon ( line ) ? ';' : '' ;
438- this . code += `${ indentation } ${ line } ${ semicolon } \n` ;
442+ const cleanLine = line . replace ( / ; $ / , '' ) ;
443+ this . code += `${ indentation } ${ cleanLine } ${ semicolon } \n` ;
439444 }
440445 }
441446
@@ -692,8 +697,12 @@ class CodeGenerator {
692697 / ^ t r y / ,
693698 / ^ c a t c h / ,
694699 / ^ f i n a l l y / ,
700+ / ^ c a s e \s / , // Added to prevent semicolons after case statements
701+ / ^ d e f a u l t : / , // Added to prevent semicolons after default:
695702 / \{ $ / ,
696703 / \} $ / ,
704+ / \} ; $ / , // Added to prevent double semicolons
705+ / ; $ / , // Added to prevent double semicolons
697706 // Also don't add semicolons after comments
698707 / ^ \/ \/ / ,
699708 / ^ \/ \* /
0 commit comments