Skip to content

missing parentheses in exponentiation with unary left side #1357

Closed
@j4k0xb

Description

@j4k0xb

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions