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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce dependency on lodash functions: values, extends #11798

Merged
merged 21 commits into from Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d494925
Replace lodash 'values' usage with Object.keys => .map(obj[key])
jayaddison Jul 6, 2020
24599f6
Block scoping: refactor letReferences, outsideLetReferences as object…
jayaddison Jul 6, 2020
13cebc7
Remove lodash dependency from babel-plugin-transform-block-scoping
jayaddison Jul 6, 2020
28ec04e
Fixup: Add missing Object.keys call
jayaddison Jul 6, 2020
a275a44
Fixup: Update remaining property accessors
jayaddison Jul 6, 2020
1449a30
Coerce Map.values() iterator results into an array via spread operator
jayaddison Jul 6, 2020
94fdc95
Fixup: Map.put -> Map.set
jayaddison Jul 6, 2020
2b6dbe4
Fixup: undo incorrect variable de-duplication
jayaddison Jul 6, 2020
6da36af
Replace array-spread-plus-map combination with Array.from call
jayaddison Jul 7, 2020
85689f2
Extract an extendMap function as an attempt to create an optimization…
jayaddison Jul 7, 2020
a4035c8
Experiment: cast objects to string (eliminates one Map/object differe…
jayaddison Jul 7, 2020
abdd147
Fixup: perform String cast on map keys, not values
jayaddison Jul 7, 2020
68b799a
Revert "Fixup: perform String cast on map keys, not values"
jayaddison Jul 7, 2020
894ff8c
Revert "Experiment: cast objects to string (eliminates one Map/object…
jayaddison Jul 7, 2020
491c093
Experiment: filter keys via Object.prototype.hasOwnProperty.call
jayaddison Jul 7, 2020
bea1b56
Revert "Experiment: filter keys via Object.prototype.hasOwnProperty.c…
jayaddison Jul 7, 2020
2119acc
Migrate back from Map-based reference storage to Object-based storage…
jayaddison Jul 7, 2020
666c1a4
Revert "Migrate back from Map-based reference storage to Object-based…
jayaddison Jul 7, 2020
5ba1cbc
Iterate over a clone of outsideRefs keys
jayaddison Jul 7, 2020
a04e689
Revert "Extract an extendMap function as an attempt to create an opti…
jayaddison Jul 7, 2020
2b03b2e
Fixup: migrate remaining Object property access to Map.get in tdz module
jayaddison Jul 8, 2020
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
3 changes: 1 addition & 2 deletions packages/babel-plugin-transform-block-scoping/package.json
Expand Up @@ -13,8 +13,7 @@
},
"main": "lib/index.js",
"dependencies": {
"@babel/helper-plugin-utils": "^7.10.4",
"lodash": "^4.17.13"
"@babel/helper-plugin-utils": "^7.10.4"
},
"keywords": [
"babel-plugin"
Expand Down
45 changes: 24 additions & 21 deletions packages/babel-plugin-transform-block-scoping/src/index.js
Expand Up @@ -2,8 +2,6 @@ import { declare } from "@babel/helper-plugin-utils";
import type NodePath from "@babel/traverse";
import type Scope from "@babel/traverse";
import { visitor as tdzVisitor } from "./tdz";
import values from "lodash/values";
import extend from "lodash/extend";
import { traverse, template, types as t } from "@babel/core";

const DONE = new WeakSet();
Expand Down Expand Up @@ -195,7 +193,7 @@ const letReferenceBlockVisitor = traverse.visitors.merge([
const letReferenceFunctionVisitor = traverse.visitors.merge([
{
ReferencedIdentifier(path, state) {
const ref = state.letReferences[path.node.name];
const ref = state.letReferences.get(path.node.name);

// not a part of our scope
if (!ref) return;
Expand Down Expand Up @@ -250,7 +248,7 @@ const continuationVisitor = {
if (path.isAssignmentExpression() || path.isUpdateExpression()) {
for (const name of Object.keys(path.getBindingIdentifiers())) {
if (
state.outsideReferences[name] !==
state.outsideReferences.get(name) !==
path.scope.getBindingIdentifier(name)
) {
continue;
Expand Down Expand Up @@ -359,9 +357,9 @@ class BlockScoping {
this.blockPath = blockPath;
this.block = blockPath.node;

this.outsideLetReferences = Object.create(null);
this.outsideLetReferences = new Map();
this.hasLetReferences = false;
this.letReferences = Object.create(null);
this.letReferences = new Map();
this.body = [];

if (loopPath) {
Expand Down Expand Up @@ -447,8 +445,8 @@ class BlockScoping {
blockScope.getFunctionParent() || blockScope.getProgramParent();
const letRefs = this.letReferences;

for (const key of Object.keys(letRefs)) {
const ref = letRefs[key];
for (const key of letRefs.keys()) {
const ref = letRefs.get(key);
const binding = blockScope.getBinding(ref.name);
if (!binding) continue;
if (binding.kind === "let" || binding.kind === "const") {
Expand Down Expand Up @@ -476,10 +474,10 @@ class BlockScoping {
// those in upper scopes and then if they do, generate a uid
// for them and replace all references with it

for (const key of Object.keys(letRefs)) {
for (const key of letRefs.keys()) {
// just an Identifier node we collected in `getLetReferences`
// this is the defining identifier of a declaration
const ref = letRefs[key];
const ref = letRefs.get(key);

// todo: could skip this if the colliding binding is in another function
if (scope.parentHasBinding(key) || scope.hasGlobal(key)) {
Expand All @@ -496,8 +494,8 @@ class BlockScoping {
}
}

for (const key of Object.keys(outsideLetRefs)) {
const ref = letRefs[key];
for (const key of outsideLetRefs.keys()) {
const ref = letRefs.get(key);
// check for collisions with a for loop's init variable and the enclosing scope's bindings
// https://github.com/babel/babel/issues/8498
if (isInLoop(this.blockPath) && blockPathScope.hasOwnBinding(key)) {
Expand All @@ -519,20 +517,20 @@ class BlockScoping {

// remap loop heads with colliding variables
if (this.loop) {
for (const name of Object.keys(outsideRefs)) {
const id = outsideRefs[name];
for (const name of outsideRefs.keys()) {
const id = outsideRefs.get(name);

if (
this.scope.hasGlobal(id.name) ||
this.scope.parentHasBinding(id.name)
) {
delete outsideRefs[id.name];
delete this.letReferences[id.name];
outsideRefs.delete(id.name);
this.letReferences.delete(id.name);

this.scope.rename(id.name);

this.letReferences[id.name] = id;
outsideRefs[id.name] = id;
this.letReferences.set(id.name, id);
outsideRefs.set(id.name, id);
jayaddison marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand All @@ -545,7 +543,7 @@ class BlockScoping {
this.hoistVarDeclarations();

// turn outsideLetReferences into an array
const args = values(outsideRefs).map(id => t.cloneNode(id));
jayaddison marked this conversation as resolved.
Show resolved Hide resolved
const args = [...outsideRefs.values()].map(node => t.cloneNode(node));
jayaddison marked this conversation as resolved.
Show resolved Hide resolved
const params = args.map(id => t.cloneNode(id));

const isSwitch = this.blockPath.isSwitchStatement();
Expand Down Expand Up @@ -702,7 +700,10 @@ class BlockScoping {
const init = this.loop.left || this.loop.init;
if (isBlockScoped(init)) {
declarators.push(init);
extend(this.outsideLetReferences, t.getBindingIdentifiers(init));
const names = t.getBindingIdentifiers(init);
for (const name of Object.keys(names)) {
this.outsideLetReferences.set(name, names[name]);
}
}
}

Expand Down Expand Up @@ -751,7 +752,9 @@ class BlockScoping {
// declaration, rather than (for example) mistakenly including the
// parameters of a function declaration. Fixes #4880.
const keys = t.getBindingIdentifiers(declar, false, true);
extend(this.letReferences, keys);
for (const key of Object.keys(keys)) {
this.letReferences.set(key, keys[key]);
}
this.hasLetReferences = true;
}

Expand Down