Skip to content

Commit 46ee612

Browse files
authored
Remove some NodePath methods (#16655)
1 parent b055f25 commit 46ee612

12 files changed

Lines changed: 127 additions & 118 deletions

File tree

packages/babel-plugin-proposal-async-do-expressions/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default declare(({ types: t, assertVersion }) => {
1010
visitor: {
1111
DoExpression: {
1212
exit(path) {
13-
if (!path.is("async")) {
13+
if (!path.node.async) {
1414
// non-async do expressions are handled by proposal-do-expressions
1515
return;
1616
}

packages/babel-traverse/src/context.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type Scope from "./scope/index.ts";
44
import type { ExplodedTraverseOptions } from "./index.ts";
55
import type * as t from "@babel/types";
66
import type { Visitor } from "./types.ts";
7+
import { popContext, pushContext, resync } from "./path/context.ts";
78

89
export default class TraversalContext<S = unknown> {
910
constructor(
@@ -124,7 +125,7 @@ export default class TraversalContext<S = unknown> {
124125
for (; visitIndex < queue.length; ) {
125126
const path = queue[visitIndex];
126127
visitIndex++;
127-
path.resync();
128+
resync.call(path);
128129

129130
if (
130131
path.contexts.length === 0 ||
@@ -133,7 +134,7 @@ export default class TraversalContext<S = unknown> {
133134
// The context might already have been pushed when this path was inserted and queued.
134135
// If we always re-pushed here, we could get duplicates and risk leaving contexts
135136
// on the stack after the traversal has completed, which could break things.
136-
path.pushContext(this);
137+
pushContext.call(path, this);
137138
}
138139

139140
// this path no longer belongs to the tree
@@ -159,7 +160,7 @@ export default class TraversalContext<S = unknown> {
159160

160161
// pop contexts
161162
for (let i = 0; i < visitIndex; i++) {
162-
queue[i].popContext();
163+
popContext.call(queue[i]);
163164
}
164165

165166
// clear queue

packages/babel-traverse/src/path/context.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ export function isDenylisted(this: NodePath): boolean {
6262
return denylist && denylist.indexOf(this.node.type) > -1;
6363
}
6464

65-
// TODO: Remove in Babel 8
66-
export { isDenylisted as isBlacklisted };
65+
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
66+
// eslint-disable-next-line no-restricted-globals
67+
exports.isBlacklisted = isDenylisted;
68+
}
6769

6870
function restoreContext(path: NodePath, context: TraversalContext) {
6971
if (path.context !== context) {
@@ -92,7 +94,7 @@ export function visit(this: NodePath): boolean {
9294
// before calling the enter visitor, but it can be true in case of
9395
// a requeued node (e.g. by .replaceWith()) that is then marked
9496
// with .skip().
95-
if (this.shouldSkip || this.call("enter")) {
97+
if (this.shouldSkip || call.call(this, "enter")) {
9698
this.debug("Skip...");
9799
return this.shouldStop;
98100
}
@@ -110,7 +112,7 @@ export function visit(this: NodePath): boolean {
110112

111113
restoreContext(this, currentContext);
112114

113-
this.call("exit");
115+
call.call(this, "exit");
114116

115117
return this.shouldStop;
116118
}
@@ -175,7 +177,7 @@ export function setContext<S = unknown>(
175177
this.opts = context.opts as typeof this.opts;
176178
}
177179

178-
this.setScope();
180+
setScope.call(this);
179181

180182
return this;
181183
}
@@ -218,15 +220,15 @@ export function _resyncKey(this: NodePath) {
218220
if (Array.isArray(this.container)) {
219221
for (let i = 0; i < this.container.length; i++) {
220222
if (this.container[i] === this.node) {
221-
this.setKey(i);
223+
setKey.call(this, i);
222224
return;
223225
}
224226
}
225227
} else {
226228
for (const key of Object.keys(this.container)) {
227229
// @ts-expect-error this.key should present in this.container
228230
if (this.container[key] === this.node) {
229-
this.setKey(key);
231+
setKey.call(this, key);
230232
return;
231233
}
232234
}
@@ -284,7 +286,7 @@ export function setup(
284286
this.container = container;
285287

286288
this.parentPath = parentPath || this.parentPath;
287-
this.setKey(key);
289+
setKey.call(this, key);
288290
}
289291

290292
export function setKey(this: NodePath, key: string | number) {

packages/babel-traverse/src/path/conversion.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import template from "@babel/template";
4343
import { environmentVisitor } from "../visitors.ts";
4444
import type NodePath from "./index.ts";
4545
import type { Visitor } from "../types.ts";
46+
import { setup } from "./context.ts";
4647

4748
export function toComputedKey(this: NodePath) {
4849
let key;
@@ -108,7 +109,8 @@ export function ensureBlock(
108109

109110
this.node.body = blockStatement(statements);
110111
const parentPath = this.get(stringPath) as NodePath;
111-
body.setup(
112+
setup.call(
113+
body,
112114
parentPath,
113115
listKey
114116
? // @ts-expect-error listKey must present in parent path

packages/babel-traverse/src/path/index.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as NodePath_comments from "./comments.ts";
2626
import * as NodePath_virtual_types_validator from "./lib/virtual-types-validator.ts";
2727
import type { NodePathAssertions } from "./generated/asserts.ts";
2828
import type { NodePathValidators } from "./generated/validators.ts";
29+
import { setup } from "./context.ts";
2930

3031
const debug = buildDebug("babel");
3132

@@ -104,7 +105,7 @@ const NodePath_Final = class NodePath {
104105
if (targetNode) paths.set(targetNode, path);
105106
}
106107

107-
path.setup(parentPath, container, listKey, key);
108+
setup.call(path, parentPath, container, listKey, key);
108109

109110
return path;
110111
}
@@ -231,11 +232,7 @@ const methods = {
231232

232233
// NodePath_introspection
233234
matchesPattern: NodePath_introspection.matchesPattern,
234-
has: NodePath_introspection.has,
235235
isStatic: NodePath_introspection.isStatic,
236-
is: NodePath_introspection.is,
237-
isnt: NodePath_introspection.isnt,
238-
equals: NodePath_introspection.equals,
239236
isNodeType: NodePath_introspection.isNodeType,
240237
canHaveVariableDeclarationOrExpression:
241238
NodePath_introspection.canHaveVariableDeclarationOrExpression,
@@ -253,20 +250,12 @@ const methods = {
253250
isInStrictMode: NodePath_introspection.isInStrictMode,
254251

255252
// NodePath_context
256-
call: NodePath_context.call,
257253
isDenylisted: NodePath_context.isDenylisted,
258-
isBlacklisted: NodePath_context.isBlacklisted,
259254
visit: NodePath_context.visit,
260255
skip: NodePath_context.skip,
261256
skipKey: NodePath_context.skipKey,
262257
stop: NodePath_context.stop,
263-
setScope: NodePath_context.setScope,
264258
setContext: NodePath_context.setContext,
265-
resync: NodePath_context.resync,
266-
popContext: NodePath_context.popContext,
267-
pushContext: NodePath_context.pushContext,
268-
setup: NodePath_context.setup,
269-
setKey: NodePath_context.setKey,
270259
requeue: NodePath_context.requeue,
271260
requeueComputedKeyAndDecorators:
272261
NodePath_context.requeueComputedKeyAndDecorators,
@@ -277,10 +266,8 @@ const methods = {
277266
// NodePath_modification
278267
insertBefore: NodePath_modification.insertBefore,
279268
insertAfter: NodePath_modification.insertAfter,
280-
updateSiblingKeys: NodePath_modification.updateSiblingKeys,
281269
unshiftContainer: NodePath_modification.unshiftContainer,
282270
pushContainer: NodePath_modification.pushContainer,
283-
hoist: NodePath_modification.hoist,
284271

285272
// NodePath_family
286273
getOpposite: NodePath_family.getOpposite,
@@ -307,21 +294,43 @@ const methods = {
307294
Object.assign(NodePath_Final.prototype, methods);
308295

309296
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
297+
// String(x) is workaround for rollup
298+
310299
// @ts-expect-error babel 7 only
311300
NodePath_Final.prototype.arrowFunctionToShadowed =
312-
// workaround for rollup
313301
// @ts-expect-error babel 7 only
314302
NodePath_conversion[String("arrowFunctionToShadowed")];
303+
304+
Object.assign(NodePath_Final.prototype, {
305+
// @ts-expect-error Babel 7 only
306+
has: NodePath_introspection[String("has")],
307+
// @ts-expect-error Babel 7 only
308+
is: NodePath_introspection[String("is")],
309+
// @ts-expect-error Babel 7 only
310+
isnt: NodePath_introspection[String("isnt")],
311+
// @ts-expect-error Babel 7 only
312+
equals: NodePath_introspection[String("equals")],
313+
// @ts-expect-error Babel 7 only
314+
hoist: NodePath_modification[String("hoist")],
315+
updateSiblingKeys: NodePath_modification.updateSiblingKeys,
316+
call: NodePath_context.call,
317+
// @ts-expect-error Babel 7 only
318+
isBlacklisted: NodePath_context[String("isBlacklisted")],
319+
setScope: NodePath_context.setScope,
320+
resync: NodePath_context.resync,
321+
popContext: NodePath_context.popContext,
322+
pushContext: NodePath_context.pushContext,
323+
setup: NodePath_context.setup,
324+
setKey: NodePath_context.setKey,
325+
});
315326
}
316327

317328
if (!process.env.BABEL_8_BREAKING) {
318329
// @ts-expect-error The original _guessExecutionStatusRelativeToDifferentFunctions only worked for paths in
319330
// different functions, but _guessExecutionStatusRelativeTo works as a replacement in those cases.
320331
NodePath_Final.prototype._guessExecutionStatusRelativeToDifferentFunctions =
321332
NodePath_introspection._guessExecutionStatusRelativeTo;
322-
}
323333

324-
if (!process.env.BABEL_8_BREAKING) {
325334
// @ts-expect-error The original _guessExecutionStatusRelativeToDifferentFunctions only worked for paths in
326335
// different functions, but _guessExecutionStatusRelativeTo works as a replacement in those cases.
327336
NodePath_Final.prototype._guessExecutionStatusRelativeToDifferentFunctions =

packages/babel-traverse/src/path/introspection.ts

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,58 +29,59 @@ export function matchesPattern(
2929
return _matchesPattern(this.node, pattern, allowPartial);
3030
}
3131

32-
/**
33-
* Check whether we have the input `key`. If the `key` references an array then we check
34-
* if the array has any items, otherwise we just check if it's falsy.
35-
*/
36-
37-
export function has<N extends t.Node>(
38-
this: NodePath<N>,
39-
key: keyof N,
40-
): boolean {
41-
const val = (this.node as N)?.[key];
42-
if (val && Array.isArray(val)) {
43-
return !!val.length;
44-
} else {
45-
return !!val;
46-
}
32+
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
33+
/**
34+
* Check whether we have the input `key`. If the `key` references an array then we check
35+
* if the array has any items, otherwise we just check if it's falsy.
36+
*/
37+
// eslint-disable-next-line no-restricted-globals
38+
exports.has = function has<N extends t.Node>(
39+
this: NodePath<N>,
40+
key: keyof N,
41+
): boolean {
42+
const val = (this.node as N)?.[key];
43+
if (val && Array.isArray(val)) {
44+
return !!val.length;
45+
} else {
46+
return !!val;
47+
}
48+
};
4749
}
4850

49-
/**
50-
* Description
51-
*/
52-
5351
export function isStatic(this: NodePath): boolean {
5452
return this.scope.isStatic(this.node);
5553
}
5654

57-
/**
58-
* Alias of `has`.
59-
*/
60-
61-
export const is = has;
62-
63-
/**
64-
* Opposite of `has`.
65-
*/
66-
67-
export function isnt<N extends t.Node>(
68-
this: NodePath<N>,
69-
key: keyof N,
70-
): boolean {
71-
return !this.has(key);
72-
}
73-
74-
/**
75-
* Check whether the path node `key` strict equals `value`.
76-
*/
55+
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
56+
/**
57+
* Alias of `has`.
58+
*/
59+
// eslint-disable-next-line no-restricted-globals
60+
exports.is = exports.has;
61+
62+
/**
63+
* Opposite of `has`.
64+
*/
65+
// eslint-disable-next-line no-restricted-globals
66+
exports.isnt = function isnt<N extends t.Node>(
67+
this: NodePath<N>,
68+
key: keyof N,
69+
): boolean {
70+
// @ts-expect-error Babel 7
71+
return !this.has(key);
72+
};
7773

78-
export function equals<N extends t.Node>(
79-
this: NodePath<N>,
80-
key: keyof N,
81-
value: any,
82-
): boolean {
83-
return (this.node as N)[key] === value;
74+
/**
75+
* Check whether the path node `key` strict equals `value`.
76+
*/
77+
// eslint-disable-next-line no-restricted-globals
78+
exports.equals = function equals<N extends t.Node>(
79+
this: NodePath<N>,
80+
key: keyof N,
81+
value: any,
82+
): boolean {
83+
return (this.node as N)[key] === value;
84+
};
8485
}
8586

8687
/**

packages/babel-traverse/src/path/lib/hoister.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Remove this file in Babel 8
2+
13
import { react } from "@babel/types";
24
import {
35
cloneNode,

0 commit comments

Comments
 (0)