Debian Codegraph @

packages/babel-traverse/src/scope/binding.ts

raw symbols SCIP
1 import type NodePath from "../path";
2 import type * as t from "@babel/types";
3 import type Scope from "./index";
4
5 export type BindingKind =
6 | "var" /* var declarator */
7 | "let" /* let declarator, class declaration id, catch clause parameters */
8 | "const" /* const/using declarator */
9 | "module" /* import specifiers */
10 | "hoisted" /* function declaration id */
11 | "param" /* function declaration parameters */
12 | "local" /* function expression id, class expression id */
13 | "unknown"; /* export specifiers */
14 /**
15 * This class is responsible for a binding inside of a scope.
16 *
17 * It tracks the following:
18 *
19 * * Node path.
20 * * Amount of times referenced by other nodes.
21 * * Paths to nodes that reassign or modify this binding.
22 * * The kind of binding. (Is it a parameter, declaration etc)
23 */
24
25 export default class Binding {
26 identifier: t.Identifier;
27 scope: Scope;
28 path: NodePath;
29 kind: BindingKind;
30
31 constructor({
32 identifier,
33 scope,
34 path,
35 kind,
36 }: {
37 identifier: t.Identifier;
38 scope: Scope;
39 path: NodePath;
40 kind: BindingKind;
41 }) {
42 this.identifier = identifier;
43 this.scope = scope;
44 this.path = path;
45 this.kind = kind;
46
47 if (
48 (kind === "var" || kind === "hoisted") &&
49 // https://github.com/rollup/rollup/issues/4654
50 // Rollup removes the path argument from this call. Add an
51 // unreachable IIFE (that rollup doesn't know is unreachable)
52 // with side effects, to prevent it from messing up with arguments.
53 // You can reproduce this with
54 // BABEL_8_BREAKING=true make prepublish-build
55 isDeclaredInLoop(
56 path ||
57 (() => {
58 throw new Error("Internal Babel error: unreachable ");
59 })(),
60 )
61 ) {
62 this.reassign(path);
63 }
64
65 this.clearValue();
66 }
67
68 constantViolations: Array<NodePath> = [];
69 constant: boolean = true;
70
71 referencePaths: Array<NodePath> = [];
72 referenced: boolean = false;
73 references: number = 0;
74
75 declare hasDeoptedValue: boolean;
76 declare hasValue: boolean;
77 declare value: any;
78
79 deoptValue() {
80 this.clearValue();
81 this.hasDeoptedValue = true;
82 }
83
84 setValue(value: any) {
85 if (this.hasDeoptedValue) return;
86 this.hasValue = true;
87 this.value = value;
88 }
89
90 clearValue() {
91 this.hasDeoptedValue = false;
92 this.hasValue = false;
93 this.value = null;
94 }
95
96 /**
97 * Register a constant violation with the provided `path`.
98 */
99
100 reassign(path: NodePath) {
101 this.constant = false;
102 if (this.constantViolations.indexOf(path) !== -1) {
103 return;
104 }
105 this.constantViolations.push(path);
106 }
107
108 /**
109 * Increment the amount of references to this binding.
110 */
111
112 reference(path: NodePath) {
113 if (this.referencePaths.indexOf(path) !== -1) {
114 return;
115 }
116 this.referenced = true;
117 this.references++;
118 this.referencePaths.push(path);
119 }
120
121 /**
122 * Decrement the amount of references to this binding.
123 */
124
125 dereference() {
126 this.references--;
127 this.referenced = !!this.references;
128 }
129 }
130
131 function isDeclaredInLoop(path: NodePath) {
132 for (
133 let { parentPath, key } = path;
134 parentPath;
135 { parentPath, key } = parentPath
136 ) {
137 if (parentPath.isFunctionParent()) return false;
138 if (
139 parentPath.isWhile() ||
140 parentPath.isForXStatement() ||
141 (parentPath.isForStatement() && key === "body")
142 ) {
143 return true;
144 }
145 }
146 return false;
147 }