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
use fxhash::{FxHashMap, FxHashSet};
use petgraph::graphmap::DiGraphMap;
use rnode::{Visit, VisitWith};
use stc_ts_ast_rnode::{RClassMember, RExpr, RMemberExpr, RMemberProp};
use stc_ts_types::{rprop_name_to_expr, Id};
use crate::{
analyzer::Analyzer,
util::graph::{Inliner, NodeId},
};
impl Analyzer<'_, '_> {
pub(super) fn calc_eval_order_of_class_methods(&mut self, mut remaining_indexes: Vec<usize>, members: &[RClassMember]) -> Vec<usize> {
let mut keys = Inliner::<MemberKey>::default();
let mut graph = DiGraphMap::<NodeId<MemberKey>, ()>::default();
let mut defined = FxHashMap::<usize, NodeId<MemberKey>>::default();
for (index, member) in members.iter().enumerate() {
if !remaining_indexes.contains(&index) {
continue;
}
match member {
RClassMember::Constructor(_) | RClassMember::StaticBlock(_) => continue,
RClassMember::TsIndexSignature(_) => {}
RClassMember::ClassProp(_) | RClassMember::PrivateProp(_) | RClassMember::Empty(_) => {
continue;
}
RClassMember::Method(..) | RClassMember::PrivateMethod(..) => {
let key = match member {
RClassMember::Method(v) => match rprop_name_to_expr(v.key.clone()) {
RExpr::Ident(i) => MemberKey::Id(i.into()),
_ => continue,
},
RClassMember::PrivateMethod(v) => MemberKey::Private(v.key.id.clone().into()),
_ => unreachable!(),
};
let key = keys.inline(key);
defined.insert(index, key);
graph.add_node(key);
let mut visitor = MethodAnalyzer {
result: Default::default(),
};
member.visit_with(&mut visitor);
for dep in visitor.result.depends_on {
let dep = keys.inline(dep);
graph.add_node(dep);
graph.add_edge(key, dep, ());
}
}
}
}
let indexes = remaining_indexes.to_vec();
for (i, i_key) in indexes.iter().copied().enumerate() {
let i_key = match defined.get(&i_key) {
Some(v) => *v,
None => continue,
};
for (j, j_key) in indexes.iter().copied().enumerate().filter(|(_, j)| i < *j) {
let j_key = match defined.get(&j_key) {
Some(v) => *v,
None => continue,
};
if graph.contains_edge(i_key, j_key) {
remaining_indexes.swap(i, j);
}
}
}
remaining_indexes
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
enum MemberKey {
Id(Id),
Private(Id),
}
#[derive(Default)]
struct AnalysisResult {
depends_on: FxHashSet<MemberKey>,
}
struct MethodAnalyzer {
result: AnalysisResult,
}
impl Visit<RMemberExpr> for MethodAnalyzer {
fn visit(&mut self, e: &RMemberExpr) {
e.obj.visit_with(self);
if matches!(e.prop, RMemberProp::Computed(..)) {
e.prop.visit_with(self);
}
if let RExpr::This(..) = &*e.obj {
match &e.prop {
RMemberProp::Ident(i) => {
self.result.depends_on.insert(MemberKey::Id(i.into()));
}
RMemberProp::PrivateName(i) => {
self.result.depends_on.insert(MemberKey::Private(i.id.clone().into()));
}
_ => {}
}
}
}
}