fallow-extract 3.45.0

AST extraction engine for fallow codebase intelligence (parser, complexity, SFC / Astro / MDX / CSS)
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Uses of the value of a load with an unknown source.
//!
//! `import(name)` or `require(name)` can load each module of the project.
//! This pass records each export name that the code reads on such a value:
//! `mod.pad`, `mod["pad"]`, `const { pad } = mod`, `'pad' in mod`, or a
//! `.then((m) => m.pad())` callback. When the value goes to code that the
//! pass does not follow, such as an alias, a call argument or a return
//! value, the pass records an escape. A discarded value reads no export.
//! A value that a module-scope function returns makes that function a
//! loader. The analysis then reads the result of each call of the function
//! with the same rules, so `const mod = await load(name); mod.pad()` reads
//! `pad`.
//! A fallback such as `mod.default ?? mod`, `mod.default || mod` or
//! `flag ? mod.default : mod` is also the loaded value when each branch is
//! the binding of the value or one member of it. The pass then reads the
//! result with the same rules. A branch with any other value is an escape.
//! An `await` of such a value reads `then` and gives the value.
//! A read of the global `require` as a value, such as `require.call(...)`
//! or `[name].map(require)`, is also an escape: the pass cannot see the
//! loads that go through it.

use fallow_types::extract::{CallResultReads, LoaderReturn, OpaqueLoads};
use oxc_ast::AstKind;
use oxc_ast::ast::{
    Argument, BinaryOperator, BindingPattern, Expression, FormalParameters, LogicalOperator,
    MemberExpression,
};
use oxc_semantic::{AstNodes, NodeId, Reference, Scoping, Semantic, SymbolId};
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::UnaryOperator;
use oxc_syntax::symbol::SymbolFlags;
use rustc_hash::{FxHashMap, FxHashSet};

use crate::js_text::JsText;
use crate::namespace_member_uses::{destructured_keys, member_chain, unwrap_upward};

/// The module-scope functions that can be a loader: the span of each
/// function with the index of its contract and its `async` flag. A loaded
/// value that another function returns is an escape.
pub type LoaderCandidates = FxHashMap<Span, (u32, bool)>;

/// The most names that the reads of the results of one call keep. A call
/// with more reads counts as an escape. This can only hide a finding, never
/// add one, and it keeps the facts of each call small.
const MAX_RESULT_NAMES: usize = 32;

/// What the reader needs to know about the program besides the AST.
#[derive(Clone, Copy)]
pub struct ReadContext<'c> {
    /// `true` for a component script whose template can read each
    /// top-level binding, such as a Vue `<script setup>` block.
    pub template_reads_root: bool,
    /// The functions whose `return` of a loaded value makes them a loader.
    pub loader_candidates: &'c LoaderCandidates,
}

/// Read the uses of each load whose `import()` or call span is in `loads`.
pub fn collect(
    semantic: &Semantic<'_>,
    loads: &FxHashSet<Span>,
    context: ReadContext<'_>,
) -> OpaqueLoads {
    let mut reader = Reader::new(semantic, context);
    reader.read_require_values();
    if loads.is_empty() {
        return reader.finish();
    }
    let mut found = 0;
    for node in reader.nodes.iter() {
        let (span, promise) = match node.kind() {
            AstKind::ImportExpression(expr) => (expr.span, true),
            AstKind::CallExpression(call) => (call.span, false),
            _ => continue,
        };
        if loads.contains(&span) {
            found += 1;
            reader.read_load(node.id(), span, promise);
        }
    }
    // A load that the pass cannot find has uses that it cannot read.
    reader.escapes |= found < loads.len();
    reader.finish()
}

/// Read the uses of the result of the call at `call`, once as the loaded
/// value and once as a promise of it. When the callee is a loader, the
/// analysis uses the reads that match the loader.
pub fn read_call_result(
    semantic: &Semantic<'_>,
    call: NodeId,
    context: ReadContext<'_>,
) -> CallResultReads {
    let span = semantic.nodes().kind(call).span();
    let read = |promise: bool| {
        let mut reader = Reader::new(semantic, context);
        reader.read_load(call, span, promise);
        let mut reads = reader.finish();
        limit_names(&mut reads);
        reads
    };
    CallResultReads {
        value: read(false),
        promise: read(true),
    }
}

