Skip to content

[Bug]: @babel/plugin-proposal-object-rest-spread duplicates assignment calls for _objectWithoutProperties #13710

@dan-kez

Description

@dan-kez

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

https://babeljs.io/repl#?browsers=%3E%201%25&build=&builtIns=false&corejs=false&spec=false&loose=false&code_lz=DYUwLgBA3hAeCsAuaEDu8A0EB0uCe8EAvsRALwQBmArgHYDGYAlgPa0DCAhsMACoAWnMAEkAzgBFqAB2BN6QkABMAFAEoA3EA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=true&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ctypescript&prettier=false&targets=&version=7.13.17&externalPlugins=%40babel%2Fplugin-proposal-object-rest-spread%407.14.7&assumptions=%7B%7D

I've also made a reproducible test case in this branch https://github.com/dan-kez/babel/tree/duplicate_function_call_in_variable_destructuring

Configuration file name

babel.config.json

Configuration

No response

Current and expected behavior

Current Behavior:
As shown in the repl above the functionCallThatIsDuplicated function is called twice in the compiled code. This is not expected as in the original code the api call is only made once.

Expected Behavior:
When using @babel/plugin-proposal-object-rest-spread right hand operation is called once and only once to match the original code.

Environment

(Reproducible in the REPL link above)

Below is my project environment but the minimal reproducible example in the REPL link shows the same error

  System:
    OS: macOS 10.15.7
  Binaries:
    Node: 14.17.5 - ~/.nvm/versions/node/v14.17.5/bin/node
    Yarn: 1.22.11 - ~/.nvm/versions/node/v14.17.5/bin/yarn
    npm: 6.14.14 - ~/.nvm/versions/node/v14.17.5/bin/npm
  Monorepos:
    Yarn Workspaces: 1.22.11
    Lerna: 3.20.2
  npmPackages:
    @babel/cli: 7.12.1 => 7.12.1
    @babel/core: 7.12.3 => 7.12.3
    @babel/helper-compilation-targets: 7.12.1 => 7.12.1
    @babel/plugin-proposal-class-properties: 7.12.1 => 7.12.1
    @babel/plugin-proposal-decorators: 7.12.1 => 7.12.1
    @babel/plugin-proposal-object-rest-spread: 7.12.1 => 7.12.1
    @babel/plugin-syntax-dynamic-import: 7.8.3 => 7.8.3
    @babel/plugin-transform-react-jsx: 7.12.1 => 7.12.1
    @babel/plugin-transform-regenerator: 7.12.1 => 7.12.1
    @babel/preset-env: 7.12.1 => 7.12.1
    @babel/preset-react: 7.12.1 => 7.12.1
    @babel/preset-typescript: 7.12.1 => 7.12.1
    @babel/runtime: 7.12.1 => 7.12.1
    babel-eslint: 10.1.0 => 10.1.0
    babel-jest: 26.6.3 => 26.6.3
    babel-loader: 8.2.2 => 8.2.2
    babel-plugin-lodash: 3.3.4 => 3.3.4
    babel-plugin-module-resolver: 4.0.0 => 4.0.0
    babel-plugin-react-docgen: 4.2.1 => 4.2.1
    babel-plugin-require-context-hook: 1.0.0 => 1.0.0
    babel-plugin-styled-components: 1.11.1 => 1.11.1
    babel-plugin-transform-dynamic-import: 2.1.0 => 2.1.0
    babel-plugin-transform-react-remove-prop-types: 0.4.24 => 0.4.24
    eslint: 7.32.0 => 7.32.0
    eslint-import-resolver-babel-module: 5.3.1 => 5.3.1
    jest: 26.6.3 => 26.6.3
    lerna: 3.20.2 => 3.20.2
    webpack: 4.46.0 => 4.46.0

Possible solution

I'm not familiar with the internal implementation of @babel/plugin-proposal-object-rest-spread but it would be possible to first assign the result of the right hand assignment to a temporary variable and then use that temporary variable in subsequent calls to _objectWithoutProperties

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: needs triageoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions