benjamn / recast

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator
MIT License
4.99k stars 350 forks source link

Misplaced parenthesis in object destructuring argument with trailing argument comma #513

Open tlrobinson opened 6 years ago

tlrobinson commented 6 years ago

I haven't been able to pin down exactly why this is happening, but here's my smallest reproduction so far. I'm using flow the parser.

class Foo {
  method = item => {
    return (
      <div className=""></div>
    );
  };
}

function x({ className, }) {
}

gets printed as

class Foo {
  method = item => {
    return (
      <div className=""></div>
    );
  };
}

function x({ (className, })) {
}

Note the extra (and mis-nested) parenthesis in the last function.

Interestingly, changing just about anything about the code above causes the problem to go away.

benjamn commented 6 years ago

This is just a guess, but in the past, bugs like this have sometimes been due to the parser providing slightly incorrect .loc.{start,end}.{line,column} information. Like, perhaps the parser gets confused by the trailing comma and decides that the className identifier location extends all the way past the end of the parameter list… or something like that.

benjamn commented 6 years ago

Can you still reproduce this? I've been trying to write a regression test, and it seems to work now. If my previous theory is correct, the Flow parser might have fixed their .loc tracking in the meantime.