Skip to content

nullish coalescing operator missing var declaration when using spreads #11375

@freben

Description

@freben

Describe the bug

It seems that since version 1.15.4, under just some circumstances, the temporary variable used in ?? nullish coalescing doesn't get declared, leading to errors in the resulting code.

Input code

export default function DropdownNavbarItemDesktop({
  items,
  ...props
}: Props): ReactNode {
  return (
    <div>
      <NavbarNavLink
        onClick={props.to ? undefined : (e) => e.preventDefault()}
      >
        {props.children ?? props.label}
      </NavbarNavLink>
    </div>
  );
}

Config

N/A

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.15.5&code=H4sIAAAAAAAAA1WOwQqCQBCG7z7Ff1QIu5vpIS9BSPQGmzvR4jYr62hB%2BO5taUGX5dvhn29%2BenTOCzRd1GAFl4EbMY5Reddpd%2BdajWfl90K3ivpWXBc%2FI8CEf78KkKZpF5J9NGU4viHJcCLVSO004R31JINnxAGBXJux%2BFDgWR3eg%2BF2GQKOd9Y07fb58abiUGLg0M8waWSIKcG2AIW7NBJLNTePk2lRFD%2FVomiuxmpPjLLEPLHqTPabz9d%2FReb1fL00TTbRFL0AHLPgWiUBAAA%3D&config=H4sIAAAAAAAAA1WPSw7CMAxE9z1F5DVbWHAHDhEFtwrKT7ErEVW9O%2FkDu%2FjNTMY%2BFiHgRQru4sjPPAQZCeOcM6HkWL4zAU4BSUUdGC5DZSoSxx0rOZsALOOGXEJI1%2B4G4z1hZqs0hJ1Z7fSafvuUtyEi0b%2BxWKXbzMy3uqVXgvXPvYr9jrJrq7%2FB1zTK5seg6TGS9YjzA2yuiPwTAQAA

SWC Info output

    Operating System:
        Platform: darwin
        Arch: arm64
        Machine Type: arm64
        Version: Darwin Kernel Version 25.2.0: Tue Nov 18 21:09:40 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6000
        CPU: (10 cores)
            Models: Apple M1 Max

    Binaries:
        Node: 22.18.0
        npm: 10.9.3
        Yarn: 4.8.1
        pnpm: N/A

    Relevant Packages:
        @swc/core: 1.15.5
        @swc/helpers: 0.5.17
        @swc/types: 0.1.25
        typescript: 5.7.3

    SWC Config:
        output: N/A
        .swcrc path: N/A

    Next.js info:
        output: N/A

Expected behavior

// props.children ?? props.label
var _props_children;
(_props_children = props.children) !== null && _props_children !== void 0 ? _props_children : props.label

This was working in 1.15.3.

Actual behavior

// props.children ?? props.label
(_props_children = props.children) !== null && _props_children !== void 0 ? _props_children : props.label

The temporary variable that created for the initial LHS assignment for the ?? operator never gets declared.

This worked correctly in 1.15.3, but is broken in 1.15.4 and newer.

Version

1.15.4

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions