This commit is contained in:
Brian Ng
2017-08-03 22:27:30 -05:00
parent d86ae2fb84
commit 9dd65c809f
4 changed files with 57 additions and 9 deletions

View File

@@ -16,7 +16,7 @@ export default function() {
}
const convertedRest = convertFunctionRest(path);
const convertedParams = convertFunctionParams(path);
const convertedParams = convertFunctionParams(path, this.opts.loose);
if (convertedRest || convertedParams) {
// Manually reprocess this scope to ensure that the moved params are updated.

View File

@@ -10,6 +10,16 @@ const buildDefaultParam = template(`
DEFAULT_VALUE;
`);
const buildLooseDefaultParam = template(`
if (ASSIGNMENT_IDENTIFIER === UNDEFINED) {
ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE;
}
`);
const buildLooseDestructuredDefaultParam = template(`
let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ;
`);
const buildArgumentsAccess = template(`
let $0 = arguments[$1];
`);
@@ -35,7 +45,7 @@ const iifeVisitor = {
},
};
export default function convertFunctionParams(path) {
export default function convertFunctionParams(path, loose) {
const { node, scope } = path;
const state = {
@@ -49,6 +59,44 @@ export default function convertFunctionParams(path) {
//
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;
}
for (let i = 0; i < params.length; i++) {
const param = params[i];

View File

@@ -28,7 +28,7 @@ function d(thing) {
};
{
var _args = thing;
var args = thing;
foo(thing);
}
}

View File

@@ -17,15 +17,15 @@ function a3(_ref3) {
c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]);
}
function a4(_ref4, _ref5) {
let {
a5
} = _ref5,
c5 = babelHelpers.objectWithoutProperties(_ref5, ["a5"]);
function a4(_ref5, _ref4) {
let {
a3
} = _ref5,
c3 = babelHelpers.objectWithoutProperties(_ref5, ["a3"]);
let {
a5
} = _ref4,
c3 = babelHelpers.objectWithoutProperties(_ref4, ["a3"]);
c5 = babelHelpers.objectWithoutProperties(_ref4, ["a5"]);
}
function a5(_ref6) {