/// Replace the names of call result reads with an escape when they pass
/// [`MAX_RESULT_NAMES`].
pub fn limit_names(reads: &mut OpaqueLoads) {
    if reads.names.len() > MAX_RESULT_NAMES {
        reads.names = Vec::new();
        reads.escapes = true;
    }
}

struct Reader<'s, 'a> {
    scoping: &'s Scoping,
    nodes: &'s AstNodes<'a>,
    names: FxHashSet<String>,
    escapes: bool,
    loaders: FxHashSet<LoaderReturn>,
    /// The bindings of loaded values that the reader read. A `var` can
    /// bind a fallback of itself, so a second read stops the loop.
    bindings: FxHashSet<SymbolId>,
    template_reads_root: bool,
    loader_candidates: &'s LoaderCandidates,
}

/// The member that an `await` reads on its operand.
const THEN: &str = "then";

/// The members of `require` that call the loader with other arguments.
const REQUIRE_CALL_FORWARDS: &[&str] = &["call", "apply", "bind"];

impl<'s, 'a> Reader<'s, 'a> {
    fn new(semantic: &'s Semantic<'a>, context: ReadContext<'s>) -> Self {
        Self {
            scoping: semantic.scoping(),
            nodes: semantic.nodes(),
            names: FxHashSet::default(),
            escapes: false,
            loaders: FxHashSet::default(),
            bindings: FxHashSet::default(),
            template_reads_root: context.template_reads_root,
            loader_candidates: context.loader_candidates,
        }
    }

    fn finish(self) -> OpaqueLoads {
        let mut names: Vec<String> = self.names.into_iter().collect();
        names.sort_unstable();
        let mut loaders: Vec<LoaderReturn> = self.loaders.into_iter().collect();
        loaders.sort_unstable();
        OpaqueLoads {
            names,
            escapes: self.escapes,
            loaders,
        }
    }

    /// Record an escape for a read of the global `require` that is not a
    /// direct call. An ambient `declare` of `require` has no runtime
    /// binding, so its references also read the global loader.
    fn read_require_values(&mut self) {
        let scoping = self.scoping;
        let unresolved = scoping
            .root_unresolved_references()
            .get("require")
            .into_iter()
            .flatten()
            .map(|&id| scoping.get_reference(id));
        let ambient = scoping
            .get_root_binding("require".into())
            .filter(|&symbol| scoping.symbol_flags(symbol).contains(SymbolFlags::Ambient))
            .into_iter()
            .flat_map(|symbol| scoping.get_resolved_references(symbol));
        let escapes = unresolved
            .chain(ambient)
            .any(|reference| self.require_value_escapes(reference));
        self.escapes |= escapes;
    }

    fn require_value_escapes(&self, reference: &Reference) -> bool {
        if !reference.is_value() {
            return false;
        }
        let node = reference.node_id();
        let span = self.nodes.kind(node).span();
        let (node, span) = unwrap_upward(self.nodes, node, span);
        match self.nodes.parent_kind(node) {
            AstKind::CallExpression(call) => call.callee.span() != span,
            AstKind::NewExpression(new_expr) => new_expr.callee.span() != span,
            AstKind::StaticMemberExpression(member) => {
                REQUIRE_CALL_FORWARDS.contains(&member.property.name.as_str())
            }
            AstKind::UnaryExpression(unary) => unary.operator != UnaryOperator::Typeof,
            AstKind::BinaryExpression(binary) => !binary.operator.is_equality(),
            AstKind::IfStatement(_) | AstKind::ExpressionStatement(_) => false,
            AstKind::ConditionalExpression(cond) => cond.test.span() != span,
            _ => true,
        }
    }

