benjamn / recast

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

`.print` add uneeded parenthsis even without modifications #1082

Open dedesite opened 2 years ago

dedesite commented 2 years ago

The README states that :

The magic of Recast is that it reprints only those parts of the syntax tree that you modify. In other words, the following identity is guaranteed:

recast.print(recast.parse(source)).code === source

But I came with a code that do not guaranty the above statement : https://astexplorer.net/#/gist/e94f3e50e6148f56b57022fbf5f04f68/latest

I know it's not a big deal, but it could be fixed, it would be great :+1: Otherwise reapplying our code formatter at the end will do the tricks.

sincerexie commented 2 years ago

Hi, have you solved the problem now?

gnprice commented 2 years ago

Quoting the input and output from that link for handy reference in the thread:

// input:
const SubClass = (MainClass) =>
  class extends MainClass {
  };

// output:
const SubClass = (MainClass) =>
  (class extends MainClass {
  });

So a pair of parens is getting added around the body of an arrow function, in a case where it's not needed. Specifically, when the body is a class expression / ClassExpression.

I don't have a diagnosis, but this looks likely related to the same area of code that was involved in issue #743 / PR #1068.

gnprice commented 2 years ago

I've sent a PR that should fix this issue: #1128.