Missing parentheses around LogicalExpression inside of UnaryExpression
See original GitHub issueI believe I’ve stumbled onto a bug in jscodeshift: I created a transform to convert
(a && b) || c();
to
if (!(a && b)) {
c();
}
However, when I wrap the initial LogicalExpression
in a !
UnaryExpression
, jscodeshift
instead outputs:
if (!a && b) {
c();
}
which has a different meaning due to the missing parentheses.
The issue on AST explorer: https://astexplorer.net/#/gist/4f8d333d868a270bd53098d4f283e8fa/5cb2224e8c7cbc178130c3da2ab6683e4a1064bc
Output of jscodeshift --version
:
jscodeshift: 0.11.0
- babel: 7.13.14
- babylon: 7.13.13
- flow: 0.148.0
- recast: 0.20.4
I don’t believe this is an issue with recast, because I wrote some code that does the same thing purely using recast, and it works:
var recast = require("recast");
const b = recast.types.builders;
const code = [
"(a && b) || c();"
].join("\n");
const ast = recast.parse(code);
const expression = ast.program.body[0].expression;
const body = b.blockStatement([b.expressionStatement(expression.right)]);
const cond = b.unaryExpression("!", expression.left);
const ifStatement = b.ifStatement(cond, body, null);
ast.program.body[0] = ifStatement;
const output = recast.print(ast).code;
console.log(output);
outputs:
if (!(a && b)) {
c();
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
Purpose of parentheses around expressions joined by logical ...
In this case, the order of operations makes the two statements (and return a != null && b != null , without any...
Read more >Logical OR (||) - JavaScript - MDN Web Docs - Mozilla
As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression following some rules.
Read more >Dafny Reference Manual
Many of the types, functions, and methods in Dafny can be parameterized by types. These type parameters are typically declared inside angle brackets...
Read more >prettierx | Yarn - Package Manager
... extra parenthesis around await inside of unary expression (#745); Fix missing ... Remove unneeded parens for FunctionExpression inside LogicalExpression ...
Read more >State of the Art Expression Evaluation - CodeProject
In any logical expression, mathematical functions are used whether ... It is made of optional opening and closing parentheses containing ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the information @mobily !!. Do you have a link to the documentation related to this?
@fcsonline I think what you need is adding
node.left.prefix = true
https://astexplorer.net/#/gist/4f8d333d868a270bd53098d4f283e8fa/b2b9239fb3c2281723659f4d55a8efb944919d51