    /// Read one load. An `import()` gives a promise until an `await`.
    fn read_load(&mut self, node: NodeId, span: Span, promise: bool) {
        let (mut node, mut span) = unwrap_upward(self.nodes, node, span);
        let mut promise = promise;
        let parent = self.nodes.parent_id(node);
        if let AstKind::AwaitExpression(await_expr) = self.nodes.kind(parent) {
            (node, span) = unwrap_upward(self.nodes, parent, await_expr.span);
            promise = false;
        }
        if promise {
            self.read_promise(node, span);
        } else {
            self.read_value(node, span, false);
        }
    }

    /// Read a promise of the loaded value: a `.then` callback, a handler
    /// whose result the code discards, or a discarded promise.
    fn read_promise(&mut self, node: NodeId, span: Span) {
        let parent = self.nodes.parent_id(node);
        let AstKind::StaticMemberExpression(member) = self.nodes.kind(parent) else {
            if !self.is_discarded(node) && !self.read_return(node, span, true) {
                self.escapes = true;
            }
            return;
        };
        let call_id = self.nodes.parent_id(parent);
        let AstKind::CallExpression(call) = self.nodes.kind(call_id) else {
            self.escapes = true;
            return;
        };
        if member.object.span() != span || call.callee.span() != member.span {
            self.escapes = true;
            return;
        }
        match member.property.name.as_str() {
            "then" => self.read_then_callback(call.arguments.first()),
            "catch" | "finally" => self.read_load(call_id, call.span, true),
            _ => self.escapes = true,
        }
    }

    /// Read the first callback of `.then()`. A callback that is not an
    /// inline function can read each export.
    fn read_then_callback(&mut self, callback: Option<&Argument<'_>>) {
        match callback {
            None => {}
            Some(Argument::ArrowFunctionExpression(arrow)) => self.read_params(&arrow.params),
            Some(Argument::FunctionExpression(function)) => {
                self.read_params(&function.params);
                if self.reads_arguments(function.span) {
                    self.escapes = true;
                }
            }
            Some(_) => self.escapes = true,
        }
    }

    /// `true` when the non-arrow function at `span` reads its `arguments`
    /// object, which holds the loaded value of a `.then` callback.
    fn reads_arguments(&self, span: Span) -> bool {
        let Some(references) = self.scoping.root_unresolved_references().get("arguments") else {
            return false;
        };
        references.iter().any(|&id| {
            let node = self.scoping.get_reference(id).node_id();
            self.nodes
                .ancestors(node)
                .find_map(|ancestor| match ancestor.kind() {
                    AstKind::Function(function) => Some(function.span == span),
                    _ => None,
                })
                .unwrap_or(false)
        })
    }

    fn read_params(&mut self, params: &FormalParameters<'_>) {
        match params.items.first() {
            Some(param) => self.read_pattern(&param.pattern, true),
            None if params.rest.is_some() => self.escapes = true,
            None => {}
        }
    }

    /// Read the loaded value at `node`. `bound` is `true` for a reference to
    /// a binding of the value, where a second binding is an alias.
    fn read_value(&mut self, node: NodeId, span: Span, bound: bool) {
        let chain = member_chain(self.nodes, node, span);
        if let Some(name) = chain.segments.into_iter().next() {
            self.names.insert(name);
            return;
        }
        let parent = self.nodes.parent_id(node);
        if parent == node {
            return;
        }
        match self.nodes.kind(parent) {
            AstKind::VariableDeclarator(declarator)
                if declarator.init.as_ref().map(GetSpan::span) == Some(span) =>
            {
                if self.is_exported_declarator(parent) && !declarator.id.is_destructuring_pattern()
                {
                    self.escapes = true;
                }
                self.read_pattern(&declarator.id, !bound);
            }
            AstKind::BinaryExpression(binary)
                if binary.operator == BinaryOperator::In && binary.right.span() == span =>
            {
                match binary.left.get_inner_expression() {
                    Expression::StringLiteral(key) => {
                        self.names.insert(key.value.text().into_owned());
                    }
                    _ => self.escapes = true,
                }
            }
            AstKind::BinaryExpression(binary) if binary.operator.is_equality() => {}
            AstKind::UnaryExpression(unary)
                if matches!(
                    unary.operator,
                    UnaryOperator::Typeof | UnaryOperator::Void | UnaryOperator::LogicalNot
                ) => {}
            AstKind::ExpressionStatement(_) if self.is_discarded(node) => {}
            _ => {
                if !self.read_return(node, span, false) {
                    self.escapes = true;
                }
            }
        }
    }

