Skip to content

Commit

Permalink
refactor: avoid mutating AST nodes (#14599)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed May 26, 2022
1 parent 5f9c532 commit eab2b10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions packages/babel-plugin-transform-block-scoping/src/index.ts
Expand Up @@ -257,16 +257,16 @@ const hoistVarDeclarationsVisitor: Visitor<BlockScoping> = {
},
};

interface LoopVisitorState {
type LoopVisitorState = {
inSwitchCase: boolean;
hasBreakContinue: boolean;
innerLabels: any[];
hasReturn: boolean;
ignoreLabeless: boolean;
LOOP_IGNORE: symbol;
loopIgnored: WeakSet<t.Node>;
isLoop: boolean;
map: any;
}
};

const loopLabelVisitor: Visitor<LoopVisitorState> = {
LabeledStatement({ node }, state) {
Expand Down Expand Up @@ -332,7 +332,7 @@ const loopVisitor: Visitor<LoopVisitorState> = {
state,
) {
const { node, scope } = path;
if (node[this.LOOP_IGNORE]) return;
if (state.loopIgnored.has(node)) return;

let replace;
let loopText = loopNodeTo(node);
Expand Down Expand Up @@ -376,7 +376,7 @@ const loopVisitor: Visitor<LoopVisitorState> = {

if (replace) {
replace = t.returnStatement(replace);
replace[this.LOOP_IGNORE] = true;
state.loopIgnored.add(replace);
path.skip();
path.replaceWith(t.inherits(replace, node));
}
Expand Down Expand Up @@ -934,7 +934,7 @@ class BlockScoping {
hasReturn: false,
isLoop: !!this.loop,
map: {},
LOOP_IGNORE: Symbol(),
loopIgnored: new WeakSet(),
};

this.blockPath.traverse(loopLabelVisitor, state);
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-plugin-transform-classes/src/index.ts
Expand Up @@ -7,7 +7,7 @@ import globals from "globals";
import transformClass from "./transformClass";
import type { Visitor, NodePath } from "@babel/traverse";

const getBuiltinClasses = category =>
const getBuiltinClasses = (category: string) =>
Object.keys(globals[category]).filter(name => /^[A-Z]/.test(name));

const builtinClasses = new Set([
Expand All @@ -33,7 +33,7 @@ export default declare((api, options: Options) => {
const noClassCalls = (api.assumption("noClassCalls") ?? loose) as boolean;

// todo: investigate traversal requeueing
const VISITED = Symbol();
const VISITED = new WeakSet();

return {
name: "transform-classes",
Expand All @@ -58,15 +58,15 @@ export default declare((api, options: Options) => {

ClassExpression(path, state) {
const { node } = path;
if (node[VISITED]) return;
if (VISITED.has(node)) return;

const inferred = nameFunction(path);
if (inferred && inferred !== node) {
path.replaceWith(inferred);
return;
}

node[VISITED] = true;
VISITED.add(node);

path.replaceWith(
transformClass(path, state.file, builtinClasses, loose, {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-plugin-transform-modules-systemjs/src/index.ts
Expand Up @@ -170,14 +170,14 @@ export default declare<PluginState>((api, options: Options) => {
api.assertVersion(7);

const { systemGlobal = "System", allowTopLevelThis = false } = options;
const IGNORE_REASSIGNMENT_SYMBOL = Symbol();
const reassignmentVisited = new WeakSet();

const reassignmentVisitor: Visitor<ReassignmentVisitorState> = {
"AssignmentExpression|UpdateExpression"(
path: NodePath<t.AssignmentExpression | t.UpdateExpression>,
) {
if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
if (reassignmentVisited.has(path.node)) return;
reassignmentVisited.add(path.node);

const arg = path.isAssignmentExpression()
? path.get("left")
Expand Down

0 comments on commit eab2b10

Please sign in to comment.