Correctly transpile when cross-param refs with obj rest (#11326)

* Transform initializers with ids in rest elements

Fix issue 11281. Transform parameters with default initializers that
have ids that are also in a parameter with a rest element.
Before, these parameters were not transformed.

* Add plugin-transform-parameters as dependency

* Remove outdated comment

* Use set instead of array for paramsWithRestElement

* Skip when encounter "Scope"

Previously, f({...R}, f = R => R) would be incorrectly transformed.

* Pass in loose mode option instead of false

* Address review and re-organize tests

Checking the RHS of an assignment pattern/checking the parent of
an identifier node fails on cases like "({...R}, a = f(R))" or
"({...R}, {[R.key]: a = 42})".

Also refactor tests by removing unecessary tests and
separating "should transform" from "should not transform" tests.
This commit is contained in:
Vedant Roy
2020-04-07 15:06:43 -04:00
committed by GitHub
parent 70cc111b35
commit aea0fcd599
7 changed files with 172 additions and 16 deletions

View File

@@ -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);

View File

@@ -41,7 +41,13 @@ const iifeVisitor = {
path.skip(),
};
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());
@@ -108,6 +114,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");
@@ -164,6 +178,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