Debian Codegraph @

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

raw symbols SCIP
1 import { types as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/ } from "@babel/core"scip-typescript npm @babel/core 7.20.12 src/`index.ts`/;
2 import type { NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, Visitorscip-typescript npm @babel/traverse 7.20.13 src/`types.ts`/Visitor# } from "@babel/traverse"scip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/;
3 import type { PluginPassscip-typescript npm @babel/core 7.20.12 src/transformation/`plugin-pass.ts`/PluginPass# } from "@babel/core"scip-typescript npm @babel/core 7.20.12 src/`index.ts`/;
4
5 type Statescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State# = {
6 topicReferencesscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:topicReferences.: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.TopicReferencescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TopicReference#>[];
7 sideEffectsBeforeFirstTopicReferencescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:sideEffectsBeforeFirstTopicReference.: boolean;
8 };
9
10 const topicReferenceVisitorscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/topicReferenceVisitor.: Visitorscip-typescript npm @babel/traverse 7.20.13 src/`types.ts`/Visitor#<Statescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#> = {
11 exitscip-typescript npm @babel/traverse 7.20.13 src/`types.ts`/VisitNodeObject#exit.(path, state) {
12 if (path.isTopicReferencescip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isTopicReference().()) {
13 state.topicReferencesscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:topicReferences..pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(path);
14 } else {
15 if (
16 state.topicReferencesscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:topicReferences..lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. === 0 &&
17 !state.sideEffectsBeforeFirstTopicReferencescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:sideEffectsBeforeFirstTopicReference. &&
18 !path.isPurescip-typescript npm @babel/traverse 7.20.13 src/path/lib/`virtual-types-validator.ts`/VirtualTypeNodePathValidators#isPure().()
19 ) {
20 state.sideEffectsBeforeFirstTopicReferencescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:sideEffectsBeforeFirstTopicReference. = true;
21 }
22 }
23 },
24 "ClassBody|Function"(_, state) {
25 if (state.topicReferencesscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:topicReferences..lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. === 0) {
26 state.sideEffectsBeforeFirstTopicReferencescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:sideEffectsBeforeFirstTopicReference. = true;
27 }
28 },
29 };
30
31 // This visitor traverses `BinaryExpression`
32 // and replaces any that use `|>`
33 // with sequence expressions containing assignment expressions
34 // with automatically generated variables,
35 // from inside to outside, from left to right.
36 const visitorscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/visitor.: Visitorscip-typescript npm @babel/traverse 7.20.13 src/`types.ts`/Visitor#<PluginPassscip-typescript npm @babel/core 7.20.12 src/transformation/`plugin-pass.ts`/PluginPass#> = {
37 BinaryExpressionscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/BinaryExpression0:: {
38 exit(path) {
39 const { scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope., nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node. } = path;
40
41 if (node.operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#operator. !== "|>") {
42 // The path node is a binary expression,
43 // but it is not a pipe expression.
44 return;
45 }
46
47 const pipeBodyPath = path.get("right");
48 if (pipeBodyPath.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..typescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrayExpression#type. === "TopicReference") {
49 // If the pipe body is itself a lone topic reference,
50 // then replace the whole expression with its left operand.
51 path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(node.leftscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#left.);
52 return;
53 }
54
55 const visitorState: Statescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State# = {
56 topicReferencesscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:topicReferences.: [],
57 // pipeBodyPath might be a function, and it won't be visited by
58 // topicReferenceVisitor because traverse() skips the top-level
59 // node. We must handle that case here manually.
60 sideEffectsBeforeFirstTopicReferencescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:sideEffectsBeforeFirstTopicReference.: pipeBodyPath.isFunctionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isFunction().(),
61 };
62 pipeBodyPath.traversescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#traverse().(topicReferenceVisitorscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/topicReferenceVisitor., visitorState);
63
64 if (
65 visitorState.topicReferencesscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:topicReferences..lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. === 1 &&
66 (!visitorState.sideEffectsBeforeFirstTopicReferencescip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:sideEffectsBeforeFirstTopicReference. ||
67 path.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..isPurescip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#isPure().(node.leftscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#left., true))
68 ) {
69 visitorState.topicReferencesscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:topicReferences.[0].replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(node.leftscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#left.);
70 path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(node.rightscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#right.);
71 return;
72 }
73
74 const topicVariable = scope.generateUidIdentifierBasedOnNodescip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#generateUidIdentifierBasedOnNode().(node);
75 scope.pushscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#push().({ idscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#push().(opts)typeLiteral191:id.: topicVariable });
76
77 // Replace topic references with the topic variable.
78 visitorState.topicReferencesscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/State#typeLiteral2:topicReferences..forEachscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#forEach().(path =>
79 path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.cloneNode(topicVariable)),
80 );
81
82 // Replace the pipe expression itself with an assignment expression.
83 path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(
84 tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.sequenceExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/sequenceExpression().([
85 tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression().(
86 "=",
87 tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.cloneNode(topicVariable),
88 // @ts-expect-error node.left must not be a PrivateName when operator is |>
89 node.leftscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#left.,
90 ),
91 node.rightscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#right.,
92 ]),
93 );
94 },
95 },
96 };
97
98 export default visitorscip-typescript npm @babel/plugin-proposal-pipeline-operator 7.18.9 src/`hackVisitor.ts`/visitor.;