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

refactor: avoid mutating AST nodes #14599

Merged
merged 1 commit into from May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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