Fixed loose option of transform-es2015-parameters handling only Assig… (#6274)

This commit is contained in:
Mateusz Burzyński 2017-09-23 01:39:39 +02:00 committed by Justin Ridgewell
parent 0f8c4dc5a1
commit d8b7bc39af
5 changed files with 68 additions and 41 deletions

View File

@ -54,53 +54,41 @@ export default function convertFunctionParams(path, loose) {
};
const body = [];
let firstOptionalIndex = null;
//
const params = path.get("params");
if (loose) {
const body = [];
for (let i = 0; i < params.length; ++i) {
const param = params[i];
if (param.isAssignmentPattern()) {
const left = param.get("left");
const right = param.get("right");
const undefinedNode = scope.buildUndefinedNode();
if (left.isIdentifier()) {
body.push(
buildLooseDefaultParam({
ASSIGNMENT_IDENTIFIER: left.node,
DEFAULT_VALUE: right.node,
UNDEFINED: undefinedNode,
}),
);
param.replaceWith(left.node);
} else if (left.isObjectPattern() || left.isArrayPattern()) {
const paramName = scope.generateUidIdentifier();
body.push(
buildLooseDestructuredDefaultParam({
ASSIGNMENT_IDENTIFIER: left.node,
DEFAULT_VALUE: right.node,
PARAMETER_NAME: paramName,
UNDEFINED: undefinedNode,
}),
);
param.replaceWith(paramName);
}
}
}
path.get("body").unshiftContainer("body", body);
return;
}
let firstOptionalIndex = null;
for (let i = 0; i < params.length; i++) {
const param = params[i];
if (param.isAssignmentPattern()) {
if (param.isAssignmentPattern() && loose) {
const left = param.get("left");
const right = param.get("right");
const undefinedNode = scope.buildUndefinedNode();
if (left.isIdentifier()) {
body.push(
buildLooseDefaultParam({
ASSIGNMENT_IDENTIFIER: left.node,
DEFAULT_VALUE: right.node,
UNDEFINED: undefinedNode,
}),
);
param.replaceWith(left.node);
} else if (left.isObjectPattern() || left.isArrayPattern()) {
const paramName = scope.generateUidIdentifier();
body.push(
buildLooseDestructuredDefaultParam({
ASSIGNMENT_IDENTIFIER: left.node,
DEFAULT_VALUE: right.node,
PARAMETER_NAME: paramName,
UNDEFINED: undefinedNode,
}),
);
param.replaceWith(paramName);
}
} else if (param.isAssignmentPattern()) {
if (firstOptionalIndex === null) firstOptionalIndex = i;
const left = param.get("left");

View File

@ -0,0 +1,8 @@
function fn(
a1,
a2 = 4,
{a3, a4},
a5,
{a6, a7} = {}) {
}

View File

@ -0,0 +1,12 @@
function fn(a1, a2, _ref, a5, _temp) {
if (a2 === void 0) {
a2 = 4;
}
var a3 = _ref.a3,
a4 = _ref.a4;
var _ref2 = _temp === void 0 ? {} : _temp,
a6 = _ref2.a6,
a7 = _ref2.a7;
}

View File

@ -0,0 +1,4 @@
// #3861
function t(x = "default", { a, b }, ...args) {
console.log(x, a, b, args);
}

View File

@ -0,0 +1,15 @@
// #3861
function t(x, _ref) {
if (x === void 0) {
x = "default";
}
var a = _ref.a,
b = _ref.b;
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
args[_key - 2] = arguments[_key];
}
console.log(x, a, b, args);
}