Parentheses removed falsely when transforming nullish-coalescing-operator
See original GitHub issueBug Report
Current Behavior When transforming an AST containing a nullish coalescing operator, parentheses are removed falsely, leading to different behavior.
Input Code
const babel = require('@babel/core')
const { readFileSync, writeFileSync } = require('fs')
const code = readFileSync('src/index.js')
// src/index.js:
// const foo = 'test'
// console.log((foo ?? '') == '')
const babelConfig = {
plugins: [
'@babel/plugin-proposal-nullish-coalescing-operator',
],
}
// this works as expected
const ast = babel.parseSync(code, babelConfig)
// this removes the parentheses
const { code: transformedCode } = babel.transformFromAstSync(ast, code)
writeFileSync('dist/index.js', transformedCode)
// dist/index.js:
// const foo = 'test';
// console.log(foo ?? '' == '');
const babelRegister = require('@babel/register')
babelRegister(babelConfig)
require('./dist')
// outputs 'test' instead of false
See also: https://github.com/dword-design/test-babel-missing-parentheses
Expected behavior/code The resulting file should still contain the parentheses.
Environment
- Babel version(s): 7.7.7
- Node/npm version: Node 11.15.0/npm 6.7.0
- OS: macOS 10.14.6
- Monorepo: no
- How you are using Babel: core, register
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (9 by maintainers)
Top Results From Across the Web
Nullish coalescing operator (??) - JavaScript - MDN Web Docs
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null ...
Read more >@babel/plugin-proposal-nullish-coalescing-operator | Yarn
Fast, reliable, and secure dependency management.
Read more >babel/plugin-proposal-nullish-coalescing-operator
When true , this transform will pretend document.all does not exist, and perform loose equality checks with null instead of strict equality checks...
Read more >Nullish Coalescing Operator Explained - Alex Devero Blog
In that case, JavaScript will convert those values to false . When these values are converted to false logical operators has no other...
Read more >Nullish Coalescing - Let Falsy Fool You No More
Falsy values. The or operator is super helpful! But in JavaScript, falsy values are kind of tricky. They include values like null and...
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 Free
Top 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
Hey, I would like to work on it. Thanks.
Its precedence should be the same as
??
or lower.In the spec, they are represented at the same level: https://tc39.es/ecma262/#sec-binary-logical-operators
??
,&&
and||
are represented using aLogicalExpression
node: you can see it on ASTExplorer. demo You need to create aLogicalExpression
function, which must returntrue
when the node needs parentheses.