swc_ecma_usage_analyzer/analyzer/
storage.rs

1use swc_atoms::Wtf8Atom;
2use swc_common::SyntaxContext;
3use swc_ecma_ast::*;
4use swc_ecma_utils::{Type, Value};
5
6use super::{ctx::Ctx, ScopeKind};
7use crate::alias::Access;
8
9pub trait Storage: Sized + Default {
10    type ScopeData: ScopeDataLike;
11    type VarData: VarDataLike;
12
13    fn new(collect_prop_atom: bool) -> Self;
14
15    fn need_collect_prop_atom(&self) -> bool;
16
17    fn add_property_atom(&mut self, atom: Wtf8Atom);
18
19    fn scope(&mut self, ctxt: SyntaxContext) -> &mut Self::ScopeData;
20
21    fn top_scope(&mut self) -> &mut Self::ScopeData;
22
23    fn var_or_default(&mut self, id: Id) -> &mut Self::VarData;
24
25    fn merge(&mut self, kind: ScopeKind, child: Self);
26
27    fn report_usage(&mut self, ctx: Ctx, i: Id);
28
29    fn report_assign(&mut self, ctx: Ctx, i: Id, is_op: bool, ty: Value<Type>);
30
31    fn declare_decl(
32        &mut self,
33        ctx: Ctx,
34        i: &Ident,
35        init_type: Option<Value<Type>>,
36        kind: Option<VarDeclKind>,
37    ) -> &mut Self::VarData;
38
39    fn get_initialized_cnt(&self) -> usize;
40    fn truncate_initialized_cnt(&mut self, len: usize);
41
42    fn mark_property_mutation(&mut self, id: Id);
43
44    fn get_var_data(&self, id: Id) -> Option<&Self::VarData>;
45}
46
47pub trait ScopeDataLike: Sized + Default + Clone {
48    fn add_declared_symbol(&mut self, id: &Ident);
49
50    fn merge(&mut self, other: Self, is_child: bool);
51
52    fn mark_used_arguments(&mut self);
53
54    fn mark_eval_called(&mut self);
55
56    fn mark_with_stmt(&mut self);
57}
58
59pub trait VarDataLike: Sized {
60    /// See `declared_as_fn_param` of [crate::analyzer::VarUsageInfo].
61    fn mark_declared_as_fn_param(&mut self);
62
63    fn mark_as_lazy_init(&mut self);
64
65    fn mark_declared_as_fn_decl(&mut self);
66
67    fn mark_declared_as_fn_expr(&mut self);
68
69    fn mark_declared_as_for_init(&mut self);
70
71    fn mark_has_property_access(&mut self);
72
73    fn mark_used_as_callee(&mut self);
74
75    fn mark_used_as_arg(&mut self);
76
77    fn mark_indexed_with_dynamic_key(&mut self);
78
79    fn add_accessed_property(&mut self, name: Wtf8Atom);
80
81    fn mark_used_as_ref(&mut self);
82
83    fn add_infects_to(&mut self, other: Access);
84
85    fn prevent_inline(&mut self);
86
87    fn mark_as_exported(&mut self);
88
89    fn mark_initialized_with_safe_value(&mut self);
90
91    fn mark_as_pure_fn(&mut self);
92
93    fn mark_used_above_decl(&mut self);
94
95    fn mark_used_recursively(&mut self);
96
97    fn is_declared(&self) -> bool;
98
99    fn mark_used_as_jsx_callee(&mut self);
100}