swc_ecma_minifier/compress/pure/
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
52
53
54
55
56
57
58
59
60
61
use std::ops::{Deref, DerefMut};

use super::Pure;

#[derive(Default, Clone, Copy)]
pub(super) struct Ctx {
    pub in_delete: bool,

    /// `true` if we are in `arg` of `++arg` or `--arg`.
    pub is_update_arg: bool,

    #[allow(unused)]
    pub is_callee: bool,

    pub _in_try_block: bool,

    pub is_lhs_of_assign: bool,

    /// `true` if we are in topmost expression of a statement
    ///
    /// This is true for `expr` of [swc_ecma_ast::ExprStmt], `test` of
    /// [swc_ecma_ast::IfStmt], and more like that.
    pub in_first_expr: bool,
}

impl<'b> Pure<'b> {
    /// RAII guard to change context temporarically
    pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx<'_, 'b> {
        let orig_ctx = self.ctx;
        self.ctx = ctx;
        WithCtx {
            pass: self,
            orig_ctx,
        }
    }
}

pub(super) struct WithCtx<'a, 'b> {
    pass: &'a mut Pure<'b>,
    orig_ctx: Ctx,
}

impl<'b> Deref for WithCtx<'_, 'b> {
    type Target = Pure<'b>;

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

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

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