Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly transpile when default parameter initializer references binding in rest pattern #11326

Merged
merged 7 commits into from Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 71 additions & 7 deletions packages/babel-plugin-proposal-object-rest-spread/src/index.js
@@ -1,6 +1,7 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
import { types as t } from "@babel/core";
import { convertFunctionParams } from "@babel/plugin-transform-parameters";
existentialism marked this conversation as resolved.
Show resolved Hide resolved

// TODO: Remove in Babel 8
// @babel/types <=7.3.3 counts FOO as referenced in var { x: FOO }.
Expand Down Expand Up @@ -177,17 +178,17 @@ export default declare((api, opts) => {
];
}

function replaceRestElement(parentPath, paramPath) {
function replaceRestElement(parentPath, paramPath, container) {
if (paramPath.isAssignmentPattern()) {
replaceRestElement(parentPath, paramPath.get("left"));
replaceRestElement(parentPath, paramPath.get("left"), container);
return;
}

if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
const elements = paramPath.get("elements");

for (let i = 0; i < elements.length; i++) {
replaceRestElement(parentPath, elements[i]);
replaceRestElement(parentPath, elements[i], container);
}
}

Expand All @@ -198,8 +199,12 @@ export default declare((api, opts) => {
t.variableDeclarator(paramPath.node, uid),
]);

parentPath.ensureBlock();
parentPath.get("body").unshiftContainer("body", declar);
if (container) {
container.push(declar);
} else {
parentPath.ensureBlock();
parentPath.get("body").unshiftContainer("body", declar);
}
paramPath.replaceWith(t.cloneNode(uid));
}
}
Expand All @@ -213,8 +218,67 @@ export default declare((api, opts) => {
// function a({ b, ...c }) {}
Function(path) {
const params = path.get("params");
for (let i = params.length - 1; i >= 0; i--) {
replaceRestElement(params[i].parentPath, params[i]);
const paramHasRestElement = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It suffices to model paramHasRestElement as Set<number> and to only include the index of which has rest element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. I used an array of booleans instead of a set since the code was about the same length and I figured array access would be (very marginally) faster than set access.

const idsInRestParams = new Set();
for (let i = 0; i < params.length; ++i) {
const param = params[i];
paramHasRestElement.push(hasRestElement(param));
if (paramHasRestElement[i]) {
for (const name of Object.keys(param.getBindingIdentifiers())) {
idsInRestParams.add(name);
}
}
}

// if true, a parameter exists that has an id in its initializer
// that is also an id bound in a rest parameter
// example: f({...R}, a = R)
let idInRest = false;

const IdentifierHandler = function(idPath) {
if (
(idPath.parentKey === "right" || idPath.parentKey === "value") &&
vedantroy marked this conversation as resolved.
Show resolved Hide resolved
idsInRestParams.has(idPath.node.name)
) {
idInRest = true;
idPath.stop();
}
};

const AssignmentPatternHandler = function(assignPath) {
const right = assignPath.get("right");
if (right.isIdentifier()) IdentifierHandler(right);
else right.traverse({ Identifier: IdentifierHandler });
if (idInRest) assignPath.stop();
};

let i;
for (i = 0; i < params.length && !idInRest; ++i) {
const param = params[i];
if (!paramHasRestElement[i]) {
if (param.isAssignmentPattern()) AssignmentPatternHandler(param);
else {
param.traverse({ AssignmentPattern: AssignmentPatternHandler });
vedantroy marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

if (!idInRest) {
for (let i = 0; i < params.length; ++i) {
const param = params[i];
if (paramHasRestElement[i]) {
replaceRestElement(param.parentPath, param);
}
}
} else {
const shouldTransformParam = idx =>
idx >= i - 1 || paramHasRestElement[idx];
convertFunctionParams(
path,
false, // TODO: What should this value be?
vedantroy marked this conversation as resolved.
Show resolved Hide resolved
shouldTransformParam,
replaceRestElement,
);
}
},
// adapted from transform-destructuring/src/index.js#pushObjectRest
Expand Down
@@ -0,0 +1,12 @@
({...R}, a = R) => {}
({...R}, {a = {R}}) => {}
({...R}, {a = {R: b}}) => {}
({...R}, {a} = R) => {}
({...R}, {a = R}) => {}
({X: Y,...R}, {a = b} = Y) => {}
({X: Y,...R}, {a = b} = X) => {}
(a = R, {...R}) => {}
({...R}, a = R, c = 2) => {}
({...R}, c = 2, a = R) => {}
({...R}, e, c = 2, a = R, f) => {}
({...R}, a = R, c = d) => { let d; }
@@ -0,0 +1,88 @@
(_ref) => {
let R = babelHelpers.extends({}, _ref);
let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R;
};

(_ref2, _ref3) => {
let R = babelHelpers.extends({}, _ref2);
let {
a = {
R
}
} = _ref3;
};

(_ref4, {
a = {
R: b
}
}) => {
let R = babelHelpers.extends({}, _ref4);
};

(_ref5) => {
let R = babelHelpers.extends({}, _ref5);
let {
a
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R;
};

(_ref6, _ref7) => {
let R = babelHelpers.extends({}, _ref6);
let {
a = R
} = _ref7;
};

(_ref8) => {
let {
X: Y
} = _ref8,
R = babelHelpers.objectWithoutProperties(_ref8, ["X"]);
let {
a = b
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Y;
};

(_ref9, {
a = b
} = X) => {
let {
X: Y
} = _ref9,
R = babelHelpers.objectWithoutProperties(_ref9, ["X"]);
};

() => {
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : R;

let _ref10 = arguments.length > 1 ? arguments[1] : undefined;

let R = babelHelpers.extends({}, _ref10);
};

(_ref11) => {
let R = babelHelpers.extends({}, _ref11);
let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R;
let c = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;
};

(_ref12, c = 2) => {
let R = babelHelpers.extends({}, _ref12);
let a = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : R;
};

(_ref13, e, c = 2) => {
let R = babelHelpers.extends({}, _ref13);
let a = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : R;
let f = arguments.length > 4 ? arguments[4] : undefined;
};

(_ref14) => {
let R = babelHelpers.extends({}, _ref14);
let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R;
let c = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : d;
return function () {
let d;
}();
};
Expand Up @@ -17,15 +17,15 @@ function a3(_ref3) {
c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]);
}

function a4(_ref5, _ref4) {
function a4(_ref4, _ref5) {
let {
a3
a5
} = _ref5,
c3 = babelHelpers.objectWithoutProperties(_ref5, ["a3"]);
c5 = babelHelpers.objectWithoutProperties(_ref5, ["a5"]);
let {
a5
a3
} = _ref4,
c5 = babelHelpers.objectWithoutProperties(_ref4, ["a5"]);
c3 = babelHelpers.objectWithoutProperties(_ref4, ["a3"]);
}

function a5(_ref6) {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-parameters/src/index.js
@@ -1,6 +1,7 @@
import { declare } from "@babel/helper-plugin-utils";
import convertFunctionParams from "./params";
import convertFunctionRest from "./rest";
export { convertFunctionParams };

export default declare((api, options) => {
api.assertVersion(7);
Expand Down
22 changes: 21 additions & 1 deletion packages/babel-plugin-transform-parameters/src/params.js
Expand Up @@ -38,7 +38,13 @@ const iifeVisitor = {
},
};

export default function convertFunctionParams(path, loose) {
// last 2 parameters are optional -- they are used by proposal-object-rest-spread/src/index.js
export default function convertFunctionParams(
path,
loose,
shouldTransformParam,
replaceRestElement,
) {
const params = path.get("params");

const isSimpleParameterList = params.every(param => param.isIdentifier());
Expand Down Expand Up @@ -105,6 +111,14 @@ export default function convertFunctionParams(path, loose) {
for (let i = 0; i < params.length; i++) {
const param = params[i];

if (shouldTransformParam && !shouldTransformParam(i)) {
continue;
}
const transformedRestNodes = [];
if (replaceRestElement) {
replaceRestElement(param.parentPath, param, transformedRestNodes);
}

const paramIsAssignmentPattern = param.isAssignmentPattern();
if (paramIsAssignmentPattern && (loose || node.kind === "set")) {
const left = param.get("left");
Expand Down Expand Up @@ -161,6 +175,12 @@ export default function convertFunctionParams(path, loose) {

param.replaceWith(t.cloneNode(uid));
}

if (transformedRestNodes) {
for (const transformedNode of transformedRestNodes) {
body.push(transformedNode);
}
}
}

// we need to cut off all trailing parameters
Expand Down