Skip to content

Commit

Permalink
Avoid mutating nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 8, 2022
1 parent ffbc535 commit f5263a4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
5 changes: 2 additions & 3 deletions packages/babel-plugin-transform-block-scoping/src/index.ts
@@ -1,6 +1,6 @@
import { declare } from "@babel/helper-plugin-utils";
import type { NodePath, Visitor, Scope, Binding } from "@babel/traverse";
import { visitor as tdzVisitor } from "./tdz";
import { skipTDZChecks, visitor as tdzVisitor } from "./tdz";
import type { TDZVisitorState } from "./tdz";
import { traverse, template, types as t } from "@babel/core";
import type { PluginPass } from "@babel/core";
Expand Down Expand Up @@ -43,8 +43,7 @@ export default declare((api, opts: Options) => {
t.cloneNode(decl.id),
decl.init || scope.buildUndefinedNode(),
);
// @ts-expect-error todo(flow->ts): avoid mutations
assign._ignoreBlockScopingTDZ = true;
skipTDZChecks.add(assign);
nodes.push(t.expressionStatement(assign));
decl.init = this.addHelper("temporalUndefined");
}
Expand Down
14 changes: 5 additions & 9 deletions packages/babel-plugin-transform-block-scoping/src/tdz.ts
Expand Up @@ -13,7 +13,7 @@ function getTDZStatus(refPath: NodePath, bindingPath: NodePath) {
}
}

const skipTDZChecks = new WeakSet();
export const skipTDZChecks = new WeakSet();

function buildTDZAssert(
status: "maybe" | "inside",
Expand Down Expand Up @@ -106,10 +106,8 @@ export const visitor: Visitor<TDZVisitorState> = {
if (!state.tdzEnabled) return;

const { node } = path;
// @ts-expect-error todo(flow->ts): avoid node mutations
if (node._ignoreBlockScopingTDZ) return;
// @ts-expect-error todo(flow->ts): avoid mutations
node._ignoreBlockScopingTDZ = true;
if (skipTDZChecks.has(node)) return;
skipTDZChecks.add(node);

const arg = path.get("argument");
if (!arg.isIdentifier()) return;
Expand All @@ -128,10 +126,8 @@ export const visitor: Visitor<TDZVisitorState> = {
if (!state.tdzEnabled) return;

const { node } = path;
// @ts-expect-error todo(flow->ts): avoid node mutations
if (node._ignoreBlockScopingTDZ) return;
// @ts-expect-error todo(flow->ts): avoid mutations
node._ignoreBlockScopingTDZ = true;
if (skipTDZChecks.has(node)) return;
skipTDZChecks.add(node);

const nodes = [];
const ids = path.getBindingIdentifiers();
Expand Down

0 comments on commit f5263a4

Please sign in to comment.