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

missing parentheses in exponentiation with unary left side #1357

Closed
j4k0xb opened this issue Aug 5, 2023 · 4 comments · Fixed by #1361
Closed

missing parentheses in exponentiation with unary left side #1357

j4k0xb opened this issue Aug 5, 2023 · 4 comments · Fixed by #1361

Comments

@j4k0xb
Copy link

j4k0xb commented Aug 5, 2023

reproduction: https://stackblitz.com/edit/stackblitz-starters-9gkcmv?file=index.js&view=editor

const { parse, print, types } = require('recast');
const b = types.builders;

const { code } = print(
  b.binaryExpression(
    '**',
    b.unaryExpression('-', b.numericLiteral(1)),
    b.numericLiteral(2)
  )
);

console.log(code);
console.log(eval(code));

it prints -1 ** 2 which is invalid syntax:

Uncaught SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence

expected: (-1) ** 2

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence#table 2.

related but not fixed by #363

@eventualbuddha
Copy link
Collaborator

Thanks for reporting this, @j4k0xb! Mind trying #1361 to see if that fixes it for you?

@j4k0xb
Copy link
Author

j4k0xb commented Aug 10, 2023

yes that fixed it, thanks!

@j4k0xb j4k0xb closed this as completed Aug 10, 2023
@j4k0xb
Copy link
Author

j4k0xb commented Aug 10, 2023

a bit related but parsing (-1) ** 2 also fails

@eventualbuddha
Copy link
Collaborator

I'm not sure why it's hardcoded to ecmaVersion: 6. I'm guessing it was added when that was the latest standard.

coderaiser pushed a commit to putoutjs/recast that referenced this issue Aug 11, 2023
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

Successfully merging a pull request may close this issue.

2 participants