p42ai / js-assistant

120+ refactorings and code-assists for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=p42ai.refactor
MIT License
119 stars 7 forks source link

Bug with return statement suggestions #4

Closed jheer closed 2 years ago

jheer commented 2 years ago

I found an example in the Arquero codebase where P42 makes buggy suggestions marked as "safe". The simplified snippet below involves a series of if-statement fallthroughs that can update the ast variable. P42 (correctly) notes that the assignment in the if(obj.window) branch can be replaced with a return statement. But afterward it continues suggesting similar replacements for other assignments which actually break the logic.

function test(obj) {
  if (obj.expr) {
    let ast;
    if (obj.field === true) {
      ast = obj.field;
    } else if (obj.func === true) {
      ast = obj.func;
    }
    if (ast) {
      if (obj.desc) {
        ast = { type: Descending, expr: ast };
      }
      if (obj.window) {
        ast = { type: Window, expr: ast, ...obj.window };
      }
      return ast;
    }
  }
}
lgrammel commented 2 years ago

Thanks for the bug report!

I've just released version 1.50.3, which fixes this issue. Please re-open this issue if you still encounter the bug in 1.50.3 or higher.