Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reprinting file causes line break changes #215

Open
sophiebits opened this issue Sep 11, 2015 · 3 comments
Open

Reprinting file causes line break changes #215

sophiebits opened this issue Sep 11, 2015 · 3 comments

Comments

@sophiebits
Copy link
Contributor

Parsing and reprinting

// I have a parent
newNode.parent
  && (
    (// Or I did but its different than the one I have now.
      !originalNode.parent ||
      newNode.parent.key !== originalNode.parent.key
    )
  )

outputs

// I have a parent
newNode.parent
  && (
    (// Or I did but its different than the one I have now.
      (!originalNode.parent || newNode.parent.key !== originalNode.parent.key)
    )
  )

which is inconvenient for codemods. Verified using 0.10.32 by running

console.log(recast.print(recast.parse("// I have a parent\nnewNode.parent\n  && (\n    (// Or I did but its different than the one I have now.\n      !originalNode.parent ||\n      newNode.parent.key !== originalNode.parent.key\n    )\n  )")).code)

in node.

@benjamn
Copy link
Owner

benjamn commented Sep 11, 2015

Does the same problem occur if the // Or I did... comment is moved to the line before the && (?

I'm pretty sure the problem lies here. Specifically, hasParens is kind of dumb and doesn't know how to skip over comments to find existing parentheses, so this code is paranoid and forcibly adds additional parentheses.

@sophiebits
Copy link
Contributor Author

It does not. I'll see if I have time to send a PR.

@gnprice
Copy link
Contributor

gnprice commented Jun 12, 2022

This issue happily no longer reproduces (as of 0.21.1.)

Running the very handy one-step original repro now gives:

> console.log(recast.print(recast.parse("// I have a parent\nnewNode.parent\n  && (\n    (// Or I did but its different than the one I have now.\n      !originalNode.parent ||\n      newNode.parent.key !== originalNode.parent.key\n    )\n  )")).code)
// I have a parent
newNode.parent
  && (
    (// Or I did but its different than the one I have now.
      !originalNode.parent ||
      newNode.parent.key !== originalNode.parent.key
    )
  )

just like the original version of the code.

Given this diagnosis:

Specifically, hasParens is kind of dumb and doesn't know how to skip over comments to find existing parentheses, so this code is paranoid and forcibly adds additional parentheses.

the change that fixed it was probably #537, which gave hasParens the ability to look at the input's token stream instead of its character stream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants