estools / escodegen

ECMAScript code generator
BSD 2-Clause "Simplified" License
2.65k stars 335 forks source link

EcmaVersion 7: Unary minus and exponentiation not working #462

Open crguezl opened 7 months ago

crguezl commented 7 months ago

escodegen does not support the combination of unary minus and exponentiation:

➜  arith2js-parallel-computing-group-parallel git:(essay-2023-02-15-miercoles) ✗ node
Welcome to Node.js v18.8.0.
Type ".help" for more information.
> let  espree = require('espree')
> let ast = espree.parse('(-2)**2') // gives an error since the default compiler is ecma 5
Uncaught [SyntaxError: Unexpected token *
> let ast3 = espree.parse('(-2)**2', { ecmaVersion: 7}) // no errors. Right AST
> let escodegen = require('escodegen')
> let gc = escodegen.generate(ast3) // escodegen does not support this feature. The code generated is wrong
> gc
'-2 ** 2;'
> eval(gc) // the evaluation of the code produces errors
Uncaught:
SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence
> -2 ** 2 # JS does not accept this expression
-2 ** 2
^^^^^
Uncaught:
SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence
> (-2) ** 2  # ... But this code works 
4
j4k0xb commented 7 months ago

can't reproduce, this was fixed years ago (#407) and generates (-2) ** 2