Skip to content

Commit

Permalink
Extract unshadow logic to utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 7, 2022
1 parent 00ec595 commit 373f733
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 80 deletions.
88 changes: 8 additions & 80 deletions packages/babel-plugin-transform-parameters/src/params.ts
@@ -1,5 +1,11 @@
import { template, types as t } from "@babel/core";
import type { NodePath, Scope, Visitor } from "@babel/traverse";
import type { NodePath } from "@babel/traverse";

import {
iifeVisitor,
collectShadowedParamsNames,
buildScopeIIFE,
} from "./shadow-utils";

const buildDefaultParam = template.statement(`
let VARIABLE_NAME =
Expand All @@ -23,34 +29,6 @@ const buildSafeArgumentsAccess = template.statement(`
let $0 = arguments.length > $1 ? arguments[$1] : undefined;
`);

const iifeVisitor: Visitor<State> = {
"ReferencedIdentifier|BindingIdentifier"(
path: NodePath<t.Identifier>,
state,
) {
const { scope, node } = path;
const { name } = node;

if (
name === "eval" ||
(scope.getBinding(name) === state.scope.parent.getBinding(name) &&
state.scope.hasOwnBinding(name))
) {
state.needsOuterBinding = true;
path.stop();
}
},
// type annotations don't use or introduce "real" bindings
"TypeAnnotation|TSTypeAnnotation|TypeParameterDeclaration|TSTypeParameterDeclaration":
(path: NodePath) => path.skip(),
};

type State = {
stop: boolean;
needsOuterBinding: boolean;
scope: Scope;
};

// last 2 parameters are optional -- they are used by proposal-object-rest-spread/src/index.js
export default function convertFunctionParams(
path: NodePath<t.Function>,
Expand All @@ -70,7 +48,6 @@ export default function convertFunctionParams(
const { node, scope } = path;

const state = {
stop: false,
needsOuterBinding: false,
scope,
};
Expand All @@ -79,41 +56,7 @@ export default function convertFunctionParams(
const shadowedParams = new Set<string>();

for (const param of params) {
for (const name of Object.keys(param.getBindingIdentifiers())) {
const constantViolations = scope.bindings[name]?.constantViolations;
if (constantViolations) {
for (const redeclarator of constantViolations) {
const node = redeclarator.node;
// If a constant violation is a var or a function declaration,
// we first check to see if it's a var without an init.
// If so, we remove that declarator.
// Otherwise, we have to wrap it in an IIFE.
switch (node.type) {
case "VariableDeclarator": {
if (node.init === null) {
const declaration = redeclarator.parentPath;
// The following uninitialized var declarators should not be removed
// for (var x in {})
// for (var x;;)
if (
!declaration.parentPath.isFor() ||
declaration.parentPath.get("body") === declaration
) {
redeclarator.remove();
break;
}
}

shadowedParams.add(name);
break;
}
case "FunctionDeclaration":
shadowedParams.add(name);
break;
}
}
}
}
collectShadowedParamsNames(param, scope, shadowedParams);
}

if (shadowedParams.size === 0) {
Expand Down Expand Up @@ -244,18 +187,3 @@ export default function convertFunctionParams(

return true;
}

function buildScopeIIFE(shadowedParams: Set<string>, body: t.BlockStatement) {
const args = [];
const params = [];

for (const name of shadowedParams) {
// We create them twice; the other option is to use t.cloneNode
args.push(t.identifier(name));
params.push(t.identifier(name));
}

return t.returnStatement(
t.callExpression(t.arrowFunctionExpression(params, body), args),
);
}
2 changes: 2 additions & 0 deletions packages/babel-plugin-transform-parameters/src/rest.ts
Expand Up @@ -300,6 +300,8 @@ export default function convertFunctionRest(path: NodePath<t.Function>) {
const pattern = rest;
rest = scope.generateUidIdentifier("ref");

console.log(Object.keys(t.getBindingIdentifiers(pattern)));

const declar = t.variableDeclaration("let", [
t.variableDeclarator(pattern, rest),
]);
Expand Down
89 changes: 89 additions & 0 deletions packages/babel-plugin-transform-parameters/src/shadow-utils.ts
@@ -0,0 +1,89 @@
import { types as t } from "@babel/core";
import type { NodePath, Scope, Visitor } from "@babel/traverse";

type State = {
needsOuterBinding: boolean;
scope: Scope;
};

export const iifeVisitor: Visitor<State> = {
"ReferencedIdentifier|BindingIdentifier"(
path: NodePath<t.Identifier>,
state,
) {
const { scope, node } = path;
const { name } = node;

if (
name === "eval" ||
(scope.getBinding(name) === state.scope.parent.getBinding(name) &&
state.scope.hasOwnBinding(name))
) {
state.needsOuterBinding = true;
path.stop();
}
},
// type annotations don't use or introduce "real" bindings
"TypeAnnotation|TSTypeAnnotation|TypeParameterDeclaration|TSTypeParameterDeclaration":
(path: NodePath) => path.skip(),
};

export function collectShadowedParamsNames(
param: NodePath<t.Function["params"][number]>,
functionScope: Scope,
shadowedParams: Set<string>,
) {
for (const name of Object.keys(param.getBindingIdentifiers())) {
const constantViolations = functionScope.bindings[name]?.constantViolations;
if (constantViolations) {
for (const redeclarator of constantViolations) {
const node = redeclarator.node;
// If a constant violation is a var or a function declaration,
// we first check to see if it's a var without an init.
// If so, we remove that declarator.
// Otherwise, we have to wrap it in an IIFE.
switch (node.type) {
case "VariableDeclarator": {
if (node.init === null) {
const declaration = redeclarator.parentPath;
// The following uninitialized var declarators should not be removed
// for (var x in {})
// for (var x;;)
if (
!declaration.parentPath.isFor() ||
declaration.parentPath.get("body") === declaration
) {
redeclarator.remove();
break;
}
}

shadowedParams.add(name);
break;
}
case "FunctionDeclaration":
shadowedParams.add(name);
break;
}
}
}
}
}

export function buildScopeIIFE(
shadowedParams: Set<string>,
body: t.BlockStatement,
) {
const args = [];
const params = [];

for (const name of shadowedParams) {
// We create them twice; the other option is to use t.cloneNode
args.push(t.identifier(name));
params.push(t.identifier(name));
}

return t.returnStatement(
t.callExpression(t.arrowFunctionExpression(params, body), args),
);
}

0 comments on commit 373f733

Please sign in to comment.