Skip to content

@babel/plugin-proposal-null-coalescing-operator injects unnecessary variable #10717

@sqal

Description

@sqal

Bug Report

Current Behavior
@babel/plugin-proposal-null-coalescing-operator creates unnecesary variable declaration when variable used in conditional expression is within function scope

Input Code
https://babeljs.io/repl#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=GYVwdgxgLglg9mABACwIZgCYBsCmBhNMAcxwAoYwAHEKANVSxBwEpEBvAKEUQgQGcoiAG4MmiALyIK1OqJyIA_AsRRkMPgDoRjHAG4OAXyA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2&prettier=false&targets=&version=7.7.3&externalPlugins=

function handleChange(inputValue) {
  const value = inputValue ?? this.value;
}

Expected behavior/code
I expect output to not have additional var _inputValue; declaration because it's unnecessary.

function handleChange(inputValue) {
  // since we don't accessing nested property in inputValue the output should be:
  var value = inputValue !== null && inputValue !== void 0 ? inputValue : this.value;
}

Babel Configuration (.babelrc, package.json, cli command)

{
  presets: [
    ['@babel/preset-env', {
      useBuiltIns: true,
      loose: true,
      modules: false,
      corejs: 3,
    }],
  ],
  plugins: [
    '@babel/plugin-proposal-optional-chaining',
    '@babel/plugin-proposal-nullish-coalescing-operator'
  ],
}

Environment

  • Babel version(s): 7.7.2
  • Node/npm version: 13.1.0
  • OS: Windows 10
  • Monorepo: no
  • How you are using Babel: babel-loader

Possible Solution

Additional context/Screenshots

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has PRoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions