Issue 1:
Bug Report

I would like to work on a fix!

Current Behavior

_objectWithoutProperties is being called directly on deeply nested destructured property chains without taking default values into account. This causes TypeError: Cannot read property 'X' of undefined errors being thrown in a code that uses fallback values to guards against missing nested properties.

The strange thing is that issue only happens for a specific environment set - Chrome >= 61, Firefox >= 60, Opera >= 48, Safari >= 11, Edge >= 16, ChromeAndroid >= 77, iOS >= 11.

I have thought that it may be some optimization/minimization/mangling issue, however, it seems that the root cause is somewhere within the babel-preset-env. Default parameter assignments seem to be dropped when object-destructuring, but not the object rest operator, is supported on the selected platform/env.

Input Code

The full case for reproduction is available on the REPL. Please disable the ENV PRESET > Enabled checkbox to see the code running properly when the legacy transforms apply and thus default values are applied.

class Class {
get getter() {
const {
firstLevelOfNesting: {
secondLevelOfNesting: {
someProperty,
...rest
} = {}
} = {}
} = this;

return rest;
}
};

This code generates the following output code when preset-env is not used:

var _this$firstLevelOfNes = this.firstLevelOfNesting;
_this$firstLevelOfNes = _this$firstLevelOfNes === void 0 ? {} : _this$firstLevelOfNes;
var _this$firstLevelOfNes2 = _this$firstLevelOfNes.secondLevelOfNesting;
_this$firstLevelOfNes2 = _this$firstLevelOfNes2 === void 0 ? {} : _this$firstLevelOfNes2;

var someProperty = _this$firstLevelOfNes2.someProperty,
rest = _objectWithoutProperties(_this$firstLevelOfNes2, ["someProperty"]);

The === void 0 ? {} : expressions apply the fallback values before following code extracts nested properties. However, this is not the case when preset-env is applied with the mentioned browser list:

const {
firstLevelOfNesting: {
secondLevelOfNesting: {
someProperty
} = {}
} = {}
} = this,
rest = _objectWithoutProperties(this.firstLevelOfNesting.secondLevelOfNesting, ["someProperty"]);

As you can see the destructured property chain is evaluated in-place without taking default values into account this.firstLevelOfNesting.secondLevelOfNesting - this causes the cannot read property X of undefined errors being thrown.

The issue also affects argument destructuring of plain functions. A single level of nesting is not affected due to _objectWithoutProperties handling undefined/null case on its own.

Expected behavior/code

The code with the specific browser list applied should behave like the code without preset-env applied and match native implementation (the code below, after pasting into Chrome DevTools, should not throw):

class Class {
get getter() {
const {
firstLevelOfNesting: {
secondLevelOfNesting: {
someProperty,
...rest
} = {}
} = {}
} = this;

return rest;
}
};

const instance = new Class();
console.log(instance.getter);

const f = ({
firstLevelOfNesting: {
secondLevelOfNesting: {
someProperty,
...rest
} = {}
} = {}
}) => rest;

console.log(f({}));

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

preset-env with the following browser list Chrome >= 61, Firefox >= 60, Opera >= 48, Safari >= 11, Edge >= 16, ChromeAndroid >= 77, iOS >= 11:

[
'Chrome >= 61',
'Firefox >= 60',
'Opera >= 48',
'Safari >= 11',
'Edge >= 16',
'ChromeAndroid >= 77',
'iOS >= 11'
]

Environment

Babel version(s): [7.8.4]

Node/npm version: [Node 12.14.0/npm 6.13.4]

OS: [Windows 10]

Monorepo: [no]

How you are using Babel: [loader, REPL]

Issue 2:
Issue originally made by @milankinen

Bug information

Babel version: 6.6.0

Node version: 4.2.0

npm version: 2.14.7

Options

--presets es2015

Description

Default parameters seem to be broken in the newest babel 6.6.0 release, at least in arrow functions.

If I run the following code:

npm i babel-cli@6.6.0 babel-preset-es2015@6.6.0
echo 'export const f = (p1, p2 = "tsers") => p1 + p2' | ./node_modules/.bin/babel --presets es2015

I'll get the following output:

"use strict";

var _arguments = arguments;
Object.defineProperty(exports, "__esModule", {
value: true
});
var f = exports.f = function f(p1) {
var p2 = _arguments.length <= 1 || _arguments[1] === undefined ? "tsers" : _arguments[1];
return p1 + p2;
};

As you can see, the second default argument "p2" is taken from "_arguments" instead of "arguments" which is not correct.

I'd appreciate a quick fix because this bug appears also in older 6.x "high level" babel releases due to semver transient dependency definitions, i.e. also the following code causes the same invalid output:

npm i babel-cli@6.5.x babel-preset-es2015@6.5.x
echo 'export const f = (p1, p2 = "tsers") => p1 + p2' | ./node_modules/.bin/babel --presets es2015

EDIT:

I tried to run the code without "exports" keyword and it works ok:

echo 'const f = (p1, p2 = "tsers") => p1 + p2' | ./node_modules/.bin/babel --presets es2015

Will transform into:

"use strict";

var f = function f(p1) {
var p2 = arguments.length <= 1 || arguments[1] === undefined ? "tsers" : arguments[1];
return p1 + p2;
};