question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parentheses removed falsely when transforming nullish-coalescing-operator

See original GitHub issue

Bug 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
sidntrivedi012commented, Jan 8, 2020

Hey, I would like to work on it. Thanks.

1reaction
nicolo-ribaudocommented, Jan 12, 2020

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

ShortCircuitExpression:
  LogicalORExpression
  CoalesceExpression

Also, how to find which function in parentheses.js implements operations on ?? operator ?

??, && and || are represented using a LogicalExpression node: you can see it on ASTExplorer. demo You need to create a LogicalExpression function, which must return true when the node needs parentheses.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found