swc_ecma_minifier/compress/pure/
ctx.rs

1use super::Pure;
2
3bitflags::bitflags! {
4#[derive(Default, Clone, Copy)]
5    pub(super) struct Ctx: u8 {
6        const IN_DELETE         = 1 << 0;
7        /// true if we are in `arg` of `++arg` or `--arg`.
8        const IS_UPDATE_ARG     = 1 << 1;
9        const IS_CALLEE         = 1 << 2;
10        const IN_TRY_BLOCK      = 1 << 3;
11        const IS_LHS_OF_ASSIGN  = 1 << 4;
12        const PRESERVE_BLOCK    = 1 << 5;
13        const IS_LABEL_BODY     = 1 << 6;
14        const IN_OPT_CHAIN      = 1 << 7;
15    }
16}
17
18impl<'b> Pure<'b> {
19    pub(super) fn do_inside_of_context<T>(
20        &mut self,
21        context: Ctx,
22        f: impl FnOnce(&mut Self) -> T,
23    ) -> T {
24        let ctx = self.ctx;
25        let inserted = ctx.complement().intersection(context);
26        if inserted.is_empty() {
27            f(self)
28        } else {
29            self.ctx.insert(inserted);
30            let result = f(self);
31            self.ctx.remove(inserted);
32            result
33        }
34    }
35
36    pub(super) fn do_outside_of_context<T>(
37        &mut self,
38        context: Ctx,
39        f: impl FnOnce(&mut Self) -> T,
40    ) -> T {
41        let ctx = self.ctx;
42        let removed = ctx.intersection(context);
43        if !removed.is_empty() {
44            self.ctx.remove(removed);
45            let result = f(self);
46            self.ctx.insert(removed);
47            result
48        } else {
49            f(self)
50        }
51    }
52}