Skip to content

Commit

Permalink
Yarn Dedup & rename hasMoreThanOneBinding to shouldStoreRHSInTemporar…
Browse files Browse the repository at this point in the history
…yVariable
  • Loading branch information
dan-kez committed Sep 7, 2021
1 parent 317065d commit 4be2624
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
Expand Up @@ -28,8 +28,8 @@
},
"devDependencies": {
"@babel/core": "workspace:*",
"@babel/parser": "workspace:*",
"@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-plugin-test-runner": "workspace:*",
"@babel/parser": "workspace:*"
},
"engines": {
"node": ">=6.9.0"
Expand Down

This file was deleted.

Expand Up @@ -4,7 +4,7 @@ import { types as t } from "@babel/core";
import { convertFunctionParams } from "@babel/plugin-transform-parameters";
import { isRequired } from "@babel/helper-compilation-targets";
import compatData from "@babel/compat-data/corejs2-built-ins";
import hasMoreThanOneBinding from "./hasMoreThanOneBinding";
import shouldStoreRHSInTemporaryVariable from "./shouldStoreRHSInTemporaryVariable";

// TODO: Remove in Babel 8
// @babel/types <=7.3.3 counts FOO as referenced in var { x: FOO }.
Expand Down Expand Up @@ -336,7 +336,7 @@ export default declare((api, opts) => {
// skip single-property case, e.g.
// const { ...x } = foo();
// since the RHS will not be duplicated
hasMoreThanOneBinding(originalPath.node.id) &&
shouldStoreRHSInTemporaryVariable(originalPath.node.id) &&
!t.isIdentifier(originalPath.node.init)
) {
// const { a, ...b } = foo();
Expand Down
@@ -0,0 +1,30 @@
import { types as t } from "@babel/core";

/**
* This is a helper function to determine if we should create an intermediate variable
* such that the RHS of an assignment is not duplicated.
*
* See https://github.com/babel/babel/pull/13711#issuecomment-914388382 for discussion
* on further optimizations.
*/
export default function shouldStoreRHSInTemporaryVariable(node) {
if (t.isArrayPattern(node)) {
const nonNullElements = node.elements.filter(element => element !== null);
if (nonNullElements.length > 1) return true;
else return shouldStoreRHSInTemporaryVariable(nonNullElements[0]);
} else if (t.isObjectPattern(node)) {
if (node.properties.length > 1) return true;
else if (node.properties.length === 0) return false;
else return shouldStoreRHSInTemporaryVariable(node.properties[0]);
} else if (t.isObjectProperty(node)) {
return shouldStoreRHSInTemporaryVariable(node.value);
} else if (t.isAssignmentPattern(node)) {
return shouldStoreRHSInTemporaryVariable(node.left);
} else if (t.isRestElement(node)) {
if (t.isIdentifier(node.argument)) return true;
return shouldStoreRHSInTemporaryVariable(node.argument);
} else {
// node is Identifier or MemberExpression
return false;
}
}
@@ -1,11 +1,11 @@
import { parse } from "@babel/parser";
import hasMoreThanOneBinding from "../lib/hasMoreThanOneBinding";
import shouldStoreRHSInTemporaryVariable from "../lib/shouldStoreRHSInTemporaryVariable";

function getFistObjectPattern(program) {
return parse(program, { sourceType: "module" }).program.body[0]
.declarations[0].id;
}
describe("hasMoreThanOneBinding", function () {
describe("shouldStoreRHSInTemporaryVariable", function () {
it.each([
["const { x: { ...y } } = z();", true],
["let { x4: { ...y4 } } = z();", true],
Expand Down Expand Up @@ -40,7 +40,7 @@ describe("hasMoreThanOneBinding", function () {
["const [,,x27] = z();", false],
])("%s", (code, expectedResult) => {
const ast = getFistObjectPattern(code);
const result = hasMoreThanOneBinding(ast);
const result = shouldStoreRHSInTemporaryVariable(ast);
expect(result).toEqual(expectedResult);
});
});
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -1412,6 +1412,7 @@ __metadata:
"@babel/helper-compilation-targets": "workspace:^7.14.5"
"@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-plugin-utils": "workspace:^7.14.5"
"@babel/parser": "workspace:*"
"@babel/plugin-syntax-object-rest-spread": ^7.8.3
"@babel/plugin-transform-parameters": "workspace:^7.14.5"
peerDependencies:
Expand Down

0 comments on commit 4be2624

Please sign in to comment.