1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Bare `this.<member>` reads in a function with its own `this` inside a class
//! body (issue #4351).
//!
//! A nested `function` or an object-literal method inside a class method has
//! its own `this`, which is usually not the class instance. A read there gives
//! no file-level `this` credit, unless the code gives the function the
//! instance: `.bind(this)`, `.call(this)`, `.apply(this)`, a call that also
//! passes `this` (`items.forEach(function () {}, this)`), a method call on
//! `this` (`this.on("x", function () {})`), an assignment to a `this.<field>`
//! or a class field, or a `this` parameter with a named type.
use fallow_types::extract::MemberAccess;
use oxc_ast::ast::*;
use oxc_span::Span;
use rustc_hash::{FxHashMap, FxHashSet};
use super::ModuleInfoExtractor;
use crate::js_text::JsText;
/// Methods that run a function with the `this` of their first argument.
const THIS_BINDING_METHODS: [&str; 3] = ["bind", "call", "apply"];
/// The state of bare `this` reads in functions with their own `this`.
#[derive(Debug, Default)]
pub(super) struct IndependentThisState {
/// The name of each open function with its own `this` inside a class
/// body, innermost last. Other code can use the name to give the
/// function the class instance.
frames: Vec<Option<String>>,
pending_reads: Vec<PendingIndependentThisRead>,
bound_function_spans: FxHashSet<Span>,
bound_function_names: FxHashSet<String>,
function_value_names: FxHashMap<Span, String>,
}
/// What `enter_function_this` changed, for `exit_function_this`.
#[derive(Debug, Clone, Copy)]
pub(super) struct FunctionThis {
independent: bool,
frame: bool,
}
/// A bare `this.<member>` read that waits for the end of the file. The read
/// gives credit only when the file gives its function the class instance by
/// name.
#[derive(Debug)]
struct PendingIndependentThisRead {
function_name: Option<String>,
member: String,
}
impl ModuleInfoExtractor {
/// Start a function. A function that is not a class method has its own
/// `this`, unless the code gives it the class instance (an earlier visit
/// saw that code) or its `this` parameter has a named type.
pub(super) fn enter_function_this(&mut self, func: &Function<'_>) -> FunctionThis {
let typed_this = func.this_param.as_ref().is_some_and(|param| {
param.type_annotation.as_deref().is_some_and(|annotation| {
matches!(annotation.type_annotation, TSType::TSTypeReference(_))
})
});
let independent = self.class_method_function_span != Some(func.span)
&& !typed_this
&& !self
.independent_this
.bound_function_spans
.contains(&func.span);
let frame = independent && !self.class_scope_stack.is_empty();
if independent {
self.independent_this_depth += 1;
}
if frame {
let name = self
.independent_this
.function_value_names
.get(&func.span)
.cloned()
.or_else(|| func.id.as_ref().map(|id| id.name.to_string()));
self.independent_this.frames.push(name);
}
FunctionThis { independent, frame }
}
/// End a function that `enter_function_this` started.
pub(super) fn exit_function_this(&mut self, function_this: FunctionThis) {
if function_this.frame {
self.independent_this.frames.pop();
}
if function_this.independent {
self.independent_this_depth -= 1;
}
}
/// Hold a bare `this.<member>` read in a function with its own `this`
/// inside a class body. Return false when the read is not in such a
/// function, so the caller records it as usual.
pub(super) fn defer_independent_this_read(&mut self, object_name: &str, member: &str) -> bool {
if object_name != "this"
|| self.independent_this_depth == 0
|| self.class_scope_stack.is_empty()
{
return false;
}
let Some(function_name) = self.independent_this.frames.last().cloned() else {
return false;
};
self.independent_this
.pending_reads
.push(PendingIndependentThisRead {
function_name,
member: member.to_string(),
});
true
}
/// Give credit to each held read whose function the file gives the class
/// instance by name, for example `g.call(this)` after `function g() {}`.
pub(super) fn resolve_independent_this_reads(&mut self) {
for read in std::mem::take(&mut self.independent_this.pending_reads) {
if read
.function_name
.as_deref()
.is_some_and(|name| self.independent_this.bound_function_names.contains(name))
{
self.member_accesses.push(MemberAccess {
object: "this".to_string(),
member: read.member,
});
}
}
}
/// Record the functions that a call gives the class instance:
/// `fn.bind(this)`, `fn.call(this)`, `fn.apply(this)`, each function
/// argument of a call that also passes `this`, and each function argument
/// of a method call on `this`. The last case covers a listener of an
/// `EventEmitter` subclass, `this.on("x", function () {})`, which the
/// emitter calls with itself as `this`.
pub(super) fn record_this_binding_call(&mut self, call: &CallExpression<'_>) {
let passes_this = call
.arguments
.iter()
.any(|argument| matches!(argument, Argument::ThisExpression(_)));
let on_this = matches!(
call.callee.get_inner_expression(),
Expression::StaticMemberExpression(callee)
if matches!(callee.object, Expression::ThisExpression(_))
);
if !passes_this && !on_this {
return;
}
if let Expression::StaticMemberExpression(callee) = call.callee.get_inner_expression()
&& THIS_BINDING_METHODS.contains(&callee.property.name.as_str())
&& matches!(call.arguments.first(), Some(Argument::ThisExpression(_)))
{
self.record_this_bound_value(&callee.object);
}
for argument in &call.arguments {
match argument {
Argument::ObjectExpression(object) => self.record_this_bound_methods(object),
Argument::SpreadElement(_) | Argument::ThisExpression(_) => {}
_ => {
if let Some(expression) = argument.as_expression() {
self.record_this_bound_value(expression);
}
}
}
}
}
/// Record a function value that runs with the class instance as `this`.
/// An inline function is matched by span, a reference by its last name.
fn record_this_bound_value(&mut self, value: &Expression<'_>) {
match value.get_inner_expression() {
Expression::FunctionExpression(func) => {
self.independent_this.bound_function_spans.insert(func.span);
}
Expression::Identifier(reference) => {
self.independent_this
.bound_function_names
.insert(reference.name.to_string());
}
Expression::StaticMemberExpression(member) => {
self.independent_this
.bound_function_names
.insert(member.property.name.to_string());
}
_ => {}
}
}
/// `handler = function () {}`: a call through the class field runs the
/// function with the instance as `this`.
pub(super) fn record_this_bound_function(&mut self, value: Option<&Expression<'_>>) {
if let Some(Expression::FunctionExpression(func)) =
value.map(Expression::get_inner_expression)
{
self.independent_this.bound_function_spans.insert(func.span);
}
}
/// `this.handler = function () {}` runs with the instance as `this` when
/// code calls `this.handler()`. `obj.run = function () {}` gets the name
/// `run`.
pub(super) fn record_member_assigned_function(
&mut self,
target: &StaticMemberExpression<'_>,
value: &Expression<'_>,
) {
if matches!(target.object, Expression::ThisExpression(_)) {
self.record_this_bound_function(Some(value));
} else {
self.record_function_value_name(target.property.name.as_str(), value);
}
}
/// `const g = function () {}` gets the name `g`.
pub(super) fn record_declarator_function_name(&mut self, declarator: &VariableDeclarator<'_>) {
if let BindingPattern::BindingIdentifier(id) = &declarator.id
&& let Some(init) = &declarator.init
{
self.record_function_value_name(id.name.as_str(), init);
}
}
/// `{ run() {} }` and `{ run: function () {} }` get the name `run`.
pub(super) fn record_property_function_name(&mut self, prop: &ObjectProperty<'_>) {
if let Some(name) = prop.key.static_name() {
self.record_function_value_name(&name.text(), &prop.value);
}
}
/// `Object.defineProperty(this, "x", { get() {} })`: the methods of an
/// object literal next to `this` run with the instance as `this`.
fn record_this_bound_methods(&mut self, object: &ObjectExpression<'_>) {
for property in &object.properties {
if let ObjectPropertyKind::ObjectProperty(property) = property {
self.record_this_bound_value(&property.value);
}
}
}
/// Record the name of a function value, so a later `name.call(this)` can
/// give its held reads credit.
fn record_function_value_name(&mut self, name: &str, value: &Expression<'_>) {
if let Expression::FunctionExpression(func) = value.get_inner_expression() {
self.independent_this
.function_value_names
.insert(func.span, name.to_string());
}
}
}