babel / babel/babel

rest spread ignores default values

Open
#10,735 1 comment 0 reactions 0 assignees View on GitHub
Has PR Spec: Object Rest/Spread
Dominant language
TypeScript
Stars
44k
Forks
6k
Avg merge
5d 15h
Merged PRs (30d)
23

Description

## Bug Report

**Current Behavior**
Default values are ignored for rest variables.

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

```js
const props = {};
const {
InputProps: {
classes,
...InputPropsOther
} = { foo: 1 },
...other
} = props;

console.log(InputPropsOther);
```
**Output Code**
See #10736

**Expected behavior/code**

logs `{ foo: 1 }`

Can't provide a runkit since no node version supports that syntax but Chrome 78 does and it matches the expected behavior.

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

preset-env with `chrome 51` as the target

**Environment**
- Babel version(s): 7.7.3
- Node/npm version: repl
- OS: [e.g. OSX 10.13.4, Windows 10]
- Monorepo: no
- How you are using Babel: repl

**Possible Solution**

**Additional context/Screenshots**
Causes crashes for nested `{}` default values because it transpiles to `props.InputProps.classe` where `props.InputProps` can be `undefined` but guarded against with a default `{}`. Omitting that default value results in "cannot read property classes of undefined"

Contributor guide

Open the contributing guide

Research direction

The issue names no source files or tests. Reproduce the supplied destructuring example in the Babel REPL with preset-env targeting Chrome 51, then inspect the generated output and compare it with the expected `{ foo: 1 }` behavior. Done means nested rest variables preserve the specified default value without crashing when the nested property is undefined.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, javascript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.