swc_ecma_usage_analyzer/alias/
ctx.rs

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
use std::ops::{Deref, DerefMut};

use bitflags::bitflags;

use super::InfectionCollector;

impl InfectionCollector {
    pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx {
        let orig_ctx = self.ctx;
        self.ctx = ctx;

        WithCtx {
            analyzer: self,
            orig_ctx,
        }
    }
}

bitflags! {
    #[derive(Debug, Default, Clone, Copy)]
    pub struct Ctx: u8 {
        const TrackExprIdent = 1 << 0;
        const IsCallee = 1 << 1;
        const IsPatDecl = 1 << 2;
    }
}

pub(super) struct WithCtx<'a> {
    analyzer: &'a mut InfectionCollector,
    orig_ctx: Ctx,
}

impl Deref for WithCtx<'_> {
    type Target = InfectionCollector;

    fn deref(&self) -> &Self::Target {
        self.analyzer
    }
}

impl DerefMut for WithCtx<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.analyzer
    }
}

impl Drop for WithCtx<'_> {
    fn drop(&mut self) {
        self.analyzer.ctx = self.orig_ctx;
    }
}