    /// Record a loader when a loader candidate returns the value at `node`,
    /// with a `return` or as the expression body of an arrow function.
    /// `promise` is `true` when the value is a promise of the loaded value.
    fn read_return(&mut self, node: NodeId, span: Span, promise: bool) -> bool {
        let parent = self.nodes.parent_id(node);
        let function = match self.nodes.kind(parent) {
            AstKind::ReturnStatement(statement)
                if statement.argument.as_ref().map(GetSpan::span) == Some(span) =>
            {
                self.nodes
                    .ancestors(parent)
                    .find_map(|ancestor| match ancestor.kind() {
                        AstKind::Function(function) => Some(function.span),
                        AstKind::ArrowFunctionExpression(arrow) => Some(arrow.span),
                        _ => None,
                    })
            }
            AstKind::ArrowFunctionExpression(arrow)
                if arrow.get_expression().map(GetSpan::span) == Some(span) =>
            {
                Some(arrow.span)
            }
            _ => None,
        };
        let Some(&(contract, is_async)) =
            function.and_then(|function| self.loader_candidates.get(&function))
        else {
            return false;
        };
        // An `async` function wraps its return value in a promise.
        self.loaders.insert(LoaderReturn {
            contract,
            promise: promise || is_async,
        });
        true
    }

    /// Read a binding of the loaded value. `alias` is `true` when a plain
    /// identifier binds the value itself, not a copy of another binding.
    fn read_pattern(&mut self, pattern: &BindingPattern<'_>, alias: bool) {
        match pattern {
            BindingPattern::BindingIdentifier(id) if alias => match id.symbol_id.get() {
                Some(symbol) => {
                    if self.template_reads_root
                        && self.scoping.symbol_scope_id(symbol) == self.scoping.root_scope_id()
                    {
                        self.escapes = true;
                    }
                    self.read_binding(symbol);
                }
                None => self.escapes = true,
            },
            BindingPattern::ObjectPattern(_) => match destructured_keys(pattern) {
                Some(keys) => self.names.extend(keys),
                None => self.escapes = true,
            },
            BindingPattern::AssignmentPattern(assign) => self.read_pattern(&assign.left, alias),
            _ => self.escapes = true,
        }
    }

    fn read_binding(&mut self, symbol: SymbolId) {
        if !self.bindings.insert(symbol) {
            return;
        }
        for reference in self.scoping.get_resolved_references(symbol) {
            if !reference.is_read() {
                continue;
            }
            let node = reference.node_id();
            let AstKind::IdentifierReference(identifier) = self.nodes.kind(node) else {
                self.escapes = true;
                continue;
            };
            let (node, span) = unwrap_upward(self.nodes, node, identifier.span);
            match self.fallback_result(symbol, node, span) {
                // The result is a new value, so a binding of it is no alias.
                Some((node, span, awaited)) => {
                    if awaited {
                        // `await` reads `then` on the value.
                        self.names.insert(THEN.to_string());
                    }
                    self.read_value(node, span, false);
                }
                None => self.read_value(node, span, true),
            }
        }
    }

