Debian Codegraph @

packages/babel-plugin-proposal-pipeline-operator/src/smartVisitor.ts

raw symbols SCIP
1 import { types as t } from "@babel/core";
2 import type { PluginPass } from "@babel/core";
3 import type { Visitor } from "@babel/traverse";
4
5 const updateTopicReferenceVisitor: Visitor<{ topicId: t.Identifier }> = {
6 PipelinePrimaryTopicReference(path) {
7 path.replaceWith(t.cloneNode(this.topicId));
8 },
9 PipelineTopicExpression(path) {
10 path.skip();
11 },
12 };
13
14 const smartVisitor: Visitor<PluginPass> = {
15 BinaryExpression(path) {
16 const { scope } = path;
17 const { node } = path;
18 const { operator, left, right } = node;
19 if (operator !== "|>") return;
20
21 const placeholder = scope.generateUidIdentifierBasedOnNode(left);
22 scope.push({ id: placeholder });
23
24 let call;
25 if (t.isPipelineTopicExpression(right)) {
26 path
27 .get("right")
28 .traverse(updateTopicReferenceVisitor, { topicId: placeholder });
29
30 call = right.expression;
31 } else {
32 // PipelineBareFunction
33 let callee = (right as t.CallExpression).callee;
34 if (t.isIdentifier(callee, { name: "eval" })) {
35 callee = t.sequenceExpression([t.numericLiteral(0), callee]);
36 }
37
38 call = t.callExpression(callee, [t.cloneNode(placeholder)]);
39 }
40
41 path.replaceWith(
42 t.sequenceExpression([
43 t.assignmentExpression(
44 "=",
45 t.cloneNode(placeholder),
46 // left must not be a PrivateName because operator is not "in"
47 left as t.Expression,
48 ),
49 call,
50 ]),
51 );
52 },
53 };
54
55 export default smartVisitor;