benmerckx / genes

Generates split ES6 modules and Typescript definitions from Haxe modules.
43 stars 8 forks source link

Generating var instead of let #71

Closed vantreeseba closed 11 months ago

vantreeseba commented 11 months ago

Sometimes genes will generate var instead of let in functions, this causes var shadowing in nested loops which makes the output basically invalid.

Here are examples:

JS GENERATOR

static getRect(map,rect,wrap) {                                                                        
    if(wrap == null) {                                                                                   
      wrap = false;                                                                                      
    }                                                                                                    
    let _g = [];                                                                                         
    let _g1 = rect.y;                                                                                    
    let _g2 = rect.y + rect.height;                                                                      
    while(_g1 < _g2) {                                                                                   
      let j = _g1++;                                                                                     
      let _g2 = rect.x;                                                                                  
      let _g3 = rect.x + rect.width;                                                                     
      while(_g2 < _g3) {                                                                                 
        let i = _g2++;                                                                                   
        if(wrap) {                                                                                       
          _g.push(map._mapData[map._width * (j % map._height) + i % map._width]);                          
        } else {                                                                                         
          _g.push(map._mapData[map._width * j + i]);                                                     
        }                                                                                                
      }                                                                                                  
    }                                                                                                    
    return _g;                                                                                           
  }           

GENES:

 static getRect(map, rect, wrap) {                                                                      
    if (wrap == null) {                                                                                  
      wrap = false;                                                                                      
    };                                                                                                   
    var _g = [];                                                                                         
    var _g1 = rect.y;                                                                                    
    var _g2 = rect.y + rect.height;                                                                      
    while (_g1 < _g2) {                                                                                             
      var j = _g1++;                                                                                             
      var _g2 = rect.x;                                                                                  
      var _g3 = rect.x + rect.width;                                                                     
      while (_g2 < _g3) {                                                                                
        var i = _g2++;                                                                                   
        if (wrap) {                                                                                      
          _g.push(map._mapData[map._width * (j % map._height) + i % map._width]);                        
        } else {                                                                                         
          _g.push(map._mapData[map._width * j + i]);                                                     
        };                                                                                               
      };                                                                                                 
    };                                                                                                   
    return _g;                                                                                           
  }        
vantreeseba commented 11 months ago

This seems to be a conflict between -D js-es=6 and genes.

I'm assuming somehow the js output is guessing it can re-use var names when it is in es6 cause it thinks it's outputting let.

This is what it looks like "fixed" without the es=6 part

notice the new g3,g4 vars in the nested loop compared to above.

static getRect(map, rect, wrap) {                                                                                      
    if (wrap == null) {                                                                                                   
      wrap = false;                                                                                                      
    };                                                                                                                   
    var _g = [];                                                                                                         
    var _g1 = rect.y;                                                                                                    
    var _g2 = rect.y + rect.height;                                                                                      
    while (_g1 < _g2) {                                                                                                  
      var j = _g1++;                                                                                                     
      var _g3 = rect.x;                                                                                                  
      var _g4 = rect.x + rect.width;                                                                                     
      while (_g3 < _g4) {                                                                                                
        var i = _g3++;                                                                                                   
        if (wrap) {                                                                                                      
          _g.push(map._mapData[map._width * (j % map._height) + i % map._width]);                                        
        } else {                                                                                                         
          _g.push(map._mapData[map._width * j + i]);                                                                     
        };                                                                                                               
      };                                                                                                                 
    };                                                                                                                   
    return _g;                                                                                                           
  }           
benmerckx commented 11 months ago

Could you include the original Haxe code that was transpiled? It'll help write a test case