    /// The outermost fallback or `await` that holds the read of `symbol` at
    /// `node`, when each branch is `symbol` or one member of it. The flag is
    /// `true` when the result goes through an `await`.
    fn fallback_result(
        &self,
        symbol: SymbolId,
        node: NodeId,
        span: Span,
    ) -> Option<(NodeId, Span, bool)> {
        let mut result = None;
        let mut awaited = false;
        let (mut node, mut span) = (node, span);
        loop {
            let parent = self.nodes.parent_id(node);
            if parent == node {
                break;
            }
            let follows = match self.nodes.kind(parent) {
                AstKind::LogicalExpression(logical)
                    if matches!(
                        logical.operator,
                        LogicalOperator::Coalesce | LogicalOperator::Or
                    ) =>
                {
                    self.is_loaded_value(symbol, &logical.left)
                        && self.is_loaded_value(symbol, &logical.right)
                }
                AstKind::ConditionalExpression(cond) if cond.test.span() != span => {
                    self.is_loaded_value(symbol, &cond.consequent)
                        && self.is_loaded_value(symbol, &cond.alternate)
                }
                AstKind::AwaitExpression(_) => {
                    awaited = true;
                    true
                }
                _ => false,
            };
            if !follows {
                break;
            }
            let parent_span = self.nodes.kind(parent).span();
            (node, span) = unwrap_upward(self.nodes, parent, parent_span);
            result = Some((node, span, awaited));
        }
        result
    }

    /// `true` when `expr` is `symbol`, one named member of it, or a
    /// fallback or an `await` of such a value.
    fn is_loaded_value(&self, symbol: SymbolId, expr: &Expression<'_>) -> bool {
        match expr.get_inner_expression() {
            Expression::Identifier(_) => self.is_reference_to(symbol, expr),
            Expression::LogicalExpression(logical) => {
                matches!(
                    logical.operator,
                    LogicalOperator::Coalesce | LogicalOperator::Or
                ) && self.is_loaded_value(symbol, &logical.left)
                    && self.is_loaded_value(symbol, &logical.right)
            }
            Expression::ConditionalExpression(cond) => {
                self.is_loaded_value(symbol, &cond.consequent)
                    && self.is_loaded_value(symbol, &cond.alternate)
            }
            Expression::AwaitExpression(await_expr) => {
                self.is_loaded_value(symbol, &await_expr.argument)
            }
            Expression::ChainExpression(chain) => chain
                .expression
                .as_member_expression()
                .is_some_and(|member| self.is_member_of(symbol, member)),
            inner => inner
                .as_member_expression()
                .is_some_and(|member| self.is_member_of(symbol, member)),
        }
    }

    fn is_member_of(&self, symbol: SymbolId, member: &MemberExpression<'_>) -> bool {
        let named = match member {
            MemberExpression::StaticMemberExpression(_) => true,
            MemberExpression::ComputedMemberExpression(computed) => {
                matches!(computed.expression, Expression::StringLiteral(_))
            }
            MemberExpression::PrivateFieldExpression(_) => false,
        };
        named && self.is_reference_to(symbol, member.object())
    }

    fn is_reference_to(&self, symbol: SymbolId, expr: &Expression<'_>) -> bool {
        let Expression::Identifier(identifier) = expr.get_inner_expression() else {
            return false;
        };
        identifier
            .reference_id
            .get()
            .and_then(|id| self.scoping.get_reference(id).symbol_id())
            == Some(symbol)
    }

    /// `true` for a declarator of `export const x = ...`. Another module can
    /// import the binding and read each export of the loaded value.
    fn is_exported_declarator(&self, declarator: NodeId) -> bool {
        let declaration = self.nodes.parent_id(declarator);
        matches!(
            self.nodes.parent_kind(declaration),
            AstKind::ExportNamedDeclaration(_) | AstKind::ExportDeclaration(_)
        )
    }

    /// `true` when the code discards the value of the expression at `node`:
    /// an expression statement or a `void` operand. The expression body of
    /// an arrow function is not a statement, so it returns the value.
    fn is_discarded(&self, node: NodeId) -> bool {
        match self.nodes.parent_kind(node) {
            AstKind::ExpressionStatement(_) => true,
            AstKind::UnaryExpression(unary) => unary.operator == UnaryOperator::Void,
            _ => false,
        }
    }
}